Spaces:
Runtime error
Runtime error
| 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) | |