niuyazhe commited on
Commit
e3e52e9
1 Parent(s): 367f69a

style(nyz): separate cn/env descriptions

Browse files
Files changed (1) hide show
  1. app.py +55 -22
app.py CHANGED
@@ -11,6 +11,43 @@ _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 +61,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 +85,16 @@ 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 +110,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
+ elif _LANG == "en":
32
+ requirement_ph = 'Click \'Next\' to Start'
33
+ requirement_label = "Requirements"
34
+ question_ph = "Your Question for LLM"
35
+ question_label = "Question"
36
+ answer_ph = "Answer From LLM"
37
+ answer_label = "Answer"
38
+ submit_label = "Submit"
39
+ next_label = "Next"
40
+ api_ph = "Your API Key (e.g. ChatGPT)"
41
+ api_label = "API key"
42
+ predict_label = "Correctness"
43
+ explanation_label = "Explanation"
44
+ game_cleared_label = "Congratulations!"
45
+ correct_label = "Correct"
46
+ wrong_label = "Wrong"
47
+ api_error_info = "Please Enter API Key Before Submitting Question."
48
+ else:
49
+ raise KeyError("invalid _LANG: {}".format(_LANG))
50
+
51
 
52
  def _need_api_key():
53
  return _LLM == 'chatgpt'
 
61
 
62
 
63
  if __name__ == '__main__':
64
+ with gr.Blocks(theme='ParityError/Interstellar') as demo:
65
  with gr.Row():
66
  with gr.Column():
67
+ gr_requirement = gr.TextArea(placeholder=requirement_ph, label=requirement_label)
68
+ gr_question = gr.TextArea(placeholder=question_ph, label=question_label)
69
+ gr_answer = gr.TextArea(placeholder=answer_ph, label=answer_label)
70
+ gr_submit = gr.Button(submit_label, interactive=False)
71
 
72
  with gr.Column():
73
+ gr_api_key = gr.Text(placeholder=api_ph, label=api_label, type='password',
74
  visible=_need_api_key())
75
  gr_uuid = gr.Text(value='')
76
+ gr_predict = gr.Label(label=predict_label)
77
+ gr_explanation = gr.TextArea(label=explanation_label)
78
+ gr_next = gr.Button(next_label)
79
 
80
 
81
  def _next_question(uuid_):
 
85
  _qid = _QUESTION_IDS.get(uuid_, -1)
86
  _qid += 1
87
  _QUESTION_IDS[uuid_] = _qid
 
88
 
89
  if _qid >= len(_QUESTIONS):
90
+ return game_cleared_label, '', '', {}, '', \
91
+ gr.Button(submit_label, interactive=False), \
92
+ gr.Button(next_label, interactive=False), uuid_
 
93
  else:
94
  executor = QuestionExecutor(_QUESTIONS[_qid], _LANG)
95
  return executor.question_text, '', '', {}, '', \
96
+ gr.Button(submit_label, interactive=True), \
97
+ gr.Button(next_label, interactive=False), \
98
  uuid_
99
 
100
 
 
110
 
111
  def _submit_answer(qs_text: str, api_key: str, uuid_: str):
112
  if _need_api_key() and not api_key:
113
+ raise gr.Error(api_error_info)
 
114
 
 
115
  _qid = _QUESTION_IDS[uuid_]
116
  executor = QuestionExecutor(
117
  _QUESTIONS[_qid], _LANG,
118
  llm=_LLM, llm_cfgs=_get_api_key_cfgs(api_key) if _need_api_key() else {}
119
  )
120
  answer_text, correctness, explanation = executor.check(qs_text)
121
+ labels = {correct_label: 1.0} if correctness else {wrong_label: 1.0}
122
  if correctness:
123
+ return answer_text, labels, explanation, gr.Button(next_label, interactive=True), uuid_
124
  else:
125
+ return answer_text, labels, explanation, gr.Button(next_label, interactive=False), uuid_
126
 
127
 
128
  gr_submit.click(