Spaces:
Sleeping
Sleeping
with gr.Tab("單張處理"):
Browse files
app.py
CHANGED
|
@@ -242,6 +242,10 @@ def process_image_to_data(password, images):
|
|
| 242 |
raise gr.Error("密码错误,请重新输入")
|
| 243 |
|
| 244 |
processed_data = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
for image_file in images:
|
| 246 |
image_url = upload_image_to_gcs(image_file, bucket)
|
| 247 |
text, question_json = process_image(image_url)
|
|
@@ -259,28 +263,94 @@ def process_image_to_data(password, images):
|
|
| 259 |
|
| 260 |
return processed_data, result, csv_file_path
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
# Gradio界面
|
| 263 |
with gr.Blocks() as demo:
|
| 264 |
-
gr.Markdown("## 批量上传图片并处理")
|
| 265 |
with gr.Row():
|
| 266 |
password_input = gr.Textbox(label="密码", type="password")
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
|
| 286 |
submit_button.click(
|
|
@@ -289,4 +359,18 @@ with gr.Blocks() as demo:
|
|
| 289 |
outputs=[result_table, result_text, download_csv_output]
|
| 290 |
)
|
| 291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
demo.launch()
|
|
|
|
| 242 |
raise gr.Error("密码错误,请重新输入")
|
| 243 |
|
| 244 |
processed_data = []
|
| 245 |
+
|
| 246 |
+
if isinstance(images, str):
|
| 247 |
+
images = [images]
|
| 248 |
+
|
| 249 |
for image_file in images:
|
| 250 |
image_url = upload_image_to_gcs(image_file, bucket)
|
| 251 |
text, question_json = process_image(image_url)
|
|
|
|
| 263 |
|
| 264 |
return processed_data, result, csv_file_path
|
| 265 |
|
| 266 |
+
def show_single_question_image(data):
|
| 267 |
+
if len(data) == 0:
|
| 268 |
+
return ""
|
| 269 |
+
question_json = data.iloc[0].to_dict() # 確保訪問的是 DataFrame 的第一行並轉換為字典
|
| 270 |
+
image_url = question_json['圖片URL']
|
| 271 |
+
return image_url
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def show_single_question_markdown(data):
|
| 275 |
+
if len(data) == 0:
|
| 276 |
+
return ""
|
| 277 |
+
question_json = data.iloc[0].to_dict() # 確保訪問的是 DataFrame 的第一行並轉換為字典
|
| 278 |
+
question = question_json['題目']
|
| 279 |
+
choice_1 = question_json['選項1']
|
| 280 |
+
choice_2 = question_json['選項2']
|
| 281 |
+
choice_3 = question_json['選項3']
|
| 282 |
+
choice_4 = question_json['選項4']
|
| 283 |
+
answer = question_json['答案']
|
| 284 |
+
|
| 285 |
+
hints = []
|
| 286 |
+
for i in range(1, 6):
|
| 287 |
+
hint_key = question_json.get(f'提示{i}', None)
|
| 288 |
+
if hint_key:
|
| 289 |
+
hints.append(hint_key)
|
| 290 |
+
else:
|
| 291 |
+
break
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
markdown = f"""
|
| 295 |
+
## 題目
|
| 296 |
+
- {question}
|
| 297 |
+
|
| 298 |
+
## 選項
|
| 299 |
+
1. {choice_1}
|
| 300 |
+
2. {choice_2}
|
| 301 |
+
3. {choice_3}
|
| 302 |
+
4. {choice_4}
|
| 303 |
+
|
| 304 |
+
## 答案: {answer}
|
| 305 |
+
|
| 306 |
+
## 提示
|
| 307 |
+
"""
|
| 308 |
+
for i, hint in enumerate(hints):
|
| 309 |
+
markdown += f"{i+1}. {hint}\n"
|
| 310 |
+
|
| 311 |
+
return markdown
|
| 312 |
+
|
| 313 |
# Gradio界面
|
| 314 |
with gr.Blocks() as demo:
|
|
|
|
| 315 |
with gr.Row():
|
| 316 |
password_input = gr.Textbox(label="密码", type="password")
|
| 317 |
+
|
| 318 |
+
with gr.Tab("批量處理"):
|
| 319 |
+
with gr.Row():
|
| 320 |
+
gr.Markdown("## 批量图片處理 + Perseus JSON 生成")
|
| 321 |
+
image_input = gr.Files(label="選擇圖片", type="filepath")
|
| 322 |
+
# sheet_default_value = "https://docs.google.com/spreadsheets/d/1ygFGLxcnPad3LMVj4bZqfGZh1n2wqhs0-vOUjuVCkSY/edit#gid=0"
|
| 323 |
+
# sheet_input = gr.Textbox(label="Google Sheets URL", value=sheet_default_value)
|
| 324 |
+
submit_button = gr.Button("開始處理圖片")
|
| 325 |
+
with gr.Row():
|
| 326 |
+
result_text = gr.Textbox(label="處理結果")
|
| 327 |
+
with gr.Row():
|
| 328 |
+
result_table = gr.Dataframe(
|
| 329 |
+
headers=["圖片URL", "文字", "題號", "題目", "選項1", "選項2", "選項3", "選項4", "答案", "提示1", "提示2", "提示3", "提示4", "提示5", "Perseus JSON"],
|
| 330 |
+
column_widths=[10, 10, 5, 20, 4, 4, 4, 4, 4,4,4,4,4,4, 10],
|
| 331 |
+
wrap=True
|
| 332 |
+
)
|
| 333 |
+
with gr.Row():
|
| 334 |
+
download_csv_output = gr.File(label="下载 CSV")
|
| 335 |
+
|
| 336 |
+
with gr.Tab("單張處理"):
|
| 337 |
+
with gr.Row():
|
| 338 |
+
gr.Markdown("## 單張图片處理")
|
| 339 |
+
single_image_input = gr.Files(label="選擇圖片", type="filepath")
|
| 340 |
+
single_submit_button = gr.Button("開始處理圖片")
|
| 341 |
+
with gr.Row():
|
| 342 |
+
single_result_text = gr.Textbox(label="處理結果")
|
| 343 |
+
with gr.Row():
|
| 344 |
+
single_result_table = gr.Dataframe(
|
| 345 |
+
headers=["圖片URL", "文字", "題號", "題目", "選項1", "選項2", "選項3", "選項4", "答案", "提示1", "提示2", "提示3", "提示4", "提示5", "Perseus JSON"],
|
| 346 |
+
column_widths=[10, 10, 5, 20, 4, 4, 4, 4, 4,4,4,4,4,4, 10],
|
| 347 |
+
wrap=True
|
| 348 |
+
)
|
| 349 |
+
with gr.Row():
|
| 350 |
+
single_question_image = gr.Image()
|
| 351 |
+
single_question_markdown = gr.Markdown(show_label=False, latex_delimiters=[{"left": "$", "right": "$", "display": False}])
|
| 352 |
+
with gr.Row():
|
| 353 |
+
single_download_csv_output = gr.File(label="下载 CSV")
|
| 354 |
|
| 355 |
|
| 356 |
submit_button.click(
|
|
|
|
| 359 |
outputs=[result_table, result_text, download_csv_output]
|
| 360 |
)
|
| 361 |
|
| 362 |
+
single_submit_button.click(
|
| 363 |
+
fn=process_image_to_data,
|
| 364 |
+
inputs=[password_input, single_image_input],
|
| 365 |
+
outputs=[single_result_table, single_result_text, single_download_csv_output]
|
| 366 |
+
).then(
|
| 367 |
+
fn=show_single_question_image,
|
| 368 |
+
inputs=[single_result_table],
|
| 369 |
+
outputs=[single_question_image]
|
| 370 |
+
).then(
|
| 371 |
+
fn=show_single_question_markdown,
|
| 372 |
+
inputs=[single_result_table],
|
| 373 |
+
outputs=[single_question_markdown]
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
demo.launch()
|