Spaces:
Runtime error
Runtime error
dev(hansbug): update
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
-
import logging
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
from llmriddles.questions import QuestionExecutor
|
| 8 |
from llmriddles.questions import list_ordered_questions
|
| 9 |
|
| 10 |
-
|
| 11 |
count = 0
|
| 12 |
_QUESTIONS = list_ordered_questions()
|
| 13 |
_LANG = os.environ.get('QUESTION_LANG', 'cn')
|
|
@@ -131,6 +131,11 @@ if __name__ == '__main__':
|
|
| 131 |
with gr.Row():
|
| 132 |
gr_submit = gr.Button(submit_label, interactive=False)
|
| 133 |
gr_next = gr.Button(next_label)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
with gr.Column():
|
| 136 |
gr_uuid = gr.Text(value='', visible=False)
|
|
@@ -140,46 +145,91 @@ if __name__ == '__main__':
|
|
| 140 |
gr.Markdown(tos_markdown)
|
| 141 |
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
def _next_question(uuid_):
|
| 144 |
global count
|
| 145 |
if not uuid_:
|
| 146 |
uuid_ = str(uuid.uuid4())
|
| 147 |
count += 1
|
| 148 |
logging.info(f'Player {count} starts the game now')
|
| 149 |
-
global
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
_qid += 1
|
| 152 |
-
|
| 153 |
|
| 154 |
if _qid >= len(_QUESTIONS):
|
| 155 |
-
del
|
| 156 |
logging.info(f'Player {count} has passed the game now')
|
| 157 |
return game_cleared_label, '', '', {}, '', \
|
| 158 |
gr.Button(submit_label, interactive=False), \
|
| 159 |
gr.Button(try_again_label, interactive=True), \
|
| 160 |
-
''
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
else:
|
| 162 |
executor = QuestionExecutor(_QUESTIONS[_qid], _LANG)
|
| 163 |
return executor.question_text, '', '', {}, '', \
|
| 164 |
gr.Button(submit_label, interactive=True), \
|
| 165 |
gr.Button(next_label, interactive=False), \
|
| 166 |
-
uuid_
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
gr_next.click(
|
| 169 |
fn=_next_question,
|
| 170 |
inputs=[gr_uuid],
|
| 171 |
outputs=[
|
| 172 |
gr_requirement, gr_question, gr_answer,
|
| 173 |
-
gr_predict, gr_explanation, gr_submit, gr_next,
|
|
|
|
| 174 |
],
|
| 175 |
)
|
| 176 |
|
| 177 |
|
| 178 |
def _submit_answer(qs_text: str, api_key: str, uuid_: str):
|
|
|
|
| 179 |
if _need_api_key() and not api_key:
|
| 180 |
raise gr.Error(api_error_info)
|
| 181 |
|
| 182 |
-
_qid =
|
| 183 |
executor = QuestionExecutor(
|
| 184 |
_QUESTIONS[_qid], _LANG,
|
| 185 |
llm=_LLM, llm_cfgs=_get_api_key_cfgs(api_key) if _need_api_key() else {'api_key': _LLM_KEY}
|
|
@@ -187,10 +237,12 @@ if __name__ == '__main__':
|
|
| 187 |
answer_text, correctness, explanation = executor.check(qs_text)
|
| 188 |
labels = {correct_label: 1.0} if correctness else {wrong_label: 1.0}
|
| 189 |
if correctness:
|
|
|
|
| 190 |
return answer_text, labels, explanation, gr.Button(next_label, interactive=True), uuid_
|
| 191 |
else:
|
| 192 |
return answer_text, labels, explanation, gr.Button(next_label, interactive=False), uuid_
|
| 193 |
|
|
|
|
| 194 |
gr_submit.click(
|
| 195 |
_submit_answer,
|
| 196 |
inputs=[gr_question, gr_api_key, gr_uuid],
|
|
|
|
| 1 |
+
import logging
|
| 2 |
import os
|
| 3 |
import uuid
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
from llmriddles.questions import QuestionExecutor
|
| 8 |
from llmriddles.questions import list_ordered_questions
|
| 9 |
|
| 10 |
+
_QUESTION_SESSIONS = {}
|
| 11 |
count = 0
|
| 12 |
_QUESTIONS = list_ordered_questions()
|
| 13 |
_LANG = os.environ.get('QUESTION_LANG', 'cn')
|
|
|
|
| 131 |
with gr.Row():
|
| 132 |
gr_submit = gr.Button(submit_label, interactive=False)
|
| 133 |
gr_next = gr.Button(next_label)
|
| 134 |
+
with gr.Row():
|
| 135 |
+
gr_select = gr.Radio(
|
| 136 |
+
choices=[(QuestionExecutor(q, _LANG).question_name, i) for i, q in enumerate(_QUESTIONS)],
|
| 137 |
+
label='ffffff'
|
| 138 |
+
)
|
| 139 |
|
| 140 |
with gr.Column():
|
| 141 |
gr_uuid = gr.Text(value='', visible=False)
|
|
|
|
| 145 |
gr.Markdown(tos_markdown)
|
| 146 |
|
| 147 |
|
| 148 |
+
def _radio_select(uuid_, select_qid):
|
| 149 |
+
global count
|
| 150 |
+
if not uuid_:
|
| 151 |
+
uuid_ = str(uuid.uuid4())
|
| 152 |
+
count += 1
|
| 153 |
+
logging.info(f'Player {count} starts the game now')
|
| 154 |
+
global _QUESTION_SESSIONS
|
| 155 |
+
if uuid_ not in _QUESTION_SESSIONS:
|
| 156 |
+
_QUESTION_SESSIONS[uuid_] = set(), select_qid
|
| 157 |
+
else:
|
| 158 |
+
_exists, _ = _QUESTION_SESSIONS[uuid_]
|
| 159 |
+
_QUESTION_SESSIONS[uuid_] = _exists, select_qid
|
| 160 |
+
|
| 161 |
+
executor = QuestionExecutor(_QUESTIONS[select_qid], _LANG)
|
| 162 |
+
return executor.question_text, '', '', {}, '', \
|
| 163 |
+
gr.Button(submit_label, interactive=True), \
|
| 164 |
+
gr.Button(next_label, interactive=False), \
|
| 165 |
+
uuid_
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
gr_select.select(
|
| 169 |
+
_radio_select,
|
| 170 |
+
inputs=[gr_uuid, gr_select],
|
| 171 |
+
outputs=[
|
| 172 |
+
gr_requirement, gr_question, gr_answer,
|
| 173 |
+
gr_predict, gr_explanation, gr_submit, gr_next, gr_uuid,
|
| 174 |
+
],
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
def _next_question(uuid_):
|
| 179 |
global count
|
| 180 |
if not uuid_:
|
| 181 |
uuid_ = str(uuid.uuid4())
|
| 182 |
count += 1
|
| 183 |
logging.info(f'Player {count} starts the game now')
|
| 184 |
+
global _QUESTION_SESSIONS
|
| 185 |
+
if uuid_ in _QUESTION_SESSIONS:
|
| 186 |
+
_exists, _qid = _QUESTION_SESSIONS[uuid_]
|
| 187 |
+
else:
|
| 188 |
+
_exists, _qid = set(), -1
|
| 189 |
_qid += 1
|
| 190 |
+
_QUESTION_SESSIONS[uuid_] = _exists, _qid
|
| 191 |
|
| 192 |
if _qid >= len(_QUESTIONS):
|
| 193 |
+
del _QUESTION_SESSIONS[uuid_]
|
| 194 |
logging.info(f'Player {count} has passed the game now')
|
| 195 |
return game_cleared_label, '', '', {}, '', \
|
| 196 |
gr.Button(submit_label, interactive=False), \
|
| 197 |
gr.Button(try_again_label, interactive=True), \
|
| 198 |
+
'', \
|
| 199 |
+
gr.Radio(
|
| 200 |
+
choices=[(QuestionExecutor(q, _LANG).question_name, i) for i, q in enumerate(_QUESTIONS)],
|
| 201 |
+
label='ffffff'
|
| 202 |
+
)
|
| 203 |
else:
|
| 204 |
executor = QuestionExecutor(_QUESTIONS[_qid], _LANG)
|
| 205 |
return executor.question_text, '', '', {}, '', \
|
| 206 |
gr.Button(submit_label, interactive=True), \
|
| 207 |
gr.Button(next_label, interactive=False), \
|
| 208 |
+
uuid_, \
|
| 209 |
+
gr.Radio(
|
| 210 |
+
choices=[(QuestionExecutor(q, _LANG).question_name, i) for i, q in enumerate(_QUESTIONS)],
|
| 211 |
+
value=_qid,
|
| 212 |
+
label='ffffff'
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
|
| 216 |
gr_next.click(
|
| 217 |
fn=_next_question,
|
| 218 |
inputs=[gr_uuid],
|
| 219 |
outputs=[
|
| 220 |
gr_requirement, gr_question, gr_answer,
|
| 221 |
+
gr_predict, gr_explanation, gr_submit, gr_next,
|
| 222 |
+
gr_uuid, gr_select,
|
| 223 |
],
|
| 224 |
)
|
| 225 |
|
| 226 |
|
| 227 |
def _submit_answer(qs_text: str, api_key: str, uuid_: str):
|
| 228 |
+
global _QUESTION_SESSIONS
|
| 229 |
if _need_api_key() and not api_key:
|
| 230 |
raise gr.Error(api_error_info)
|
| 231 |
|
| 232 |
+
_exists, _qid = _QUESTION_SESSIONS[uuid_]
|
| 233 |
executor = QuestionExecutor(
|
| 234 |
_QUESTIONS[_qid], _LANG,
|
| 235 |
llm=_LLM, llm_cfgs=_get_api_key_cfgs(api_key) if _need_api_key() else {'api_key': _LLM_KEY}
|
|
|
|
| 237 |
answer_text, correctness, explanation = executor.check(qs_text)
|
| 238 |
labels = {correct_label: 1.0} if correctness else {wrong_label: 1.0}
|
| 239 |
if correctness:
|
| 240 |
+
_QUESTION_SESSIONS[uuid_] = (_exists | {_qid}), _qid
|
| 241 |
return answer_text, labels, explanation, gr.Button(next_label, interactive=True), uuid_
|
| 242 |
else:
|
| 243 |
return answer_text, labels, explanation, gr.Button(next_label, interactive=False), uuid_
|
| 244 |
|
| 245 |
+
|
| 246 |
gr_submit.click(
|
| 247 |
_submit_answer,
|
| 248 |
inputs=[gr_question, gr_api_key, gr_uuid],
|