Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import logging
|
4 |
+
import dotenv
|
5 |
+
import yaml
|
6 |
+
import PyPDF2
|
7 |
+
from langchain_community.vectorstores import FAISS
|
8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
9 |
+
from langchain.prompts import PromptTemplate
|
10 |
+
from langchain.llms import HuggingFaceHub
|
11 |
+
import os
|
12 |
+
dotenv.load_dotenv()
|
13 |
+
|
14 |
+
# Load configuration from YAML
|
15 |
+
def load_config():
|
16 |
+
with open("config.yaml", "r") as f:
|
17 |
+
return yaml.safe_load(f)
|
18 |
+
|
19 |
+
config = load_config()
|
20 |
+
hf_token = os.getenv("Gem") # Store API token in .env
|
21 |
+
logging.basicConfig(level=logging.INFO)
|
22 |
+
|
23 |
+
# Load embedding model
|
24 |
+
embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
|
25 |
+
|
26 |
+
# Extract text from PDFs
|
27 |
+
def extract_text_from_pdf(file):
|
28 |
+
reader = PyPDF2.PdfReader(file)
|
29 |
+
text = ""
|
30 |
+
for page in reader.pages:
|
31 |
+
text += page.extract_text() or ""
|
32 |
+
return text.strip()
|
33 |
+
|
34 |
+
# Get interview questions and assess responses
|
35 |
+
def get_interview_response(jd_text, resume_text, candidate_response=None):
|
36 |
+
prompt_template = """
|
37 |
+
You are an AI interviewer assessing a candidate for a job role.
|
38 |
+
|
39 |
+
JOB DESCRIPTION:
|
40 |
+
{jd_text}
|
41 |
+
|
42 |
+
CANDIDATE PROFILE:
|
43 |
+
{resume_text}
|
44 |
+
|
45 |
+
1. Start by asking an **introductory question**: "Tell me about yourself."
|
46 |
+
2. Then, based on the job description, ask a **technical question**.
|
47 |
+
3. If the candidate has already responded, evaluate their answer and provide constructive feedback.
|
48 |
+
|
49 |
+
Maintain a professional yet friendly tone.
|
50 |
+
"""
|
51 |
+
|
52 |
+
prompt = PromptTemplate(
|
53 |
+
input_variables=["jd_text", "resume_text"],
|
54 |
+
template=prompt_template
|
55 |
+
).format(jd_text=jd_text, resume_text=resume_text)
|
56 |
+
|
57 |
+
if candidate_response:
|
58 |
+
prompt += f"\n\nCANDIDATE RESPONSE: {candidate_response}\n\nAssess the response and provide feedback."
|
59 |
+
|
60 |
+
llm = HuggingFaceHub(
|
61 |
+
repo_id=config["model_name"],
|
62 |
+
model_kwargs={"temperature": config["temperature"], "max_length": 200},
|
63 |
+
huggingfacehub_api_token=hf_token
|
64 |
+
)
|
65 |
+
|
66 |
+
return llm(prompt).strip()
|
67 |
+
|
68 |
+
# Streamlit UI
|
69 |
+
st.set_page_config(page_title="AI Interviewer", layout="centered")
|
70 |
+
|
71 |
+
st.title("🤖 AI Interview Chatbot")
|
72 |
+
st.write("Upload a Job Description and Resume to start the interview.")
|
73 |
+
|
74 |
+
jd_file = st.file_uploader("Upload Job Description (PDF)", type=["pdf"])
|
75 |
+
resume_file = st.file_uploader("Upload Candidate Resume (PDF)", type=["pdf"])
|
76 |
+
|
77 |
+
if jd_file and resume_file:
|
78 |
+
jd_text = extract_text_from_pdf(jd_file)
|
79 |
+
resume_text = extract_text_from_pdf(resume_file)
|
80 |
+
|
81 |
+
if "interview_history" not in st.session_state:
|
82 |
+
st.session_state["interview_history"] = []
|
83 |
+
first_question = get_interview_response(jd_text, resume_text)
|
84 |
+
st.session_state["interview_history"].append(("AI", first_question))
|
85 |
+
|
86 |
+
for role, msg in st.session_state["interview_history"]:
|
87 |
+
st.chat_message(role).write(msg)
|
88 |
+
|
89 |
+
query = st.chat_input("Your Response:")
|
90 |
+
|
91 |
+
if query:
|
92 |
+
response = get_interview_response(jd_text, resume_text, query)
|
93 |
+
st.session_state["interview_history"].append(("You", query))
|
94 |
+
st.session_state["interview_history"].append(("AI", response))
|
95 |
+
st.rerun()
|