Spaces:
Runtime error
Runtime error
mkw18
commited on
Commit
·
b93eac7
1
Parent(s):
7d12d67
refine
Browse files- app.py +139 -39
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,13 +1,12 @@
|
|
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 schedule
|
9 |
import openai
|
10 |
import requests
|
|
|
11 |
|
12 |
openai.api_key = os.environ.get('APIKEY')
|
13 |
rd.seed(time.time())
|
@@ -64,39 +63,137 @@ def showInput(input, chatbot):
|
|
64 |
return chatbot
|
65 |
|
66 |
|
67 |
-
def predict(input, chatbot, messages, idx, answer):
|
68 |
chatbot.append((parse_text(input), ""))
|
69 |
-
messages.
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
else:
|
76 |
-
completion = openai.ChatCompletion.create(
|
77 |
-
model="gpt-3.5-turbo",
|
78 |
-
messages=messages,
|
79 |
-
logit_bias={42468: 10, 28938: 10}
|
80 |
-
)
|
81 |
-
response=completion.choices[0].message.content.strip()
|
82 |
messages.append({"role": "assistant", "content": response})
|
83 |
-
|
84 |
-
# messages1 = messages + [{"role": 'user', "content": '我猜到汤底了吗?请回答是或否,注意仅出现“汤底”二字或者我并没有问到汤底相关细节时不能说明我猜到了汤底。'}]
|
85 |
-
# completion1 = openai.ChatCompletion.create(
|
86 |
-
# model="gpt-3.5-turbo",
|
87 |
-
# messages=messages1,
|
88 |
-
# )
|
89 |
-
# response1=completion1.choices[0].message.content.strip()
|
90 |
-
# if '不' in response1 or '否' in response1 or '没' in response1:
|
91 |
-
# response = response
|
92 |
-
# finish = False
|
93 |
-
# else:
|
94 |
-
# response += f'恭喜您已经猜到汤底,汤底是:{answer}\n点击“再来一局”按钮开始下一局游戏。'
|
95 |
-
# finish = True
|
96 |
-
data = {'predict': messages, 'idx': idx, 'isfinished': finish, 'answer': answer}
|
97 |
requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
98 |
chatbot[-1] = (parse_text(input), parse_text(response))
|
99 |
-
return chatbot, messages
|
100 |
|
101 |
|
102 |
def reset_user_input():
|
@@ -119,8 +216,10 @@ def reset_state(request: gr.Request):
|
|
119 |
chatbot = data['chatbot']
|
120 |
messages = data['messages']
|
121 |
answer = data['answer']
|
|
|
|
|
122 |
idx = data['idx']
|
123 |
-
return chatbot, messages, gr.update(value=""), gr.update(value="显示答案"), answer, idx, gr.update(value=data['story'].strip()), False
|
124 |
|
125 |
|
126 |
def show_hide_answer(answer, show_ans):
|
@@ -139,16 +238,18 @@ with gr.Blocks() as demo:
|
|
139 |
|
140 |
with gr.Row():
|
141 |
rule = gr.Textbox(label='规则', value='海龟汤是一个推理类游戏,游戏开始时会给出一段隐去关键信息的叙述,即汤面,玩家根据汤面推理,提出能够通过“是”或“否”来回答的问题,通过提问不同可能性,缩小真相的范围,直到最终猜到真相(即汤底)的关键信息。玩家可以点击“再来一局”按钮随机一场新的游戏,点击“显示答案”可查看汤底。', lines=1, max_lines=3).style(container=False)
|
142 |
-
data = {'refresh': ''}
|
143 |
-
|
144 |
-
data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
|
145 |
-
data = json.loads(str(data, encoding="utf-8"))
|
146 |
chatbot = gr.Chatbot([(None, '点击“再来一局”开始游戏')])
|
|
|
147 |
answer = gr.State('点击“再来一局”开始游戏')
|
148 |
idx = gr.State(0)
|
149 |
show_ans = gr.State(False)
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
|
152 |
with gr.Row():
|
153 |
with gr.Column(scale=4):
|
154 |
question = gr.Textbox(label='汤面', value='点击“再来一局”开始游戏',
|
@@ -166,13 +267,12 @@ with gr.Blocks() as demo:
|
|
166 |
container=False)
|
167 |
answerBtn = gr.Button("显示答案")
|
168 |
|
169 |
-
messages = gr.State(data['messages'])
|
170 |
|
171 |
# submitBtn.click(showInput, [user_input, chatbot], [chatbot])
|
172 |
-
user_input.submit(predict, [user_input, chatbot, messages, idx, answer], [chatbot, messages],
|
173 |
show_progress=True)
|
174 |
user_input.submit(reset_user_input, [], [user_input])
|
175 |
-
submitBtn.click(predict, [user_input, chatbot, messages, idx, answer], [chatbot, messages],
|
176 |
show_progress=True)
|
177 |
submitBtn.click(reset_user_input, [], [user_input])
|
178 |
|
|
|
1 |
import gradio as gr
|
2 |
import mdtex2html
|
|
|
3 |
import random as rd
|
4 |
import os
|
5 |
import json
|
6 |
import time
|
|
|
7 |
import openai
|
8 |
import requests
|
9 |
+
from nltk.translate.bleu_score import sentence_bleu
|
10 |
|
11 |
openai.api_key = os.environ.get('APIKEY')
|
12 |
rd.seed(time.time())
|
|
|
63 |
return chatbot
|
64 |
|
65 |
|
66 |
+
def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history):
|
67 |
chatbot.append((parse_text(input), ""))
|
68 |
+
messages1 = messages[:10].copy()
|
69 |
+
if len(known) > 0:
|
70 |
+
messages1 += [{"role": 'user', "content": f"{' '.join(known)}\n请回答是或否或无关。"}, {"role": "assistant", "content": '是。'}, {"role": 'user', "content": f"{input}\n请回答是或否或无关。"}]
|
71 |
+
else:
|
72 |
+
messages1 += [{"role": 'user', "content": f"{input}\n请回答是或否或无关。"}]
|
73 |
+
messages.append({"role": 'user', "content": f"{input}\n请回答是或否或无关。"})
|
74 |
+
llm = True
|
75 |
+
finished = False
|
76 |
+
response = ''
|
77 |
+
for key in story_key:
|
78 |
+
key = key.strip()
|
79 |
+
if ' ' in key:
|
80 |
+
key = key.split(' ')[1]
|
81 |
+
bleu = sentence_bleu(key, input.replace('?', '。'), weights=(1, 0, 0, 0))
|
82 |
+
print(bleu)
|
83 |
+
if bleu > 0.85:
|
84 |
+
response = '这是汤面中已有的信息,请提一个新问题。'
|
85 |
+
llm = False
|
86 |
+
break
|
87 |
+
if llm:
|
88 |
+
for key in history:
|
89 |
+
key = key.strip()
|
90 |
+
if ' ' in key:
|
91 |
+
key = key.split(' ')[1]
|
92 |
+
bleu = sentence_bleu(key, input.replace('?', '。'), weights=(1, 0, 0, 0))
|
93 |
+
print(bleu)
|
94 |
+
if bleu > 0.85:
|
95 |
+
response = '这是已经提问过的内容,请提一个新问题。'
|
96 |
+
llm = False
|
97 |
+
break
|
98 |
+
if llm:
|
99 |
+
history.append(input.replace('?', '。'))
|
100 |
+
data = {'predict': messages, 'idx': idx, 'isfinished': False, 'answer': answer, 'known': known, 'history': history, 'reasoning': reasoning, 'answer_key': answer_key, 'bingo': bingo}
|
101 |
+
response=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
102 |
+
if response.status_code == 200:
|
103 |
+
data = json.loads(str(response.content, encoding="utf-8"))
|
104 |
+
response = data['response']
|
105 |
+
known = data['known']
|
106 |
+
history = data['history']
|
107 |
+
reasoning = data['reasoning']
|
108 |
+
answer_key = data['answer_key']
|
109 |
+
bingo = data['bingo']
|
110 |
+
chatbot[-1] = (parse_text(input), parse_text(response))
|
111 |
+
messages.append({"role": "assistant", "content": response})
|
112 |
+
else:
|
113 |
+
completion = openai.ChatCompletion.create(
|
114 |
+
model="gpt-3.5-turbo",
|
115 |
+
messages=messages1,
|
116 |
+
)
|
117 |
+
response=completion.choices[0].message.content.strip()
|
118 |
+
relevant = False
|
119 |
+
if response.startswith("是"):
|
120 |
+
summary = openai.ChatCompletion.create(
|
121 |
+
model="gpt-3.5-turbo",
|
122 |
+
messages=[{"role": "user", "content": f"请将以下内容转述为陈述句,并简化为一句话:\n{input}"}],
|
123 |
+
)
|
124 |
+
summary = summary.choices[0].message.content.strip()
|
125 |
+
print(summary)
|
126 |
+
relevant = True
|
127 |
+
elif response.startswith("不是") or response.startswith("否"):
|
128 |
+
summary = openai.ChatCompletion.create(
|
129 |
+
model="gpt-3.5-turbo",
|
130 |
+
messages=[{"role": "user", "content": f"请将以下内容取反义然后转述为陈述句,并简化为一句话:\n{input}"}],
|
131 |
+
)
|
132 |
+
summary = summary.choices[0].message.content.strip()
|
133 |
+
print(summary)
|
134 |
+
relevant = True
|
135 |
+
if relevant:
|
136 |
+
history.append(summary)
|
137 |
+
known.append(summary)
|
138 |
+
reasoning.append(summary)
|
139 |
+
if len(reasoning) >= 2:
|
140 |
+
merge = openai.ChatCompletion.create(
|
141 |
+
model="gpt-3.5-turbo",
|
142 |
+
messages=[{"role": "user", "content": f"请将以下内容简化为一句话:\n{' '.join(reasoning)}"}],
|
143 |
+
)
|
144 |
+
merge = merge.choices[0].message.content.strip()
|
145 |
+
else:
|
146 |
+
merge = summary
|
147 |
+
for key in answer_key:
|
148 |
+
key = key.strip()
|
149 |
+
if ' ' in key:
|
150 |
+
key1 = key.split(' ')[1]
|
151 |
+
else:
|
152 |
+
key1 = key
|
153 |
+
if len(input.replace('?', '')) < len(key1):
|
154 |
+
continue
|
155 |
+
compare = openai.ChatCompletion.create(
|
156 |
+
model="gpt-3.5-turbo",
|
157 |
+
messages=[{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}],
|
158 |
+
)
|
159 |
+
compare = compare.choices[0].message.content.strip()
|
160 |
+
if compare.startswith('是'):
|
161 |
+
vote = 1
|
162 |
+
comp_msg = [{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{input.replace('?', '。')}"},{"role": "assistant", "content": "是"},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{input.replace('?', '。')}"}]
|
163 |
+
compare = openai.ChatCompletion.create(
|
164 |
+
model="gpt-3.5-turbo",
|
165 |
+
messages=comp_msg,
|
166 |
+
)
|
167 |
+
compare = compare.choices[0].message.content.strip()
|
168 |
+
if compare.startswith('是'):
|
169 |
+
vote += 1
|
170 |
+
comp_msg += [{"role": "assistant", "content": compare},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{input.replace('?', '。')}"}]
|
171 |
+
compare = openai.ChatCompletion.create(
|
172 |
+
model="gpt-3.5-turbo",
|
173 |
+
messages=comp_msg,
|
174 |
+
)
|
175 |
+
compare = compare.choices[0].message.content.strip()
|
176 |
+
if compare.startswith('是'):
|
177 |
+
vote += 1
|
178 |
+
if vote >= 2:
|
179 |
+
bingo += 1
|
180 |
+
print(key)
|
181 |
+
answer_key.remove(key)
|
182 |
+
reasoning = []
|
183 |
+
break
|
184 |
+
if bingo >= len(answer_key):
|
185 |
+
finished = True
|
186 |
+
response += f'恭喜你猜到了汤底,汤底是:{answer}\n点击"再来一局"按钮开始下一局游戏。'
|
187 |
+
messages.append({"role": "assistant", "content": response})
|
188 |
+
data = {'predict': messages, 'idx': idx, 'isfinished': finished, 'answer': answer}
|
189 |
+
requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
190 |
+
chatbot[-1] = (parse_text(input), parse_text(response))
|
191 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
messages.append({"role": "assistant", "content": response})
|
193 |
+
data = {'predict': messages, 'idx': idx, 'isfinished': finished, 'answer': answer}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
195 |
chatbot[-1] = (parse_text(input), parse_text(response))
|
196 |
+
return chatbot, messages, known, bingo, reasoning, history
|
197 |
|
198 |
|
199 |
def reset_user_input():
|
|
|
216 |
chatbot = data['chatbot']
|
217 |
messages = data['messages']
|
218 |
answer = data['answer']
|
219 |
+
story_key = data['story_key']
|
220 |
+
answer_key = data['answer_key']
|
221 |
idx = data['idx']
|
222 |
+
return chatbot, messages, gr.update(value=""), gr.update(value="显示答案"), answer, idx, gr.update(value=data['story'].strip()), False, story_key, answer_key, [], 0, [], []
|
223 |
|
224 |
|
225 |
def show_hide_answer(answer, show_ans):
|
|
|
238 |
|
239 |
with gr.Row():
|
240 |
rule = gr.Textbox(label='规则', value='海龟汤是一个推理类游戏,游戏开始时会给出一段隐去关键信息的叙述,即汤面,玩家根据汤面推理,提出能够通过“是”或“否”来回答的问题,通过提问不同可能性,缩小真相的范围,直到最终猜到真相(即汤底)的关键信息。玩家可以点击“再来一局”按钮随机一场新的游戏,点击“显示答案”可查看汤底。', lines=1, max_lines=3).style(container=False)
|
|
|
|
|
|
|
|
|
241 |
chatbot = gr.Chatbot([(None, '点击“再来一局”开始游戏')])
|
242 |
+
messages = gr.State([])
|
243 |
answer = gr.State('点击“再来一局”开始游戏')
|
244 |
idx = gr.State(0)
|
245 |
show_ans = gr.State(False)
|
246 |
+
known = gr.State([])
|
247 |
+
story_key = gr.State([])
|
248 |
+
answer_key = gr.State([])
|
249 |
+
bingo = gr.State(0)
|
250 |
+
reasoning = gr.State([])
|
251 |
+
history = gr.State([])
|
252 |
|
|
|
253 |
with gr.Row():
|
254 |
with gr.Column(scale=4):
|
255 |
question = gr.Textbox(label='汤面', value='点击“再来一局”开始游戏',
|
|
|
267 |
container=False)
|
268 |
answerBtn = gr.Button("显示答案")
|
269 |
|
|
|
270 |
|
271 |
# submitBtn.click(showInput, [user_input, chatbot], [chatbot])
|
272 |
+
user_input.submit(predict, [user_input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history], [chatbot, messages, known, bingo, reasoning, history],
|
273 |
show_progress=True)
|
274 |
user_input.submit(reset_user_input, [], [user_input])
|
275 |
+
submitBtn.click(predict, [user_input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history], [chatbot, messages, known, bingo, reasoning, history],
|
276 |
show_progress=True)
|
277 |
submitBtn.click(reset_user_input, [], [user_input])
|
278 |
|
requirements.txt
CHANGED
@@ -5,4 +5,4 @@ pandas
|
|
5 |
openpyxl
|
6 |
paramiko
|
7 |
requests
|
8 |
-
|
|
|
5 |
openpyxl
|
6 |
paramiko
|
7 |
requests
|
8 |
+
nltk
|