AudreyMireille commited on
Commit
fd3da74
1 Parent(s): 3386f4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ from PIL import Image
4
+ import requests
5
+
6
+ # Load the TrOCR model and processor
7
+ processor = TrOCRProcessor.from_pretrained('microsoft/trocr-large-printed')
8
+ model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-printed')
9
+
10
+ def extract_text_from_image(image_url):
11
+ image = Image.open(requests.get(image_url, stream=True).raw).convert("RGB")
12
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
13
+ generated_ids = model.generate(pixel_values)
14
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
15
+ return generated_text
16
+
17
+ # Create the Gradio interface
18
+ gr.Interface(
19
+ fn=extract_text_from_image,
20
+ inputs=gr.Textbox(label="Enter image URL:"),
21
+ outputs=gr.Textbox(label="Extracted Text"),
22
+ title="Text Extraction from Images",
23
+ description="Extract text from an image using optical character recognition (OCR).",
24
+ ).launch()