File size: 2,057 Bytes
53a67f8
ef9cde4
 
53a67f8
 
ef9cde4
8239d17
d8cb72d
7b56d83
d8cb72d
 
7b56d83
ef9cde4
d1de333
8239d17
d1de333
 
d8cb72d
 
d1de333
 
7b56d83
53a67f8
8239d17
 
 
 
d8cb72d
7b56d83
8239d17
d8cb72d
 
 
 
 
 
 
53a67f8
7b56d83
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from gradio_client import Client
import gradio as gr

# ื—ื™ื‘ื•ืจ ืœ-Space ืขื ื”ืžื•ื“ืœ ื‘-Hugging Face
client = Client("dicta-il/dictalm2.0-instruct-demo")

def chat_with_model(history):
    # ืงื‘ืœืช ื”ื”ื•ื“ืขื” ื”ืื—ืจื•ื ื” ืฉื ืฉืœื—ื” ืขืœ ื™ื“ื™ ื”ืžืฉืชืžืฉ
    prompt = history[-1]["content"]
    # ืฉืœื™ื—ืช ื”ื”ื•ื“ืขื” ืœืžื•ื“ืœ ื•ืงื‘ืœืช ืชื’ื•ื‘ื”
    result = client.predict(message=prompt, api_name="/chat")
    return history + [{"role": "user", "content": prompt}, {"role": "bot", "content": result}]

# ื™ืฆื™ืจืช ืžืžืฉืง ืžืชืงื“ื ืขื Gradio ื‘ืฆื•ืจืช ืฆ'ื˜-ื‘ื•ื˜ ื‘ืกื’ื ื•ืŸ ืืงื“ืžื™
with gr.Blocks(theme="default") as demo:
    gr.HTML("""
    <div style="background-color: #f5f5f5; padding: 20px; text-align: center;">
        <h1 style="color: #003366; font-family: Arial, sans-serif;">ืฆ'ืื˜ ืขื ืžื•ื“ืœ DictaLM</h1>
        <p style="font-family: Arial, sans-serif; color: #333;">ื‘ืจื•ื›ื™ื ื”ื‘ืื™ื ืœืฆ'ืื˜ ื”ืื™ื ื˜ืจืืงื˜ื™ื‘ื™ ืฉืœื ื•, ื”ืžืืคืฉืจ ืœื›ื ืœื”ืชื ืกื•ืช ื‘ืฉื™ื—ื” ืขื ืžื•ื“ืœ AI ืžืชืงื“ื.</p>
    </div>
    """)
    chatbot = gr.Chatbot(label="ืฆ'ืื˜ ืขื ืžื•ื“ืœ DictaLM", type="messages")
    with gr.Row():
        user_input = gr.Textbox(placeholder="ื”ื›ื ืก ืืช ื”ื”ื•ื“ืขื” ืฉืœืš ื›ืืŸ...", label="", lines=1)
        send_button = gr.Button("ืฉืœื—")
    
    def user_chat(history, message):
        # ื”ื•ืกืคืช ื”ื•ื“ืขืช ื”ืžืฉืชืžืฉ ืœื”ื™ืกื˜ื•ืจื™ื” ืœืคื ื™ ืฉืœื™ื—ืช ื”ืฉืืœื” ืœืžื•ื“ืœ
        return history + [{"role": "user", "content": message}], ""

    # ืฉืœื™ื—ืช ื”ื”ื•ื“ืขื” ื’ื ื‘ืœื—ื™ืฆื” ืขืœ Enter ื•ื’ื ืขืœ ื™ื“ื™ ืœื—ื™ืฆื” ืขืœ ื›ืคืชื•ืจ "ืฉืœื—"
    user_input.submit(fn=user_chat, inputs=[chatbot, user_input], outputs=[chatbot, user_input], queue=False).then(
        fn=chat_with_model, inputs=chatbot, outputs=chatbot
    )
    send_button.click(fn=user_chat, inputs=[chatbot, user_input], outputs=[chatbot, user_input], queue=False).then(
        fn=chat_with_model, inputs=chatbot, outputs=chatbot
    )

demo.launch()