Spaces:
Runtime error
Runtime error
Update app.py (#1)
Browse files- Update app.py (6b6eb54081619bdccba0f01ce657214fc719a357)
Co-authored-by: Umair Mir <umaircqai@users.noreply.huggingface.co>
app.py
CHANGED
|
@@ -1,64 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from sentence_transformers import SentenceTransformer, util
|
| 4 |
import gradio as gr
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
import requests
|
| 7 |
+
from huggingface_hub import HfApi
|
| 8 |
|
| 9 |
+
# Load environment variables
|
| 10 |
+
import os
|
| 11 |
+
from huggingface_hub import HfApi
|
|
|
|
| 12 |
|
| 13 |
+
hf_token = os.getenv("hf_token")
|
| 14 |
+
api = HfApi(token=hf_token)
|
| 15 |
|
| 16 |
+
# Load embedding model
|
| 17 |
+
embed_model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# API call function to Zephyr model
|
| 20 |
+
def call_zephyr_api(prompt):
|
| 21 |
+
url = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 22 |
+
headers = {
|
| 23 |
+
"Authorization": f"Bearer {hf_token}",
|
| 24 |
+
"Content-Type": "application/json"
|
| 25 |
+
}
|
| 26 |
+
payload = {
|
| 27 |
+
"inputs": prompt,
|
| 28 |
+
"parameters": {
|
| 29 |
+
"max_new_tokens": 512,
|
| 30 |
+
"temperature": 0.7
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
try:
|
| 34 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 35 |
+
response.raise_for_status()
|
| 36 |
+
return response.json()[0]["generated_text"]
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"⚠️ Error generating suggestions: {e}"
|
| 39 |
|
| 40 |
+
# Resume matcher function
|
| 41 |
+
def resume_matcher_and_advisor(resume_text, job_desc):
|
| 42 |
+
embeddings = embed_model.encode([resume_text, job_desc], convert_to_tensor=True)
|
| 43 |
+
similarity_score = util.pytorch_cos_sim(embeddings[0], embeddings[1]).item()
|
| 44 |
+
res_score = round(similarity_score * 100, 2)
|
| 45 |
|
| 46 |
+
if res_score > 85:
|
| 47 |
+
suggestions = "✅ This resume is a good fit for the job description!"
|
| 48 |
+
else:
|
| 49 |
+
prompt = f"""<|system|>
|
| 50 |
+
You are a professional resume reviewer.
|
| 51 |
+
<|user|>
|
| 52 |
+
Compare the following resume with the job description and give bullet-point suggestions to improve the resume so that it better fits the job requirements.
|
| 53 |
+
- Be critical and constructive.
|
| 54 |
+
- Do NOT assume the resume matches the job.
|
| 55 |
+
- DO NOT rewrite the resume or job.
|
| 56 |
+
- DO NOT hallucinate skills.
|
| 57 |
+
Job Description:
|
| 58 |
+
{job_desc}
|
| 59 |
+
Resume:
|
| 60 |
+
{resume_text}
|
| 61 |
+
</s>"""
|
| 62 |
+
suggestions = call_zephyr_api(prompt)
|
| 63 |
|
| 64 |
+
return res_score, suggestions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Launch Gradio interface
|
| 67 |
+
iface = gr.Interface(
|
| 68 |
+
fn=resume_matcher_and_advisor,
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Textbox(lines=15, label="Paste Resume Text"),
|
| 71 |
+
gr.Textbox(lines=15, label="Paste Job Description")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
],
|
| 73 |
+
outputs=[
|
| 74 |
+
gr.Number(label="Similarity Score (%)"),
|
| 75 |
+
gr.Textbox(label="Improvement Suggestions")
|
| 76 |
+
],
|
| 77 |
+
title="🧠 Resume Matcher & Advisor",
|
| 78 |
+
description="Paste a resume and job description. Get a match score and improvement suggestions."
|
| 79 |
)
|
| 80 |
|
| 81 |
+
iface.launch(share=True)
|
|
|
|
|
|