Spaces:
Running
on
Zero
Running
on
Zero
Odulana Hammed
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Load Meta-Llama Vision-Instruct model
|
6 |
+
processor = AutoProcessor.from_pretrained("meta-llama/Llama-3.2-11B-Vision-Instruct")
|
7 |
+
model = AutoModelForImageTextToText.from_pretrained("meta-llama/Llama-3.2-11B-Vision-Instruct")
|
8 |
+
|
9 |
+
def extract_text_from_image(image):
|
10 |
+
"""
|
11 |
+
Function to extract text from a handwritten image using the Meta-Llama model.
|
12 |
+
"""
|
13 |
+
try:
|
14 |
+
# Preprocess the image
|
15 |
+
inputs = processor(images=image, return_tensors="pt").to("cuda")
|
16 |
+
|
17 |
+
# Generate predictions
|
18 |
+
outputs = model.generate(**inputs)
|
19 |
+
|
20 |
+
# Decode the generated text
|
21 |
+
extracted_text = processor.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
|
23 |
+
return extracted_text
|
24 |
+
except Exception as e:
|
25 |
+
return f"An error occurred: {str(e)}"
|
26 |
+
|
27 |
+
# Define Gradio interface
|
28 |
+
title = "Handwritten Text Extraction"
|
29 |
+
description = """
|
30 |
+
Upload a handwritten image, and this app will use Meta-Llama Vision-Instruct to extract text from the image.
|
31 |
+
"""
|
32 |
+
|
33 |
+
demo = gr.Interface(
|
34 |
+
fn=extract_text_from_image,
|
35 |
+
inputs=gr.Image(type="pil", label="Upload Handwritten Image"),
|
36 |
+
outputs=gr.Textbox(label="Extracted Text"),
|
37 |
+
title=title,
|
38 |
+
description=description,
|
39 |
+
)
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|