File size: 2,971 Bytes
cbbdd68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
    }