-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
285 lines (227 loc) · 12.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import gradio as gr
import pandas as pd
import base64
from PIL import Image
from io import BytesIO
# 题库文件路径
questions_csv_path = "Data/all_questions.csv"
# 全局变量
questions_dict = {}
current_question_index = 1
current_checked_question_index = 0
last_question_index = 1
high_worry_rate = 0
wait_question_index_list = []
def read_questions():
global questions_dict
df = pd.read_csv(questions_csv_path)
# 将DataFrame转换为列表
questions_list = df.values.tolist()
for question in questions_list:
questions_dict[question[0]] = question
def change_high_worry_rate(worry_rate):
global high_worry_rate
high_worry_rate = worry_rate * 0.01
def get_question():
global questions_dict
global wait_question_index_list
num_smallest_attempts = 1
if not wait_question_index_list:
# 按照答题次数有小到大排序,再按照回答错误次数由大到小排序
questions_dict_sort = sorted(questions_dict.items(), key=lambda item: (item[1][-2], -item[1][-1]))
# 获取答题次数的前num_smallest_attempts小的题目
unique_attempts = sorted(set(item[1][-2] for item in questions_dict_sort))
while True:
num_smallest_attempts_tmp = min(num_smallest_attempts, len(unique_attempts))
selected_attempts = unique_attempts[:num_smallest_attempts_tmp]
# 获取指定数量的最小答题次数的index
wait_question_index_list = [item[0] for item in questions_dict_sort if item[1][-2] in selected_attempts]
# 根据正确率过滤
wait_question_index_list = [
item for item in wait_question_index_list
if questions_dict[item][8] == 0 or questions_dict[item][9] / questions_dict[item][8] >= high_worry_rate]
if wait_question_index_list:
break
elif num_smallest_attempts_tmp == len(unique_attempts):
return -1
else:
num_smallest_attempts += 1
question_index = wait_question_index_list[0]
wait_question_index_list.remove(question_index)
return question_index
def get_previous_question():
global current_question_index
global last_question_index
current_question_index = last_question_index
questions_index = questions_dict[current_question_index][0]
question_text, question_image = extract_base64_to_image(questions_dict[current_question_index][1])
question_option_A_text, question_option_A_image = extract_base64_to_image(questions_dict[current_question_index][3])
question_option_B_text, question_option_B_image = extract_base64_to_image(questions_dict[current_question_index][4])
question_option_C_text, question_option_C_image = extract_base64_to_image(questions_dict[current_question_index][5])
question_option_D_text, question_option_D_image = extract_base64_to_image(questions_dict[current_question_index][6])
image = question_image + question_option_A_image + question_option_B_image + question_option_C_image + question_option_D_image
return (questions_index,
questions_dict[current_question_index][-2], questions_dict[current_question_index][-1],
question_text, image,
question_option_A_text,
question_option_B_text,
question_option_C_text,
question_option_D_text,
"", "")
def get_next_question():
global current_question_index
global last_question_index
if current_question_index == last_question_index:
current_question_index += 1
else:
last_question_index = current_question_index
current_question_index = get_question()
questions_index = questions_dict[current_question_index][0]
question_text, question_image = extract_base64_to_image(questions_dict[current_question_index][1])
question_option_A_text, question_option_A_image = extract_base64_to_image(questions_dict[current_question_index][3])
question_option_B_text, question_option_B_image = extract_base64_to_image(questions_dict[current_question_index][4])
question_option_C_text, question_option_C_image = extract_base64_to_image(questions_dict[current_question_index][5])
question_option_D_text, question_option_D_image = extract_base64_to_image(questions_dict[current_question_index][6])
image = question_image + question_option_A_image + question_option_B_image + question_option_C_image + question_option_D_image
return (questions_index,
questions_dict[current_question_index][-2], questions_dict[current_question_index][-1],
question_text, image,
question_option_A_text,
question_option_B_text,
question_option_C_text,
question_option_D_text,
"", "")
def check_answer(options):
global current_question_index
global current_checked_question_index
if current_checked_question_index != current_question_index:
current_checked_question_index = current_question_index
questions_dict[current_question_index][8] += 1
if options == questions_dict[current_question_index][2]:
pass
else:
questions_dict[current_question_index][9] += 1
return (questions_dict[current_question_index][2], questions_dict[current_question_index][7],
questions_dict[current_question_index][-2], questions_dict[current_question_index][-1])
def save_record():
global questions_dict
new_columns_order = ['question_id', 'question', 'answer', 'A', 'B', 'C', 'D', 'explain', 'answer_time',
'worry_time']
questions_list_s = questions_dict.values()
df_s = pd.DataFrame(questions_list_s, columns=new_columns_order)
df_s.to_csv(questions_csv_path, index=False, encoding="utf_8")
def extract_base64_to_image(text):
if '[image:base64]' in text:
[text, images_base64] = text.split('[image:base64]')
else:
image = r'E:\PythonFile\--AnswerProgram\tmp\img.png'
text = text
return text, []
if '|' in images_base64:
images_base64 = images_base64.split('|')
else:
images_base64 = [images_base64]
images = []
for image_base64 in images_base64:
# base64解码
image_data = base64.b64decode(image_base64)
# 转换为图像
image = Image.open(BytesIO(image_data))
images.append(image)
return text, images
def modify_sava(question_textbox,
A_option_textbox, B_option_textbox, C_option_textbox, D_option_textbox,
answer_textbox, explain_textbox):
global current_question_index
global questions_dict
questions_dict[current_question_index][1] = question_textbox
questions_dict[current_question_index][3] = A_option_textbox
questions_dict[current_question_index][4] = B_option_textbox
questions_dict[current_question_index][5] = C_option_textbox
questions_dict[current_question_index][6] = D_option_textbox
if answer_textbox != '':
questions_dict[current_question_index][2] = answer_textbox
if explain_textbox != '':
questions_dict[current_question_index][7] = explain_textbox
question_text, question_image = extract_base64_to_image(questions_dict[current_question_index][1])
question_option_A_text, question_option_A_image = extract_base64_to_image(questions_dict[current_question_index][3])
question_option_B_text, question_option_B_image = extract_base64_to_image(questions_dict[current_question_index][4])
question_option_C_text, question_option_C_image = extract_base64_to_image(questions_dict[current_question_index][5])
question_option_D_text, question_option_D_image = extract_base64_to_image(questions_dict[current_question_index][6])
image = question_image + question_option_A_image + question_option_B_image + question_option_C_image + question_option_D_image
return (question_text, image,
question_option_A_text, question_option_B_text,
question_option_C_text, question_option_D_text)
def main():
global current_question_index
global questions_dict
read_questions()
with gr.Blocks() as demo:
with gr.Row():
question_num_textbox = gr.Textbox(label="题号: ",
value='',
interactive=False)
ans_question_time_textbox = gr.Textbox(label="该题共回答次数: ",
value='',
interactive=False)
wrong_question_time_textbox = gr.Textbox(label="该题回答错误次数: ",
value='',
interactive=False)
with gr.Row():
question_textbox = gr.Textbox(label="题目: ",
value='',
interactive=True)
image = gr.Gallery(height=300)
with gr.Row():
A_option_textbox = gr.Textbox(label="选项A: ", value='', interactive=True)
B_option_textbox = gr.Textbox(label="选项B: ", value='', interactive=True)
C_option_textbox = gr.Textbox(label="选项C: ", value='', interactive=True)
D_option_textbox = gr.Textbox(label="选项D: ", value='', interactive=True)
options_dropdown = gr.Dropdown(choices=["A", "B", "C", "D", "不会"], label="请选择: ")
with gr.Row():
previous_button = gr.Button("上一题")
check_button = gr.Button("查看答案与解析")
save_button = gr.Button("保存做题记录")
next_button = gr.Button("下一题")
with gr.Row():
answer_textbox = gr.Textbox(label="答案: ", value="", interactive=True)
explain_textbox = gr.Textbox(label="解析: ", value="", interactive=True)
with gr.Row():
high_worry_rate_slider = gr.Slider(0, 100, value=0, label="题目只输出错误率大于等于:")
with gr.Row():
# 保存修改后的题目
modify_sava_button = gr.Button("保存修改后的题目")
previous_button.click(fn=get_previous_question,
outputs=[question_num_textbox,
ans_question_time_textbox, wrong_question_time_textbox,
question_textbox, image,
A_option_textbox,
B_option_textbox,
C_option_textbox,
D_option_textbox,
answer_textbox, explain_textbox])
next_button.click(fn=get_next_question,
outputs=[question_num_textbox,
ans_question_time_textbox, wrong_question_time_textbox,
question_textbox, image,
A_option_textbox,
B_option_textbox,
C_option_textbox,
D_option_textbox,
answer_textbox, explain_textbox])
save_button.click(fn=save_record, outputs=[])
check_button.click(fn=check_answer, inputs=options_dropdown,
outputs=[answer_textbox, explain_textbox,
ans_question_time_textbox, wrong_question_time_textbox])
high_worry_rate_slider.change(fn=change_high_worry_rate, inputs=high_worry_rate_slider, outputs=[])
modify_sava_button.click(fn=modify_sava,
inputs=[question_textbox,
A_option_textbox, B_option_textbox,
C_option_textbox, D_option_textbox,
answer_textbox, explain_textbox],
outputs=[question_textbox, image,
A_option_textbox, B_option_textbox,
C_option_textbox, D_option_textbox])
demo.launch(share=True)
if __name__ == '__main__':
main()