Spaces:
Runtime error
Runtime error
fanyang
commited on
Commit
·
217d8ad
1
Parent(s):
e8920d2
+ app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Environment variables for backend URL and model name
|
7 |
+
BACKEND_URL = os.getenv('BACKEND_URL','')
|
8 |
+
MODEL_NAME = os.getenv('MODEL_NAME')
|
9 |
+
API_KEY = os.getenv('API_KEY')
|
10 |
+
|
11 |
+
# Custom headers for the API request
|
12 |
+
HEADERS = {
|
13 |
+
'orionstar-api-key': API_KEY,
|
14 |
+
'Content-Type': 'application/json'
|
15 |
+
}
|
16 |
+
|
17 |
+
def clear_session():
|
18 |
+
"""Clears the chat session."""
|
19 |
+
return '', None
|
20 |
+
|
21 |
+
def chat_stream_generator(url, payload):
|
22 |
+
"""Generator function to stream chat responses from the backend."""
|
23 |
+
answer = ''
|
24 |
+
with requests.post(url, json=payload, headers=HEADERS, stream=True) as response:
|
25 |
+
if response.encoding is None:
|
26 |
+
response.encoding = 'utf-8'
|
27 |
+
for line in response.iter_lines(decode_unicode=True):
|
28 |
+
if line:
|
29 |
+
line = line.replace('data: ', '')
|
30 |
+
if line != '[DONE]':
|
31 |
+
data = json.loads(line)
|
32 |
+
if 'choices' in data and data['choices']:
|
33 |
+
choice = data['choices'][0]
|
34 |
+
if 'delta' in choice and choice['delta'].get('content'):
|
35 |
+
answer += choice['delta']['content']
|
36 |
+
yield answer
|
37 |
+
|
38 |
+
def generate_chat(input_text: str, history=None):
|
39 |
+
"""Generates chat responses and updates the chat history."""
|
40 |
+
if input_text is None:
|
41 |
+
input_text = ''
|
42 |
+
if history is None:
|
43 |
+
history = []
|
44 |
+
history = history[-5:] # Keep the last 5 messages in history
|
45 |
+
url = BACKEND_URL
|
46 |
+
payload = {
|
47 |
+
"model": MODEL_NAME,
|
48 |
+
"stream": True,
|
49 |
+
"messages": [
|
50 |
+
{"role": "system", "content": "你是一个AI助手,请用专业的知识解答我的问题"},
|
51 |
+
{"role": "user", "content": input_text}
|
52 |
+
]
|
53 |
+
}
|
54 |
+
|
55 |
+
gen = chat_stream_generator(url, payload)
|
56 |
+
for response in gen:
|
57 |
+
history.append((input_text, response))
|
58 |
+
yield None, history
|
59 |
+
history.pop()
|
60 |
+
history.append((input_text, response))
|
61 |
+
return None, gen
|
62 |
+
|
63 |
+
# Gradio interface
|
64 |
+
block = gr.Blocks()
|
65 |
+
|
66 |
+
with block as demo:
|
67 |
+
gr.Markdown("<center><h1>猎户星空-Yi-34B-Chat对话模型演示</h1></center>")
|
68 |
+
gr.Markdown("""
|
69 |
+
* Yi系列模型是由零一万物团队开源的大模型,在多个权威的中文、英文及通用领域 benchmark 上取得不错的效果。
|
70 |
+
* [猎户星空](https://www.orionstar.com/)推出的Orionstar-Yi-34B-Chat更进一步挖掘了Yi-34B的潜力。通过对大量高质量微调语料库的深度训练,我们致力于将其打造成为ChatGPT领域中的杰出开源替代品。
|
71 |
+
* Orionstar-Yi-34B-Chat,在C-Eval、MMLU、CMMLU等主流测评集上表现出色,显著领先于国内其他开源对话模型(截止2023年11月)。与其他开源模型的详细对比请查看[这里](https://github.com/OrionStarAI/OrionStar-Yi-34B-Chat)。
|
72 |
+
* 请在[Gihub](https://github.com/OrionStarAI/OrionStar-Yi-34B-Chat)点击Star支持我们。
|
73 |
+
* 加入[官方微信交流群](https://cdn.jsdelivr.net/gh/OrionStarAI/OrionStar-Yi-34B-Chat@main/pics/wechat_group.jpg)""")
|
74 |
+
|
75 |
+
chatbot = gr.Chatbot(label='OrionStar-Yi-34B-Chat', elem_classes="control-height")
|
76 |
+
message = gr.Textbox(label='Input')
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
submit = gr.Button("🚀 Submit")
|
80 |
+
clear_history = gr.Button("🧹 Clear History")
|
81 |
+
|
82 |
+
submit.click(
|
83 |
+
fn=generate_chat,
|
84 |
+
inputs=[message, chatbot],
|
85 |
+
outputs=[message, chatbot]
|
86 |
+
)
|
87 |
+
clear_history.click(
|
88 |
+
fn=clear_session,
|
89 |
+
inputs=[],
|
90 |
+
outputs=[message, chatbot],
|
91 |
+
queue=False
|
92 |
+
)
|
93 |
+
|
94 |
+
demo.queue(api_open=False).launch(server_name='0.0.0.0', height=800, share=False,show_api=False)
|