Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,146 +4,137 @@ import os
|
|
| 4 |
import PyPDF2
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
-
# API
|
| 8 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 9 |
HF_API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 10 |
MODEL_CHECK_PROMPT = "Say hello!"
|
| 11 |
|
| 12 |
-
#
|
| 13 |
def check_api_ready():
|
| 14 |
if not HF_API_TOKEN:
|
| 15 |
-
return False, "β HF_API_TOKEN not set. Please add
|
| 16 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 17 |
try:
|
| 18 |
-
resp = requests.post(HF_API_URL, headers=headers, json={"inputs": MODEL_CHECK_PROMPT}, timeout=
|
| 19 |
if resp.status_code == 200:
|
| 20 |
data = resp.json()
|
| 21 |
if isinstance(data, dict) and "error" in data:
|
| 22 |
-
return False, f"β Model
|
| 23 |
if isinstance(data, list) and data[0].get("generated_text"):
|
| 24 |
-
return True, "β
Model is
|
| 25 |
elif resp.status_code == 401:
|
| 26 |
-
return False, "β Unauthorized.
|
| 27 |
else:
|
| 28 |
-
return False, f"β Unexpected API
|
| 29 |
except Exception as e:
|
| 30 |
-
return False, f"β API connection
|
| 31 |
|
| 32 |
-
#
|
| 33 |
def lottie_html():
|
| 34 |
-
# Robotics-Students.json must exist in app root folder
|
| 35 |
return """
|
| 36 |
<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
|
| 37 |
<lottie-player src='Robotics-Students.json' background='transparent' speed='1' style='width:340px; height:340px; margin-bottom:12px;' loop autoplay></lottie-player>
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def extract_text_from_pdf(pdf_file):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if content:
|
| 47 |
-
text += content + "\n"
|
| 48 |
-
return text.strip()
|
| 49 |
-
|
| 50 |
-
# --- AI-Powered Interview Questions ---
|
| 51 |
def ai_generate_questions(resume_text, job_title):
|
| 52 |
prompt = (
|
| 53 |
-
f"You are an AI interview
|
| 54 |
f"Candidate Resume:\n{resume_text}\n"
|
| 55 |
f"Target Role: {job_title}\n"
|
| 56 |
-
f"Generate 10 realistic interview questions based on
|
| 57 |
-
f"and
|
| 58 |
)
|
| 59 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 60 |
-
payload = {
|
| 61 |
-
|
| 62 |
-
"parameters": {"max_new_tokens": 512, "temperature": 0.7, "return_full_text": False},
|
| 63 |
-
}
|
| 64 |
try:
|
| 65 |
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=60)
|
| 66 |
data = response.json()
|
| 67 |
-
if
|
| 68 |
return f"<div style='color:red;'>β API Error: {data['error']}</div>"
|
| 69 |
return data[0]["generated_text"].strip()
|
| 70 |
except Exception as e:
|
| 71 |
return f"<div style='color:red;'>β Exception: {str(e)}</div>"
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
def
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
#
|
| 99 |
def interface(pdf_file, job_title):
|
| 100 |
is_ready, msg = check_api_ready()
|
| 101 |
if not is_ready:
|
| 102 |
-
|
| 103 |
-
return
|
| 104 |
|
| 105 |
if not pdf_file:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
if not job_title.strip():
|
| 109 |
-
name = pdf_file.name.replace('.pdf',
|
| 110 |
-
|
| 111 |
-
return render_output_html(True, msg, name, "---", "Waiting for job input", 1, out)
|
| 112 |
|
| 113 |
-
name = pdf_file.name.replace(
|
| 114 |
try:
|
| 115 |
pdf_file.seek(0)
|
| 116 |
resume_text = extract_text_from_pdf(pdf_file)
|
| 117 |
if not resume_text:
|
| 118 |
-
raise Exception("
|
| 119 |
except Exception as e:
|
| 120 |
-
|
| 121 |
-
return render_output_html(True, msg, name, job_title, "Error", 0, out)
|
| 122 |
|
| 123 |
-
yield
|
| 124 |
-
|
| 125 |
-
)
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
questions = [q.strip() for q in ai_response.split("\n") if q.strip()]
|
| 129 |
-
output = "<div style='margin-top:20px;'><b>β
Interview Questions + Tips:</b></div>"
|
| 130 |
for i, q in enumerate(questions[:10], 1):
|
| 131 |
-
|
|
|
|
| 132 |
|
| 133 |
-
yield
|
| 134 |
|
|
|
|
| 135 |
with gr.Interface(
|
| 136 |
fn=interface,
|
| 137 |
inputs=[
|
| 138 |
-
gr.File(label="Upload Resume
|
| 139 |
-
gr.Textbox(label="Target Job Title", placeholder="e.g.
|
| 140 |
],
|
| 141 |
outputs=gr.HTML(),
|
| 142 |
-
title="Robot Resume Analyzer + AI Interview Coach (PDF Powered)",
|
| 143 |
-
live=False,
|
| 144 |
allow_flagging="never",
|
|
|
|
|
|
|
| 145 |
) as demo:
|
| 146 |
-
pass
|
| 147 |
-
|
| 148 |
-
if __name__ == "__main__":
|
| 149 |
demo.launch()
|
|
|
|
| 4 |
import PyPDF2
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
+
# Hugging Face API and model
|
| 8 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 9 |
HF_API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 10 |
MODEL_CHECK_PROMPT = "Say hello!"
|
| 11 |
|
| 12 |
+
# β
Check whether the API key and model are working correctly
|
| 13 |
def check_api_ready():
|
| 14 |
if not HF_API_TOKEN:
|
| 15 |
+
return False, "β HF_API_TOKEN not set. Please add in Space secrets."
|
| 16 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 17 |
try:
|
| 18 |
+
resp = requests.post(HF_API_URL, headers=headers, json={"inputs": MODEL_CHECK_PROMPT}, timeout=20)
|
| 19 |
if resp.status_code == 200:
|
| 20 |
data = resp.json()
|
| 21 |
if isinstance(data, dict) and "error" in data:
|
| 22 |
+
return False, f"β Model Error: {data['error']}"
|
| 23 |
if isinstance(data, list) and data[0].get("generated_text"):
|
| 24 |
+
return True, "β
Model is ready!"
|
| 25 |
elif resp.status_code == 401:
|
| 26 |
+
return False, "β Unauthorized. Check your API token."
|
| 27 |
else:
|
| 28 |
+
return False, f"β Unexpected API response: {resp.text}"
|
| 29 |
except Exception as e:
|
| 30 |
+
return False, f"β API connection failed: {str(e)}"
|
| 31 |
|
| 32 |
+
# β
Tooltip animation HTML from Lottie JSON (place Robotics-Students.json in Space root)
|
| 33 |
def lottie_html():
|
|
|
|
| 34 |
return """
|
| 35 |
<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
|
| 36 |
<lottie-player src='Robotics-Students.json' background='transparent' speed='1' style='width:340px; height:340px; margin-bottom:12px;' loop autoplay></lottie-player>
|
| 37 |
"""
|
| 38 |
|
| 39 |
+
# β
PDF text extractor
|
| 40 |
def extract_text_from_pdf(pdf_file):
|
| 41 |
+
reader = PyPDF2.PdfReader(BytesIO(pdf_file.read()))
|
| 42 |
+
return "\n".join([page.extract_text() or "" for page in reader.pages]).strip()
|
| 43 |
+
|
| 44 |
+
# β
Generate questions and answers using language model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def ai_generate_questions(resume_text, job_title):
|
| 46 |
prompt = (
|
| 47 |
+
f"You are an AI interview coach.\n"
|
| 48 |
f"Candidate Resume:\n{resume_text}\n"
|
| 49 |
f"Target Role: {job_title}\n"
|
| 50 |
+
f"Generate 10 realistic interview questions based on this personβs resume, "
|
| 51 |
+
f"and for each question provide a coaching tip to help them answer effectively."
|
| 52 |
)
|
| 53 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 54 |
+
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 512, "temperature": 0.7}}
|
| 55 |
+
|
|
|
|
|
|
|
| 56 |
try:
|
| 57 |
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=60)
|
| 58 |
data = response.json()
|
| 59 |
+
if "error" in data:
|
| 60 |
return f"<div style='color:red;'>β API Error: {data['error']}</div>"
|
| 61 |
return data[0]["generated_text"].strip()
|
| 62 |
except Exception as e:
|
| 63 |
return f"<div style='color:red;'>β Exception: {str(e)}</div>"
|
| 64 |
|
| 65 |
+
# β
Format final output HTML
|
| 66 |
+
def render_output(name, job, status, queue, messages, model_message, show_lottie=True):
|
| 67 |
+
lottie_side = lottie_html() if show_lottie else ""
|
| 68 |
+
model_msg = f"<div style='margin-top:12px;font-size:0.9rem;color:#46a546;'>{model_message}</div>" if model_message else ""
|
| 69 |
+
left_panel = f"""
|
| 70 |
+
{lottie_side}
|
| 71 |
+
<div style='background:#f4f6fa;padding:18px;border-radius:16px;
|
| 72 |
+
max-width:340px;box-shadow:0 2px 14px #ccd4e6b3;font-family:sans-serif;'>
|
| 73 |
+
<div style='font-size:1.25rem;font-weight:700;'>π€ ROBOT RESUME ANALYZER</div>
|
| 74 |
+
<div style='margin-top:10px;'><b>Status:</b> <span style='color:#058c42'>{status}</span></div>
|
| 75 |
+
<div style='margin-top:10px;'><b>Current:</b> {name}</div>
|
| 76 |
+
<div><b>Position:</b> {job}</div>
|
| 77 |
+
<div style='margin-top:10px;'><b>Analysis Progress:</b></div>
|
| 78 |
+
<div style='font-size:2rem;'>π₯</div>
|
| 79 |
+
<div><b>QUEUE</b></div>
|
| 80 |
+
<div style='font-size:1.5rem;font-weight:700;'>{queue}</div>
|
| 81 |
+
<div><i>Analyzing Next...</i></div>
|
| 82 |
+
{model_msg}
|
| 83 |
+
</div>
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
return f"""
|
| 87 |
+
<div style='display:flex;gap:30px;'>
|
| 88 |
+
{left_panel}
|
| 89 |
+
<div style='max-width:620px;margin-top:15px;'>{messages}</div>
|
| 90 |
+
</div>
|
| 91 |
+
"""
|
| 92 |
|
| 93 |
+
# β
Main app logic with staged UI updates
|
| 94 |
def interface(pdf_file, job_title):
|
| 95 |
is_ready, msg = check_api_ready()
|
| 96 |
if not is_ready:
|
| 97 |
+
err_html = f"<div style='color:red;'><b>ERROR:</b> {msg}</div>"
|
| 98 |
+
return render_output("---", "---", "Unavailable", 0, err_html, msg, show_lottie=False)
|
| 99 |
|
| 100 |
if not pdf_file:
|
| 101 |
+
return render_output("---", "---", "Waiting for PDF", 1, "<b>Please upload a PDF resume.</b>", msg)
|
| 102 |
+
|
| 103 |
if not job_title.strip():
|
| 104 |
+
name = pdf_file.name.replace('.pdf','')
|
| 105 |
+
return render_output(name, "---", "Waiting for Job Title", 1, "<b>Please enter a target job title.</b>", msg)
|
|
|
|
| 106 |
|
| 107 |
+
name = pdf_file.name.replace(".pdf", "")
|
| 108 |
try:
|
| 109 |
pdf_file.seek(0)
|
| 110 |
resume_text = extract_text_from_pdf(pdf_file)
|
| 111 |
if not resume_text:
|
| 112 |
+
raise Exception("PDF contains no readable text")
|
| 113 |
except Exception as e:
|
| 114 |
+
return render_output(name, job_title, "PDF Error", 1, f"<div style='color:red;'>Failed to extract PDF: {e}</div>", msg)
|
|
|
|
| 115 |
|
| 116 |
+
yield render_output(name, job_title, "Analyzing...", 1, "<i>Generating questions with AI...</i>", msg)
|
| 117 |
+
|
| 118 |
+
ai_output = ai_generate_questions(resume_text, job_title)
|
| 119 |
+
questions = [q.strip() for q in ai_output.split("\n") if q.strip()]
|
| 120 |
|
| 121 |
+
html = "<h3>β
Interview Questions & Coaching Tips</h3>"
|
|
|
|
|
|
|
| 122 |
for i, q in enumerate(questions[:10], 1):
|
| 123 |
+
html += f"<div style='background:#fff;padding:16px;border-radius:10px;margin-bottom:12px;box-shadow:0 2px 6px #ddd;'>"
|
| 124 |
+
html += f"<b>Q{i}:</b> {q}</div>"
|
| 125 |
|
| 126 |
+
yield render_output(name, job_title, "Done β
", 0, html, msg)
|
| 127 |
|
| 128 |
+
# β
Launch Gradio app
|
| 129 |
with gr.Interface(
|
| 130 |
fn=interface,
|
| 131 |
inputs=[
|
| 132 |
+
gr.File(label="Upload PDF Resume", type="binary", file_types=[".pdf"]),
|
| 133 |
+
gr.Textbox(label="Target Job Title", placeholder="e.g. Data Analyst"),
|
| 134 |
],
|
| 135 |
outputs=gr.HTML(),
|
|
|
|
|
|
|
| 136 |
allow_flagging="never",
|
| 137 |
+
title="π€ AI Resume Analyzer + Interview Coach (PDF + Lottie)",
|
| 138 |
+
live=False,
|
| 139 |
) as demo:
|
|
|
|
|
|
|
|
|
|
| 140 |
demo.launch()
|