natalis3405 commited on
Commit
13f302e
·
1 Parent(s): a915827
Files changed (1) hide show
  1. app.py +21 -73
app.py CHANGED
@@ -34,8 +34,7 @@ def load_model(model_choice):
34
  "FLAN-T5 Small (60M)": "google/flan-t5-small",
35
  "FLAN-T5 Base (220M)": "google/flan-t5-base",
36
  "FLAN-T5 Large (770M)": "google/flan-t5-large",
37
- "FLAN-T5 XL (3B)": "google/flan-t5-xl",
38
- "FLAN-T5 XXL (11B)": "google/flan-t5-xxl"
39
  }
40
 
41
  selected_model = model_map.get(model_choice, "google/flan-t5-base")
@@ -59,61 +58,41 @@ def load_model(model_choice):
59
 
60
  return loaded_generator
61
 
62
- def extract_dl_info(image, model_choice, manual_text, use_manual_text):
63
  """
64
  Extracts driver's license information from an image file using OCR and AI model processing.
65
 
66
  Args:
67
  image: PIL Image object from Gradio
68
  model_choice: Selected model from radio button
69
- manual_text: Manual text input from user
70
- use_manual_text: Boolean to determine if manual text should be used instead of OCR
71
 
72
  Returns:
73
- tuple: (raw_ocr_text, cleaned_ocr_text, json_result, status_message)
74
  """
75
  try:
76
- # Determine text source
77
- if use_manual_text and manual_text and manual_text.strip():
78
- # Use manual text input
79
- print("Using manual text input...")
80
- raw_text = manual_text.strip()
81
- cleaned_text = clean_ocr_text(raw_text)
82
- print(f"Manual text: {raw_text}")
83
- print(f"Cleaned text: {cleaned_text}")
84
- status = "Using manual text input"
85
- else:
86
- # Use OCR on image
87
- if image is None:
88
- return "No image provided", "No image provided", json.dumps({"error": "No image provided"}, indent=2), "Error: No image provided"
89
-
90
- # Convert Gradio image to RGB
91
- if hasattr(image, 'convert'):
92
- image = image.convert("RGB")
93
-
94
- # Use Tesseract OCR to extract text
95
- print("Extracting text with Tesseract OCR...")
96
- status = "Extracting text with OCR..."
97
- raw_text = pytesseract.image_to_string(image)
98
- print(f"Raw OCR text: {raw_text}")
99
-
100
- # Clean the OCR text using regex (only for cleaning, not extraction)
101
- cleaned_text = clean_ocr_text(raw_text)
102
- print(f"Cleaned OCR text: {cleaned_text}")
103
- status = "OCR completed, processing with AI..."
104
 
105
  # Use AI model to process cleaned OCR text and extract structured information
106
  print("Processing cleaned OCR text with AI model...")
107
 
108
  # Load model only when needed
109
  try:
110
- status = "Loading AI model..."
111
  generator = load_model(model_choice)
112
- status = "AI model loaded, extracting information..."
113
  except Exception as e:
114
  error_msg = f"Failed to load AI model: {str(e)}"
115
  print(error_msg)
116
- return raw_text, cleaned_text, json.dumps({"error": error_msg}, indent=2), f"Error: {error_msg}"
117
 
118
  # Create specific prompts for the AI model to extract individual fields
119
  name_prompt = f"From this driver's license text, what is the person's full name? Text: {cleaned_text} Answer:"
@@ -176,13 +155,12 @@ def extract_dl_info(image, model_choice, manual_text, use_manual_text):
176
 
177
  # Format JSON result
178
  json_result = json.dumps(extracted, indent=2)
179
- status = "Extraction completed successfully!"
180
 
181
- return raw_text, cleaned_text, json_result, status
182
 
183
  except Exception as e:
184
  error_msg = f"Processing failed: {str(e)}"
185
- return error_msg, error_msg, json.dumps({"error": error_msg}, indent=2), f"Error: {error_msg}"
186
 
187
  # Create Gradio interface
188
  with gr.Blocks(title="Driver's License Information Extractor") as demo:
@@ -202,34 +180,14 @@ with gr.Blocks(title="Driver's License Information Extractor") as demo:
202
  "FLAN-T5 Small (60M)",
203
  "FLAN-T5 Base (220M)",
204
  "FLAN-T5 Large (770M)",
205
- "FLAN-T5 XL (3B)",
206
- "FLAN-T5 XXL (11B)"
207
  ],
208
  value="FLAN-T5 Base (220M)",
209
  label="Select AI Model",
210
  info="Larger models are more accurate but slower to load and process"
211
  )
212
 
213
- use_manual_text = gr.Checkbox(
214
- label="Use Manual Text Input",
215
- value=False,
216
- info="Check this to type text manually instead of using OCR"
217
- )
218
-
219
- manual_text_input = gr.Textbox(
220
- label="Manual Text Input",
221
- lines=6,
222
- placeholder="Type or paste the driver's license text here...",
223
- visible=False
224
- )
225
-
226
  submit_btn = gr.Button("Extract Information", variant="primary")
227
- status_text = gr.Textbox(
228
- label="Status",
229
- value="Ready to process",
230
- interactive=False,
231
- visible=True
232
- )
233
 
234
  with gr.Column():
235
  raw_ocr = gr.Textbox(
@@ -248,20 +206,10 @@ with gr.Blocks(title="Driver's License Information Extractor") as demo:
248
  label="Extracted Information (JSON)"
249
  )
250
 
251
- # Show/hide manual text input based on checkbox
252
- def toggle_manual_input(use_manual):
253
- return gr.update(visible=use_manual)
254
-
255
- use_manual_text.change(
256
- fn=toggle_manual_input,
257
- inputs=[use_manual_text],
258
- outputs=[manual_text_input]
259
- )
260
-
261
  submit_btn.click(
262
  fn=extract_dl_info,
263
- inputs=[image_input, model_choice, manual_text_input, use_manual_text],
264
- outputs=[raw_ocr, cleaned_text, json_result, status_text]
265
  )
266
 
267
  if __name__ == "__main__":
 
34
  "FLAN-T5 Small (60M)": "google/flan-t5-small",
35
  "FLAN-T5 Base (220M)": "google/flan-t5-base",
36
  "FLAN-T5 Large (770M)": "google/flan-t5-large",
37
+ "FLAN-T5 XL (3B)": "google/flan-t5-xl"
 
38
  }
39
 
40
  selected_model = model_map.get(model_choice, "google/flan-t5-base")
 
58
 
59
  return loaded_generator
60
 
61
+ def extract_dl_info(image, model_choice):
62
  """
63
  Extracts driver's license information from an image file using OCR and AI model processing.
64
 
65
  Args:
66
  image: PIL Image object from Gradio
67
  model_choice: Selected model from radio button
 
 
68
 
69
  Returns:
70
+ tuple: (raw_ocr_text, cleaned_ocr_text, json_result)
71
  """
72
  try:
73
+ # Convert Gradio image to RGB
74
+ if hasattr(image, 'convert'):
75
+ image = image.convert("RGB")
76
+
77
+ # Use Tesseract OCR to extract text
78
+ print("Extracting text with Tesseract OCR...")
79
+ raw_text = pytesseract.image_to_string(image)
80
+ print(f"Raw OCR text: {raw_text}")
81
+
82
+ # Clean the OCR text using regex (only for cleaning, not extraction)
83
+ cleaned_text = clean_ocr_text(raw_text)
84
+ print(f"Cleaned OCR text: {cleaned_text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  # Use AI model to process cleaned OCR text and extract structured information
87
  print("Processing cleaned OCR text with AI model...")
88
 
89
  # Load model only when needed
90
  try:
 
91
  generator = load_model(model_choice)
 
92
  except Exception as e:
93
  error_msg = f"Failed to load AI model: {str(e)}"
94
  print(error_msg)
95
+ return raw_text, cleaned_text, json.dumps({"error": error_msg}, indent=2)
96
 
97
  # Create specific prompts for the AI model to extract individual fields
98
  name_prompt = f"From this driver's license text, what is the person's full name? Text: {cleaned_text} Answer:"
 
155
 
156
  # Format JSON result
157
  json_result = json.dumps(extracted, indent=2)
 
158
 
159
+ return raw_text, cleaned_text, json_result
160
 
161
  except Exception as e:
162
  error_msg = f"Processing failed: {str(e)}"
163
+ return error_msg, error_msg, json.dumps({"error": error_msg}, indent=2)
164
 
165
  # Create Gradio interface
166
  with gr.Blocks(title="Driver's License Information Extractor") as demo:
 
180
  "FLAN-T5 Small (60M)",
181
  "FLAN-T5 Base (220M)",
182
  "FLAN-T5 Large (770M)",
183
+ "FLAN-T5 XL (3B)"
 
184
  ],
185
  value="FLAN-T5 Base (220M)",
186
  label="Select AI Model",
187
  info="Larger models are more accurate but slower to load and process"
188
  )
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  submit_btn = gr.Button("Extract Information", variant="primary")
 
 
 
 
 
 
191
 
192
  with gr.Column():
193
  raw_ocr = gr.Textbox(
 
206
  label="Extracted Information (JSON)"
207
  )
208
 
 
 
 
 
 
 
 
 
 
 
209
  submit_btn.click(
210
  fn=extract_dl_info,
211
+ inputs=[image_input, model_choice],
212
+ outputs=[raw_ocr, cleaned_text, json_result]
213
  )
214
 
215
  if __name__ == "__main__":