HansBug commited on
Commit
0497fcb
1 Parent(s): 888a212

dev(hansbug): add uuid_ to page

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
 
3
  import gradio as gr
4
 
@@ -6,6 +7,7 @@ from llmriddles.questions import QuestionExecutor
6
  from llmriddles.questions import list_ordered_questions
7
 
8
  _QUESTION_ID = -1
 
9
  _QUESTIONS = list_ordered_questions()
10
  _LANG = os.environ.get('QUESTION_LANG', 'cn')
11
  _LLM = os.environ.get('QUESTION_LLM', 'chatgpt')
@@ -34,54 +36,67 @@ if __name__ == '__main__':
34
  with gr.Column():
35
  gr_api_key = gr.Text(placeholder='Your API Key', label='API Key', type='password',
36
  visible=_need_api_key())
 
37
  gr_predict = gr.Label(label='Correctness')
38
  gr_explanation = gr.TextArea(label='Explanation')
39
  gr_next = gr.Button('Next')
40
 
41
 
42
- def _next_question():
43
- global _QUESTION_ID
44
- _QUESTION_ID += 1
 
 
 
 
 
45
 
46
  if _QUESTION_ID >= len(_QUESTIONS):
47
  return 'Congratulations!', '', '', {}, '', \
48
  gr.Button('Submit', interactive=False), \
49
- gr.Button('Next', interactive=False)
 
50
  else:
51
  executor = QuestionExecutor(_QUESTIONS[_QUESTION_ID], _LANG)
52
  return executor.question_text, '', '', {}, '', \
53
  gr.Button('Submit', interactive=True), \
54
- gr.Button('Next', interactive=False)
 
55
 
56
 
57
  gr_next.click(
58
  fn=_next_question,
59
- inputs=[],
60
- outputs=[gr_requirement, gr_question, gr_answer, gr_predict, gr_explanation, gr_submit, gr_next],
 
 
 
61
  )
62
 
63
 
64
- def _submit_answer(qs_text: str, api_key: str):
65
  if _need_api_key() and not api_key:
66
  return '---', {}, 'Please Enter API Key Before Submitting Question.', \
67
  gr.Button('Next', interactive=False)
68
 
 
 
69
  executor = QuestionExecutor(
70
- _QUESTIONS[_QUESTION_ID], _LANG,
71
  llm=_LLM, llm_cfgs=_get_api_key_cfgs(api_key) if _need_api_key() else {}
72
  )
73
  answer_text, correctness, explanation = executor.check(qs_text)
74
  labels = {'Correct': 1.0} if correctness else {'Wrong': 1.0}
75
  if correctness:
76
- return answer_text, labels, explanation, gr.Button('Next', interactive=True)
77
  else:
78
- return answer_text, labels, explanation, gr.Button('Next', interactive=False)
79
 
80
 
81
  gr_submit.click(
82
  _submit_answer,
83
- inputs=[gr_question, gr_api_key],
84
- outputs=[gr_answer, gr_predict, gr_explanation, gr_next],
85
  )
86
 
87
  demo.launch()
 
1
  import os
2
+ import uuid
3
 
4
  import gradio as gr
5
 
 
7
  from llmriddles.questions import list_ordered_questions
8
 
9
  _QUESTION_ID = -1
10
+ _QUESTION_IDS = {}
11
  _QUESTIONS = list_ordered_questions()
12
  _LANG = os.environ.get('QUESTION_LANG', 'cn')
13
  _LLM = os.environ.get('QUESTION_LLM', 'chatgpt')
 
36
  with gr.Column():
37
  gr_api_key = gr.Text(placeholder='Your API Key', label='API Key', type='password',
38
  visible=_need_api_key())
39
+ gr_uuid = gr.Text(value='')
40
  gr_predict = gr.Label(label='Correctness')
41
  gr_explanation = gr.TextArea(label='Explanation')
42
  gr_next = gr.Button('Next')
43
 
44
 
45
+ def _next_question(uuid_):
46
+ if not uuid_:
47
+ uuid_ = str(uuid.uuid4())
48
+ global _QUESTION_IDS
49
+ _qid = _QUESTION_IDS.get(uuid_, -1)
50
+ _qid += 1
51
+ _QUESTION_IDS[uuid_] = _qid
52
+ print(_QUESTION_IDS)
53
 
54
  if _QUESTION_ID >= len(_QUESTIONS):
55
  return 'Congratulations!', '', '', {}, '', \
56
  gr.Button('Submit', interactive=False), \
57
+ gr.Button('Next', interactive=False), \
58
+ uuid_
59
  else:
60
  executor = QuestionExecutor(_QUESTIONS[_QUESTION_ID], _LANG)
61
  return executor.question_text, '', '', {}, '', \
62
  gr.Button('Submit', interactive=True), \
63
+ gr.Button('Next', interactive=False), \
64
+ uuid_
65
 
66
 
67
  gr_next.click(
68
  fn=_next_question,
69
+ inputs=[gr_uuid],
70
+ outputs=[
71
+ gr_requirement, gr_question, gr_answer,
72
+ gr_predict, gr_explanation, gr_submit, gr_next, gr_uuid,
73
+ ],
74
  )
75
 
76
 
77
+ def _submit_answer(qs_text: str, api_key: str, uuid_: str):
78
  if _need_api_key() and not api_key:
79
  return '---', {}, 'Please Enter API Key Before Submitting Question.', \
80
  gr.Button('Next', interactive=False)
81
 
82
+ print(_QUESTION_IDS)
83
+ _qid = _QUESTION_IDS[uuid_]
84
  executor = QuestionExecutor(
85
+ _QUESTIONS[_qid], _LANG,
86
  llm=_LLM, llm_cfgs=_get_api_key_cfgs(api_key) if _need_api_key() else {}
87
  )
88
  answer_text, correctness, explanation = executor.check(qs_text)
89
  labels = {'Correct': 1.0} if correctness else {'Wrong': 1.0}
90
  if correctness:
91
+ return answer_text, labels, explanation, gr.Button('Next', interactive=True), uuid_
92
  else:
93
+ return answer_text, labels, explanation, gr.Button('Next', interactive=False), uuid_
94
 
95
 
96
  gr_submit.click(
97
  _submit_answer,
98
+ inputs=[gr_question, gr_api_key, gr_uuid],
99
+ outputs=[gr_answer, gr_predict, gr_explanation, gr_next, gr_uuid],
100
  )
101
 
102
  demo.launch()