process_qid_to_data
Browse files
app.py
CHANGED
@@ -247,7 +247,10 @@ def create_csv(processed_data):
|
|
247 |
return file_path
|
248 |
|
249 |
def process_single_image(image):
|
250 |
-
if isinstance(image, str)
|
|
|
|
|
|
|
251 |
image_url = upload_image_to_gcs(image, bucket)
|
252 |
else: # If the image is an image object (from gr.Image)
|
253 |
temp_file_path = "/tmp/temp_image.png"
|
@@ -493,6 +496,31 @@ def show_multiple_questions_markdown(data):
|
|
493 |
|
494 |
return markdown
|
495 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
496 |
# Gradio็้ข
|
497 |
with gr.Blocks() as demo:
|
498 |
with gr.Row():
|
@@ -556,6 +584,26 @@ with gr.Blocks() as demo:
|
|
556 |
wrap=True
|
557 |
)
|
558 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
559 |
submit_button.click(
|
560 |
fn=process_image_to_data,
|
561 |
inputs=[password_input, image_input],
|
@@ -590,6 +638,20 @@ with gr.Blocks() as demo:
|
|
590 |
outputs=[pdf_question_markdown]
|
591 |
)
|
592 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
593 |
demo.launch()
|
594 |
|
595 |
|
|
|
247 |
return file_path
|
248 |
|
249 |
def process_single_image(image):
|
250 |
+
if isinstance(image, str) and image.startswith("http"):
|
251 |
+
# If the image is a URL, use it directly
|
252 |
+
image_url = image
|
253 |
+
elif isinstance(image, str): # If the image is a file path
|
254 |
image_url = upload_image_to_gcs(image, bucket)
|
255 |
else: # If the image is an image object (from gr.Image)
|
256 |
temp_file_path = "/tmp/temp_image.png"
|
|
|
496 |
|
497 |
return markdown
|
498 |
|
499 |
+
|
500 |
+
# ====== Junyi Q_ID ======
|
501 |
+
def process_qid_to_data(password, q_id):
|
502 |
+
# ๆชขๆฅๅฏ็ขผ
|
503 |
+
if password != PASSWORD:
|
504 |
+
raise gr.Error("ๅฏ็ ้่ฏฏ๏ผ่ฏท้ๆฐ่พๅ
ฅ")
|
505 |
+
# ๆ นๆ Junyi Q_ID ๅๅพ้ก็ฎๆชๅ
|
506 |
+
processed_data = []
|
507 |
+
# ็ฒๅๅ็็ๆนๆณใๆ ผๅผ๏ผhttps://storage.googleapis.com/exercise-render-img-junyi/P-qid.jpg
|
508 |
+
image_url = f"https://storage.googleapis.com/exercise-render-img-junyi/p_{q_id}.jpg"
|
509 |
+
# ่็ๅ็
|
510 |
+
text, question_json = process_image(image_url)
|
511 |
+
# ็ๆ Perseus JSON
|
512 |
+
perseus_json_str = build_perseus_json(question_json)
|
513 |
+
# ่ฟๅ่็็ตๆ
|
514 |
+
processed_data.append([image_url, text] + list(question_json.values()) + [perseus_json_str])
|
515 |
+
print("======process_and_upload=====")
|
516 |
+
print("image_url:", image_url)
|
517 |
+
|
518 |
+
question_count = len(processed_data)
|
519 |
+
result = f"ๅ็่็ๅฎๆ๏ผ็ธฝๅ
ฑๅฎๆ {question_count} ้้ก็ฎ"
|
520 |
+
csv_file_path = create_csv(processed_data)
|
521 |
+
|
522 |
+
return processed_data, result, csv_file_path
|
523 |
+
|
524 |
# Gradio็้ข
|
525 |
with gr.Blocks() as demo:
|
526 |
with gr.Row():
|
|
|
584 |
wrap=True
|
585 |
)
|
586 |
|
587 |
+
with gr.Tab("Junyi_Q_ID"):
|
588 |
+
with gr.Row():
|
589 |
+
gr.Markdown("## Junyi Q_ID")
|
590 |
+
with gr.Row():
|
591 |
+
junyi_q_id_input = gr.Textbox(label="Junyi Q_ID")
|
592 |
+
junyi_q_id_submit_button = gr.Button("้ๅง่็ Junyi Q_ID")
|
593 |
+
with gr.Row():
|
594 |
+
junyi_q_id_result_text = gr.Textbox(label="่็็ตๆ")
|
595 |
+
junyi_q_id_download_csv_output = gr.File(label="ไธ่ฝฝ CSV")
|
596 |
+
with gr.Row():
|
597 |
+
junyi_q_id_question_image = gr.Image()
|
598 |
+
junyi_q_id_question_markdown = gr.Markdown(show_label=False, latex_delimiters=[{"left": "$", "right": "$", "display": False}])
|
599 |
+
with gr.Accordion(open=False):
|
600 |
+
with gr.Row():
|
601 |
+
junyi_q_id_result_table = gr.Dataframe(
|
602 |
+
headers=["ๅ็URL", "ๆๅญ", "้ก่", "้ก็ฎ", "้ธ้
1", "้ธ้
2", "้ธ้
3", "้ธ้
4", "็ญๆก", "ๆ็คบ1", "ๆ็คบ2", "ๆ็คบ3", "ๆ็คบ4", "ๆ็คบ5", "Perseus JSON"],
|
603 |
+
column_widths=[10, 10, 5, 20, 4, 4, 4, 4, 4,4,4,4,4,4, 10],
|
604 |
+
wrap=True
|
605 |
+
)
|
606 |
+
|
607 |
submit_button.click(
|
608 |
fn=process_image_to_data,
|
609 |
inputs=[password_input, image_input],
|
|
|
638 |
outputs=[pdf_question_markdown]
|
639 |
)
|
640 |
|
641 |
+
junyi_q_id_submit_button.click(
|
642 |
+
fn=process_qid_to_data,
|
643 |
+
inputs=[password_input, junyi_q_id_input],
|
644 |
+
outputs=[junyi_q_id_result_table, junyi_q_id_result_text, junyi_q_id_download_csv_output]
|
645 |
+
).then(
|
646 |
+
fn=show_single_question_image,
|
647 |
+
inputs=[junyi_q_id_result_table],
|
648 |
+
outputs=[junyi_q_id_question_image]
|
649 |
+
).then(
|
650 |
+
fn=show_single_question_markdown,
|
651 |
+
inputs=[junyi_q_id_result_table],
|
652 |
+
outputs=[junyi_q_id_question_markdown]
|
653 |
+
)
|
654 |
+
|
655 |
demo.launch()
|
656 |
|
657 |
|