Nassira commited on
Commit
96ef78f
·
verified ·
1 Parent(s): edf9070

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +22 -98
main.py CHANGED
@@ -1,102 +1,12 @@
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:
@@ -105,14 +15,14 @@ def main():
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é'}")
@@ -120,4 +30,18 @@ def main():
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import tempfile
3
+ import os
 
 
4
  from cv_analyzer import analyze_cv
5
+ from personal_information import analyze_personal_info
6
  from spelling_grammar_checker import evaluate_cv_text
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def main():
9
+ st.title('Analyse complète de CV')
10
  uploaded_file = st.file_uploader("Uploader un fichier CV (PDF ou Image)", type=["pdf", "png", "jpg", "jpeg"])
11
 
12
  if uploaded_file is not None:
 
15
  temp_file.write(uploaded_file.getvalue())
16
  temp_file_path = temp_file.name
17
 
18
+ # Analyse des sections du CV
19
  cv_analysis = analyze_cv(temp_file_path)
 
 
 
20
  st.header('Analyse des sections du CV')
21
  st.write(f"Sections présentes: {', '.join(cv_analysis['present_sections'])}")
22
  st.write(f"Score des sections: {cv_analysis['score_sections']}")
23
 
24
+ # Analyse des informations personnelles
25
+ personal_info = analyze_personal_info(temp_file_path)
26
  st.header('Informations personnelles extraites')
27
  st.write(f"Email: {personal_info['email'] if personal_info['email'] else 'Non trouvé'}")
28
  st.write(f"Téléphone: {personal_info['phone'] if personal_info['phone'] else 'Non trouvé'}")
 
30
  st.write(f"Pays: {personal_info['country'] if personal_info['country'] else 'Non trouvé'}")
31
  st.write(f"Score des informations personnelles: {personal_info['score_personal_information']}")
32
 
33
+ # Évaluation orthographique et grammaticale
34
+ score_erreurs = evaluate_cv_text(temp_file_path, 'ocr_weights.json')
35
+ st.header('Évaluation orthographique et grammaticale')
36
+ st.write(f"Score d'erreurs : {score_erreurs}")
37
+
38
+ # Calcul du score total
39
+ score_total = cv_analysis['score_sections'] + personal_info['score_personal_information'] + score_erreurs
40
+ st.header('Score Total')
41
+ st.write(f"Score total du CV : {score_total}")
42
+
43
+ # Nettoyage du fichier temporaire
44
+ os.unlink(temp_file_path)
45
+
46
+ if __name__ == "__main__":
47
+ main()