Spaces:
Runtime error
Runtime error
mkw18
commited on
Commit
·
f997697
1
Parent(s):
093897f
debug
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ 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())
|
@@ -64,16 +65,19 @@ def showInput(input, 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": input})
|
74 |
llm = True
|
75 |
finished = False
|
76 |
response = ''
|
|
|
77 |
for key in story_key:
|
78 |
key = key.strip()
|
79 |
if ' ' in key:
|
@@ -83,6 +87,7 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
83 |
response = '这是汤面中已有的信息,请提一个新问题。'
|
84 |
llm = False
|
85 |
break
|
|
|
86 |
if llm:
|
87 |
for key in history:
|
88 |
key = key.strip()
|
@@ -93,10 +98,13 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
93 |
response = '这是已经提问过的内容,请提一个新问题。'
|
94 |
llm = False
|
95 |
break
|
|
|
96 |
if llm:
|
97 |
history.append(input.replace('?', '。'))
|
98 |
data = {'predict': messages1, 'idx': idx, 'isfinished': False, 'answer': answer}
|
|
|
99 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
|
|
100 |
if completion.status_code == 200:
|
101 |
response = str(completion.content, encoding="utf-8")
|
102 |
else:
|
@@ -105,11 +113,14 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
105 |
messages=messages1,
|
106 |
)
|
107 |
response=completion.choices[0].message.content.strip()
|
|
|
108 |
relevant = False
|
109 |
if response.startswith("是"):
|
110 |
decl_msg = [{"role": "user", "content": f"请将以下内容转述为陈述句,并简化为一句话:\n{input}"}]
|
111 |
data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
|
|
112 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
|
|
113 |
if completion.status_code == 200:
|
114 |
summary = str(completion.content, encoding="utf-8")
|
115 |
else:
|
@@ -118,11 +129,14 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
118 |
messages=decl_msg,
|
119 |
)
|
120 |
summary = summary.choices[0].message.content.strip()
|
|
|
121 |
relevant = True
|
122 |
elif response.startswith("不是") or response.startswith("否"):
|
123 |
decl_msg = [{"role": "user", "content": f"请将以下内容取反义然后转述为陈述句,并简化为一句话:\n{input}"}]
|
124 |
data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
|
|
125 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
|
|
126 |
if completion.status_code == 200:
|
127 |
summary = str(completion.content, encoding="utf-8")
|
128 |
else:
|
@@ -131,6 +145,7 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
131 |
messages=decl_msg
|
132 |
)
|
133 |
summary = summary.choices[0].message.content.strip()
|
|
|
134 |
relevant = True
|
135 |
if relevant:
|
136 |
history.append(summary)
|
@@ -139,7 +154,9 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
139 |
if len(reasoning) >= 2:
|
140 |
simp_msg = [{"role": "user", "content": f"请将以下内容简化为一句话:\n{' '.join(reasoning)}"}]
|
141 |
data = {'predict': simp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
|
|
142 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
|
|
143 |
if completion.status_code == 200:
|
144 |
merge = str(completion.content, encoding="utf-8")
|
145 |
else:
|
@@ -148,6 +165,7 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
148 |
messages=simp_msg,
|
149 |
)
|
150 |
merge = merge.choices[0].message.content.strip()
|
|
|
151 |
else:
|
152 |
merge = summary
|
153 |
for key in answer_key:
|
@@ -160,7 +178,9 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
160 |
continue
|
161 |
comp_msg = [{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
|
162 |
data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
|
|
163 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
|
|
164 |
if completion.status_code == 200:
|
165 |
compare = str(completion.content, encoding="utf-8")
|
166 |
else:
|
@@ -169,11 +189,14 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
169 |
messages=comp_msg
|
170 |
)
|
171 |
compare = compare.choices[0].message.content.strip()
|
|
|
172 |
if compare.startswith('是'):
|
173 |
vote = 1
|
174 |
comp_msg += [{"role": "assistant", "content": "是"},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
|
175 |
data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
|
|
176 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
|
|
177 |
if completion.status_code == 200:
|
178 |
compare = str(completion.content, encoding="utf-8")
|
179 |
else:
|
@@ -182,11 +205,14 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
182 |
messages=comp_msg,
|
183 |
)
|
184 |
compare = compare.choices[0].message.content.strip()
|
|
|
185 |
if compare.startswith('是'):
|
186 |
vote += 1
|
187 |
comp_msg += [{"role": "assistant", "content": compare},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
|
188 |
data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
|
|
189 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
|
|
190 |
if completion.status_code == 200:
|
191 |
compare = str(completion.content, encoding="utf-8")
|
192 |
else:
|
@@ -195,6 +221,7 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
195 |
messages=comp_msg,
|
196 |
)
|
197 |
compare = compare.choices[0].message.content.strip()
|
|
|
198 |
if compare.startswith('是'):
|
199 |
vote += 1
|
200 |
if vote >= 2:
|
@@ -203,13 +230,17 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
|
|
203 |
print(key)
|
204 |
reasoning = []
|
205 |
break
|
|
|
206 |
if bingo >= len(answer_key):
|
207 |
finished = True
|
208 |
response += f'恭喜你猜到了汤底,汤底是:{answer}\n点击"再来一局"按钮开始下一局游戏。'
|
|
|
209 |
messages.append({"role": "assistant", "content": response})
|
210 |
data = {'predict': messages, 'idx': idx, 'isfinished': finished, 'answer': answer}
|
|
|
211 |
requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
212 |
chatbot[-1] = (parse_text(input), parse_text(response))
|
|
|
213 |
return chatbot, messages, known, bingo, reasoning, history
|
214 |
|
215 |
|
|
|
7 |
import openai
|
8 |
import requests
|
9 |
from nltk.translate.bleu_score import sentence_bleu
|
10 |
+
import time
|
11 |
|
12 |
openai.api_key = os.environ.get('APIKEY')
|
13 |
rd.seed(time.time())
|
|
|
65 |
|
66 |
|
67 |
def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history):
|
68 |
+
start = time.time()
|
69 |
chatbot.append((parse_text(input), ""))
|
70 |
messages1 = messages[:10].copy()
|
71 |
if len(known) > 0:
|
72 |
messages1 += [{"role": 'user', "content": f"{' '.join(known)}\n请回答是或否或无关。"}, {"role": "assistant", "content": '是。'}, {"role": 'user', "content": f"{input}\n请回答是或否或无关。"}]
|
73 |
else:
|
74 |
messages1 += [{"role": 'user', "content": f"{input}\n请回答是或否或无关。"}]
|
75 |
+
print(f"Init: {time.time() - start}")
|
76 |
messages.append({"role": 'user', "content": input})
|
77 |
llm = True
|
78 |
finished = False
|
79 |
response = ''
|
80 |
+
print(f"Start judge: {time.time() - start}")
|
81 |
for key in story_key:
|
82 |
key = key.strip()
|
83 |
if ' ' in key:
|
|
|
87 |
response = '这是汤面中已有的信息,请提一个新问题。'
|
88 |
llm = False
|
89 |
break
|
90 |
+
print(f"Filter story: {time.time() - start}")
|
91 |
if llm:
|
92 |
for key in history:
|
93 |
key = key.strip()
|
|
|
98 |
response = '这是已经提问过的内容,请提一个新问题。'
|
99 |
llm = False
|
100 |
break
|
101 |
+
print(f"Filter history: {time.time() - start}")
|
102 |
if llm:
|
103 |
history.append(input.replace('?', '。'))
|
104 |
data = {'predict': messages1, 'idx': idx, 'isfinished': False, 'answer': answer}
|
105 |
+
print(f"Start Request 1: {time.time() - start}")
|
106 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
107 |
+
print(f"Request 1: {time.time() - start}")
|
108 |
if completion.status_code == 200:
|
109 |
response = str(completion.content, encoding="utf-8")
|
110 |
else:
|
|
|
113 |
messages=messages1,
|
114 |
)
|
115 |
response=completion.choices[0].message.content.strip()
|
116 |
+
print(f"Request openai 1: {time.time() - start}")
|
117 |
relevant = False
|
118 |
if response.startswith("是"):
|
119 |
decl_msg = [{"role": "user", "content": f"请将以下内容转述为陈述句,并简化为一句话:\n{input}"}]
|
120 |
data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
121 |
+
print(f"Start Request 2: {time.time() - start}")
|
122 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
123 |
+
print(f"Request 2: {time.time() - start}")
|
124 |
if completion.status_code == 200:
|
125 |
summary = str(completion.content, encoding="utf-8")
|
126 |
else:
|
|
|
129 |
messages=decl_msg,
|
130 |
)
|
131 |
summary = summary.choices[0].message.content.strip()
|
132 |
+
print(f"Request openai 2: {time.time() - start}")
|
133 |
relevant = True
|
134 |
elif response.startswith("不是") or response.startswith("否"):
|
135 |
decl_msg = [{"role": "user", "content": f"请将以下内容取反义然后转述为陈述句,并简化为一句话:\n{input}"}]
|
136 |
data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
137 |
+
print(f"Start Request 2: {time.time() - start}")
|
138 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
139 |
+
print(f"Request 2: {time.time() - start}")
|
140 |
if completion.status_code == 200:
|
141 |
summary = str(completion.content, encoding="utf-8")
|
142 |
else:
|
|
|
145 |
messages=decl_msg
|
146 |
)
|
147 |
summary = summary.choices[0].message.content.strip()
|
148 |
+
print(f"Request openai 2: {time.time() - start}")
|
149 |
relevant = True
|
150 |
if relevant:
|
151 |
history.append(summary)
|
|
|
154 |
if len(reasoning) >= 2:
|
155 |
simp_msg = [{"role": "user", "content": f"请将以下内容简化为一句话:\n{' '.join(reasoning)}"}]
|
156 |
data = {'predict': simp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
157 |
+
print(f"Start Request 3: {time.time() - start}")
|
158 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
159 |
+
print(f"Request 3: {time.time() - start}")
|
160 |
if completion.status_code == 200:
|
161 |
merge = str(completion.content, encoding="utf-8")
|
162 |
else:
|
|
|
165 |
messages=simp_msg,
|
166 |
)
|
167 |
merge = merge.choices[0].message.content.strip()
|
168 |
+
print(f"Request openai 3: {time.time() - start}")
|
169 |
else:
|
170 |
merge = summary
|
171 |
for key in answer_key:
|
|
|
178 |
continue
|
179 |
comp_msg = [{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
|
180 |
data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
181 |
+
print(f"Start Request 4: {time.time() - start}")
|
182 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
183 |
+
print(f"Request 4: {time.time() - start}")
|
184 |
if completion.status_code == 200:
|
185 |
compare = str(completion.content, encoding="utf-8")
|
186 |
else:
|
|
|
189 |
messages=comp_msg
|
190 |
)
|
191 |
compare = compare.choices[0].message.content.strip()
|
192 |
+
print(f"Request openai 4: {time.time() - start}")
|
193 |
if compare.startswith('是'):
|
194 |
vote = 1
|
195 |
comp_msg += [{"role": "assistant", "content": "是"},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
|
196 |
data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
197 |
+
print(f"Start Request 5: {time.time() - start}")
|
198 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
199 |
+
print(f"Request 5: {time.time() - start}")
|
200 |
if completion.status_code == 200:
|
201 |
compare = str(completion.content, encoding="utf-8")
|
202 |
else:
|
|
|
205 |
messages=comp_msg,
|
206 |
)
|
207 |
compare = compare.choices[0].message.content.strip()
|
208 |
+
print(f"Request openai 5: {time.time() - start}")
|
209 |
if compare.startswith('是'):
|
210 |
vote += 1
|
211 |
comp_msg += [{"role": "assistant", "content": compare},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
|
212 |
data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
|
213 |
+
print(f"Start Request 6: {time.time() - start}")
|
214 |
completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
215 |
+
print(f"Request 6: {time.time() - start}")
|
216 |
if completion.status_code == 200:
|
217 |
compare = str(completion.content, encoding="utf-8")
|
218 |
else:
|
|
|
221 |
messages=comp_msg,
|
222 |
)
|
223 |
compare = compare.choices[0].message.content.strip()
|
224 |
+
print(f"Request openai 6: {time.time() - start}")
|
225 |
if compare.startswith('是'):
|
226 |
vote += 1
|
227 |
if vote >= 2:
|
|
|
230 |
print(key)
|
231 |
reasoning = []
|
232 |
break
|
233 |
+
print(f"Finish compare: {time.time() - start}")
|
234 |
if bingo >= len(answer_key):
|
235 |
finished = True
|
236 |
response += f'恭喜你猜到了汤底,汤底是:{answer}\n点击"再来一局"按钮开始下一局游戏。'
|
237 |
+
print(f"Finish bingo: {time.time() - start}")
|
238 |
messages.append({"role": "assistant", "content": response})
|
239 |
data = {'predict': messages, 'idx': idx, 'isfinished': finished, 'answer': answer}
|
240 |
+
print(f"Finish predict: {time.time() - start}")
|
241 |
requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
242 |
chatbot[-1] = (parse_text(input), parse_text(response))
|
243 |
+
print(f"Finish save: {time.time() - start}")
|
244 |
return chatbot, messages, known, bingo, reasoning, history
|
245 |
|
246 |
|