vrindagopinath commited on
Commit
edb0289
·
verified ·
1 Parent(s): e54728f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ from PIL import Image
4
+
5
+ def extract_malayalam_text(image, api_key):
6
+ """
7
+ Extract handwritten Malayalam text from an image using Gemini 2.0 Flash
8
+
9
+ Args:
10
+ image (PIL.Image): Uploaded image
11
+ api_key (str): Google AI Studio API Key
12
+
13
+ Returns:
14
+ str: Extracted text from the image
15
+ """
16
+ # Validate inputs
17
+ if image is None:
18
+ return "Please upload an image first."
19
+
20
+ if not api_key:
21
+ return "Please provide a valid API key."
22
+
23
+ try:
24
+ # Configure the Gemini API directly with the provided key
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
+ # Generate content from the image
31
+ response = model.generate_content(
32
+ [
33
+ "You are an expert in extracting handwritten Malayalam text from grocery lists. "
34
+ "Carefully transcribe each item in Malayalam script. "
35
+ "Provide a clear, accurate list of items. "
36
+ "If the text is unclear, mention potential uncertainties.",
37
+ image
38
+ ],
39
+ generation_config=genai.types.GenerationConfig(
40
+ temperature=0.2, # Low temperature for precise extraction
41
+ max_output_tokens=300 # Adjust based on expected list length
42
+ )
43
+ )
44
+
45
+ # Return the extracted text
46
+ return response.text
47
+
48
+ except Exception as e:
49
+ return f"An error occurred: {str(e)}"
50
+
51
+ def create_malayalam_ocr_interface():
52
+ """
53
+ Create Gradio interface for Malayalam OCR
54
+ """
55
+ with gr.Blocks() as demo:
56
+ # Title and description
57
+ gr.Markdown("# Malayalam Handwritten Text Extractor")
58
+ gr.Markdown("Upload a handwritten Malayalam grocery list image for text extraction.")
59
+
60
+ # API Key input
61
+ api_key_input = gr.Textbox(
62
+ label="Google AI Studio API Key",
63
+ type="password",
64
+ placeholder="Enter your Gemini API key"
65
+ )
66
+
67
+ # Image upload component
68
+ image_input = gr.Image(
69
+ type="pil",
70
+ label="Upload Malayalam Grocery List Image"
71
+ )
72
+
73
+ # Extract button
74
+ extract_btn = gr.Button("Extract Text")
75
+
76
+ # Output text area
77
+ output_text = gr.Textbox(
78
+ label="Extracted Malayalam Text",
79
+ lines=10
80
+ )
81
+
82
+ # Connect the components
83
+ extract_btn.click(
84
+ fn=extract_malayalam_text,
85
+ inputs=[image_input, api_key_input],
86
+ outputs=output_text
87
+ )
88
+
89
+ return demo
90
+
91
+ def main():
92
+ # Create and launch the Gradio interface
93
+ demo = create_malayalam_ocr_interface()
94
+ demo.launch(
95
+ share=True, # Create a public link
96
+ debug=True # Show detailed errors
97
+ )
98
+
99
+ if __name__ == "__main__":
100
+ main()