vrindagopinath commited on
Commit
0e8f347
·
verified ·
1 Parent(s): 04302ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -6,15 +6,15 @@ import os
6
  # Direct API key placement
7
  API_KEY = 'AIzaSyBMZrhMXRpQKp7M-JcN2Qk73afeta5Mv5Y'
8
 
9
- def extract_exact_text(image):
10
  """
11
- Extract text exactly as it appears in the uploaded image
12
 
13
  Args:
14
  image (PIL.Image): Uploaded image
15
 
16
  Returns:
17
- str: Extracted text preserving original format
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
- # Prompt to extract text exactly as in the image
31
  response = model.generate_content(
32
  [
33
- "CRITICAL INSTRUCTIONS: "
34
- "Extract the text EXACTLY as it appears in the image. "
35
- "Preserve: "
36
- "1. Exact text content "
37
- "2. Original formatting "
38
- "3. Line breaks "
39
- "4. Spacing "
40
- "5. Case sensitivity "
41
- "Do not modify or interpret the text in any way.",
42
  image
43
  ],
44
  generation_config=genai.types.GenerationConfig(
45
- temperature=0.1, # Lowest temperature for precise extraction
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 text extraction
54
- if not extracted_text or extracted_text.strip() == "":
55
- return "No text detected. Please check the image quality."
 
 
 
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=extract_exact_text,
65
- inputs=gr.Image(type="pil", label="Upload Image with Text"),
66
- outputs=gr.Textbox(label="Extracted Text", lines=10),
67
- title="Exact Text Extractor",
68
- description="Upload an image to extract text exactly as it appears."
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