Danial7 commited on
Commit
4a02d42
Β·
verified Β·
1 Parent(s): ef61986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -40
app.py CHANGED
@@ -5,6 +5,10 @@ import os
5
  from sklearn.feature_extraction.text import TfidfVectorizer
6
  from sklearn.metrics.pairwise import cosine_similarity
7
  from keybert import KeyBERT
 
 
 
 
8
 
9
  st.set_page_config(page_title="Universal Smart CV Analyzer", layout="wide")
10
 
@@ -27,6 +31,7 @@ def load_data():
27
 
28
  certs, scholarships, edu_tech, edu_nontech, visa_data, skills_data = load_data()
29
 
 
30
  def extract_text_from_pdf(file):
31
  reader = PyPDF2.PdfReader(file)
32
  text = ""
@@ -34,11 +39,13 @@ def extract_text_from_pdf(file):
34
  text += page.extract_text()
35
  return text
36
 
 
37
  def extract_keywords(text, num_keywords=10):
38
  kw_model = KeyBERT()
39
  keywords = kw_model.extract_keywords(text, top_n=num_keywords, stop_words='english')
40
  return [kw[0].lower() for kw in keywords]
41
 
 
42
  def identify_field(keywords):
43
  fields = {
44
  "Engineering": ["engineer", "mechanical", "electrical", "civil", "plc", "automation"],
@@ -50,20 +57,18 @@ def identify_field(keywords):
50
  "Healthcare": ["nursing", "surgery", "hospital", "patient"],
51
  "Construction": ["carpentry", "plumbing", "hvac", "gardening", "mining"]
52
  }
53
- scores = {}
54
- for field, terms in fields.items():
55
- scores[field] = len(set(keywords).intersection(terms))
56
  return max(scores, key=scores.get)
57
 
 
58
  def is_technical_background(keywords):
59
  tech_terms = ["engineer", "machine learning", "python", "developer", "software", "automation", "plc", "ai"]
60
  non_tech_terms = ["marketing", "finance", "content", "seo", "branding", "accounting", "creative"]
61
-
62
  tech_score = len(set(keywords).intersection(tech_terms))
63
  non_tech_score = len(set(keywords).intersection(non_tech_terms))
64
-
65
  return "Technical" if tech_score >= non_tech_score else "Non-Technical"
66
 
 
67
  def calculate_cv_score(text, keywords):
68
  ideal = " ".join(keywords)
69
  tfidf = TfidfVectorizer()
@@ -71,9 +76,11 @@ def calculate_cv_score(text, keywords):
71
  score = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
72
  return round(score * 100)
73
 
 
74
  def filter_data_by_field(df, field_col, field):
75
  return df[df[field_col].str.lower() == field.lower()]
76
 
 
77
  def suggest_visa_opportunities(keywords, visa_data):
78
  matched_rows = []
79
  for _, row in visa_data.iterrows():
@@ -81,78 +88,132 @@ def suggest_visa_opportunities(keywords, visa_data):
81
  matched_rows.append(row)
82
  return pd.DataFrame(matched_rows)
83
 
 
84
  def suggest_upskilling(keywords, skills_data):
85
  all_skills = set(skills_data["Skill"].str.lower())
86
- current_skills = set(keywords)
87
  missing_skills = all_skills - current_skills
88
- return sorted(missing_skills)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  if uploaded_file:
91
  text = extract_text_from_pdf(uploaded_file)
92
  st.success("βœ… CV Text Extracted")
93
 
94
- # πŸ”‘ Keywords
95
  keywords = extract_keywords(text)
96
  st.subheader("πŸ”‘ Extracted Keywords")
97
  st.write(keywords)
98
 
99
- # 🎯 Field Identification
100
  field = identify_field(keywords)
101
  st.subheader("🎯 Identified Field / Domain")
102
  st.write(field)
103
 
104
- # 🧠 Technical or Non-Technical
105
  tech_class = is_technical_background(keywords)
106
  st.subheader("🧠 CV Background Type")
107
  st.write(tech_class)
108
 
109
- # πŸ“Š Skill Score
110
  score = calculate_cv_score(text, keywords)
111
  st.subheader("πŸ“Š CV Skill Score")
112
  st.metric(label="Score", value=f"{score}/100")
113
 
114
- st.markdown("---")
 
 
 
 
 
 
 
 
115
 
116
- # πŸ“š Recommended Certifications
117
  st.subheader("πŸ“š Recommended Certifications")
118
  certs_field = filter_data_by_field(certs, "Field", field)
119
- if not certs_field.empty:
120
- st.dataframe(certs_field)
121
- else:
122
- st.write("No certifications found for your field.")
123
 
124
- # πŸŽ“ Scholarships
125
- st.subheader("πŸŽ“ Scholarship Opportunities")
126
  scholarships_field = filter_data_by_field(scholarships, "Field", field)
127
- if not scholarships_field.empty:
128
- st.dataframe(scholarships_field)
129
- else:
130
- st.write("No scholarships available for your field.")
131
 
132
- # πŸŽ“ Education Opportunities
133
  st.subheader("πŸŽ“ Education Opportunities")
134
  if tech_class == "Technical":
135
  edu_field = filter_data_by_field(edu_tech, "Field", field)
136
  else:
137
  edu_field = filter_data_by_field(edu_nontech, "Field", field)
 
138
 
139
- if not edu_field.empty:
140
- st.dataframe(edu_field)
141
- else:
142
- st.write("No education opportunities found.")
143
-
144
- # 🌍 Visa Opportunities
145
  st.subheader("🌍 Visa Opportunities Based on Your Skills")
146
  visa_matches = suggest_visa_opportunities(keywords, visa_data)
147
- if not visa_matches.empty:
148
- st.dataframe(visa_matches)
149
- else:
150
- st.write("No visa matches found for your skill set.")
151
 
152
- # πŸ“ˆ Suggested Skills to Upskill
153
- st.subheader("πŸ“ˆ Suggested Skills to Acquire for Better Opportunities")
154
- missing_skills = suggest_upskilling(keywords, skills_data)
155
- if missing_skills:
156
- st.write(", ".join(missing_skills))
157
  else:
158
- st.write("You already have most in-demand skills covered!")
 
 
 
 
 
 
 
5
  from sklearn.feature_extraction.text import TfidfVectorizer
6
  from sklearn.metrics.pairwise import cosine_similarity
7
  from keybert import KeyBERT
8
+ from datetime import datetime
9
+ import plotly.express as px
10
+ from fpdf import FPDF
11
+ import requests
12
 
13
  st.set_page_config(page_title="Universal Smart CV Analyzer", layout="wide")
14
 
 
31
 
32
  certs, scholarships, edu_tech, edu_nontech, visa_data, skills_data = load_data()
33
 
34
+ # Extract text from PDF
35
  def extract_text_from_pdf(file):
36
  reader = PyPDF2.PdfReader(file)
37
  text = ""
 
39
  text += page.extract_text()
40
  return text
41
 
42
+ # Keyword extraction
43
  def extract_keywords(text, num_keywords=10):
44
  kw_model = KeyBERT()
45
  keywords = kw_model.extract_keywords(text, top_n=num_keywords, stop_words='english')
46
  return [kw[0].lower() for kw in keywords]
47
 
48
+ # Field identification
49
  def identify_field(keywords):
50
  fields = {
51
  "Engineering": ["engineer", "mechanical", "electrical", "civil", "plc", "automation"],
 
57
  "Healthcare": ["nursing", "surgery", "hospital", "patient"],
58
  "Construction": ["carpentry", "plumbing", "hvac", "gardening", "mining"]
59
  }
60
+ scores = {field: len(set(keywords).intersection(terms)) for field, terms in fields.items()}
 
 
61
  return max(scores, key=scores.get)
62
 
63
+ # Technical background
64
  def is_technical_background(keywords):
65
  tech_terms = ["engineer", "machine learning", "python", "developer", "software", "automation", "plc", "ai"]
66
  non_tech_terms = ["marketing", "finance", "content", "seo", "branding", "accounting", "creative"]
 
67
  tech_score = len(set(keywords).intersection(tech_terms))
68
  non_tech_score = len(set(keywords).intersection(non_tech_terms))
 
69
  return "Technical" if tech_score >= non_tech_score else "Non-Technical"
70
 
71
+ # CV skill score
72
  def calculate_cv_score(text, keywords):
73
  ideal = " ".join(keywords)
74
  tfidf = TfidfVectorizer()
 
76
  score = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
77
  return round(score * 100)
78
 
79
+ # Field data filter
80
  def filter_data_by_field(df, field_col, field):
81
  return df[df[field_col].str.lower() == field.lower()]
82
 
83
+ # Visa opportunities
84
  def suggest_visa_opportunities(keywords, visa_data):
85
  matched_rows = []
86
  for _, row in visa_data.iterrows():
 
88
  matched_rows.append(row)
89
  return pd.DataFrame(matched_rows)
90
 
91
+ # Upskilling suggestions
92
  def suggest_upskilling(keywords, skills_data):
93
  all_skills = set(skills_data["Skill"].str.lower())
94
+ current_skills = set([kw.lower() for kw in keywords])
95
  missing_skills = all_skills - current_skills
96
+ suggested = skills_data[skills_data["Skill"].str.lower().isin(missing_skills)]
97
+ return suggested.sort_values("Importance", ascending=False)
98
+
99
+ # 🎯 Job listings using Adzuna API
100
+ def get_job_listings(keywords, location="Pakistan", results_per_page=10):
101
+ app_id = "f4efd3a2" # Replace with your Adzuna app_id
102
+ app_key = "5702f3c0507ac69f98aa15f855b06901" # Replace with your Adzuna app_key
103
+ base_url = "https://api.adzuna.com/v1/api/jobs/pk/search/1"
104
+ query = " ".join(keywords)
105
+
106
+ params = {
107
+ "app_id": app_id,
108
+ "app_key": app_key,
109
+ "results_per_page": results_per_page,
110
+ "what": query,
111
+ "where": location,
112
+ "content-type": "application/json"
113
+ }
114
 
115
+ try:
116
+ response = requests.get(base_url, params=params)
117
+ response.raise_for_status()
118
+ jobs = response.json().get("results", [])
119
+ return pd.DataFrame(jobs)
120
+ except Exception as e:
121
+ st.error(f"Error fetching job listings: {e}")
122
+ return pd.DataFrame()
123
+
124
+ # Timeline generation
125
+ def generate_timeline(data=None):
126
+ timeline = pd.DataFrame({
127
+ "Task": ["Certifications", "Scholarships", "Education", "Visa Search"],
128
+ "Start": ["2025-06-01", "2025-07-01", "2025-08-01", "2025-09-01"],
129
+ "Finish": ["2025-06-30", "2025-07-30", "2025-09-30", "2025-10-15"]
130
+ })
131
+ fig = px.timeline(timeline, x_start="Start", x_end="Finish", y="Task", color="Task")
132
+ fig.update_yaxes(categoryorder='total ascending')
133
+ st.plotly_chart(fig)
134
+
135
+ # PDF Report
136
+ class PDF(FPDF):
137
+ def header(self):
138
+ self.set_font('Arial', 'B', 12)
139
+ self.cell(0, 10, 'Career Roadmap Report', ln=True, align='C')
140
+ def chapter_title(self, title):
141
+ self.set_font('Arial', 'B', 10)
142
+ self.cell(0, 10, title, ln=True)
143
+ def chapter_body(self, body):
144
+ self.set_font('Arial', '', 9)
145
+ self.multi_cell(0, 10, body)
146
+
147
+ def generate_pdf_report(field, score, keywords, upskills):
148
+ pdf = PDF()
149
+ pdf.add_page()
150
+ pdf.chapter_title("Field: " + field)
151
+ pdf.chapter_title("Score: " + str(score))
152
+ pdf.chapter_body("Keywords: " + ", ".join(keywords))
153
+ pdf.chapter_title("Suggested Upskilling:")
154
+ pdf.chapter_body(", ".join(upskills))
155
+ pdf.output("report.pdf")
156
+ st.success("πŸ“„ PDF Report Generated: report.pdf")
157
+
158
+ # 🌟 MAIN APP LOGIC
159
  if uploaded_file:
160
  text = extract_text_from_pdf(uploaded_file)
161
  st.success("βœ… CV Text Extracted")
162
 
 
163
  keywords = extract_keywords(text)
164
  st.subheader("πŸ”‘ Extracted Keywords")
165
  st.write(keywords)
166
 
 
167
  field = identify_field(keywords)
168
  st.subheader("🎯 Identified Field / Domain")
169
  st.write(field)
170
 
 
171
  tech_class = is_technical_background(keywords)
172
  st.subheader("🧠 CV Background Type")
173
  st.write(tech_class)
174
 
 
175
  score = calculate_cv_score(text, keywords)
176
  st.subheader("πŸ“Š CV Skill Score")
177
  st.metric(label="Score", value=f"{score}/100")
178
 
179
+ st.subheader("πŸ“ˆ Suggested Skills to Acquire for Better Opportunities")
180
+ missing_skills_df = suggest_upskilling(keywords, skills_data)
181
+ if not missing_skills_df.empty:
182
+ for i, row in missing_skills_df.head(10).iterrows():
183
+ st.progress(row["Importance"] / 100, text=row["Skill"])
184
+ missing_list = missing_skills_df["Skill"].tolist()
185
+ else:
186
+ st.write("You already have most in-demand skills covered!")
187
+ missing_list = []
188
 
 
189
  st.subheader("πŸ“š Recommended Certifications")
190
  certs_field = filter_data_by_field(certs, "Field", field)
191
+ st.dataframe(certs_field)
 
 
 
192
 
193
+ st.subheader("πŸŽ“ Scholarships")
 
194
  scholarships_field = filter_data_by_field(scholarships, "Field", field)
195
+ st.dataframe(scholarships_field)
 
 
 
196
 
 
197
  st.subheader("πŸŽ“ Education Opportunities")
198
  if tech_class == "Technical":
199
  edu_field = filter_data_by_field(edu_tech, "Field", field)
200
  else:
201
  edu_field = filter_data_by_field(edu_nontech, "Field", field)
202
+ st.dataframe(edu_field)
203
 
 
 
 
 
 
 
204
  st.subheader("🌍 Visa Opportunities Based on Your Skills")
205
  visa_matches = suggest_visa_opportunities(keywords, visa_data)
206
+ st.dataframe(visa_matches)
 
 
 
207
 
208
+ st.subheader("πŸ’Ό Job Listings")
209
+ job_df = get_job_listings(keywords)
210
+ if not job_df.empty:
211
+ st.dataframe(job_df[["title", "company", "location", "description"]])
 
212
  else:
213
+ st.write("No job listings available right now.")
214
+
215
+ st.subheader("πŸ—“οΈ Personalized Timeline")
216
+ generate_timeline()
217
+
218
+ if st.button("πŸ“„ Generate PDF Report"):
219
+ generate_pdf_report(field, score, keywords, missing_list)