MuhammadQASIM111 commited on
Commit
204b514
·
verified ·
1 Parent(s): e0d6520

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -51
app.py CHANGED
@@ -2,22 +2,27 @@ import warnings
2
  warnings.filterwarnings('ignore')
3
 
4
  import os
5
- os.system("pip install pymupdf")
6
  import fitz # PyMuPDF for PDF processing
7
-
8
  import docx # python-docx for DOCX processing
9
  import gradio as gr
10
- import os
11
  from crewai import Agent, Task, Crew
12
  from crewai_tools import SerperDevTool
13
 
 
 
14
 
15
- os.environ['OPENAI_API_KEY'] = os.getenv("openaikey")
 
16
  os.environ["OPENAI_MODEL_NAME"] = 'gpt-4o-mini'
17
  os.environ["SERPER_API_KEY"] = os.getenv("serper_key")
18
  os.environ["GRADIO_SERVER_PORT"] = "7861"
 
 
19
  os.system('pip install pydantic --upgrade')
 
20
 
 
21
  def extract_text_from_pdf(file_path):
22
  """Extracts text from a PDF file using PyMuPDF."""
23
  doc = fitz.open(file_path)
@@ -26,6 +31,7 @@ def extract_text_from_pdf(file_path):
26
  text += page.get_text()
27
  return text
28
 
 
29
  def extract_text_from_docx(file_path):
30
  """Extracts text from a DOCX file using python-docx."""
31
  doc = docx.Document(file_path)
@@ -34,6 +40,7 @@ def extract_text_from_docx(file_path):
34
  fullText.append(para.text)
35
  return "\n".join(fullText)
36
 
 
37
  def extract_text_from_resume(file_path):
38
  """Determines file type and extracts text."""
39
  if file_path.endswith(".pdf"):
@@ -43,92 +50,94 @@ def extract_text_from_resume(file_path):
43
  else:
44
  return "Unsupported file format."
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
-
48
- # Agent 1: Resume Strategist
49
  resume_feedback = Agent(
50
  role="Professional Resume Advisor",
51
  goal="Give feedback on the resume to make it stand out in the job market.",
52
  verbose=True,
53
  backstory="With a strategic mind and an eye for detail, you excel at providing feedback on resumes to highlight the most relevant skills and experiences."
54
- )
55
-
56
 
57
  # Task for Resume Strategist Agent: Align Resume with Job Requirements
58
  resume_feedback_task = Task(
59
- description=(
60
- """Give feedback on the resume to make it stand out for recruiters.
61
- Review every section, inlcuding the summary, work experience, skills, and education. Suggest to add relevant sections if they are missing.
62
- Also give an overall score to the resume out of 10. This is the resume: {resume}"""
63
- ),
64
  expected_output="The overall score of the resume followed by the feedback in bullet points.",
65
  agent=resume_feedback
66
  )
67
 
68
- # Agent 2: Resume Strategist
69
  resume_advisor = Agent(
70
  role="Professional Resume Writer",
71
- goal="Based on the feedback recieved from Resume Advisor, make changes to the resume to make it stand out in the job market.",
72
  verbose=True,
73
  backstory="With a strategic mind and an eye for detail, you excel at refining resumes based on the feedback to highlight the most relevant skills and experiences."
74
  )
75
 
76
- # Task for Resume Strategist Agent: Align Resume with Job Requirements
77
  resume_advisor_task = Task(
78
- description=(
79
- """Rewrite the resume based on the feedback to make it stand out for recruiters. You can adjust and enhance the resume but don't make up facts.
80
- Review and update every section, including the summary, work experience, skills, and education to better reflect the candidates abilities. This is the resume: {resume}"""
81
- ),
82
- expected_output= "Resume in markdown format that effectively highlights the candidate's qualifications and experiences",
83
- # output_file="improved_resume.md",
84
  context=[resume_feedback_task],
85
  agent=resume_advisor
86
  )
87
 
 
88
  search_tool = SerperDevTool()
89
 
90
-
91
- # Agent 3: Researcher
92
  job_researcher = Agent(
93
- role = "Senior Recruitment Consultant",
94
- goal = "Find the 5 most relevant, recently posted jobs based on the improved resume recieved from resume advisor and the location preference",
95
- tools = [search_tool],
96
- verbose = True,
97
- backstory = """As a senior recruitment consultant your prowess in finding the most relevant jobs based on the resume and location preference is unmatched.
98
- You can scan the resume efficiently, identify the most suitable job roles and search for the best suited recently posted open job positions at the preffered location."""
99
- )
100
 
 
101
  research_task = Task(
102
- description = """Find the 5 most relevant recent job postings based on the resume recieved from resume advisor and location preference. This is the preferred location: {location} .
103
- Use the tools to gather relevant content and shortlist the 5 most relevant, recent, job openings""",
104
- expected_output=(
105
- "A bullet point list of the 5 job openings, with the appropriate links and detailed description about each job, in markdown format"
106
- ),
107
- # output_file="relevant_jobs.md",
108
  agent=job_researcher
109
  )
110
 
111
-
112
  crew = Crew(
113
  agents=[resume_feedback, resume_advisor, job_researcher],
114
  tasks=[resume_feedback_task, resume_advisor_task, research_task],
115
  verbose=True
116
  )
117
 
118
-
119
-
120
  def resume_agent(file_path, location):
121
  resume_text = extract_text_from_resume(file_path)
122
 
123
- result = crew.kickoff(inputs={"resume": resume_text, "location": location})
124
-
125
- # Extract outputs
126
- feedback = resume_feedback_task.output.raw.strip("```markdown").strip("```").strip()
127
- improved_resume = resume_advisor_task.output.raw.strip("```markdown").strip("```").strip()
128
- job_roles = research_task.output.raw.strip("```markdown").strip("```").strip()
 
129
 
130
  return feedback, improved_resume, job_roles
131
-
132
 
133
  # Gradio Interface
134
  with gr.Blocks() as demo:
@@ -148,14 +157,11 @@ with gr.Blocks() as demo:
148
 
149
  # Define the click event for the submit button
150
  def format_outputs(feedback, improved_resume, job_roles):
151
- # Add bold headings to each section
152
- feedback_with_heading = f"## Resume Feedback:**\n\n{feedback}"
153
  improved_resume_with_heading = f"## Improved Resume:\n\n{improved_resume}"
154
  job_roles_with_heading = f"## Relevant Job Roles:\n\n{job_roles}"
155
  return feedback_with_heading, improved_resume_with_heading, job_roles_with_heading
156
 
157
-
158
-
159
  submit_button.click(
160
  lambda: gr.update(value="Processing..."),
161
  inputs=[],
@@ -174,5 +180,6 @@ with gr.Blocks() as demo:
174
  outputs=submit_button
175
  )
176
 
 
177
  demo.queue()
178
  demo.launch()
 
2
  warnings.filterwarnings('ignore')
3
 
4
  import os
5
+ import requests
6
  import fitz # PyMuPDF for PDF processing
 
7
  import docx # python-docx for DOCX processing
8
  import gradio as gr
 
9
  from crewai import Agent, Task, Crew
10
  from crewai_tools import SerperDevTool
11
 
12
+ # Retrieve the Gemini API key from environment variables
13
+ gemini_key = os.getenv("geminiapikey") # Ensure you have set the Gemini API key in your Hugging Face Space
14
 
15
+ # Other API keys (assuming they are also set in the Hugging Face Space environment)
16
+ os.environ["OPENAI_API_KEY"] = os.getenv("openaikey")
17
  os.environ["OPENAI_MODEL_NAME"] = 'gpt-4o-mini'
18
  os.environ["SERPER_API_KEY"] = os.getenv("serper_key")
19
  os.environ["GRADIO_SERVER_PORT"] = "7861"
20
+
21
+ # Install necessary packages (ensure pymupdf and pydantic are installed)
22
  os.system('pip install pydantic --upgrade')
23
+ os.system("pip install pymupdf")
24
 
25
+ # Function to extract text from PDF
26
  def extract_text_from_pdf(file_path):
27
  """Extracts text from a PDF file using PyMuPDF."""
28
  doc = fitz.open(file_path)
 
31
  text += page.get_text()
32
  return text
33
 
34
+ # Function to extract text from DOCX
35
  def extract_text_from_docx(file_path):
36
  """Extracts text from a DOCX file using python-docx."""
37
  doc = docx.Document(file_path)
 
40
  fullText.append(para.text)
41
  return "\n".join(fullText)
42
 
43
+ # Function to determine file type and extract text from the resume
44
  def extract_text_from_resume(file_path):
45
  """Determines file type and extracts text."""
46
  if file_path.endswith(".pdf"):
 
50
  else:
51
  return "Unsupported file format."
52
 
53
+ # Function to call the Gemini API for processing
54
+ def get_gemini_response(input_data):
55
+ """Calls the Gemini API with the provided input."""
56
+ url = "https://gemini.api.endpoint" # Replace with the actual Gemini API endpoint
57
+ headers = {
58
+ "Authorization": f"Bearer {gemini_key}",
59
+ "Content-Type": "application/json"
60
+ }
61
+ payload = {
62
+ "input": input_data
63
+ }
64
+
65
+ response = requests.post(url, json=payload, headers=headers)
66
+
67
+ if response.status_code == 200:
68
+ return response.json() # Return the Gemini API response
69
+ else:
70
+ return {"feedback": "Error fetching feedback from Gemini API."}
71
 
72
+ # Resume feedback agent
 
73
  resume_feedback = Agent(
74
  role="Professional Resume Advisor",
75
  goal="Give feedback on the resume to make it stand out in the job market.",
76
  verbose=True,
77
  backstory="With a strategic mind and an eye for detail, you excel at providing feedback on resumes to highlight the most relevant skills and experiences."
78
+ )
 
79
 
80
  # Task for Resume Strategist Agent: Align Resume with Job Requirements
81
  resume_feedback_task = Task(
82
+ description="""Give feedback on the resume to make it stand out for recruiters. Review every section, including the summary, work experience, skills, and education. Suggest to add relevant sections if they are missing. Also give an overall score to the resume out of 10. This is the resume: {resume}""",
 
 
 
 
83
  expected_output="The overall score of the resume followed by the feedback in bullet points.",
84
  agent=resume_feedback
85
  )
86
 
87
+ # Resume advisor agent
88
  resume_advisor = Agent(
89
  role="Professional Resume Writer",
90
+ goal="Based on the feedback received from Resume Advisor, make changes to the resume to make it stand out in the job market.",
91
  verbose=True,
92
  backstory="With a strategic mind and an eye for detail, you excel at refining resumes based on the feedback to highlight the most relevant skills and experiences."
93
  )
94
 
95
+ # Task for Resume Advisor Agent: Align Resume with Job Requirements
96
  resume_advisor_task = Task(
97
+ description="""Rewrite the resume based on the feedback to make it stand out for recruiters. You can adjust and enhance the resume but don't make up facts. Review and update every section, including the summary, work experience, skills, and education to better reflect the candidate's abilities. This is the resume: {resume}""",
98
+ expected_output="Resume in markdown format that effectively highlights the candidate's qualifications and experiences",
 
 
 
 
99
  context=[resume_feedback_task],
100
  agent=resume_advisor
101
  )
102
 
103
+ # Job researcher agent
104
  search_tool = SerperDevTool()
105
 
 
 
106
  job_researcher = Agent(
107
+ role="Senior Recruitment Consultant",
108
+ goal="Find the 5 most relevant, recently posted jobs based on the improved resume received from resume advisor and the location preference",
109
+ tools=[search_tool],
110
+ verbose=True,
111
+ backstory="As a senior recruitment consultant, your prowess in finding the most relevant jobs based on the resume and location preference is unmatched."
112
+ )
 
113
 
114
+ # Task for Job Researcher Agent
115
  research_task = Task(
116
+ description="""Find the 5 most relevant recent job postings based on the resume received from resume advisor and location preference. This is the preferred location: {location}. Use the tools to gather relevant content and shortlist the 5 most relevant, recent job openings""",
117
+ expected_output="A bullet point list of the 5 job openings, with the appropriate links and detailed description about each job, in markdown format",
 
 
 
 
118
  agent=job_researcher
119
  )
120
 
121
+ # Creating the Crew
122
  crew = Crew(
123
  agents=[resume_feedback, resume_advisor, job_researcher],
124
  tasks=[resume_feedback_task, resume_advisor_task, research_task],
125
  verbose=True
126
  )
127
 
128
+ # Function to run the entire agent pipeline
 
129
  def resume_agent(file_path, location):
130
  resume_text = extract_text_from_resume(file_path)
131
 
132
+ # Call the Gemini API to process the resume or provide feedback
133
+ gemini_response = get_gemini_response(resume_text)
134
+
135
+ # Extract feedback, improved resume, and job roles from the Gemini API response
136
+ feedback = gemini_response.get("feedback", "No feedback from Gemini API.")
137
+ improved_resume = gemini_response.get("improved_resume", "No improvements from Gemini API.")
138
+ job_roles = gemini_response.get("job_roles", "No job roles from Gemini API.")
139
 
140
  return feedback, improved_resume, job_roles
 
141
 
142
  # Gradio Interface
143
  with gr.Blocks() as demo:
 
157
 
158
  # Define the click event for the submit button
159
  def format_outputs(feedback, improved_resume, job_roles):
160
+ feedback_with_heading = f"## Resume Feedback:\n\n{feedback}"
 
161
  improved_resume_with_heading = f"## Improved Resume:\n\n{improved_resume}"
162
  job_roles_with_heading = f"## Relevant Job Roles:\n\n{job_roles}"
163
  return feedback_with_heading, improved_resume_with_heading, job_roles_with_heading
164
 
 
 
165
  submit_button.click(
166
  lambda: gr.update(value="Processing..."),
167
  inputs=[],
 
180
  outputs=submit_button
181
  )
182
 
183
+ # Launch the Gradio app
184
  demo.queue()
185
  demo.launch()