Create personal_information.py
Browse files- personal_information.py +102 -0
personal_information.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import json
|
| 4 |
+
import tempfile
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from llama_cpp import Llama
|
| 8 |
+
from cv_prompt import get_location_prompt
|
| 9 |
+
from ocr_extractor import process_file
|
| 10 |
+
|
| 11 |
+
# Load the model
|
| 12 |
+
model_path = hf_hub_download("TheBloke/Mistral-7B-Instruct-v0.2-GGUF", filename="mistral-7b-instruct-v0.2.Q4_K_M.gguf")
|
| 13 |
+
|
| 14 |
+
# Initialize the Mistral model
|
| 15 |
+
llm = Llama(
|
| 16 |
+
model_path=model_path,
|
| 17 |
+
n_ctx=32768,
|
| 18 |
+
n_gpu_layers=2,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Load personal info scores
|
| 22 |
+
with open('personal_info_scores.json', 'r') as f:
|
| 23 |
+
personal_info_scores = json.load(f)
|
| 24 |
+
|
| 25 |
+
def is_valid_email(email):
|
| 26 |
+
parts = email.split('@')
|
| 27 |
+
return len(parts) == 2 and parts[1].lower() == 'gmail.com'
|
| 28 |
+
|
| 29 |
+
def clean_phone_number(phone):
|
| 30 |
+
cleaned = ''.join(char for char in phone if char.isdigit() or char == '+')
|
| 31 |
+
if cleaned.startswith('00'):
|
| 32 |
+
cleaned = '+' + cleaned[2:]
|
| 33 |
+
if not cleaned.startswith('+'):
|
| 34 |
+
cleaned = '+' + cleaned
|
| 35 |
+
return cleaned if 7 <= len(cleaned) <= 15 else None
|
| 36 |
+
|
| 37 |
+
def extract_email_and_phone(text):
|
| 38 |
+
gmail_regex = r'\b[A-Za-z0-9._%+-]+@gmail\.com\b'
|
| 39 |
+
phone_regex = r'''(?x)
|
| 40 |
+
(?:
|
| 41 |
+
(?:(?:\+|00)(?:\d{1,3})[\s.-]?)?
|
| 42 |
+
(?:
|
| 43 |
+
(?:\(?\d{1,4}\)?[\s.-]?){1,3}
|
| 44 |
+
\d{3,4}[\s.-]?\d{3,4}
|
| 45 |
+
)
|
| 46 |
+
|
|
| 47 |
+
(?:\d{3,4}[\s.-]?){2,3}\d{3,4}
|
| 48 |
+
)
|
| 49 |
+
'''
|
| 50 |
+
email_matches = re.findall(gmail_regex, text, re.IGNORECASE)
|
| 51 |
+
phone_matches = re.findall(phone_regex, text)
|
| 52 |
+
|
| 53 |
+
valid_emails = [e for e in email_matches if is_valid_email(e)]
|
| 54 |
+
valid_phones = [clean_phone_number(p) for p in phone_matches if clean_phone_number(p)]
|
| 55 |
+
|
| 56 |
+
email = valid_emails[0] if valid_emails else None
|
| 57 |
+
phone = valid_phones[0] if valid_phones else None
|
| 58 |
+
|
| 59 |
+
return email, phone
|
| 60 |
+
|
| 61 |
+
def extract_location(text):
|
| 62 |
+
location_prompt = get_location_prompt(text)
|
| 63 |
+
output = llm(location_prompt, max_tokens=100)
|
| 64 |
+
generated_text = output['choices'][0]['text'].strip()
|
| 65 |
+
|
| 66 |
+
if "Not found" in generated_text:
|
| 67 |
+
return None, None
|
| 68 |
+
|
| 69 |
+
city_match = re.search(r'City:\s*(.+?),', generated_text)
|
| 70 |
+
country_match = re.search(r'Country:\s*(.+)', generated_text)
|
| 71 |
+
|
| 72 |
+
city = city_match.group(1) if city_match else None
|
| 73 |
+
country = country_match.group(1) if country_match else None
|
| 74 |
+
|
| 75 |
+
return city, country
|
| 76 |
+
|
| 77 |
+
def analyze_personal_info(file_path):
|
| 78 |
+
text = process_file(file_path, 'ocr_weights.json')
|
| 79 |
+
|
| 80 |
+
city, country = extract_location(text)
|
| 81 |
+
email, phone = extract_email_and_phone(text)
|
| 82 |
+
|
| 83 |
+
score = 0
|
| 84 |
+
if email:
|
| 85 |
+
score += personal_info_scores['email']
|
| 86 |
+
if phone:
|
| 87 |
+
score += personal_info_scores['phone']
|
| 88 |
+
if city:
|
| 89 |
+
score += personal_info_scores['city']
|
| 90 |
+
if country:
|
| 91 |
+
score += personal_info_scores['country']
|
| 92 |
+
|
| 93 |
+
result = {
|
| 94 |
+
"email": email,
|
| 95 |
+
"phone": phone,
|
| 96 |
+
"city": city,
|
| 97 |
+
"country": country,
|
| 98 |
+
"score_personal_information": score
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
return json.dumps(result)
|
| 102 |
+
|