|
import os |
|
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' |
|
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' |
|
|
|
import gradio as gr |
|
import base64 |
|
import requests |
|
import json |
|
import re |
|
import uuid |
|
from datetime import datetime |
|
import time |
|
import shutil |
|
import tempfile |
|
|
|
|
|
try: |
|
import fitz |
|
PYMUPDF_AVAILABLE = True |
|
except ImportError: |
|
PYMUPDF_AVAILABLE = False |
|
print("Warning: PyMuPDF not found. PDF processing will be disabled.") |
|
|
|
try: |
|
import docx |
|
from PIL import Image, ImageDraw, ImageFont |
|
DOCX_AVAILABLE = True |
|
except ImportError: |
|
DOCX_AVAILABLE = False |
|
print("Warning: python-docx or Pillow not found. DOCX processing will be disabled.") |
|
|
|
|
|
|
|
try: |
|
from deepface import DeepFace |
|
DEEPFACE_AVAILABLE = True |
|
except ImportError: |
|
DEEPFACE_AVAILABLE = False |
|
print("Warning: deepface library not found. Facial recognition features will be disabled.") |
|
class DeepFaceMock: |
|
def represent(self, *args, **kwargs): return [] |
|
def verify(self, *args, **kwargs): return {'verified': False} |
|
DeepFace = DeepFaceMock() |
|
|
|
|
|
|
|
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY") |
|
IMAGE_MODEL = "opengvlab/internvl3-14b:free" |
|
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions" |
|
FACE_DETECTOR_BACKEND = 'retinaface' |
|
FACE_RECOGNITION_MODEL_NAME = 'VGG-Face' |
|
|
|
|
|
processed_files_data = [] |
|
person_profiles = {} |
|
|
|
|
|
|
|
def render_text_to_image(text, output_path): |
|
"""Renders a string of text onto a new image file.""" |
|
if not DOCX_AVAILABLE: |
|
raise ImportError("Pillow or python-docx is not installed.") |
|
try: |
|
font = ImageFont.truetype("DejaVuSans.ttf", 15) |
|
except IOError: |
|
print("Default font not found, using basic PIL font.") |
|
font = ImageFont.load_default() |
|
padding = 20 |
|
image_width = 800 |
|
lines = [] |
|
for paragraph in text.split('\n'): |
|
words = paragraph.split() |
|
line = "" |
|
for word in words: |
|
if hasattr(font, 'getbbox'): |
|
box = font.getbbox(line + word) |
|
line_width = box[2] - box[0] |
|
else: |
|
line_width = font.getsize(line + word)[0] |
|
if line_width <= image_width - 2 * padding: |
|
line += word + " " |
|
else: |
|
lines.append(line.strip()) |
|
line = word + " " |
|
lines.append(line.strip()) |
|
_, top, _, bottom = font.getbbox("A") |
|
line_height = bottom - top + 5 |
|
image_height = len(lines) * line_height + 2 * padding |
|
img = Image.new('RGB', (image_width, int(image_height)), color='white') |
|
draw = ImageDraw.Draw(img) |
|
y = padding |
|
for line in lines: |
|
draw.text((padding, y), line, font=font, fill='black') |
|
y += line_height |
|
img.save(output_path, format='PNG') |
|
|
|
|
|
def convert_file_to_images(original_filepath, temp_output_dir): |
|
filename_lower = original_filepath.lower() |
|
output_paths = [] |
|
if filename_lower.endswith('.pdf'): |
|
if not PYMUPDF_AVAILABLE: raise RuntimeError("PDF processing is disabled (PyMuPDF not installed).") |
|
doc = fitz.open(original_filepath) |
|
for i, page in enumerate(doc): |
|
pix = page.get_pixmap(dpi=200) |
|
output_filepath = os.path.join(temp_output_dir, f"{os.path.basename(original_filepath)}_page_{i+1}.png") |
|
pix.save(output_filepath) |
|
output_paths.append({"path": output_filepath, "page": i + 1}) |
|
doc.close() |
|
elif filename_lower.endswith('.docx'): |
|
if not DOCX_AVAILABLE: raise RuntimeError("DOCX processing is disabled (python-docx or Pillow not installed).") |
|
doc = docx.Document(original_filepath) |
|
full_text = "\n".join([para.text for para in doc.paragraphs]) |
|
if not full_text.strip(): full_text = "--- Document is empty or contains only non-text elements ---" |
|
output_filepath = os.path.join(temp_output_dir, f"{os.path.basename(original_filepath)}.png") |
|
render_text_to_image(full_text, output_filepath) |
|
output_paths.append({"path": output_filepath, "page": 1}) |
|
elif filename_lower.endswith(('.png', '.jpg', '.jpeg', '.webp', '.bmp', '.tiff')): |
|
output_paths.append({"path": original_filepath, "page": 1}) |
|
else: |
|
raise TypeError(f"Unsupported file type: {os.path.basename(original_filepath)}") |
|
return output_paths |
|
|
|
|
|
|
|
def extract_json_from_text(text): |
|
if not text: return {"error": "Empty text provided for JSON extraction."} |
|
match_block = re.search(r"```json\s*(\{.*?\})\s*```", text, re.DOTALL | re.IGNORECASE) |
|
if match_block: json_str = match_block.group(1) |
|
else: |
|
text_stripped = text.strip() |
|
if text_stripped.startswith("`") and text_stripped.endswith("`"): json_str = text_stripped[1:-1] |
|
else: json_str = text_stripped |
|
try: return json.loads(json_str) |
|
except json.JSONDecodeError as e: |
|
try: |
|
first_brace, last_brace = json_str.find('{'), json_str.rfind('}') |
|
if -1 < first_brace < last_brace: return json.loads(json_str[first_brace : last_brace+1]) |
|
else: return {"error": f"Invalid JSON (no outer braces): {e}", "original_text": text} |
|
except json.JSONDecodeError as e2: return {"error": f"Invalid JSON (substring failed): {e2}", "original_text": text} |
|
|
|
def get_ocr_prompt(): |
|
return """You are an advanced OCR and information extraction AI...""" |
|
|
|
def call_openrouter_ocr(image_filepath): |
|
|
|
if not OPENROUTER_API_KEY: return {"error": "OpenRouter API Key not configured."} |
|
try: |
|
with open(image_filepath, "rb") as f: encoded_image = base64.b64encode(f.read()).decode("utf-8") |
|
mime_type = "image/jpeg" |
|
if image_filepath.lower().endswith(".png"): mime_type = "image/png" |
|
elif image_filepath.lower().endswith(".webp"): mime_type = "image/webp" |
|
data_url = f"data:{mime_type};base64,{encoded_image}" |
|
payload = {"model": IMAGE_MODEL, "messages": [{"role": "user", "content": [{"type": "text", "text": get_ocr_prompt()}, {"type": "image_url", "image_url": {"url": data_url}}]}], "max_tokens": 3500, "temperature": 0.1} |
|
headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}", "Content-Type": "application/json", "HTTP-Referer": os.environ.get("GRADIO_ROOT_PATH", "http://localhost:7860"),"X-Title": "Gradio Document Processor"} |
|
response = requests.post(OPENROUTER_API_URL, headers=headers, json=payload, timeout=180) |
|
response.raise_for_status() |
|
result = response.json() |
|
if "choices" in result and result["choices"]: return extract_json_from_text(result["choices"][0]["message"]["content"]) |
|
else: return {"error": "No 'choices' in API response from OpenRouter.", "details": result} |
|
except requests.exceptions.Timeout: return {"error": "API request timed out."} |
|
except requests.exceptions.RequestException as e: |
|
error_message = f"API Request Error: {e}" |
|
if hasattr(e, 'response') and e.response is not None: error_message += f" Status: {e.response.status_code}, Response: {e.response.text}" |
|
return {"error": error_message} |
|
except Exception as e: return {"error": f"An unexpected error during OCR: {e}"} |
|
|
|
def get_facial_embeddings_with_deepface(image_filepath): |
|
|
|
if not DEEPFACE_AVAILABLE: return {"error": "DeepFace library not installed.", "embeddings": []} |
|
try: |
|
embedding_objs = DeepFace.represent(img_path=image_filepath, model_name=FACE_RECOGNITION_MODEL_NAME, detector_backend=FACE_DETECTOR_BACKEND, enforce_detection=False, align=True) |
|
embeddings = [obj['embedding'] for obj in embedding_objs if 'embedding' in obj] |
|
if not embeddings: return {"message": "No face detected or embedding failed.", "embeddings": []} |
|
return {"embeddings": embeddings, "count": len(embeddings)} |
|
except Exception as e: |
|
if "could not find any face" in str(e).lower() or "No face detected" in str(e): return {"message": "No face detected.", "embeddings": []} |
|
print(f"DeepFace represent error: {e}") |
|
return {"error": f"Facial embedding extraction failed: {type(e).__name__}", "embeddings": []} |
|
|
|
def extract_entities_from_ocr(ocr_json): |
|
|
|
if not ocr_json or not isinstance(ocr_json, dict) or "extracted_fields" not in ocr_json or not isinstance(ocr_json.get("extracted_fields"), dict): |
|
doc_type = ocr_json.get("document_type_detected", "Unknown (OCR err)") if isinstance(ocr_json, dict) else "Unknown" |
|
return {"name": None, "dob": None, "main_id": None, "doc_type": doc_type, "all_names_roles": []} |
|
fields = ocr_json["extracted_fields"] |
|
doc_type = ocr_json.get("document_type_detected", "Unknown") |
|
name_keys = ["primary person name", "full name", "name", "account holder name", "guest name", "cardholder name", "policy holder name", "applicant name", "beneficiary name", "student name", "employee name", "sender name", "receiver name", "patient name", "traveler name", "customer name", "member name", "user name", "mother's name", "father's name", "spouse's name"] |
|
dob_keys = ["date of birth", "dob"] |
|
id_keys = ["passport number", "document number", "id number", "personal no", "member id", "customer id", "account number", "reservation number", "booking reference"] |
|
extracted_name, all_names_roles, extracted_dob, extracted_main_id = None, [], None, None |
|
|
|
for key_pattern in name_keys: |
|
for actual_field_key, value in fields.items(): |
|
if key_pattern == actual_field_key.lower() and value and isinstance(value, str) and value.strip(): |
|
if not extracted_name: extracted_name = value.strip() |
|
all_names_roles.append({"name_text": value.strip(), "source_key": actual_field_key}) |
|
|
|
return {"name": extracted_name, "dob": extracted_dob, "main_id": extracted_main_id, "doc_type": doc_type, "all_names_roles": all_names_roles} |
|
|
|
def normalize_name(name): |
|
if not name: return "" |
|
return "".join(filter(str.isalnum, name)).lower() |
|
|
|
def are_faces_similar(emb1_list, emb2_gallery_list): |
|
if not DEEPFACE_AVAILABLE or not emb1_list or not emb2_gallery_list: return False |
|
for emb1 in emb1_list: |
|
for emb2 in emb2_gallery_list: |
|
try: |
|
result = DeepFace.verify(img1_path=emb1, img2_path=emb2, model_name=FACE_RECOGNITION_MODEL_NAME, enforce_detection=False) |
|
if result.get("verified", False): return True |
|
except Exception as e: print(f"DeepFace verify error: {e}") |
|
return False |
|
|
|
def get_person_id_and_update_profiles(doc_id, entities, facial_embeddings, current_persons_data, linking_method_log): |
|
|
|
main_id = entities.get("main_id") |
|
name = entities.get("name") |
|
dob = entities.get("dob") |
|
if main_id: |
|
|
|
return "person_id_..." |
|
if facial_embeddings: |
|
|
|
return "person_key..." |
|
|
|
return "unidentified_..." |
|
|
|
def format_dataframe_data(current_files_data): |
|
df_rows = [] |
|
|
|
for f_data in current_files_data: |
|
|
|
df_rows.append([...]) |
|
return df_rows |
|
|
|
def format_persons_markdown(current_persons_data, current_files_data): |
|
if not current_persons_data: return "No persons identified yet." |
|
|
|
return "..." |
|
|
|
|
|
def process_uploaded_files(files_list, progress=gr.Progress(track_tqdm=True)): |
|
global processed_files_data, person_profiles |
|
processed_files_data, person_profiles = [], {} |
|
temp_dir = tempfile.mkdtemp() |
|
if not OPENROUTER_API_KEY or not files_list: |
|
|
|
shutil.rmtree(temp_dir) |
|
return |
|
|
|
job_queue = [] |
|
for original_file_obj in progress.tqdm(files_list, desc="Pre-processing Files"): |
|
try: |
|
image_page_list = convert_file_to_images(original_file_obj.name, temp_dir) |
|
total_pages = len(image_page_list) |
|
for item in image_page_list: |
|
job_queue.append({"original_filename": os.path.basename(original_file_obj.name), "page_number": item["page"], "total_pages": total_pages, "image_path": item["path"]}) |
|
except Exception as e: |
|
job_queue.append({"original_filename": os.path.basename(original_file_obj.name), "error": str(e)}) |
|
|
|
|
|
for job in job_queue: |
|
if "error" in job: |
|
processed_files_data.append({"doc_id": str(uuid.uuid4()), "original_filename": job["original_filename"], "page_number": 1, "status": f"Error: {job['error']}"}) |
|
else: |
|
processed_files_data.append({"doc_id": str(uuid.uuid4()), "original_filename": job["original_filename"], "page_number": job["page_number"], "total_pages": job["total_pages"], "filepath": job["image_path"], "status": "Queued", "ocr_json": None, "entities": None, "face_analysis_result": None, "facial_embeddings": None, "assigned_person_key": None, "linking_method": ""}) |
|
|
|
|
|
|
|
|
|
shutil.rmtree(temp_dir) |
|
|
|
|
|
yield (final_df_data, final_persons_md, "{}", f"All {len(processed_files_data)} pages analyzed.") |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
gr.Markdown("# π Intelligent Document Processor & Classifier v3 (PDF/DOCX Support)") |
|
gr.Markdown("Upload multiple documents (PDFs, DOCX, and images).") |
|
|
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
files_input = gr.Files(label="Upload Documents (Bulk)", file_count="multiple") |
|
process_button = gr.Button("π Process Uploaded Documents", variant="primary") |
|
with gr.Column(scale=2): |
|
overall_status_textbox = gr.Textbox(label="Current Task & Overall Progress", interactive=False, lines=2) |
|
|
|
gr.Markdown("---") |
|
gr.Markdown("## Document & Page Processing Details") |
|
dataframe_headers = ["Original File", "Page", "Status", "Type", "Face?", "Name", "DOB", "Main ID", "Person Key", "Linking Method"] |
|
document_status_df = gr.Dataframe( |
|
headers=dataframe_headers, |
|
datatype=["str"] * len(dataframe_headers), |
|
label="Individual Page Status & Extracted Entities", |
|
row_count=(1, "dynamic"), |
|
col_count=(len(dataframe_headers), "fixed"), |
|
wrap=True |
|
|
|
) |
|
|
|
with gr.Accordion("Selected Page Full OCR JSON", open=False): |
|
ocr_json_output = gr.Code(label="OCR JSON", language="json", interactive=False) |
|
|
|
gr.Markdown("---") |
|
person_classification_output_md = gr.Markdown("## Classified Persons & Documents\nNo persons identified yet.") |
|
|
|
process_button.click( |
|
fn=process_uploaded_files, inputs=[files_input], |
|
outputs=[document_status_df, person_classification_output_md, ocr_json_output, overall_status_textbox] |
|
) |
|
|
|
@document_status_df.select(show_progress="hidden") |
|
def display_selected_ocr(evt: gr.SelectData): |
|
if evt.index is None or evt.index[0] is None: return "{}" |
|
selected_row_index = evt.index[0] |
|
if 0 <= selected_row_index < len(processed_files_data): |
|
selected_doc_data = processed_files_data[selected_row_index] |
|
if selected_doc_data and selected_doc_data.get("ocr_json"): |
|
return json.dumps(selected_doc_data["ocr_json"], indent=2, ensure_ascii=False) |
|
return json.dumps({"message": "No OCR data or selection out of bounds."}, indent=2) |
|
document_status_df.select(display_selected_ocr, inputs=None, outputs=ocr_json_output) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.queue().launch(debug=True, share=os.environ.get("GRADIO_SHARE", "true").lower() == "true") |