etiennebcp commited on
Commit
3750374
Β·
verified Β·
1 Parent(s): 85834ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -7,7 +7,7 @@ import os
7
 
8
  print("=== DEBUG: Starting app.py ===")
9
 
10
- # Get example images
11
  example_dir = os.path.join(os.environ.get("HOME", "/home/user"), "app", "example_images")
12
  example_images = []
13
 
@@ -27,11 +27,9 @@ def encode_image_to_base64(image: Image.Image) -> str:
27
 
28
  def load_image_any(image):
29
  """
30
- Gradio Image can return:
31
- - filepath (str) if type="filepath"
32
- - PIL.Image if type="pil"
33
- - None
34
- We normalize to PIL.Image in RGB.
35
  """
36
  if image is None:
37
  return None
@@ -39,7 +37,7 @@ def load_image_any(image):
39
  return Image.open(image).convert("RGB")
40
  if isinstance(image, Image.Image):
41
  return image.convert("RGB")
42
- # Fallback: try to coerce
43
  return Image.open(image).convert("RGB")
44
 
45
  def query_vllm_api(image, temperature, max_tokens=12_000):
@@ -50,11 +48,11 @@ def query_vllm_api(image, temperature, max_tokens=12_000):
50
  return "No image provided", "No image provided", "Please upload an image first."
51
 
52
  try:
53
- # Optional: Resize image if needed (to avoid huge uploads)
54
  max_size = 2048
55
  if max(pil_img.size) > max_size:
56
  ratio = max_size / max(pil_img.size)
57
- new_size = tuple(int(dim * ratio) for dim in pil_img.size)
58
  pil_img = pil_img.resize(new_size, Image.Resampling.LANCZOS)
59
 
60
  image_b64 = encode_image_to_base64(pil_img)
@@ -84,11 +82,11 @@ def query_vllm_api(image, temperature, max_tokens=12_000):
84
  data = response.json()
85
  result = data["choices"][0]["message"]["content"]
86
 
87
- # Handle the thinking/answer parsing
88
  try:
89
  reasoning = result.split("<think>")[1].split("</think>")[0]
90
  answer = result.split("<answer>")[1].split("</answer>")[0]
91
- except IndexError:
92
  reasoning = "No thinking trace found"
93
  answer = result
94
 
@@ -122,8 +120,8 @@ with gr.Blocks(title="NuMarkdown-8B-Thinking") as demo:
122
  <a href="https://huggingface.co/numind/NuMarkdown-8B-Thinking" target="_blank" rel="noopener noreferrer" style="color: white; text-decoration: none; margin: 0 10px; font-weight: 500;">πŸ€— Model</a>
123
  </div>
124
  </div>
125
- <p>NuMarkdown-8B-Thinking is the first reasoning OCR VLM. It is specifically trained to convert documents into clean Markdown files, well suited for RAG applications.</p>
126
- <p>NOTE: In this space we downsize large images and restrict the maximum output of the model, so performance could improve if you run the model yourself.</p>
127
  """
128
  )
129
 
@@ -132,17 +130,9 @@ with gr.Blocks(title="NuMarkdown-8B-Thinking") as demo:
132
  temperature = gr.Slider(0.1, 1.5, value=0.4, step=0.1, label="Temperature")
133
  btn = gr.Button("Generate Response", variant="primary", size="lg")
134
 
135
- # βœ… Use filepath so the input panel reliably shows both uploads and Examples
136
  img_in = gr.Image(type="filepath", label="Upload Image")
137
 
138
- # Optional: show what Gradio is actually passing to backend (for debugging)
139
- dbg = gr.Textbox(label="Debug (image value)", interactive=False)
140
-
141
- def debug_image_value(image):
142
- return f"{type(image)}: {image}"
143
-
144
- img_in.change(debug_image_value, inputs=img_in, outputs=dbg)
145
-
146
  with gr.Column(scale=2):
147
  with gr.Accordion("πŸ” Model Outputs", open=True):
148
  with gr.Tabs():
@@ -169,7 +159,6 @@ with gr.Blocks(title="NuMarkdown-8B-Thinking") as demo:
169
  outputs=[thinking, raw_answer, output],
170
  )
171
 
172
- # βœ… Examples: list-of-lists + inputs as list, works reliably across Gradio versions
173
  if example_images:
174
  gr.Examples(
175
  examples=[[p] for p in example_images[:5]],
@@ -181,16 +170,23 @@ print("=== DEBUG: Gradio interface created ===")
181
 
182
  if __name__ == "__main__":
183
  print("=== DEBUG: About to launch Gradio ===")
 
 
 
 
 
 
 
 
184
  demo.launch(
185
  server_name="0.0.0.0",
186
  server_port=7860,
187
  share=True,
188
- # Security: allow serving local example files if your environment requires it.
189
- # Harmless if not needed; helpful in many deployments.
190
- allowed_paths=[example_dir] if os.path.exists(example_dir) else None,
191
  theme=gr.themes.Soft(),
 
192
  css="""
193
  * { font-family: 'Inter', 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif !important; }
194
  """,
195
  )
 
196
  print("=== DEBUG: Gradio launched ===")
 
7
 
8
  print("=== DEBUG: Starting app.py ===")
9
 
10
+ # Example images directory
11
  example_dir = os.path.join(os.environ.get("HOME", "/home/user"), "app", "example_images")
12
  example_images = []
13
 
 
27
 
28
  def load_image_any(image):
29
  """
30
+ With gr.Image(type="filepath"), image is a str path.
31
+ With other types it can be PIL.Image.
32
+ Normalize to PIL.Image RGB.
 
 
33
  """
34
  if image is None:
35
  return None
 
37
  return Image.open(image).convert("RGB")
38
  if isinstance(image, Image.Image):
39
  return image.convert("RGB")
40
+ # Best-effort fallback
41
  return Image.open(image).convert("RGB")
42
 
43
  def query_vllm_api(image, temperature, max_tokens=12_000):
 
48
  return "No image provided", "No image provided", "Please upload an image first."
49
 
50
  try:
51
+ # Optional resize to avoid huge uploads
52
  max_size = 2048
53
  if max(pil_img.size) > max_size:
54
  ratio = max_size / max(pil_img.size)
55
+ new_size = (int(pil_img.size[0] * ratio), int(pil_img.size[1] * ratio))
56
  pil_img = pil_img.resize(new_size, Image.Resampling.LANCZOS)
57
 
58
  image_b64 = encode_image_to_base64(pil_img)
 
82
  data = response.json()
83
  result = data["choices"][0]["message"]["content"]
84
 
85
+ # Parse optional <think>/<answer>
86
  try:
87
  reasoning = result.split("<think>")[1].split("</think>")[0]
88
  answer = result.split("<answer>")[1].split("</answer>")[0]
89
+ except Exception:
90
  reasoning = "No thinking trace found"
91
  answer = result
92
 
 
120
  <a href="https://huggingface.co/numind/NuMarkdown-8B-Thinking" target="_blank" rel="noopener noreferrer" style="color: white; text-decoration: none; margin: 0 10px; font-weight: 500;">πŸ€— Model</a>
121
  </div>
122
  </div>
123
+ <p>NuMarkdown-8B-Thinking converts documents into clean Markdown, well suited for RAG applications.</p>
124
+ <p>NOTE: We downsize large images and restrict max output tokens in this demo.</p>
125
  """
126
  )
127
 
 
130
  temperature = gr.Slider(0.1, 1.5, value=0.4, step=0.1, label="Temperature")
131
  btn = gr.Button("Generate Response", variant="primary", size="lg")
132
 
133
+ # βœ… Use filepath so preview works consistently with Examples and uploads
134
  img_in = gr.Image(type="filepath", label="Upload Image")
135
 
 
 
 
 
 
 
 
 
136
  with gr.Column(scale=2):
137
  with gr.Accordion("πŸ” Model Outputs", open=True):
138
  with gr.Tabs():
 
159
  outputs=[thinking, raw_answer, output],
160
  )
161
 
 
162
  if example_images:
163
  gr.Examples(
164
  examples=[[p] for p in example_images[:5]],
 
170
 
171
  if __name__ == "__main__":
172
  print("=== DEBUG: About to launch Gradio ===")
173
+
174
+ # βœ… IMPORTANT:
175
+ # If you set allowed_paths, include Gradio's upload temp dir, otherwise previews break.
176
+ # Uploads typically land in /tmp/gradio/...
177
+ allowed = ["/tmp/gradio"]
178
+ if os.path.exists(example_dir):
179
+ allowed.append(example_dir)
180
+
181
  demo.launch(
182
  server_name="0.0.0.0",
183
  server_port=7860,
184
  share=True,
 
 
 
185
  theme=gr.themes.Soft(),
186
+ allowed_paths=allowed, # βœ… include /tmp/gradio so uploaded previews render
187
  css="""
188
  * { font-family: 'Inter', 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif !important; }
189
  """,
190
  )
191
+
192
  print("=== DEBUG: Gradio launched ===")