anoopreddyyeddula commited on
Commit
df33ba6
·
2 Parent(s): 75d7defdb14a92

fix: resolve merge conflicts and update UI

Browse files
Files changed (1) hide show
  1. app.py +67 -197
app.py CHANGED
@@ -5,23 +5,31 @@ from transformers import pipeline
5
  import gradio as gr
6
  import pdf2image
7
  import PyPDF2
8
- import pandas as pd
9
  import io
10
- from datetime import datetime
 
11
  import cv2
 
 
 
 
 
12
 
13
  # Initialize the OCR reader
14
  reader = easyocr.Reader(['en'])
15
 
16
- # Use text classification model
17
  text_classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
18
 
19
- # Use document classification model - using a compatible model
20
  doc_classifier = pipeline("image-classification", model="microsoft/resnet-50")
21
 
22
  def convert_pdf_to_images(pdf_file):
23
- """Convert PDF to list of images"""
24
  try:
 
 
 
25
  # Save PDF content to a temporary file
26
  pdf_content = pdf_file.read()
27
  pdf_buffer = io.BytesIO(pdf_content)
@@ -38,218 +46,80 @@ def convert_pdf_to_images(pdf_file):
38
  pdf_buffer.seek(0)
39
 
40
  # Convert to images
41
- images = pdf2image.convert_from_bytes(
42
- pdf_buffer.read(),
43
- dpi=300, # Increase DPI for better quality
44
- fmt='PNG'
45
- )
46
 
47
  if not images:
48
  raise ValueError("No images extracted from PDF")
49
-
 
 
50
  return images
51
  except Exception as e:
52
- print(f"PDF conversion error: {str(e)}") # Debug logging
53
  return None
54
 
55
  def process_single_image(image):
56
- """Process a single image and return results"""
57
- extracted_text = ocr_function(image)
58
-
59
- validation_result = text_classifier(extracted_text[:512])
60
- sentiment = validation_result[0]['label']
61
- confidence = validation_result[0]['score']
62
-
63
- doc_result = doc_classifier(image)
64
- doc_type = doc_result[0]['label']
65
- doc_confidence = doc_result[0]['score']
66
-
67
- return {
68
- 'text': extracted_text,
69
- 'validation': sentiment,
70
- 'validation_confidence': confidence,
71
- 'doc_type': doc_type,
72
- 'doc_confidence': doc_confidence
73
- }
74
-
75
- def generate_report(results):
76
- """Generate a formatted report from results"""
77
- df = pd.DataFrame(results)
78
-
79
- # Create Excel buffer
80
- excel_buffer = io.BytesIO()
81
- with pd.ExcelWriter(excel_buffer) as writer:
82
- df.to_excel(writer, index=False)
83
- excel_buffer.seek(0)
84
-
85
- # Create summary text
86
- summary = f"""
87
- Insurance Claim Processing Report
88
- Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
89
-
90
- Total Pages Processed: {len(results)}
91
- Document Types Found: {', '.join(set(df['doc_type']))}
92
- """
93
-
94
- return summary, excel_buffer
95
-
96
- def ocr_function(image):
97
  try:
98
- # Convert PIL image to numpy array
99
- if isinstance(image, Image.Image):
100
- image = np.array(image)
101
-
102
- # Image preprocessing
103
- if len(image.shape) == 2: # Convert grayscale to RGB
104
- image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
105
- elif len(image.shape) == 3 and image.shape[2] == 4: # Convert RGBA to RGB
106
- image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
107
-
108
- # Perform OCR
109
- results = reader.readtext(image)
110
-
111
- # Extract text
112
- text = ' '.join([result[1] for result in results])
113
-
114
- if not text.strip():
115
- return "No text was detected in the image"
116
-
117
- return text.strip()
118
- except Exception as e:
119
- print(f"OCR Error: {str(e)}") # Debug logging
120
- return f"OCR Error: {str(e)}"
121
 
122
- def validate_text(text):
123
- try:
124
- result = text_classifier(text[:512]) # Limit text length to avoid token limit
125
- return result[0]['label']
126
- except Exception as e:
127
- return f"Validation Error: {str(e)}"
128
 
129
- def classify_document(image):
130
- try:
131
- # Convert PIL image to RGB if needed
132
- if image.mode != 'RGB':
133
- image = image.convert('RGB')
134
- result = doc_classifier(image)
135
- return f"Document Type: {result[0]['label']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  except Exception as e:
137
- return f"Classification Error: {str(e)}"
 
138
 
139
- def validate_image(image):
140
- """Validate image before processing"""
 
141
  try:
142
- if image is None:
143
- return False, "No image provided"
144
-
145
- if isinstance(image, np.ndarray):
146
- image = Image.fromarray(image)
147
-
148
- if not isinstance(image, Image.Image):
149
- return False, "Invalid image format"
150
-
151
- # Check image size
152
- if image.size[0] * image.size[1] > 5000 * 5000:
153
- return False, "Image too large. Maximum size: 5000x5000"
154
-
155
- return True, image
156
- except Exception as e:
157
- return False, f"Image validation error: {str(e)}"
158
 
159
- def validate_file_type(file):
160
- """Validate file type and size"""
161
- try:
162
- if not hasattr(file, 'name'):
163
- return False, "Invalid file object"
164
-
165
- # Get file extension
166
- file_ext = file.name.lower().split('.')[-1]
167
-
168
- # Check allowed extensions
169
- allowed_extensions = {'pdf', 'png', 'jpg', 'jpeg', 'tiff'}
170
- if file_ext not in allowed_extensions:
171
- return False, f"Unsupported file type. Allowed types: {', '.join(allowed_extensions)}"
172
-
173
- # Check file size (max 10MB)
174
- MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB in bytes
175
- file.seek(0, 2) # Seek to end of file
176
- file_size = file.tell()
177
- file.seek(0) # Reset file pointer
178
-
179
- if file_size > MAX_FILE_SIZE:
180
- return False, "File too large. Maximum size: 10MB"
181
-
182
- return True, None
183
- except Exception as e:
184
- return False, f"File validation error: {str(e)}"
185
 
186
- def process_claim(file):
187
- try:
188
- if file is None:
189
- return "No file provided", "N/A", "N/A", None
190
-
191
- # Validate file type and size
192
- is_valid, error_message = validate_file_type(file)
193
- if not is_valid:
194
- return error_message, "Error", "Error", None
195
-
196
- print(f"Processing file: {file.name}")
197
- print(f"File type: {type(file)}")
198
-
199
- # Process PDF
200
- if file.name.lower().endswith('.pdf'):
201
- images = convert_pdf_to_images(file)
202
- if not images:
203
- return "Failed to convert PDF to images. Please check if the PDF is valid.", "Error", "Error", None
204
- else:
205
- # Process image
206
- try:
207
- img = Image.open(file)
208
- images = [img]
209
- except Exception as e:
210
- return f"Image processing error: {str(e)}", "Error", "Error", None
211
-
212
- # Process each page/image
213
- results = []
214
- for idx, img in enumerate(images):
215
- try:
216
- # Validate image
217
- valid, validated_img = validate_image(img)
218
- if not valid:
219
- return f"Invalid image on page {idx + 1}: {validated_img}", "Error", "Error", None
220
-
221
- # Process image
222
- result = process_single_image(validated_img)
223
- result['page'] = idx + 1
224
- results.append(result)
225
-
226
- except Exception as e:
227
- return f"Error processing page {idx + 1}: {str(e)}", "Error", "Error", None
228
-
229
- if not results:
230
- return "No valid results obtained from processing", "Error", "Error", None
231
-
232
- # Generate report
233
- try:
234
- summary, excel_file = generate_report(results)
235
- except Exception as e:
236
- return f"Error generating report: {str(e)}", "Error", "Error", None
237
-
238
- # Return results
239
- return (
240
- "\n\n=== Page Break ===\n\n".join([r['text'] for r in results]),
241
- "\n".join([f"Page {r['page']}: {r['validation']} ({r['validation_confidence']:.2%})" for r in results]),
242
- "\n".join([f"Page {r['page']}: {r['doc_type']} ({r['doc_confidence']:.2%})" for r in results]),
243
- excel_file
244
- )
245
-
246
  except Exception as e:
247
- print(f"Error in process_claim: {str(e)}")
248
- return f"Processing error: {str(e)}", "Error", "Error", None
249
 
250
  # Create the Gradio interface with improved UI and error handling
251
  iface = gr.Interface(
252
- fn=process_claim,
253
  inputs=[
254
  gr.File(
255
  label="Upload Insurance Document",
 
5
  import gradio as gr
6
  import pdf2image
7
  import PyPDF2
 
8
  import io
9
+ import pandas as pd
10
+ import logging
11
  import cv2
12
+ from datetime import datetime
13
+ import time
14
+
15
+ # Set up logging for error handling
16
+ logging.basicConfig(level=logging.DEBUG)
17
 
18
  # Initialize the OCR reader
19
  reader = easyocr.Reader(['en'])
20
 
21
+ # Use text classification model (distilbert for sentiment analysis or text validation)
22
  text_classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
23
 
24
+ # Use document classification model (ResNet50 as an example)
25
  doc_classifier = pipeline("image-classification", model="microsoft/resnet-50")
26
 
27
  def convert_pdf_to_images(pdf_file):
28
+ """Convert PDF to list of images with detailed logging"""
29
  try:
30
+ logging.debug("Starting PDF to image conversion...")
31
+ start_time = time.time()
32
+
33
  # Save PDF content to a temporary file
34
  pdf_content = pdf_file.read()
35
  pdf_buffer = io.BytesIO(pdf_content)
 
46
  pdf_buffer.seek(0)
47
 
48
  # Convert to images
49
+ images = pdf2image.convert_from_bytes(pdf_buffer.read(), dpi=300, fmt='PNG')
 
 
 
 
50
 
51
  if not images:
52
  raise ValueError("No images extracted from PDF")
53
+
54
+ # Log how long the conversion took
55
+ logging.debug(f"PDF to images conversion completed in {time.time() - start_time} seconds.")
56
  return images
57
  except Exception as e:
58
+ logging.error(f"PDF conversion error: {str(e)}")
59
  return None
60
 
61
  def process_single_image(image):
62
+ """Process a single image with detailed logging"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  try:
64
+ logging.debug("Starting image processing...")
65
+ start_time = time.time()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Ensure 'img' is properly assigned, handling both PDFs and images
68
+ if isinstance(image, Image.Image): # if it's a PIL Image
69
+ img = image
70
+ else:
71
+ raise ValueError("Uploaded file is not a valid image.")
 
72
 
73
+ # OCR processing and classification
74
+ extracted_text = reader.readtext(np.array(img))
75
+ extracted_text = ' '.join([text[1] for text in extracted_text])
76
+
77
+ validation_result = text_classifier(extracted_text[:512])
78
+ sentiment = validation_result[0]['label']
79
+ confidence = validation_result[0]['score']
80
+
81
+ doc_result = doc_classifier(img)
82
+ doc_type = doc_result[0]['label']
83
+ doc_confidence = doc_result[0]['score']
84
+
85
+ # Log how long processing took
86
+ logging.debug(f"Image processing completed in {time.time() - start_time} seconds.")
87
+
88
+ return {
89
+ 'text': extracted_text,
90
+ 'validation': sentiment,
91
+ 'validation_confidence': confidence,
92
+ 'doc_type': doc_type,
93
+ 'doc_confidence': doc_confidence
94
+ }
95
  except Exception as e:
96
+ logging.error(f"Error processing the image: {str(e)}")
97
+ return {'error': f"Error processing the image: {str(e)}"}
98
 
99
+ # Gradio interface setup
100
+ def gradio_interface(input_file):
101
+ """Handle both PDF and image files uploaded by the user"""
102
  try:
103
+ if input_file.name.lower().endswith('.pdf'):
104
+ images = convert_pdf_to_images(input_file)
105
+ if images is None:
106
+ return {'error': 'Invalid PDF or unable to extract images'}
107
+ result = process_single_image(images[0])
108
+ elif input_file.name.lower().endswith(('png', 'jpg', 'jpeg')):
109
+ img = Image.open(input_file)
110
+ result = process_single_image(img)
111
+ else:
112
+ return {'error': 'Unsupported file type. Please upload a valid image or PDF.'}
 
 
 
 
 
 
113
 
114
+ return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  except Exception as e:
117
+ logging.error(f"Error in file processing: {str(e)}")
118
+ return {'error': f"Error processing the file: {str(e)}"}
119
 
120
  # Create the Gradio interface with improved UI and error handling
121
  iface = gr.Interface(
122
+ fn=gradio_interface,
123
  inputs=[
124
  gr.File(
125
  label="Upload Insurance Document",