youngtsai commited on
Commit
f7622b4
โ€ข
1 Parent(s): 4c45396

process_qid_to_data

Browse files
Files changed (1) hide show
  1. app.py +63 -1
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): # If the image is a file path
 
 
 
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