sifujohn commited on
Commit
795edde
·
verified ·
1 Parent(s): d6dc043

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
+ from PIL import Image
5
+ import time
6
+
7
+ MODEL = "microsoft/trocr-small-printed"
8
+
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+
11
+ # Load once at startup
12
+ processor = TrOCRProcessor.from_pretrained(MODEL)
13
+ model = VisionEncoderDecoderModel.from_pretrained(MODEL).to(device)
14
+
15
+ def extract_text(image):
16
+ if image is None:
17
+ return "⚠️ Please upload an image."
18
+
19
+ start_time = time.time()
20
+
21
+ if not isinstance(image, Image.Image):
22
+ image = Image.fromarray(image)
23
+
24
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
25
+
26
+ generated_ids = model.generate(pixel_values)
27
+ text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
28
+
29
+ runtime = round(time.time() - start_time, 2)
30
+
31
+ return f"""📝 Extracted Text:
32
+
33
+ {text}
34
+
35
+ ⏱ Processed in {runtime} seconds
36
+ """
37
+
38
+ demo = gr.Interface(
39
+ fn=extract_text,
40
+ inputs=gr.Image(type="pil", label="Upload Image"),
41
+ outputs=gr.Textbox(label="OCR Result"),
42
+ title="🖼 Image → Text Demo",
43
+ description="Upload an image with printed text. Powered by Microsoft TrOCR running locally on Hugging Face Spaces.",
44
+ examples=[
45
+ ["https://huggingface.co/datasets/nielsr/image_dummy/raw/main/receipt.png"]
46
+ ]
47
+ )
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch()