Spaces:
Runtime error
Runtime error
Create app1.py
Browse files
app1.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
client = openai.OpenAI(
|
5 |
+
api_key="EMPTY",
|
6 |
+
base_url="http://localhost:7777/v1"
|
7 |
+
)
|
8 |
+
def chatbot_mistral(messages:list,systemt_prompt=""):
|
9 |
+
|
10 |
+
if messages[0]['role'] != "system":
|
11 |
+
sys = {"role":"system","content":systemt_prompt}
|
12 |
+
messages.insert(0,sys)
|
13 |
+
else:
|
14 |
+
messages[0]['content'] = systemt_prompt
|
15 |
+
|
16 |
+
|
17 |
+
# model_name = "dewu-chat"
|
18 |
+
call_args = {
|
19 |
+
'temperature': 0.7,
|
20 |
+
'top_p': 0.9,
|
21 |
+
'top_k': 40,
|
22 |
+
'max_tokens': 2048, # output-len
|
23 |
+
'presence_penalty': 1.0,
|
24 |
+
'frequency_penalty': 0.0,
|
25 |
+
"repetition_penalty":1.0,
|
26 |
+
"stop":["</s>"],
|
27 |
+
# "stop":["<|eot_id|>","<|end_of_text|>"],
|
28 |
+
"stream":True
|
29 |
+
}
|
30 |
+
|
31 |
+
# create a chat completion
|
32 |
+
for chunk in openai.ChatCompletion.create(model="dewu-chat",messages=messages,extra_body=call_args):
|
33 |
+
if hasattr(chunk.choices[0].delta, "content"):
|
34 |
+
response = chunk.choices[0].delta.content
|
35 |
+
yield response
|
36 |
+
|
37 |
+
|
38 |
+
def respond(
|
39 |
+
message,
|
40 |
+
history: list[tuple[str, str]],
|
41 |
+
system_message,
|
42 |
+
max_tokens,
|
43 |
+
temperature,
|
44 |
+
top_p,
|
45 |
+
):
|
46 |
+
messages = [{"role": "system", "content": system_message}]
|
47 |
+
|
48 |
+
for val in history:
|
49 |
+
if val[0]:
|
50 |
+
messages.append({"role": "user", "content": val[0]})
|
51 |
+
if val[1]:
|
52 |
+
messages.append({"role": "assistant", "content": val[1]})
|
53 |
+
|
54 |
+
messages.append({"role": "user", "content": message})
|
55 |
+
|
56 |
+
response = ""
|
57 |
+
|
58 |
+
for i in chatbot_mistral(messages):
|
59 |
+
yield i
|
60 |
+
"""
|
61 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
62 |
+
"""
|
63 |
+
demo = gr.ChatInterface(
|
64 |
+
respond,
|
65 |
+
additional_inputs=[
|
66 |
+
gr.Textbox(value="You are a helpful, respectful and honest assistant.Help humman as much as you can.", label="System message"),
|
67 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
68 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
69 |
+
gr.Slider(
|
70 |
+
minimum=0.1,
|
71 |
+
maximum=1.0,
|
72 |
+
value=0.95,
|
73 |
+
step=0.05,
|
74 |
+
label="Top-p (nucleus sampling)",
|
75 |
+
),
|
76 |
+
],
|
77 |
+
)
|
78 |
+
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
demo.launch()
|