Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
#import torch
|
6 |
+
|
7 |
+
# Load the processor and model
|
8 |
+
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-printed')
|
9 |
+
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-printed')
|
10 |
+
|
11 |
+
def generate_text(input_image):
|
12 |
+
# Convert Gradio input image to PIL Image
|
13 |
+
image = Image.fromarray(input_image)
|
14 |
+
# Process the image and generate text
|
15 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
16 |
+
generated_ids = model.generate(pixel_values)
|
17 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
18 |
+
return generated_text
|
19 |
+
|
20 |
+
# Define the Gradio interface
|
21 |
+
iface = gr.Interface(
|
22 |
+
fn=generate_text,
|
23 |
+
inputs=gr.Image(), # Gradio Image input
|
24 |
+
outputs=gr.Textbox(), # Gradio Textbox output
|
25 |
+
)
|
26 |
+
|
27 |
+
if __name__ == "__main__":
|
28 |
+
iface.launch()
|