wlike commited on
Commit
7be5daa
·
verified ·
1 Parent(s): 03997e0

create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) XVERSE Inc. All Rights Reserved.
2
+ #
3
+ # -*- encoding: utf-8 -*-
4
+
5
+ import gradio as gr
6
+ import openai
7
+ import os
8
+
9
+ TITLE="XChat"
10
+
11
+ client = openai.Client(
12
+ base_url="https://api.xverse.cn/v1",
13
+ api_key=os.environ["API_KEY"]
14
+ )
15
+
16
+ def predict(msg, history=[]):
17
+ messages = []
18
+ tuples = []
19
+ if len(os.environ["SYSTEM_PROMPT"]) > 0:
20
+ messages.append({"role": "system", "content": os.environ["SYSTEM_PROMPT"]})
21
+ for i in range(0, len(history), 2):
22
+ messages.append({"role": "user", "content": history[i]})
23
+ messages.append({"role": "assistant", "content": history[i+1]})
24
+ tuples.append((history[i], history[i+1]))
25
+ messages.append({"role": "user", "content": msg})
26
+ response = client.chat.completions.create(
27
+ model=os.environ["MODEL"],
28
+ messages=messages,
29
+ max_tokens=int(os.environ["MAX_TOKENS"]),
30
+ top_p=float(os.environ["TOP_P"]),
31
+ temperature=float(os.environ["TEMPERATURE"]),
32
+ presence_penalty=float(os.environ["PRESENCE_PENALTY"]),
33
+ stream=True
34
+ )
35
+ ### 非流式输出
36
+ #txt = response.choices[0].message.content
37
+ #tuples.append((msg, txt))
38
+ #history.append(msg)
39
+ #history.append(txt)
40
+ #return tuples, history
41
+
42
+ ### 流式输出
43
+ snippet = ""
44
+ i = 0
45
+ for chunk in response:
46
+ i += 1
47
+ if chunk.choices[0].delta.content is not None:
48
+ snippet = snippet + chunk.choices[0].delta.content
49
+ if i == 1:
50
+ tuples.append((msg, snippet))
51
+ history.append(msg)
52
+ history.append(snippet)
53
+ else:
54
+ tuples[-1] = (msg, snippet)
55
+ history[-1] = snippet
56
+ yield tuples, history
57
+
58
+ examples = [["你是谁?", None], ["你会干什么?", None], ["写一篇爱护环境的小作文", None]]
59
+
60
+ def reset():
61
+ return None, []
62
+
63
+ def clear_textbox():
64
+ return gr.update(value="")
65
+
66
+ css = """
67
+ h1 {
68
+ text-align: center;
69
+ display: block;
70
+ }
71
+ """
72
+
73
+ with gr.Blocks(css=css) as chat_demo:
74
+ gr.Markdown("""# <center><font size=8>{}</center>""".format(TITLE))
75
+ gr.Markdown("""<center><font size=4>\
76
+ <a href="https://github.com/xverse-ai">GitHub</a>&nbsp; | &nbsp;\
77
+ <a href="https://chat.xverse.cn">Web</a>&nbsp; | &nbsp;\
78
+ <a href="https://help.xverse.cn/docs/api-reference">API</a>\
79
+ </center>"""
80
+ )
81
+ chatbot = gr.Chatbot(elem_id="chatbot", height=800, bubble_full_width=False, likeable=False)
82
+ state = gr.State([])
83
+
84
+ with gr.Row():
85
+ txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter", container=False)
86
+
87
+ with gr.Row():
88
+ submit_btn = gr.Button(value="Submit")
89
+ reset_btn = gr.Button(value="Reset")
90
+
91
+ txt.submit(fn=predict, inputs=[txt, state], outputs=[chatbot, state])
92
+ txt.submit(fn=clear_textbox, inputs=None, outputs=[txt])
93
+ submit_btn.click(fn=predict, inputs=[txt, state], outputs=[chatbot, state])
94
+ submit_btn.click(fn=clear_textbox, inputs=None, outputs=[txt])
95
+ reset_btn.click(fn=reset, inputs=None, outputs=[chatbot, state])
96
+ gr.Examples(examples=examples, inputs=[txt])
97
+
98
+
99
+ if __name__ == "__main__":
100
+ chat_demo.queue()
101
+ chat_demo.launch(share=True)