vrindagopinath commited on
Commit
23172e9
·
verified ·
1 Parent(s): 7290b66

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ from PIL import Image
4
+ import os
5
+
6
+ # Direct API key placement (NOT RECOMMENDED)
7
+ API_KEY = 'AIzaSyBMZrhMXRpQKp7M-JcN2Qk73afeta5Mv5Y'
8
+
9
+ def extract_malayalam_text(image):
10
+ """
11
+ Extract handwritten Malayalam text from an image using Gemini 2.0 Flash
12
+
13
+ Args:
14
+ image (PIL.Image): Uploaded image
15
+
16
+ Returns:
17
+ str: Extracted text from the image
18
+ """
19
+ # Validate input
20
+ if image is None:
21
+ return "Please upload an image first."
22
+
23
+ try:
24
+ # Configure the Gemini API
25
+ genai.configure(api_key=API_KEY)
26
+
27
+ # Use Gemini 2.0 Flash model
28
+ model = genai.GenerativeModel('gemini-2.0-flash')
29
+
30
+ # Highly specific prompt for Malayalam text extraction
31
+ response = model.generate_content(
32
+ [
33
+ "CRITICAL INSTRUCTIONS: "
34
+ "This is a handwritten Malayalam grocery list. "
35
+ "You MUST extract the text ONLY in Malayalam script. "
36
+ "Rules for extraction: "
37
+ "1. Use pure Malayalam Unicode characters "
38
+ "2. Maintain original handwriting style "
39
+ "3. If any text is unclear, mark with [uncertain] "
40
+ "4. Do not translate or modify original text "
41
+ "5. Preserve exact Malayalam script as written",
42
+ image
43
+ ],
44
+ generation_config=genai.types.GenerationConfig(
45
+ temperature=0.1, # Ultra-low temperature for precise extraction
46
+ max_output_tokens=300 # Adjust based on expected list length
47
+ )
48
+ )
49
+
50
+ # Return the extracted text
51
+ extracted_text = response.text
52
+
53
+ # Additional validation
54
+ if not any('\u0D00' <= char <= '\u0D7F' for char in extracted_text):
55
+ return "No Malayalam characters detected. Please check the image quality."
56
+
57
+ return extracted_text
58
+
59
+ except Exception as e:
60
+ return f"An error occurred: {str(e)}"
61
+
62
+ # Create Gradio Interface
63
+ demo = gr.Interface(
64
+ fn=extract_malayalam_text,
65
+ inputs=gr.Image(type="pil", label="Upload Malayalam Grocery List Image"),
66
+ outputs=gr.Textbox(label="Extracted Malayalam Text", lines=10),
67
+ title="Malayalam Handwritten Text Extractor",
68
+ description="Upload a handwritten Malayalam grocery list image for text extraction."
69
+ )
70
+
71
+ # Launch the app
72
+ if __name__ == "__main__":
73
+ demo.launch()