mkw18 commited on
Commit
ce1e006
1 Parent(s): e84d92f
Files changed (2) hide show
  1. app.py +145 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import mdtex2html
3
+ import pandas as pd
4
+ import random as rd
5
+ import os
6
+ import json
7
+ import time
8
+ import openai
9
+ import paramiko
10
+ import requests
11
+
12
+ openai.api_key = os.environ.get('APIKEY')
13
+ rd.seed(time.time())
14
+
15
+ def postprocess(self, y):
16
+ if y is None:
17
+ return []
18
+ for i, (message, response) in enumerate(y):
19
+ y[i] = (
20
+ None if message is None else mdtex2html.convert((message)),
21
+ None if response is None else mdtex2html.convert(response),
22
+ )
23
+ return y
24
+
25
+
26
+ gr.Chatbot.postprocess = postprocess
27
+
28
+
29
+ def parse_text(text):
30
+ """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
31
+ lines = text.split("\n")
32
+ lines = [line for line in lines if line != ""]
33
+ count = 0
34
+ for i, line in enumerate(lines):
35
+ if "```" in line:
36
+ count += 1
37
+ items = line.split('`')
38
+ if count % 2 == 1:
39
+ lines[i] = f'<pre><code class="language-{items[-1]}">'
40
+ else:
41
+ lines[i] = f'<br></code></pre>'
42
+ else:
43
+ if i > 0:
44
+ if count % 2 == 1:
45
+ line = line.replace("`", "\`")
46
+ line = line.replace("<", "&lt;")
47
+ line = line.replace(">", "&gt;")
48
+ line = line.replace(" ", "&nbsp;")
49
+ line = line.replace("*", "&ast;")
50
+ line = line.replace("_", "&lowbar;")
51
+ line = line.replace("-", "&#45;")
52
+ line = line.replace(".", "&#46;")
53
+ line = line.replace("!", "&#33;")
54
+ line = line.replace("(", "&#40;")
55
+ line = line.replace(")", "&#41;")
56
+ line = line.replace("$", "&#36;")
57
+ lines[i] = "<br>"+line
58
+ text = "".join(lines)
59
+ return text
60
+
61
+
62
+ def showInput(input, chatbot):
63
+ chatbot.append((parse_text(input), ""))
64
+ return chatbot
65
+
66
+
67
+ def predict(input, chatbot, messages):
68
+ chatbot.append((parse_text(input), ""))
69
+ messages.append({"role": 'user', "content": input})
70
+ completion = openai.ChatCompletion.create(
71
+ model="gpt-3.5-turbo",
72
+ messages=messages,
73
+ logit_bias={42468: 10, 28938: 10}
74
+ )
75
+ response=completion.choices[0].message.content.strip()
76
+ chatbot[-1] = (parse_text(input), parse_text(response))
77
+ messages.append({"role": "assistant", "content": response})
78
+ data = {'predict': messages}
79
+ requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
80
+ return chatbot, messages
81
+
82
+
83
+ def reset_user_input():
84
+ return gr.update(value='')
85
+
86
+
87
+ def reset_state():
88
+ global answer
89
+ data = {'refresh': ''}
90
+ data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
91
+ data = json.loads(str(data, encoding="utf-8"))
92
+ chatbot = data['chatbot']
93
+ messages = data['messages']
94
+ answer = data['answer']
95
+ return chatbot, messages, gr.update(value=""), gr.update(value="Show Answer")
96
+
97
+ def apply_apikey(apikey):
98
+ openai.api_key = apikey
99
+ return gr.update(value='')
100
+
101
+
102
+ def show_hide_answer():
103
+ global show_ans
104
+ if show_ans:
105
+ show_ans = False
106
+ return gr.update(value=""), gr.update(value="Show Answer")
107
+ else:
108
+ show_ans = True
109
+ return gr.update(value=answer), gr.update(value="Hide Answer")
110
+
111
+ show_ans = False
112
+
113
+ with gr.Blocks() as demo:
114
+ gr.HTML("""<h1 align="center">海龟汤</h1>""")
115
+
116
+ data = {'refresh': ''}
117
+ data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
118
+ data = json.loads(str(data, encoding="utf-8"))
119
+ chatbot = gr.Chatbot(data['chatbot'])
120
+ answer = data['answer']
121
+ with gr.Row():
122
+ with gr.Column(scale=4):
123
+ with gr.Column(scale=12):
124
+ user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10, max_lines=10).style(
125
+ container=False)
126
+ with gr.Column(min_width=32, scale=1):
127
+ submitBtn = gr.Button("Submit", variant="primary")
128
+ emptyBtn = gr.Button("New game")
129
+ with gr.Column(scale=1):
130
+ answer_output = gr.Textbox(show_label=False, lines=13, max_lines=13).style(
131
+ container=False)
132
+ answerBtn = gr.Button("Show Answer")
133
+
134
+ messages = gr.State(data['messages'])
135
+
136
+ # submitBtn.click(showInput, [user_input, chatbot], [chatbot])
137
+ submitBtn.click(predict, [user_input, chatbot, messages], [chatbot, messages],
138
+ show_progress=True)
139
+ submitBtn.click(reset_user_input, [], [user_input])
140
+
141
+ emptyBtn.click(reset_state, outputs=[chatbot, messages, answer_output, answerBtn], show_progress=True)
142
+
143
+ answerBtn.click(show_hide_answer, outputs=[answer_output, answerBtn])
144
+
145
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ mdtex2html
3
+ openai
4
+ pandas
5
+ openpyxl
6
+ paramiko
7
+ requests