devmalik-official commited on
Commit
e990ba3
·
verified ·
1 Parent(s): af788b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -57
app.py CHANGED
@@ -1,88 +1,109 @@
1
  import gradio as gr
2
  import json
3
- import os
4
  import PyPDF2
5
  import docx
6
 
7
 
8
- # -----------------------------
9
- # Resume Parsing
10
- # -----------------------------
11
 
12
  def extract_text(file):
13
 
14
  if file is None:
15
  return ""
16
 
17
- file_name = file.name
 
18
 
19
- if file_name.endswith(".pdf"):
20
  reader = PyPDF2.PdfReader(file)
21
- text = ""
22
  for page in reader.pages:
23
  text += page.extract_text() or ""
24
- return text
25
 
26
- elif file_name.endswith(".docx"):
27
- doc = docx.Document(file)
28
- text = "\n".join([p.text for p in doc.paragraphs])
29
- return text
30
 
31
- elif file_name.endswith(".txt"):
32
- return file.read().decode("utf-8")
33
 
34
- return ""
35
 
36
 
37
- # -----------------------------
38
- # Simple Resume Analyzer
39
- # -----------------------------
40
 
41
  def analyze_resume(resume_text):
42
 
43
- if not resume_text or len(resume_text.strip()) == 0:
44
  return {
45
  "score": 0,
46
- "message": "No resume text detected"
 
 
47
  }
48
 
49
  text = resume_text.lower()
50
 
51
- technical_keywords = [
52
- "python","java","c++","machine learning","ai",
53
- "data","sql","tensorflow","pytorch","pandas",
54
- "numpy","git","linux"
55
  ]
56
 
57
  soft_keywords = [
58
- "teamwork","communication","leadership",
59
  "problem solving","adaptability"
60
  ]
61
 
62
- tech_found = [k for k in technical_keywords if k in text]
63
  soft_found = [k for k in soft_keywords if k in text]
64
 
65
- score = min(100, (len(tech_found)*8 + len(soft_found)*5))
66
 
67
- result = {
 
 
 
 
 
 
68
  "score": score,
69
  "technical_skills": tech_found,
70
  "soft_skills": soft_found,
71
- "recommendation": "Add more quantified achievements and skills." if score < 50 else "Good resume. Minor improvements recommended."
72
  }
73
 
74
- return result
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- # -----------------------------
 
78
  # Export Functions
79
- # -----------------------------
80
 
81
  def export_json(data):
82
 
83
- if data is None:
84
- return None
85
-
86
  file_path = "analysis.json"
87
 
88
  with open(file_path,"w") as f:
@@ -93,9 +114,6 @@ def export_json(data):
93
 
94
  def export_text(data):
95
 
96
- if data is None:
97
- return None
98
-
99
  file_path = "analysis.txt"
100
 
101
  with open(file_path,"w") as f:
@@ -104,9 +122,9 @@ def export_text(data):
104
  return file_path
105
 
106
 
107
- # -----------------------------
108
  # Processing Pipeline
109
- # -----------------------------
110
 
111
  def process_resume(file):
112
 
@@ -114,30 +132,32 @@ def process_resume(file):
114
 
115
  analysis = analyze_resume(text)
116
 
117
- return text, analysis
 
 
118
 
119
 
120
- # -----------------------------
121
  # UI
122
- # -----------------------------
123
 
124
  with gr.Blocks(title="Resume Analyzer") as demo:
125
 
126
  gr.Markdown("# AI Resume Analyzer")
127
- gr.Markdown("Upload your resume and get an instant score.")
128
 
129
- with gr.Row():
130
-
131
- resume_file = gr.File(label="Upload Resume (PDF, DOCX, TXT)")
132
 
133
- load_btn = gr.Button("Analyze Resume")
134
 
135
  resume_text = gr.Textbox(
136
  label="Extracted Resume Text",
137
  lines=10
138
  )
139
 
140
- analysis_output = gr.JSON(label="Analysis Result")
 
 
141
 
142
  with gr.Row():
143
 
@@ -146,32 +166,32 @@ with gr.Blocks(title="Resume Analyzer") as demo:
146
 
147
  download_file = gr.File(label="Download Analysis")
148
 
149
- # -----------------------------
150
  # Button Actions
151
- # -----------------------------
152
 
153
- load_btn.click(
154
  process_resume,
155
  inputs=resume_file,
156
- outputs=[resume_text, analysis_output]
157
  )
158
 
159
  export_json_btn.click(
160
  export_json,
161
- inputs=analysis_output,
162
  outputs=download_file
163
  )
164
 
165
  export_text_btn.click(
166
  export_text,
167
- inputs=analysis_output,
168
  outputs=download_file
169
  )
170
 
171
 
172
- # -----------------------------
173
  # Launch
174
- # -----------------------------
175
 
176
  if __name__ == "__main__":
177
  demo.launch()
 
1
  import gradio as gr
2
  import json
 
3
  import PyPDF2
4
  import docx
5
 
6
 
7
+ # ----------------------------
8
+ # Resume Text Extraction
9
+ # ----------------------------
10
 
11
  def extract_text(file):
12
 
13
  if file is None:
14
  return ""
15
 
16
+ filename = file.name
17
+ text = ""
18
 
19
+ if filename.endswith(".pdf"):
20
  reader = PyPDF2.PdfReader(file)
 
21
  for page in reader.pages:
22
  text += page.extract_text() or ""
 
23
 
24
+ elif filename.endswith(".docx"):
25
+ document = docx.Document(file)
26
+ for para in document.paragraphs:
27
+ text += para.text + "\n"
28
 
29
+ elif filename.endswith(".txt"):
30
+ text = file.read().decode("utf-8")
31
 
32
+ return text
33
 
34
 
35
+ # ----------------------------
36
+ # Resume Analyzer
37
+ # ----------------------------
38
 
39
  def analyze_resume(resume_text):
40
 
41
+ if not resume_text.strip():
42
  return {
43
  "score": 0,
44
+ "technical_skills": [],
45
+ "soft_skills": [],
46
+ "recommendation": "No text detected in resume"
47
  }
48
 
49
  text = resume_text.lower()
50
 
51
+ tech_keywords = [
52
+ "python","java","c++","sql","machine learning",
53
+ "data analysis","tensorflow","pandas","numpy",
54
+ "git","linux","ai"
55
  ]
56
 
57
  soft_keywords = [
58
+ "communication","teamwork","leadership",
59
  "problem solving","adaptability"
60
  ]
61
 
62
+ tech_found = [k for k in tech_keywords if k in text]
63
  soft_found = [k for k in soft_keywords if k in text]
64
 
65
+ score = min(100, len(tech_found)*8 + len(soft_found)*5)
66
 
67
+ recommendation = (
68
+ "Add more technical skills and measurable achievements."
69
+ if score < 50 else
70
+ "Good resume. Minor improvements recommended."
71
+ )
72
+
73
+ return {
74
  "score": score,
75
  "technical_skills": tech_found,
76
  "soft_skills": soft_found,
77
+ "recommendation": recommendation
78
  }
79
 
 
80
 
81
+ # ----------------------------
82
+ # Format analysis for UI
83
+ # ----------------------------
84
+
85
+ def format_analysis(result):
86
+
87
+ return f"""
88
+ ## Resume Score: {result['score']}/100
89
+
90
+ ### Technical Skills Found
91
+ {', '.join(result['technical_skills']) if result['technical_skills'] else "None"}
92
+
93
+ ### Soft Skills Found
94
+ {', '.join(result['soft_skills']) if result['soft_skills'] else "None"}
95
+
96
+ ### Recommendation
97
+ {result['recommendation']}
98
+ """
99
 
100
+
101
+ # ----------------------------
102
  # Export Functions
103
+ # ----------------------------
104
 
105
  def export_json(data):
106
 
 
 
 
107
  file_path = "analysis.json"
108
 
109
  with open(file_path,"w") as f:
 
114
 
115
  def export_text(data):
116
 
 
 
 
117
  file_path = "analysis.txt"
118
 
119
  with open(file_path,"w") as f:
 
122
  return file_path
123
 
124
 
125
+ # ----------------------------
126
  # Processing Pipeline
127
+ # ----------------------------
128
 
129
  def process_resume(file):
130
 
 
132
 
133
  analysis = analyze_resume(text)
134
 
135
+ formatted = format_analysis(analysis)
136
+
137
+ return text, formatted, analysis
138
 
139
 
140
+ # ----------------------------
141
  # UI
142
+ # ----------------------------
143
 
144
  with gr.Blocks(title="Resume Analyzer") as demo:
145
 
146
  gr.Markdown("# AI Resume Analyzer")
147
+ gr.Markdown("Upload your resume and get instant feedback.")
148
 
149
+ resume_file = gr.File(label="Upload Resume (PDF / DOCX / TXT)")
 
 
150
 
151
+ analyze_btn = gr.Button("Analyze Resume")
152
 
153
  resume_text = gr.Textbox(
154
  label="Extracted Resume Text",
155
  lines=10
156
  )
157
 
158
+ analysis_output = gr.Markdown(label="Analysis Result")
159
+
160
+ analysis_state = gr.State()
161
 
162
  with gr.Row():
163
 
 
166
 
167
  download_file = gr.File(label="Download Analysis")
168
 
169
+ # ----------------------------
170
  # Button Actions
171
+ # ----------------------------
172
 
173
+ analyze_btn.click(
174
  process_resume,
175
  inputs=resume_file,
176
+ outputs=[resume_text, analysis_output, analysis_state]
177
  )
178
 
179
  export_json_btn.click(
180
  export_json,
181
+ inputs=analysis_state,
182
  outputs=download_file
183
  )
184
 
185
  export_text_btn.click(
186
  export_text,
187
+ inputs=analysis_state,
188
  outputs=download_file
189
  )
190
 
191
 
192
+ # ----------------------------
193
  # Launch
194
+ # ----------------------------
195
 
196
  if __name__ == "__main__":
197
  demo.launch()