Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,15 @@ import os
|
|
| 6 |
# Direct API key placement
|
| 7 |
API_KEY = 'AIzaSyBMZrhMXRpQKp7M-JcN2Qk73afeta5Mv5Y'
|
| 8 |
|
| 9 |
-
def
|
| 10 |
"""
|
| 11 |
-
Extract text
|
| 12 |
|
| 13 |
Args:
|
| 14 |
image (PIL.Image): Uploaded image
|
| 15 |
|
| 16 |
Returns:
|
| 17 |
-
str: Extracted text
|
| 18 |
"""
|
| 19 |
# Validate input
|
| 20 |
if image is None:
|
|
@@ -27,22 +27,22 @@ def extract_exact_text(image):
|
|
| 27 |
# Use Gemini 2.0 Flash model
|
| 28 |
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 29 |
|
| 30 |
-
#
|
| 31 |
response = model.generate_content(
|
| 32 |
[
|
| 33 |
-
"CRITICAL INSTRUCTIONS: "
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
"
|
| 42 |
image
|
| 43 |
],
|
| 44 |
generation_config=genai.types.GenerationConfig(
|
| 45 |
-
temperature=0.1, #
|
| 46 |
max_output_tokens=500 # Increased to capture more text
|
| 47 |
)
|
| 48 |
)
|
|
@@ -50,9 +50,12 @@ def extract_exact_text(image):
|
|
| 50 |
# Extract text from response
|
| 51 |
extracted_text = response.text
|
| 52 |
|
| 53 |
-
# Validate
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return extracted_text
|
| 58 |
|
|
@@ -61,11 +64,11 @@ def extract_exact_text(image):
|
|
| 61 |
|
| 62 |
# Create Gradio Interface
|
| 63 |
demo = gr.Interface(
|
| 64 |
-
fn=
|
| 65 |
-
inputs=gr.Image(type="pil", label="Upload
|
| 66 |
-
outputs=gr.Textbox(label="Extracted Text", lines=10),
|
| 67 |
-
title="
|
| 68 |
-
description="Upload an image
|
| 69 |
)
|
| 70 |
|
| 71 |
# Launch the app
|
|
|
|
| 6 |
# Direct API key placement
|
| 7 |
API_KEY = 'AIzaSyBMZrhMXRpQKp7M-JcN2Qk73afeta5Mv5Y'
|
| 8 |
|
| 9 |
+
def extract_malayalam_text(image):
|
| 10 |
"""
|
| 11 |
+
Extract text specifically in Malayalam from the image
|
| 12 |
|
| 13 |
Args:
|
| 14 |
image (PIL.Image): Uploaded image
|
| 15 |
|
| 16 |
Returns:
|
| 17 |
+
str: Extracted Malayalam text
|
| 18 |
"""
|
| 19 |
# Validate input
|
| 20 |
if image is None:
|
|
|
|
| 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 FOR MALAYALAM TEXT EXTRACTION: "
|
| 34 |
+
"1. This image contains text in Malayalam script ONLY. "
|
| 35 |
+
"2. Extract ONLY Malayalam characters and script. "
|
| 36 |
+
"3. Use pure Malayalam Unicode characters. "
|
| 37 |
+
"4. If the text is in another language, state 'No Malayalam text found'. "
|
| 38 |
+
"5. Preserve exact Malayalam script, including any special characters. "
|
| 39 |
+
"6. DO NOT translate or convert to any other script. "
|
| 40 |
+
"7. If there are multiple lines, preserve line breaks. "
|
| 41 |
+
"8. Focus EXCLUSIVELY on Malayalam script.",
|
| 42 |
image
|
| 43 |
],
|
| 44 |
generation_config=genai.types.GenerationConfig(
|
| 45 |
+
temperature=0.1, # Ultra-low temperature for precise extraction
|
| 46 |
max_output_tokens=500 # Increased to capture more text
|
| 47 |
)
|
| 48 |
)
|
|
|
|
| 50 |
# Extract text from response
|
| 51 |
extracted_text = response.text
|
| 52 |
|
| 53 |
+
# Validate Malayalam characters
|
| 54 |
+
malayalam_chars = [char for char in extracted_text if '\u0D00' <= char <= '\u0D7F']
|
| 55 |
+
|
| 56 |
+
# Check if extracted text contains Malayalam characters
|
| 57 |
+
if not malayalam_chars:
|
| 58 |
+
return "No Malayalam text detected. Please verify the image contains Malayalam script."
|
| 59 |
|
| 60 |
return extracted_text
|
| 61 |
|
|
|
|
| 64 |
|
| 65 |
# Create Gradio Interface
|
| 66 |
demo = gr.Interface(
|
| 67 |
+
fn=extract_malayalam_text,
|
| 68 |
+
inputs=gr.Image(type="pil", label="Upload Malayalam Text Image"),
|
| 69 |
+
outputs=gr.Textbox(label="Extracted Malayalam Text", lines=10),
|
| 70 |
+
title="Malayalam Text Extractor",
|
| 71 |
+
description="Upload an image containing Malayalam text for precise extraction."
|
| 72 |
)
|
| 73 |
|
| 74 |
# Launch the app
|