Tonic commited on
Commit
405302e
β€’
1 Parent(s): 7dcbad8

improve file handling , display html

Browse files
Files changed (3) hide show
  1. .gitignore +1 -1
  2. app.py +4 -7
  3. notes.py +92 -0
.gitignore CHANGED
@@ -1,3 +1,3 @@
1
  .DS_Store
2
  .venv/*
3
- notes.py
 
1
  .DS_Store
2
  .venv/*
3
+ .notes.py
app.py CHANGED
@@ -73,17 +73,14 @@ def update_inputs(task):
73
  gr.update(visible=True, choices=["red", "green", "blue"]),
74
  ]
75
  def ocr_demo(image, task, ocr_type, ocr_box, ocr_color):
76
- res, html_content = process_image(image, task, ocr_type, ocr_box, ocr_color)
77
 
78
  res = f"$$ {res} $$"
79
- # res = res.replace("$$ \\begin{tabular}", "\\begin{tabular}")
80
- # res = res.replace("\\end{tabular} $$", "\\end{tabular}")
81
- # res = res.replace("\\(", "")
82
- # res = res.replace("\\)", "")
83
 
84
  if html_content:
85
- html_string = f'<iframe srcdoc="{html_content}" width="100%" height="600px"></iframe>'
86
- return res, html_string
 
87
  return res, None
88
 
89
  def cleanup_old_files():
 
73
  gr.update(visible=True, choices=["red", "green", "blue"]),
74
  ]
75
  def ocr_demo(image, task, ocr_type, ocr_box, ocr_color):
76
+ res, html_content, unique_id = process_image(image, task, ocr_type, ocr_box, ocr_color)
77
 
78
  res = f"$$ {res} $$"
 
 
 
 
79
 
80
  if html_content:
81
+ iframe = f'<iframe srcdoc="{html_content}" width="100%" height="600px"></iframe>'
82
+ link = f'<a href="file={results_folder / f"{unique_id}.html"}" target="_blank">View Full Result</a>'
83
+ return res, f"{link}<br>{iframe}"
84
  return res, None
85
 
86
  def cleanup_old_files():
notes.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def ocr_demo(image, task, ocr_type, ocr_box, ocr_color):
2
+ res, html_content = process_image(image, task, ocr_type, ocr_box, ocr_color)
3
+
4
+ res = f"$$ {res} $$"
5
+ # res = res.replace("$$ \\begin{tabular}", "\\begin{tabular}")
6
+ # res = res.replace("\\end{tabular} $$", "\\end{tabular}")
7
+ # res = res.replace("\\(", "")
8
+ # res = res.replace("\\)", "")
9
+
10
+ if html_content:
11
+ html_string = f'<iframe srcdoc="{html_content}" width="100%" height="600px"></iframe>'
12
+ return res, html_string
13
+ return res, None
14
+
15
+ @spaces.GPU
16
+ def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
17
+ demo_html = os.path.join(results_folder, "demo.html")
18
+ html_file = os.path.join(results_folder, f"{task.replace(' ', '_').lower()}.html")
19
+ tikz_file = os.path.join(results_folder, "tikz.html")
20
+
21
+ unique_id = str(uuid.uuid4())
22
+
23
+ with tempfile.NamedTemporaryFile(mode='w+', suffix='.html', delete=False, dir=results_folder) as temp_file:
24
+ temp_html_path = temp_file.name
25
+
26
+ if task == "Plain Text OCR":
27
+ res = model.chat(tokenizer, image, ocr_type='ocr')
28
+ return res, None, unique_id
29
+ else:
30
+ if task == "Format Text OCR":
31
+ res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file=temp_html_path)
32
+ elif task == "Fine-grained OCR (Box)":
33
+ res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_box=ocr_box, render=True, save_render_file=temp_html_path)
34
+ elif task == "Fine-grained OCR (Color)":
35
+ res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_color=ocr_color, render=True, save_render_file=temp_html_path)
36
+ elif task == "Multi-crop OCR":
37
+ res = model.chat_crop(tokenizer, image, ocr_type='format', render=True, save_render_file=temp_html_path)
38
+ elif task == "Render Formatted OCR":
39
+ res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file=temp_html_path)
40
+
41
+ # html_content = None
42
+ if os.path.exists(temp_html_path):
43
+ with open(temp_html_path, 'r') as f:
44
+ html_content = f.read()
45
+ if os.path.exists(demo_html):
46
+ with open(demo_html, 'r') as f:
47
+ html_content = f.read()
48
+ elif os.path.exists(html_file):
49
+ with open(html_file, 'r') as f:
50
+ html_content = f.read()
51
+ elif os.path.exists(tikz_file):
52
+ with open(tikz_file, 'r') as f:
53
+ html_content = f.read()
54
+ else:
55
+ html_content = None
56
+
57
+ return res, html_content, unique_id
58
+
59
+ @spaces.GPU
60
+ def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
61
+ demo_html = os.path.join(results_folder, "demo.html")
62
+ html_file = os.path.join(results_folder, f"{task.replace(' ', '_').lower()}.html")
63
+ tikz_file = os.path.join(results_folder, "tikz.html")
64
+
65
+ if task == "Plain Text OCR":
66
+ res = model.chat(tokenizer, image, ocr_type='ocr')
67
+ return res, None
68
+ else:
69
+ if task == "Format Text OCR":
70
+ res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file=demo_html)
71
+ elif task == "Fine-grained OCR (Box)":
72
+ res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_box=ocr_box, render=True, save_render_file=demo_html)
73
+ elif task == "Fine-grained OCR (Color)":
74
+ res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_color=ocr_color, render=True, save_render_file=demo_html)
75
+ elif task == "Multi-crop OCR":
76
+ res = model.chat_crop(tokenizer, image, ocr_type='format', render=True, save_render_file=demo_html)
77
+ elif task == "Render Formatted OCR":
78
+ res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file=demo_html)
79
+
80
+ if os.path.exists(demo_html):
81
+ with open(demo_html, 'r') as f:
82
+ html_content = f.read()
83
+ elif os.path.exists(html_file):
84
+ with open(html_file, 'r') as f:
85
+ html_content = f.read()
86
+ elif os.path.exists(tikz_file):
87
+ with open(tikz_file, 'r') as f:
88
+ html_content = f.read()
89
+ else:
90
+ html_content = None
91
+
92
+ return res, html_content