Ziptoze commited on
Commit
4aadd8c
·
verified ·
1 Parent(s): dbe24b8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -2
app.py CHANGED
@@ -25,6 +25,8 @@ try:
25
  else:
26
  device = "cpu"
27
  dtype = torch.float32 # CPUs handle float32 best
 
 
28
  print("Running on CPU mode")
29
 
30
  model_id = "lightonai/LightOnOCR-2-1B"
@@ -58,10 +60,10 @@ except Exception as e:
58
  processor = None
59
 
60
  # --- Helper Functions ---
61
- def resize_for_ocr(image, max_dim=896):
62
  """
63
  Resize image to be faster.
64
- Lowered max_dim from 1280 to 896 for CPU deployment to ensure reasonable speed.
65
  """
66
  if image is None: return None
67
  w, h = image.size
@@ -229,6 +231,15 @@ def stream_ocr(image):
229
  except Exception as e:
230
  yield f"Error during processing: {str(e)}", None
231
 
 
 
 
 
 
 
 
 
 
232
  # --- Aesthetic Custom CSS ---
233
  custom_css = """
234
  /* Dark Purple Gradient Background */
@@ -343,6 +354,17 @@ with gr.Blocks(title="Ultra OCR") as demo:
343
  gr.Markdown("### Download Results")
344
  output_file = gr.File(label="Download Word (.docx)", type="filepath")
345
 
 
 
 
 
 
 
 
 
 
 
 
346
  # Interactions
347
  run_btn.click(
348
  fn=stream_ocr,
 
25
  else:
26
  device = "cpu"
27
  dtype = torch.float32 # CPUs handle float32 best
28
+ # Prevent CPU thrashing on Spaces (usually 2 vCPUs)
29
+ torch.set_num_threads(4)
30
  print("Running on CPU mode")
31
 
32
  model_id = "lightonai/LightOnOCR-2-1B"
 
60
  processor = None
61
 
62
  # --- Helper Functions ---
63
+ def resize_for_ocr(image, max_dim=768):
64
  """
65
  Resize image to be faster.
66
+ Lowered max_dim from 1280->896->768 for CPU deployment to ensure reasonable speed.
67
  """
68
  if image is None: return None
69
  w, h = image.size
 
231
  except Exception as e:
232
  yield f"Error during processing: {str(e)}", None
233
 
234
+ # --- Prepare Examples ---
235
+ example_images = []
236
+ data_dir = os.path.join(os.path.dirname(__file__), 'data')
237
+ if os.path.exists(data_dir):
238
+ valid_exts = {".jpg", ".jpeg", ".png", ".bmp", ".webp"}
239
+ # Gradio Examples expects a list of lists: [[path1], [path2], ...]
240
+ example_images = [[os.path.join(data_dir, f)] for f in os.listdir(data_dir)
241
+ if os.path.splitext(f)[1].lower() in valid_exts]
242
+
243
  # --- Aesthetic Custom CSS ---
244
  custom_css = """
245
  /* Dark Purple Gradient Background */
 
354
  gr.Markdown("### Download Results")
355
  output_file = gr.File(label="Download Word (.docx)", type="filepath")
356
 
357
+ # Example Gallery
358
+ if example_images:
359
+ gr.HTML("<hr>")
360
+ gr.Markdown("### 📂 Sample Documents")
361
+ gr.Examples(
362
+ examples=example_images,
363
+ inputs=input_img,
364
+ label="Click a sample to test",
365
+ examples_per_page=5
366
+ )
367
+
368
  # Interactions
369
  run_btn.click(
370
  fn=stream_ocr,