File size: 1,399 Bytes
6c44e27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from transformers import AutoProcessor, AutoModelForImageTextToText
from PIL import Image

# Load Meta-Llama Vision-Instruct model
processor = AutoProcessor.from_pretrained("meta-llama/Llama-3.2-11B-Vision-Instruct")
model = AutoModelForImageTextToText.from_pretrained("meta-llama/Llama-3.2-11B-Vision-Instruct")

def extract_text_from_image(image):
    """
    Function to extract text from a handwritten image using the Meta-Llama model.
    """
    try:
        # Preprocess the image
        inputs = processor(images=image, return_tensors="pt").to("cuda")
        
        # Generate predictions
        outputs = model.generate(**inputs)
        
        # Decode the generated text
        extracted_text = processor.decode(outputs[0], skip_special_tokens=True)
        
        return extracted_text
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Define Gradio interface
title = "Handwritten Text Extraction"
description = """
Upload a handwritten image, and this app will use Meta-Llama Vision-Instruct to extract text from the image.
"""

demo = gr.Interface(
    fn=extract_text_from_image,
    inputs=gr.Image(type="pil", label="Upload Handwritten Image"),
    outputs=gr.Textbox(label="Extracted Text"),
    title=title,
    description=description,
)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)