TuTuHuss commited on
Commit
44724a1
·
2 Parent(s): d5fabd0 34f951b

Merge branch 'main' of github.com:opendilab/LLMRiddles

Browse files
Files changed (1) hide show
  1. app.py +60 -23
app.py CHANGED
@@ -11,6 +11,45 @@ _QUESTIONS = list_ordered_questions()
11
  _LANG = os.environ.get('QUESTION_LANG', 'cn')
12
  _LLM = os.environ.get('QUESTION_LLM', 'chatgpt')
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  def _need_api_key():
16
  return _LLM == 'chatgpt'
@@ -24,21 +63,21 @@ def _get_api_key_cfgs(api_key):
24
 
25
 
26
  if __name__ == '__main__':
27
- with gr.Blocks() as demo:
28
  with gr.Row():
29
  with gr.Column():
30
- gr_requirement = gr.TextArea(placeholder='Click \'Next\' to Start', label='Requirements')
31
- gr_question = gr.TextArea(placeholder='Your Question for LLM', label='Question')
32
- gr_answer = gr.TextArea(placeholder='Answer From LLM', label='Answer')
33
- gr_submit = gr.Button('Submit', interactive=False)
34
 
35
  with gr.Column():
36
- gr_api_key = gr.Text(placeholder='Your API Key', label='API Key', type='password',
37
  visible=_need_api_key())
38
- gr_uuid = gr.Text(value='')
39
- gr_predict = gr.Label(label='Correctness')
40
- gr_explanation = gr.TextArea(label='Explanation')
41
- gr_next = gr.Button('Next')
42
 
43
 
44
  def _next_question(uuid_):
@@ -48,18 +87,18 @@ if __name__ == '__main__':
48
  _qid = _QUESTION_IDS.get(uuid_, -1)
49
  _qid += 1
50
  _QUESTION_IDS[uuid_] = _qid
51
- print(_QUESTION_IDS)
52
 
53
  if _qid >= len(_QUESTIONS):
54
- return 'Congratulations!', '', '', {}, '', \
55
- gr.Button('Submit', interactive=False), \
56
- gr.Button('Next', interactive=False), \
57
- uuid_
 
58
  else:
59
  executor = QuestionExecutor(_QUESTIONS[_qid], _LANG)
60
  return executor.question_text, '', '', {}, '', \
61
- gr.Button('Submit', interactive=True), \
62
- gr.Button('Next', interactive=False), \
63
  uuid_
64
 
65
 
@@ -75,21 +114,19 @@ if __name__ == '__main__':
75
 
76
  def _submit_answer(qs_text: str, api_key: str, uuid_: str):
77
  if _need_api_key() and not api_key:
78
- return '---', {}, 'Please Enter API Key Before Submitting Question.', \
79
- gr.Button('Next', interactive=False), uuid_
80
 
81
- print(_QUESTION_IDS)
82
  _qid = _QUESTION_IDS[uuid_]
83
  executor = QuestionExecutor(
84
  _QUESTIONS[_qid], _LANG,
85
  llm=_LLM, llm_cfgs=_get_api_key_cfgs(api_key) if _need_api_key() else {}
86
  )
87
  answer_text, correctness, explanation = executor.check(qs_text)
88
- labels = {'Correct': 1.0} if correctness else {'Wrong': 1.0}
89
  if correctness:
90
- return answer_text, labels, explanation, gr.Button('Next', interactive=True), uuid_
91
  else:
92
- return answer_text, labels, explanation, gr.Button('Next', interactive=False), uuid_
93
 
94
 
95
  gr_submit.click(
 
11
  _LANG = os.environ.get('QUESTION_LANG', 'cn')
12
  _LLM = os.environ.get('QUESTION_LLM', 'chatgpt')
13
 
14
+ if _LANG == "cn":
15
+ requirement_ph = "点击\"下一题\"开始游戏"
16
+ requirement_label = "游戏须知"
17
+ question_ph = "你对大语言模型的提问"
18
+ question_label = "提问栏"
19
+ answer_ph = "大语言模型的回答"
20
+ answer_label = "回答栏"
21
+ submit_label = "提交"
22
+ next_label = "下一题"
23
+ api_ph = "你个人的大语言模型 API Key (例如:ChatGPT)"
24
+ api_label = "API key"
25
+ predict_label = "结果正确性"
26
+ explanation_label = "结果解释"
27
+ game_cleared_label = "祝贺!你已成功通关!"
28
+ correct_label = "正确"
29
+ wrong_label = "错误"
30
+ api_error_info = "请在提交问题之前先输入你的 API Key"
31
+ try_again_label = "再玩一次"
32
+ elif _LANG == "en":
33
+ requirement_ph = 'Click \'Next\' to Start'
34
+ requirement_label = "Requirements"
35
+ question_ph = "Your Question for LLM"
36
+ question_label = "Question"
37
+ answer_ph = "Answer From LLM"
38
+ answer_label = "Answer"
39
+ submit_label = "Submit"
40
+ next_label = "Next"
41
+ api_ph = "Your API Key (e.g. ChatGPT)"
42
+ api_label = "API key"
43
+ predict_label = "Correctness"
44
+ explanation_label = "Explanation"
45
+ game_cleared_label = "Congratulations!"
46
+ correct_label = "Correct"
47
+ wrong_label = "Wrong"
48
+ api_error_info = "Please Enter API Key Before Submitting Question."
49
+ try_again_label = "Try Again"
50
+ else:
51
+ raise KeyError("invalid _LANG: {}".format(_LANG))
52
+
53
 
54
  def _need_api_key():
55
  return _LLM == 'chatgpt'
 
63
 
64
 
65
  if __name__ == '__main__':
66
+ with gr.Blocks(theme='ParityError/Interstellar') as demo:
67
  with gr.Row():
68
  with gr.Column():
69
+ gr_requirement = gr.TextArea(placeholder=requirement_ph, label=requirement_label)
70
+ gr_question = gr.TextArea(placeholder=question_ph, label=question_label)
71
+ gr_answer = gr.TextArea(placeholder=answer_ph, label=answer_label)
72
+ gr_submit = gr.Button(submit_label, interactive=False)
73
 
74
  with gr.Column():
75
+ gr_api_key = gr.Text(placeholder=api_ph, label=api_label, type='password',
76
  visible=_need_api_key())
77
+ gr_uuid = gr.Text(value='', visible=False)
78
+ gr_predict = gr.Label(label=predict_label)
79
+ gr_explanation = gr.TextArea(label=explanation_label)
80
+ gr_next = gr.Button(next_label)
81
 
82
 
83
  def _next_question(uuid_):
 
87
  _qid = _QUESTION_IDS.get(uuid_, -1)
88
  _qid += 1
89
  _QUESTION_IDS[uuid_] = _qid
 
90
 
91
  if _qid >= len(_QUESTIONS):
92
+ del _QUESTION_IDS[uuid_]
93
+ return game_cleared_label, '', '', {}, '', \
94
+ gr.Button(submit_label, interactive=False), \
95
+ gr.Button(try_again_label, interactive=True), \
96
+ ''
97
  else:
98
  executor = QuestionExecutor(_QUESTIONS[_qid], _LANG)
99
  return executor.question_text, '', '', {}, '', \
100
+ gr.Button(submit_label, interactive=True), \
101
+ gr.Button(next_label, interactive=False), \
102
  uuid_
103
 
104
 
 
114
 
115
  def _submit_answer(qs_text: str, api_key: str, uuid_: str):
116
  if _need_api_key() and not api_key:
117
+ raise gr.Error(api_error_info)
 
118
 
 
119
  _qid = _QUESTION_IDS[uuid_]
120
  executor = QuestionExecutor(
121
  _QUESTIONS[_qid], _LANG,
122
  llm=_LLM, llm_cfgs=_get_api_key_cfgs(api_key) if _need_api_key() else {}
123
  )
124
  answer_text, correctness, explanation = executor.check(qs_text)
125
+ labels = {correct_label: 1.0} if correctness else {wrong_label: 1.0}
126
  if correctness:
127
+ return answer_text, labels, explanation, gr.Button(next_label, interactive=True), uuid_
128
  else:
129
+ return answer_text, labels, explanation, gr.Button(next_label, interactive=False), uuid_
130
 
131
 
132
  gr_submit.click(