Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
|
7 |
+
# Direct API key placement
|
8 |
+
API_KEY = 'AIzaSyBMZrhMXRpQKp7M-JcN2Qk73afeta5Mv5Y'
|
9 |
+
|
10 |
+
def extract_malayalam_letters(image):
|
11 |
+
"""
|
12 |
+
Extract ONLY Malayalam letters from an image using Gemini 2.0 Flash
|
13 |
+
|
14 |
+
Args:
|
15 |
+
image (PIL.Image): Uploaded image
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
str: Extracted Malayalam letters
|
19 |
+
"""
|
20 |
+
# Validate input
|
21 |
+
if image is None:
|
22 |
+
return "Please upload an image first."
|
23 |
+
|
24 |
+
try:
|
25 |
+
# Configure the Gemini API
|
26 |
+
genai.configure(api_key=API_KEY)
|
27 |
+
|
28 |
+
# Use Gemini 2.0 Flash model
|
29 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
30 |
+
|
31 |
+
# Prompt to focus on Malayalam letters
|
32 |
+
response = model.generate_content(
|
33 |
+
[
|
34 |
+
"CRITICAL INSTRUCTIONS: "
|
35 |
+
"This is a handwritten image. "
|
36 |
+
"Extract ONLY Malayalam letters. "
|
37 |
+
"Rules for extraction: "
|
38 |
+
"1. Return ONLY pure Malayalam Unicode characters "
|
39 |
+
"2. Remove ALL numbers and English letters "
|
40 |
+
"3. Preserve original order "
|
41 |
+
"4. Do not translate or modify text",
|
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 text length
|
47 |
+
)
|
48 |
+
)
|
49 |
+
|
50 |
+
# Extract text from response
|
51 |
+
extracted_text = response.text
|
52 |
+
|
53 |
+
# Filter to keep only Malayalam letters
|
54 |
+
malayalam_letters = ''.join(
|
55 |
+
char for char in extracted_text
|
56 |
+
if '\u0D00' <= char <= '\u0D7F'
|
57 |
+
)
|
58 |
+
|
59 |
+
# Validate Malayalam letters extraction
|
60 |
+
if not malayalam_letters:
|
61 |
+
return "No Malayalam letters detected. Please check the image quality."
|
62 |
+
|
63 |
+
return malayalam_letters
|
64 |
+
|
65 |
+
except Exception as e:
|
66 |
+
return f"An error occurred: {str(e)}"
|
67 |
+
|
68 |
+
# Create Gradio Interface
|
69 |
+
demo = gr.Interface(
|
70 |
+
fn=extract_malayalam_letters,
|
71 |
+
inputs=gr.Image(type="pil", label="Upload Image with Malayalam Text"),
|
72 |
+
outputs=gr.Textbox(label="Extracted Malayalam Letters", lines=10),
|
73 |
+
title="Malayalam Letters Extractor",
|
74 |
+
description="Upload an image to extract ONLY Malayalam letters."
|
75 |
+
)
|
76 |
+
|
77 |
+
# Launch the app
|
78 |
+
if __name__ == "__main__":
|
79 |
+
demo.launch()
|