File size: 2,307 Bytes
75a8e67
 
 
 
 
 
 
 
29753df
 
75a8e67
 
 
 
 
 
 
29f1553
75a8e67
 
 
 
 
 
 
 
29f1553
75a8e67
29f1553
75a8e67
29f1553
75a8e67
 
 
 
29f1553
75a8e67
 
 
 
 
 
 
 
 
 
 
d2d0697
 
75a8e67
29f1553
 
75a8e67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import gradio as gr
from http import HTTPStatus
import openai
from typing import Generator, List, Optional, Tuple, Dict
from urllib.error import HTTPError

API_URL = os.getenv('API_URL')
API_KEY = os.getenv('API_KEY')
oai_client = openai.OpenAI(api_key=API_KEY, base_url=API_URL)

History = List[Tuple[str, str]]
Messages = List[Dict[str, str]]

def clear_session() -> History:
    return '', []

def history_to_messages(history: History) -> Messages:
    messages = []
    for h in history:
        messages.append({'role': 'user', 'content': h[0]})
        messages.append({'role': 'assistant', 'content': h[1]})
    return messages

def messages_to_history(messages: Messages) -> Tuple[str, History]:
    history = []
    for q, r in zip(messages[0::2], messages[1::2]):
        history.append([q['content'], r['content']])
    return history

def model_chat(query: Optional[str], history: Optional[History]) -> Generator[Tuple[str, History], None, None]:
    if query is None:
        query = ''
    if history is None:
        history = []
    messages = history_to_messages(history)
    messages.append({'role': 'user', 'content': query})
    gen = oai_client.chat.completions.create(
        model='dicta-il/dictalm2.0-instruct',
        messages=messages,
        temperature=0.7,
        max_tokens=512,
        top_p=0.9,
        stream=True
    )
    messages.append({'role': 'assistant', 'content': ''})
    for completion in gen:
        print(completion)
        text = completion.choices[0].content
        messages[-1]['content'] += text
        history = messages_to_history(messages)
        yield '', history
    
with gr.Blocks() as demo:
    gr.Markdown("""<center><font size=8>DictaLM2.0-Instruct Chat Demo</center>""")
    
    chatbot = gr.Chatbot(label='dicta-il/dictalm2.0-instruct')
    textbox = gr.Textbox(lines=2, label='Input')

    with gr.Row():
        clear_history = gr.Button("🧹 Clear history")
        sumbit = gr.Button("🚀 Send")

    sumbit.click(model_chat,
                 inputs=[textbox, chatbot],
                 outputs=[textbox, chatbot])
    clear_history.click(fn=clear_session,
                        inputs=[],
                        outputs=[textbox, chatbot])

demo.queue(api_open=False).launch(max_threads=10,height=800, share=False)