Download personal_information.py from Nassira/all_code: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/spaces/Nassira/all_code/resolve/main/personal_information.py
- Command line
-
hf download hf://spaces/Nassira/all_code/personal_information.py
-
curl -L -o personal_information.py https://huggingface.co/spaces/Nassira/all_code/resolve/main/personal_information.py
2.97 kB
| import os | |
| import re | |
| import json | |
| import tempfile | |
| import streamlit as st | |
| from huggingface_hub import hf_hub_download | |
| from llama_cpp import Llama | |
| from cv_prompt import get_location_prompt | |
| from ocr_extractor import process_file | |
| from cv_analyzer import analyze_cv | |
| from spelling_grammar_checker import evaluate_cv_text | |
| def load_model(): | |
| model_path = hf_hub_download("TheBloke/Mistral-7B-Instruct-v0.2-GGUF", filename="mistral-7b-instruct-v0.2.Q4_K_M.gguf") | |
| return Llama(model_path=model_path, n_ctx=32768, n_gpu_layers=2) | |
| llm = load_model() | |
| with open('personal_info_scores.json', 'r') as f: | |
| personal_info_scores = json.load(f) | |
| def is_valid_email(email): | |
| parts = email.split('@') | |
| return len(parts) == 2 and parts[1].lower() == 'gmail.com' | |
| def clean_phone_number(phone): | |
| cleaned = ''.join(char for char in phone if char.isdigit() or char == '+') | |
| if cleaned.startswith('00'): | |
| cleaned = '+' + cleaned[2:] | |
| if not cleaned.startswith('+'): | |
| cleaned = '+' + cleaned | |
| return cleaned if 7 <= len(cleaned) <= 15 else None | |
| def extract_email_and_phone(text): | |
| gmail_regex = r'\b[A-Za-z0-9._%+-]+@gmail\.com\b' | |
| phone_regex = r'''(?x) | |
| (?: | |
| (?:(?:\+|00)(?:\d{1,3})[\s.-]?)? | |
| (?: | |
| (?:\(?\d{1,4}\)?[\s.-]?){1,3} | |
| \d{3,4}[\s.-]?\d{3,4} | |
| ) | |
| | | |
| (?:\d{3,4}[\s.-]?){2,3}\d{3,4} | |
| ) | |
| ''' | |
| email_matches = re.findall(gmail_regex, text, re.IGNORECASE) | |
| phone_matches = re.findall(phone_regex, text) | |
| valid_emails = [e for e in email_matches if is_valid_email(e)] | |
| valid_phones = [clean_phone_number(p) for p in phone_matches if clean_phone_number(p)] | |
| email = valid_emails[0] if valid_emails else None | |
| phone = valid_phones[0] if valid_phones else None | |
| return email, phone | |
| def extract_location(text): | |
| location_prompt = get_location_prompt(text) | |
| output = llm(location_prompt, max_tokens=100) | |
| generated_text = output['choices'][0]['text'].strip() | |
| if "Not found" in generated_text: | |
| return None, None | |
| city_match = re.search(r'City:\s*(.+?),', generated_text) | |
| country_match = re.search(r'Country:\s*(.+)', generated_text) | |
| city = city_match.group(1) if city_match else None | |
| country = country_match.group(1) if country_match else None | |
| return city, country | |
| def analyze_personal_info(file_path): | |
| text = process_file(file_path, 'ocr_weights.json') | |
| city, country = extract_location(text) | |
| email, phone = extract_email_and_phone(text) | |
| score = 0 | |
| if email: | |
| score += personal_info_scores['email'] | |
| if phone: | |
| score += personal_info_scores['phone'] | |
| if city: | |
| score += personal_info_scores['city'] | |
| if country: | |
| score += personal_info_scores['country'] | |
| return { | |
| "email": email, | |
| "phone": phone, | |
| "city": city, | |
| "country": country, | |
| "score_personal_information": score | |
| } | |