Afeezee commited on
Commit
fb5a2ab
·
verified ·
1 Parent(s): 3880843

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from pdfminer.high_level import extract_text
4
+ from docx import Document
5
+ from groq import Groq
6
+ from reportlab.lib.pagesizes import letter
7
+ from reportlab.pdfgen import canvas
8
+
9
+ # Initialize Groq client with your API key
10
+ key = "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"
11
+ client = Groq(api_key=key)
12
+
13
+ # Function to read PDF files
14
+ def read_pdf(file_path):
15
+ text = extract_text(file_path)
16
+ return text
17
+
18
+ # Function to read DOCX files
19
+ def read_docx(file_path):
20
+ doc = Document(file_path)
21
+ text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
22
+ return text
23
+
24
+ # Function to analyze CV for role fit using Llama 3.3 and Groq API
25
+ def analyze_cv_with_llama(cv_text, job_description):
26
+ completion = client.chat.completions.create(
27
+ model="llama-3.3-70b-versatile",
28
+ messages=[
29
+ {
30
+ "role": "system",
31
+ "content": (
32
+ "You are a hiring expert with extensive experience in talent acquisition. "
33
+ "Analyze the following CV content against a job description and score over 100 in these areas: "
34
+ "Relevance to the job, Work experience, Skills, Educational background, Achievements and Impact, "
35
+ "and Format and Presentation. Provide detailed scores and feedback for each area."
36
+ )
37
+ },
38
+ {"role": "user", "content": f"Job Description:\n{job_description}\n\nCV:\n{cv_text}"}
39
+ ],
40
+ temperature=0.7,
41
+ max_tokens=2048,
42
+ top_p=0.9,
43
+ stream=False,
44
+ stop=None,
45
+ )
46
+ # Collect and return the analysis response
47
+ analysis_text = ''.join([chunk.message.content for chunk in completion.choices])
48
+ return analysis_text
49
+
50
+ # Function to generate a PDF with analysis results
51
+ def generate_pdf(content, output_path):
52
+ pdf = canvas.Canvas(output_path, pagesize=letter)
53
+ pdf.setFont("Helvetica", 10)
54
+ width, height = letter
55
+ y = height - 40 # Start position
56
+
57
+ for line in content.split("\n"):
58
+ pdf.drawString(40, y, line)
59
+ y -= 15 # Move down for the next line
60
+ if y < 40: # Create a new page if space is insufficient
61
+ pdf.showPage()
62
+ pdf.setFont("Helvetica", 10)
63
+ y = height - 40
64
+
65
+ pdf.save()
66
+
67
+ # Function to handle file upload and job screening
68
+ def process_file(file, job_description):
69
+ # Save the uploaded file to a temporary location
70
+ file_path = file.name
71
+ file_extension = os.path.splitext(file_path)[1].lower()
72
+
73
+ # Read the file content based on its format
74
+ if file_extension == ".pdf":
75
+ cv_text = read_pdf(file_path)
76
+ elif file_extension == ".docx":
77
+ cv_text = read_docx(file_path)
78
+ else:
79
+ return "Unsupported file format", None
80
+
81
+ # Analyze the CV against the job description
82
+ analysis_result = analyze_cv_with_llama(cv_text, job_description)
83
+
84
+ # Generate PDF with analysis results
85
+ output_pdf_path = "cv_analysis_result.pdf"
86
+ generate_pdf(analysis_result, output_pdf_path)
87
+
88
+ return analysis_result, output_pdf_path
89
+
90
+ # Gradio Interface
91
+ def main():
92
+ with gr.Blocks() as iface:
93
+ with gr.Row():
94
+ file_input = gr.File(label="Upload CV/Resume (PDF or DOCX)")
95
+ job_description_input = gr.Textbox(label="Enter Job Description", lines=5, placeholder="Type the job description here...")
96
+
97
+ analysis_output = gr.Markdown(label="CV Screening Analysis")
98
+ download_button = gr.File(label="Download Analysis as PDF")
99
+
100
+ def process_input(file, job_description):
101
+ if not file or not job_description.strip():
102
+ return "Please upload a file and provide a job description.", None
103
+ return process_file(file, job_description)
104
+
105
+ submit_button = gr.Button("Analyze CV")
106
+ submit_button.click(process_input, [file_input, job_description_input], [analysis_output, download_button])
107
+
108
+ iface.launch(share=True)
109
+
110
+ if __name__ == "__main__":
111
+ main()