baohuynhbk14 commited on
Commit
2268a48
·
verified ·
1 Parent(s): ce5a300

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -66
app.py CHANGED
@@ -333,7 +333,6 @@ def generate_video(model_name: str, text: str, video_path: str,
333
  time.sleep(0.01)
334
  yield buffer, buffer
335
 
336
- # --- Hàm generate_pdf (MỚI - Từ Script 1 và ĐÃ CHỈNH SỬA) ---
337
  @spaces.GPU
338
  def generate_pdf(model_name: str, text: str, state: Dict[str, Any],
339
  max_new_tokens: int = 2048,
@@ -403,20 +402,20 @@ def generate_pdf(model_name: str, text: str, state: Dict[str, Any],
403
 
404
  # --- Định nghĩa Examples (Kết hợp từ 2 script) ---
405
  image_examples = [
406
- ["Explain the content in detail.", "images/D.jpg"],
407
- ["Explain the content (ocr).", "images/O.jpg"],
408
- ["What is the core meaning of the poem?", "images/S.jpg"],
409
- ["Provide a detailed caption for the image.", "images/A.jpg"],
410
- ]
411
- video_examples = [
412
- ["Explain the ad in detail", "videos/1.mp4"],
413
- ["Identify the main actions in the video", "videos/2.mp4"],
414
- ]
415
- # Thêm từ Script 1
416
- pdf_examples = [
417
- ["Extract the content precisely.", "examples/pdfs/doc1.pdf"],
418
- ["Analyze and provide a short report.", "examples/pdfs/doc2.pdf"]
419
  ]
 
 
 
 
 
 
 
 
 
420
 
421
  css = """
422
  #main-title h1 {
@@ -427,11 +426,9 @@ css = """
427
  }
428
  """
429
 
430
- # --- Giao diện Gradio (Từ Script 2, đã thêm Tab PDF) ---
431
  with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
432
 
433
- # Thêm từ Script 1
434
- pdf_state = gr.State(value=get_initial_pdf_state())
435
 
436
  gr.Markdown("# **Qwen3-VL-Outpost**", elem_id="main-title")
437
  with gr.Row():
@@ -443,27 +440,25 @@ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
443
  image_submit = gr.Button("Submit", variant="primary")
444
  gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
445
 
446
- with gr.TabItem("Video Inference"):
447
- video_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
448
- video_upload = gr.Video(label="Upload Video", height=290)
449
- video_submit = gr.Button("Submit", variant="primary")
450
- gr.Examples(examples=video_examples, inputs=[video_query, video_upload])
451
 
452
- # --- Tab PDF MỚI (Từ Script 1) ---
453
- with gr.TabItem("PDF Inference"):
454
- with gr.Row():
455
- with gr.Column(scale=1):
456
- pdf_query = gr.Textbox(label="Query Input", placeholder="e.g., 'Summarize this document'")
457
- pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
458
- pdf_submit = gr.Button("Submit", variant="primary")
459
- with gr.Column(scale=1):
460
- pdf_preview_img = gr.Image(label="PDF Preview", height=290)
461
- with gr.Row():
462
- prev_page_btn = gr.Button(" Previous")
463
- page_info = gr.HTML('<div style="text-align:center;">No file loaded</div>')
464
- next_page_btn = gr.Button("Next ▶")
465
- gr.Examples(examples=pdf_examples, inputs=[pdf_query, pdf_upload])
466
- # --- Kết thúc Tab PDF ---
467
 
468
  with gr.Accordion("Advanced options", open=False):
469
  max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
@@ -487,45 +482,44 @@ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
487
  value="Qwen3-VL-4B-Instruct"
488
  )
489
 
490
- # --- Event Handlers (Đã thêm các sự kiện PDF) ---
491
  image_submit.click(
492
  fn=generate_image,
493
  inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
494
  outputs=[output, markdown_output]
495
  )
496
 
497
- video_submit.click(
498
- fn=generate_video,
499
- inputs=[model_choice, video_query, video_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
500
- outputs=[output, markdown_output]
501
- )
502
 
503
- # --- Thêm sự kiện cho PDF ---
504
- pdf_submit.click(
505
- fn=generate_pdf,
506
- # Thêm 'model_choice' vào inputs
507
- inputs=[model_choice, pdf_query, pdf_state, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
508
- outputs=[output, markdown_output]
509
- )
510
 
511
- pdf_upload.change(
512
- fn=load_and_preview_pdf,
513
- inputs=[pdf_upload],
514
- outputs=[pdf_preview_img, pdf_state, page_info]
515
- )
516
 
517
- prev_page_btn.click(
518
- fn=lambda s: navigate_pdf_page("prev", s),
519
- inputs=[pdf_state],
520
- outputs=[pdf_preview_img, pdf_state, page_info]
521
- )
522
 
523
- next_page_btn.click(
524
- fn=lambda s: navigate_pdf_page("next", s),
525
- inputs=[pdf_state],
526
- outputs=[pdf_preview_img, pdf_state, page_info]
527
- )
528
- # --- Kết thúc thêm sự kiện PDF ---
529
 
530
  if __name__ == "__main__":
531
  demo.queue(max_size=50).launch(mcp_server=True, ssr_mode=False, show_error=True)
 
333
  time.sleep(0.01)
334
  yield buffer, buffer
335
 
 
336
  @spaces.GPU
337
  def generate_pdf(model_name: str, text: str, state: Dict[str, Any],
338
  max_new_tokens: int = 2048,
 
402
 
403
  # --- Định nghĩa Examples (Kết hợp từ 2 script) ---
404
  image_examples = [
405
+ ["Explain the content in detail.", "images/force.jpg"],
406
+ ["Explain the content (ocr).", "images/ocr.jpg"],
407
+ ["Extract the content in the json format", "images/bill.jpg"],
408
+ ["Choose the right answer .", "images/math.jpg"],
 
 
 
 
 
 
 
 
 
409
  ]
410
+ # video_examples = [
411
+ # ["Explain the ad in detail", "videos/1.mp4"],
412
+ # ["Identify the main actions in the video", "videos/2.mp4"],
413
+ # ]
414
+ # # Thêm từ Script 1
415
+ # pdf_examples = [
416
+ # ["Extract the content precisely.", "examples/pdfs/doc1.pdf"],
417
+ # ["Analyze and provide a short report.", "examples/pdfs/doc2.pdf"]
418
+ # ]
419
 
420
  css = """
421
  #main-title h1 {
 
426
  }
427
  """
428
 
 
429
  with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
430
 
431
+ # pdf_state = gr.State(value=get_initial_pdf_state())
 
432
 
433
  gr.Markdown("# **Qwen3-VL-Outpost**", elem_id="main-title")
434
  with gr.Row():
 
440
  image_submit = gr.Button("Submit", variant="primary")
441
  gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
442
 
443
+ # with gr.TabItem("Video Inference"):
444
+ # video_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
445
+ # video_upload = gr.Video(label="Upload Video", height=290)
446
+ # video_submit = gr.Button("Submit", variant="primary")
447
+ # gr.Examples(examples=video_examples, inputs=[video_query, video_upload])
448
 
449
+ # with gr.TabItem("PDF Inference"):
450
+ # with gr.Row():
451
+ # with gr.Column(scale=1):
452
+ # pdf_query = gr.Textbox(label="Query Input", placeholder="e.g., 'Summarize this document'")
453
+ # pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
454
+ # pdf_submit = gr.Button("Submit", variant="primary")
455
+ # with gr.Column(scale=1):
456
+ # pdf_preview_img = gr.Image(label="PDF Preview", height=290)
457
+ # with gr.Row():
458
+ # prev_page_btn = gr.Button("◀ Previous")
459
+ # page_info = gr.HTML('<div style="text-align:center;">No file loaded</div>')
460
+ # next_page_btn = gr.Button("Next ")
461
+ # gr.Examples(examples=pdf_examples, inputs=[pdf_query, pdf_upload])
 
 
462
 
463
  with gr.Accordion("Advanced options", open=False):
464
  max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
 
482
  value="Qwen3-VL-4B-Instruct"
483
  )
484
 
 
485
  image_submit.click(
486
  fn=generate_image,
487
  inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
488
  outputs=[output, markdown_output]
489
  )
490
 
491
+ # video_submit.click(
492
+ # fn=generate_video,
493
+ # inputs=[model_choice, video_query, video_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
494
+ # outputs=[output, markdown_output]
495
+ # )
496
 
497
+ # # --- Thêm sự kiện cho PDF ---
498
+ # pdf_submit.click(
499
+ # fn=generate_pdf,
500
+ # # Thêm 'model_choice' vào inputs
501
+ # inputs=[model_choice, pdf_query, pdf_state, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
502
+ # outputs=[output, markdown_output]
503
+ # )
504
 
505
+ # pdf_upload.change(
506
+ # fn=load_and_preview_pdf,
507
+ # inputs=[pdf_upload],
508
+ # outputs=[pdf_preview_img, pdf_state, page_info]
509
+ # )
510
 
511
+ # prev_page_btn.click(
512
+ # fn=lambda s: navigate_pdf_page("prev", s),
513
+ # inputs=[pdf_state],
514
+ # outputs=[pdf_preview_img, pdf_state, page_info]
515
+ # )
516
 
517
+ # next_page_btn.click(
518
+ # fn=lambda s: navigate_pdf_page("next", s),
519
+ # inputs=[pdf_state],
520
+ # outputs=[pdf_preview_img, pdf_state, page_info]
521
+ # )
522
+
523
 
524
  if __name__ == "__main__":
525
  demo.queue(max_size=50).launch(mcp_server=True, ssr_mode=False, show_error=True)