import gradio as gr import google.generativeai as genai from PIL import Image import os # Direct API key placement (NOT RECOMMENDED) API_KEY = 'AIzaSyBMZrhMXRpQKp7M-JcN2Qk73afeta5Mv5Y' def extract_malayalam_text(image): """ Extract handwritten Malayalam text from an image using Gemini 2.0 Flash Args: image (PIL.Image): Uploaded image Returns: str: Extracted text from the image """ # Validate input if image is None: return "Please upload an image first." try: # Configure the Gemini API genai.configure(api_key=API_KEY) # Use Gemini 2.0 Flash model model = genai.GenerativeModel('gemini-2.0-flash') # Highly specific prompt for Malayalam text extraction response = model.generate_content( [ "CRITICAL INSTRUCTIONS: " "This is a handwritten Malayalam grocery list. " "You MUST extract the text ONLY in Malayalam script. " "Rules for extraction: " "1. Use pure Malayalam Unicode characters " "2. Maintain original handwriting style " "3. If any text is unclear, mark with [uncertain] " "4. Do not translate or modify original text " "5. Preserve exact Malayalam script as written", image ], generation_config=genai.types.GenerationConfig( temperature=0.1, # Ultra-low temperature for precise extraction max_output_tokens=300 # Adjust based on expected list length ) ) # Return the extracted text extracted_text = response.text # Additional validation if not any('\u0D00' <= char <= '\u0D7F' for char in extracted_text): return "No Malayalam characters detected. Please check the image quality." return extracted_text except Exception as e: return f"An error occurred: {str(e)}" # Create Gradio Interface demo = gr.Interface( fn=extract_malayalam_text, inputs=gr.Image(type="pil", label="Upload Malayalam Grocery List Image"), outputs=gr.Textbox(label="Extracted Malayalam Text", lines=10), title="Malayalam Handwritten Text Extractor", description="Upload a handwritten Malayalam grocery list image for text extraction." ) # Launch the app if __name__ == "__main__": demo.launch()