natalis3405 commited on
Commit
9dd47a2
·
1 Parent(s): 13f302e
Files changed (1) hide show
  1. app.py +5 -61
app.py CHANGED
@@ -4,11 +4,6 @@ from PIL import Image
4
  import pytesseract
5
  import re
6
  import json
7
- import os
8
-
9
- # Global variable to store the loaded model
10
- loaded_generator = None
11
- current_model = None
12
 
13
  def clean_ocr_text(raw_text):
14
  """Clean OCR text using regex - only for cleaning, not extraction"""
@@ -26,45 +21,12 @@ def clean_ocr_text(raw_text):
26
 
27
  return cleaned.strip()
28
 
29
- def load_model(model_choice):
30
- """Load the AI model only when needed"""
31
- global loaded_generator, current_model
32
-
33
- model_map = {
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")
41
-
42
- # Only load if different model is requested
43
- if current_model != selected_model:
44
- print(f"Loading model: {selected_model}")
45
- try:
46
- loaded_generator = pipeline("text2text-generation", model=selected_model)
47
- current_model = selected_model
48
- print(f"Model {selected_model} loaded successfully")
49
- except Exception as e:
50
- print(f"Error loading model {selected_model}: {e}")
51
- # Fallback to base model
52
- if selected_model != "google/flan-t5-base":
53
- print("Falling back to base model...")
54
- loaded_generator = pipeline("text2text-generation", model="google/flan-t5-base")
55
- current_model = "google/flan-t5-base"
56
- else:
57
- raise e
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)
@@ -86,13 +48,8 @@ def extract_dl_info(image, model_choice):
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:"
@@ -174,19 +131,6 @@ with gr.Blocks(title="Driver's License Information Extractor") as demo:
174
  type="pil",
175
  height=400
176
  )
177
-
178
- model_choice = gr.Radio(
179
- choices=[
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():
@@ -208,9 +152,9 @@ with gr.Blocks(title="Driver's License Information Extractor") as demo:
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__":
216
- demo.launch(server_name="127.0.0.1", server_port=7860, share=False)
 
4
  import pytesseract
5
  import re
6
  import json
 
 
 
 
 
7
 
8
  def clean_ocr_text(raw_text):
9
  """Clean OCR text using regex - only for cleaning, not extraction"""
 
21
 
22
  return cleaned.strip()
23
 
24
+ def extract_dl_info(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  """
26
  Extracts driver's license information from an image file using OCR and AI model processing.
27
 
28
  Args:
29
  image: PIL Image object from Gradio
 
30
 
31
  Returns:
32
  tuple: (raw_ocr_text, cleaned_ocr_text, json_result)
 
48
  # Use AI model to process cleaned OCR text and extract structured information
49
  print("Processing cleaned OCR text with AI model...")
50
 
51
+ # Load a text generation pipeline for structured extraction
52
+ generator = pipeline("text2text-generation", model="google/flan-t5-base")
 
 
 
 
 
53
 
54
  # Create specific prompts for the AI model to extract individual fields
55
  name_prompt = f"From this driver's license text, what is the person's full name? Text: {cleaned_text} Answer:"
 
131
  type="pil",
132
  height=400
133
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  submit_btn = gr.Button("Extract Information", variant="primary")
135
 
136
  with gr.Column():
 
152
 
153
  submit_btn.click(
154
  fn=extract_dl_info,
155
+ inputs=[image_input],
156
  outputs=[raw_ocr, cleaned_text, json_result]
157
  )
158
 
159
  if __name__ == "__main__":
160
+ demo.launch(share=True)