BasitAliii commited on
Commit
0d0e01d
·
verified ·
1 Parent(s): 7d8ed6e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import numpy as np
4
+ import faiss
5
+ from sentence_transformers import SentenceTransformer
6
+ from groq import Groq
7
+
8
+ # Initialize LLM and embeddings
9
+ client = Groq()
10
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
11
+ EMBED_DIM = 384
12
+ faiss_index = faiss.IndexFlatIP(EMBED_DIM)
13
+ candidates = []
14
+
15
+ # ---------------- HELPERS ----------------
16
+ def embed_text(text: str):
17
+ vec = embedder.encode([text])
18
+ vec = vec / np.linalg.norm(vec)
19
+ return vec.astype("float32")
20
+
21
+ def llm_generate(prompt: str):
22
+ completion = client.chat.completions.create(
23
+ model="llama-3.1-8b-instant",
24
+ messages=[{"role": "user", "content": prompt}],
25
+ temperature=0.3
26
+ )
27
+ return completion.choices[0].message.content
28
+
29
+ # ---------------- FUNCTIONALITY ----------------
30
+
31
+ def add_candidate(name, skills, experience):
32
+ profile_text = f"Name: {name}\nSkills: {skills}\nExperience: {experience}"
33
+ vec = embed_text(profile_text)
34
+ faiss_index.add(vec)
35
+ candidates.append(profile_text)
36
+ return f"Candidate '{name}' added successfully!"
37
+
38
+ def generate_bio(raw_data):
39
+ prompt = f"""You are a professional HR recruiter.
40
+ Write a concise, formal candidate bio using the data below.
41
+ Rules:
42
+ - Professional tone
43
+ - No exaggeration
44
+ - No emojis
45
+ - Max 120 words
46
+
47
+ Candidate Data:
48
+ {raw_data}"""
49
+ return llm_generate(prompt)
50
+
51
+ def rewrite_job(job_desc):
52
+ prompt = f"""Rewrite the job description below to comply with professional recruitment standards.
53
+ Ensure it is inclusive, clear, and well-structured.
54
+
55
+ Job Description:
56
+ {job_desc}"""
57
+ return llm_generate(prompt)
58
+
59
+ def recommend_candidates(job_query):
60
+ if len(candidates) == 0:
61
+ return "No candidates available.", "", ""
62
+
63
+ job_vec = embed_text(job_query)
64
+ scores, ids = faiss_index.search(job_vec, k=min(3, len(candidates)))
65
+
66
+ top_candidates = []
67
+ explanations = []
68
+
69
+ for rank, idx in enumerate(ids[0]):
70
+ candidate = candidates[idx]
71
+ explain_prompt = f"""Explain why the candidate below is a good match for the job.
72
+ Base reasoning only on skills and experience.
73
+ Keep it concise and professional.
74
+
75
+ Candidate:
76
+ {candidate}
77
+
78
+ Job:
79
+ {job_query}"""
80
+ explanation = llm_generate(explain_prompt)
81
+ top_candidates.append(f"Rank {rank+1}: {candidate}")
82
+ explanations.append(explanation)
83
+
84
+ return "\n\n".join(top_candidates), "\n\n".join(explanations), "Done"
85
+
86
+ # ---------------- GRADIO INTERFACE ----------------
87
+
88
+ with gr.Blocks() as demo:
89
+ gr.Markdown("# AI-Powered Recruitment MVP")
90
+
91
+ with gr.Tab("Add Candidate"):
92
+ name_input = gr.Textbox(label="Candidate Name")
93
+ skills_input = gr.Textbox(label="Skills")
94
+ exp_input = gr.Textbox(label="Experience")
95
+ add_btn = gr.Button("Add Candidate")
96
+ add_output = gr.Textbox(label="Status")
97
+ add_btn.click(add_candidate, inputs=[name_input, skills_input, exp_input], outputs=add_output)
98
+
99
+ with gr.Tab("Candidate Bio Generator"):
100
+ raw_input = gr.Textbox(label="Candidate Raw Data", lines=5)
101
+ bio_btn = gr.Button("Generate Bio")
102
+ bio_output = gr.Textbox(label="Candidate Bio", lines=5)
103
+ bio_btn.click(generate_bio, inputs=raw_input, outputs=bio_output)
104
+
105
+ with gr.Tab("Job Description Filter"):
106
+ job_input = gr.Textbox(label="Job Description", lines=5)
107
+ rewrite_btn = gr.Button("Rewrite Job")
108
+ rewrite_output = gr.Textbox(label="Rewritten Job Description", lines=5)
109
+ rewrite_btn.click(rewrite_job, inputs=job_input, outputs=rewrite_output)
110
+
111
+ with gr.Tab("Candidate Recommendation"):
112
+ query_input = gr.Textbox(label="Job Requirements", lines=5)
113
+ rec_btn = gr.Button("Recommend Candidates")
114
+ rec_candidates = gr.Textbox(label="Top Candidates", lines=5)
115
+ rec_explanation = gr.Textbox(label="Recommendation Explanation", lines=10)
116
+ rec_status = gr.Textbox(label="Status")
117
+ rec_btn.click(recommend_candidates, inputs=query_input, outputs=[rec_candidates, rec_explanation, rec_status])
118
+
119
+ demo.launch()