Spaces:
Runtime error
Runtime error
Added demo
Browse files- app.py +77 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from http import HTTPStatus
|
| 4 |
+
import openai
|
| 5 |
+
from typing import Generator, List, Optional, Tuple, Dict
|
| 6 |
+
from urllib.error import HTTPError
|
| 7 |
+
default_system = 'You are a helpful assistant.'
|
| 8 |
+
|
| 9 |
+
API_URL = os.getenv('API_URL')
|
| 10 |
+
oai_client = openai.OpenAI(api_key="<enter api key>", base_url=API_URL)
|
| 11 |
+
|
| 12 |
+
History = List[Tuple[str, str]]
|
| 13 |
+
Messages = List[Dict[str, str]]
|
| 14 |
+
|
| 15 |
+
def clear_session() -> History:
|
| 16 |
+
return '', []
|
| 17 |
+
|
| 18 |
+
def modify_system_session(system: str) -> str:
|
| 19 |
+
if system is None or len(system) == 0:
|
| 20 |
+
system = default_system
|
| 21 |
+
return system, system, []
|
| 22 |
+
|
| 23 |
+
def history_to_messages(history: History, system: str) -> Messages:
|
| 24 |
+
messages = []
|
| 25 |
+
for h in history:
|
| 26 |
+
messages.append({'role': 'user', 'content': h[0]})
|
| 27 |
+
messages.append({'role': 'assistant', 'content': h[1]})
|
| 28 |
+
return messages
|
| 29 |
+
|
| 30 |
+
def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
| 31 |
+
system = messages[0]['content']
|
| 32 |
+
history = []
|
| 33 |
+
for q, r in zip(messages[1::2], messages[2::2]):
|
| 34 |
+
history.append([q['content'], r['content']])
|
| 35 |
+
return system, history
|
| 36 |
+
|
| 37 |
+
def model_chat(query: Optional[str], history: Optional[History]) -> Generator[Tuple[str, str, History]]:
|
| 38 |
+
if query is None:
|
| 39 |
+
query = ''
|
| 40 |
+
if history is None:
|
| 41 |
+
history = []
|
| 42 |
+
messages = history_to_messages(history, system)
|
| 43 |
+
messages.append({'role': 'user', 'content': query})
|
| 44 |
+
oai_client.completions.create()
|
| 45 |
+
gen = oai_client.chat.completions.create(
|
| 46 |
+
model='dicta-il/dictalm2.0-instruct',
|
| 47 |
+
messages=messages,
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
max_tokens=512,
|
| 50 |
+
top_p=0.9,
|
| 51 |
+
stream=True
|
| 52 |
+
)
|
| 53 |
+
messages.append({'role': 'assistant', 'content': ''})
|
| 54 |
+
for completion in gen:
|
| 55 |
+
text = completion.choices[0].text
|
| 56 |
+
messages[-1]['content'] += text
|
| 57 |
+
system, history = messages_to_history(messages)
|
| 58 |
+
yield '', history, system
|
| 59 |
+
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
gr.Markdown("""<center><font size=8>DictaLM2.0-Instruct Chat Demo</center>""")
|
| 62 |
+
|
| 63 |
+
chatbot = gr.Chatbot(label='dicta-il/dictalm2.0-instruct')
|
| 64 |
+
textbox = gr.Textbox(lines=2, label='Input')
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
clear_history = gr.Button("🧹 Clear history")
|
| 68 |
+
sumbit = gr.Button("🚀 Send")
|
| 69 |
+
|
| 70 |
+
sumbit.click(model_chat,
|
| 71 |
+
inputs=[textbox, chatbot],
|
| 72 |
+
outputs=[textbox, chatbot])
|
| 73 |
+
clear_history.click(fn=clear_session,
|
| 74 |
+
inputs=[],
|
| 75 |
+
outputs=[textbox, chatbot])
|
| 76 |
+
|
| 77 |
+
demo.queue(api_open=False).launch(max_threads=10,height=800, share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openai
|