Nassira commited on
Commit
cbbdd68
·
verified ·
1 Parent(s): a914b7f

Create personal_information.py

Browse files
Files changed (1) hide show
  1. personal_information.py +123 -0
personal_information.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from cv_analyzer import analyze_cv
11
+ from spelling_grammar_checker import evaluate_cv_text
12
+
13
+ def load_model():
14
+ model_path = hf_hub_download("TheBloke/Mistral-7B-Instruct-v0.2-GGUF", filename="mistral-7b-instruct-v0.2.Q4_K_M.gguf")
15
+ return Llama(model_path=model_path, n_ctx=32768, n_gpu_layers=2)
16
+
17
+ llm = load_model()
18
+
19
+ with open('personal_info_scores.json', 'r') as f:
20
+ personal_info_scores = json.load(f)
21
+
22
+ def is_valid_email(email):
23
+ parts = email.split('@')
24
+ return len(parts) == 2 and parts[1].lower() == 'gmail.com'
25
+
26
+ def clean_phone_number(phone):
27
+ cleaned = ''.join(char for char in phone if char.isdigit() or char == '+')
28
+ if cleaned.startswith('00'):
29
+ cleaned = '+' + cleaned[2:]
30
+ if not cleaned.startswith('+'):
31
+ cleaned = '+' + cleaned
32
+ return cleaned if 7 <= len(cleaned) <= 15 else None
33
+
34
+ def extract_email_and_phone(text):
35
+ gmail_regex = r'\b[A-Za-z0-9._%+-]+@gmail\.com\b'
36
+ phone_regex = r'''(?x)
37
+ (?:
38
+ (?:(?:\+|00)(?:\d{1,3})[\s.-]?)?
39
+ (?:
40
+ (?:\(?\d{1,4}\)?[\s.-]?){1,3}
41
+ \d{3,4}[\s.-]?\d{3,4}
42
+ )
43
+ |
44
+ (?:\d{3,4}[\s.-]?){2,3}\d{3,4}
45
+ )
46
+ '''
47
+ email_matches = re.findall(gmail_regex, text, re.IGNORECASE)
48
+ phone_matches = re.findall(phone_regex, text)
49
+
50
+ valid_emails = [e for e in email_matches if is_valid_email(e)]
51
+ valid_phones = [clean_phone_number(p) for p in phone_matches if clean_phone_number(p)]
52
+
53
+ email = valid_emails[0] if valid_emails else None
54
+ phone = valid_phones[0] if valid_phones else None
55
+
56
+ return email, phone
57
+
58
+ def extract_location(text):
59
+ location_prompt = get_location_prompt(text)
60
+ output = llm(location_prompt, max_tokens=100)
61
+ generated_text = output['choices'][0]['text'].strip()
62
+
63
+ if "Not found" in generated_text:
64
+ return None, None
65
+
66
+ city_match = re.search(r'City:\s*(.+?),', generated_text)
67
+ country_match = re.search(r'Country:\s*(.+)', generated_text)
68
+
69
+ city = city_match.group(1) if city_match else None
70
+ country = country_match.group(1) if country_match else None
71
+
72
+ return city, country
73
+
74
+ def analyze_personal_info(file_path):
75
+ text = process_file(file_path, 'ocr_weights.json')
76
+
77
+ city, country = extract_location(text)
78
+ email, phone = extract_email_and_phone(text)
79
+
80
+ score = 0
81
+ if email:
82
+ score += personal_info_scores['email']
83
+ if phone:
84
+ score += personal_info_scores['phone']
85
+ if city:
86
+ score += personal_info_scores['city']
87
+ if country:
88
+ score += personal_info_scores['country']
89
+
90
+ return {
91
+ "email": email,
92
+ "phone": phone,
93
+ "city": city,
94
+ "country": country,
95
+ "score_personal_information": score
96
+ }
97
+
98
+ def main():
99
+ st.title('Analyse de CV')
100
+ uploaded_file = st.file_uploader("Uploader un fichier CV (PDF ou Image)", type=["pdf", "png", "jpg", "jpeg"])
101
+
102
+ if uploaded_file is not None:
103
+ with st.spinner('Analyse du CV en cours...'):
104
+ with tempfile.NamedTemporaryFile(delete=False, suffix=f".{uploaded_file.name.split('.')[-1]}") as temp_file:
105
+ temp_file.write(uploaded_file.getvalue())
106
+ temp_file_path = temp_file.name
107
+
108
+ cv_analysis = analyze_cv(temp_file_path)
109
+ personal_info = analyze_personal_info(temp_file_path)
110
+ score_erreurs = evaluate_cv_text(temp_file_path, 'ocr_weights.json')
111
+
112
+ st.header('Analyse des sections du CV')
113
+ st.write(f"Sections présentes: {', '.join(cv_analysis['present_sections'])}")
114
+ st.write(f"Score des sections: {cv_analysis['score_sections']}")
115
+
116
+ st.header('Informations personnelles extraites')
117
+ st.write(f"Email: {personal_info['email'] if personal_info['email'] else 'Non trouvé'}")
118
+ st.write(f"Téléphone: {personal_info['phone'] if personal_info['phone'] else 'Non trouvé'}")
119
+ st.write(f"Ville: {personal_info['city'] if personal_info['city'] else 'Non trouvée'}")
120
+ st.write(f"Pays: {personal_info['country'] if personal_info['country'] else 'Non trouvé'}")
121
+ st.write(f"Score des informations personnelles: {personal_info['score_personal_information']}")
122
+
123
+ st.header('Évaluation orthographique et