Files changed (2) hide show
  1. data/images/code.jpg +0 -3
  2. src/app.py +20 -10
data/images/code.jpg DELETED

Git LFS Details

  • SHA256: f12a94ce0603cd33057b41ea49aeb76d294149e3591867cb6dd37f75c9c1e6ca
  • Pointer size: 130 Bytes
  • Size of remote file: 73.3 kB
src/app.py CHANGED
@@ -60,9 +60,9 @@ sample_data = [
60
  "pad": True,
61
  },
62
  {
63
- "preview_image": str(SAMPLES_PATH / "code.jpg"),
64
  "prompts": ["Convert code to text."],
65
- "image": str(SAMPLES_PATH / "code.jpg"),
66
  "name": "Code Recognition",
67
  "pad": True,
68
  },
@@ -527,29 +527,25 @@ with gr.Blocks(fill_height=True, css_paths=css_file_path, head_paths=head_file_p
527
  else:
528
  cb[-1] = gr.ChatMessage(role="assistant", content=answer)
529
 
530
- if "convert this page to docling" in msg.lower() or ("convert" in msg.lower() and "otsl" in msg.lower()):
531
  try:
532
  doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([answer], [Image.open(image_path)])
533
  doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
534
  markdown_output = doc.export_to_markdown()
535
- response = gr.ChatMessage(
536
  role="assistant",
537
  content=f"\nConverted to Markdown using docling.\n\n**MD Output:**\n\n{markdown_output}",
538
  )
539
- cb.append(response)
540
  except Exception as e:
541
  error_response = gr.ChatMessage(role="assistant", content=f"Error creating markdown output: {e!s}")
542
  cb.append(error_response)
 
543
  elif "convert formula to latex" in msg.lower():
544
  try:
545
  doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([answer], [Image.open(image_path)])
546
  doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
547
  markdown_output = doc.export_to_markdown()
548
- if markdown_output.count("$$") >= 2:
549
- parts = markdown_output.split("$$", 2)
550
- formula = parts[1].strip()
551
- wrapped = f"$$\n\\begin{{aligned}}\n{formula}\n\\end{{aligned}}\n$$"
552
- markdown_output = parts[0] + wrapped + parts[2]
553
  md_response = gr.ChatMessage(
554
  role="assistant",
555
  content=f"\nConverted to Markdown using docling.\n\n**LaTeX Output:**\n\n{markdown_output}",
@@ -559,6 +555,20 @@ with gr.Blocks(fill_height=True, css_paths=css_file_path, head_paths=head_file_p
559
  error_response = gr.ChatMessage(role="assistant", content=f"Error creating LaTeX output: {e!s}")
560
  cb.append(error_response)
561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
562
  if has_loc_tags:
563
  try:
564
  annotated_image = draw_bounding_boxes(image_path, answer, is_doctag_response=has_doctag)
 
60
  "pad": True,
61
  },
62
  {
63
+ "preview_image": str(SAMPLES_PATH / "7666.jpg"),
64
  "prompts": ["Convert code to text."],
65
+ "image": str(SAMPLES_PATH / "7666.jpg"),
66
  "name": "Code Recognition",
67
  "pad": True,
68
  },
 
527
  else:
528
  cb[-1] = gr.ChatMessage(role="assistant", content=answer)
529
 
530
+ if "convert" in msg.lower() and "otsl" in msg.lower():
531
  try:
532
  doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([answer], [Image.open(image_path)])
533
  doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
534
  markdown_output = doc.export_to_markdown()
535
+ md_response = gr.ChatMessage(
536
  role="assistant",
537
  content=f"\nConverted to Markdown using docling.\n\n**MD Output:**\n\n{markdown_output}",
538
  )
539
+ cb.append(md_response)
540
  except Exception as e:
541
  error_response = gr.ChatMessage(role="assistant", content=f"Error creating markdown output: {e!s}")
542
  cb.append(error_response)
543
+
544
  elif "convert formula to latex" in msg.lower():
545
  try:
546
  doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([answer], [Image.open(image_path)])
547
  doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
548
  markdown_output = doc.export_to_markdown()
 
 
 
 
 
549
  md_response = gr.ChatMessage(
550
  role="assistant",
551
  content=f"\nConverted to Markdown using docling.\n\n**LaTeX Output:**\n\n{markdown_output}",
 
555
  error_response = gr.ChatMessage(role="assistant", content=f"Error creating LaTeX output: {e!s}")
556
  cb.append(error_response)
557
 
558
+ elif "extract all key-value pairs" in msg.lower() and "doctag format" in msg.lower():
559
+ try:
560
+ doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([answer], [Image.open(image_path)])
561
+ doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
562
+ html_output = doc.export_to_html()
563
+ html_response = gr.ChatMessage(
564
+ role="assistant",
565
+ content=f"\nExtracted key-value pairs using docling.\n\n**HTML Output:**\n\n{html_output}",
566
+ )
567
+ cb.append(html_response)
568
+ except Exception as e:
569
+ error_response = gr.ChatMessage(role="assistant", content=f"Error creating HTML output: {e!s}")
570
+ cb.append(error_response)
571
+
572
  if has_loc_tags:
573
  try:
574
  annotated_image = draw_bounding_boxes(image_path, answer, is_doctag_response=has_doctag)