-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
271 lines (241 loc) · 11.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from dataclasses import dataclass
import streamlit as st
import chromadb
import time
from langchain.memory import ConversationBufferMemory
from langchain.chains import RetrievalQA, ConversationChain
from langchain.prompts.prompt import PromptTemplate
from langchain_openai import OpenAIEmbeddings
from langchain_openai import ChatOpenAI
from prompts.prompts import Template
from typing import Literal
from langchain_community.vectorstores import Chroma
from langchain.retrievers import MergerRetriever
from langchain_text_splitters import CharacterTextSplitter
from PyPDF2 import PdfReader
import streamlit as st
from audio_recorder_streamlit import audio_recorder
from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
# api_key = os.getenv("OPENAI_API_KEY")
api_key = st.secrets['OPENAI_API_KEY']
def speech_to_text(audiofile):
client = OpenAI(api_key=api_key)
audio_file= open(audiofile, "rb")
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
return transcription.text
def text_to_speech(speech_file_path,chat_format):
client = OpenAI(api_key=api_key)
response = client.audio.speech.create(
model="tts-1",
voice="echo",
input=chat_format
)
response.stream_to_file(speech_file_path)
@dataclass
class Message:
"""Class for keeping track of interview history."""
origin: Literal["human", "ai"]
message: str
def jd_retrieval(jd):
"""create embeddings for job description"""
embeddings = OpenAIEmbeddings()
if "resumeEmbeddings" in st.session_state and "resumeTimestamp" in st.session_state:
# Check if the embeddings are not too old (e.g., 1 hour)
if time.time() - st.session_state.resumeTimestamp < 3600:
return st.session_state.resumeEmbeddings
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(chunk_size=100, chunk_overlap=0)
jd_splitted = text_splitter.split_text(jd)
chroma_jd = Chroma.from_texts(
jd_splitted,embeddings,
collection_metadata={"hnsw:space": "cosine"} # l2 is the default(persist_directory="vector_storage/resume_store")
)
retriever_jd = chroma_jd.as_retriever(search_type = "similarity", search_kwargs = {"k":1})
# Cache the embeddings and timestamp
st.session_state.resumeEmbeddings = retriever_jd
st.session_state.resumeTimestamp = time.time()
return retriever_jd
# passing resume then extract its embeddings and return into its retrieval format.
def resume_retrieval(resume):
'''Create embeddings for the resume'''
if "resumeEmbeddings" in st.session_state and "resumeTimestamp" in st.session_state:
# Check if the embeddings are not too old (e.g., 1 hour)
if time.time() - st.session_state.resumeTimestamp < 3600:
return st.session_state.resumeEmbeddings
embeddings = OpenAIEmbeddings()
pdf_reader = PdfReader(resume)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(chunk_size=100, chunk_overlap=0)
resume_splitted = text_splitter.split_text(text)
chroma_resume = Chroma.from_texts(
resume_splitted,embeddings,
collection_metadata={"hnsw:space": "cosine"} # l2 is the default (persist_directory="vector_storage/resume_store")
)
retriever_resume = chroma_resume.as_retriever(search_type = "similarity", search_kwargs = {"k":2})
# Cache the embeddings and timestamp
st.session_state.resumeEmbeddings = retriever_resume
st.session_state.resumeTimestamp = time.time()
return retriever_resume
def initialize_session_state_resume(input_text,resume):
# Check if resume and job description are not empty
if not resume or not input_text:
raise ValueError("Resume and job description text must not be empty.")
if "jdRetriever" not in st.session_state:
st.session_state.jdRetriever = jd_retrieval(input_text)
# convert resume to embeddings
if "resumeRetriever" not in st.session_state:
st.session_state.resumeRetriever = resume_retrieval(resume)
if 'merger' not in st.session_state:
st.session_state.merger = MergerRetriever(retrievers=[st.session_state.jdRetriever,st.session_state.resumeRetriever])
if "job_chain_type_kwargs" not in st.session_state:
interview_prompt = PromptTemplate(input_variables=["context","question"],
template=Template.jd_template)
st.session_state.job_chain_type_kwargs = {"prompt": interview_prompt}
if "resume_history" not in st.session_state:
st.session_state.resume_history = []
st.session_state.resume_history.append(Message(origin="ai", message="Hello, I am your interivewer today. I will ask you some questions regarding your resume and your experience. Please start by saying hello or introducing yourself. Note: The maximum length of your answer is 4097 tokens!"))
# token count
if "token_count" not in st.session_state:
st.session_state.token_count = 0
# memory buffer for resume screen
if "resume_memory" not in st.session_state:
st.session_state.resume_memory = ConversationBufferMemory()
#guideline for resume screen
if "resume_guideline" not in st.session_state:
llm = ChatOpenAI(
model_name = "gpt-4o",
temperature = 0.5,)
st.session_state.resume_guideline = RetrievalQA.from_chain_type(
llm=llm,
chain_type_kwargs=st.session_state.job_chain_type_kwargs,
chain_type='stuff',
retriever=st.session_state.merger,
memory = st.session_state.resume_memory).invoke("Create an interview guideline and prepare only two questions for each topic. Make sure the questions tests the knowledge")
# llm chain for resume screen
if "resume_screen" not in st.session_state:
llm = ChatOpenAI(
model_name="gpt-4o",
temperature=0.7, )
PROMPT = PromptTemplate(
input_variables=["history", "input"],
template= """I want you to act as an interviewer strictly following the guideline in the current conversation.
Ask me questions and wait for my answers like a human. Do not write explanations.
Candidate has no assess to the guideline.
Only ask one question at a time.
Do ask follow-up questions if you think it's necessary.
Do not ask the same question.
Do not repeat the question.
Candidate has no assess to the guideline.
You name is Intelligent HR.
I want you to only reply as an interviewer.
Do not write all the conversation at once.
Candiate has no assess to the guideline.
Current Conversation:
{history}
Candidate: {input}
AI: """)
st.session_state.resume_screen = ConversationChain(prompt=PROMPT, llm = llm, memory = st.session_state.resume_memory)
if "feedback" not in st.session_state:
llm = ChatOpenAI(
model_name = "gpt-4o",
temperature = 0.5,)
st.session_state.feedback = ConversationChain(
prompt=PromptTemplate(input_variables = ["history", "input"], template = Template.feedback_template),
llm=llm,
memory = st.session_state.resume_memory,
)
# function to define the feedback of the interview
def show_feedback():
if "feedback" in st.session_state:
feedback_response = st.session_state.feedback.invoke(
"please give evalution regarding the interview"
)
with st.expander("Evaluation"):
st.write(feedback_response)
# st.sidebar.success("Select a demo above.")
st.set_page_config(page_title="AI-hr")
st.title("Interview AI")
def main():
job_desc = st.text_area("Provide a brief description here", value=st.session_state.input_text if "input_text" in st.session_state else "")
if job_desc:
st.session_state.input_text = job_desc
st.success("Job description saved.")
resume = st.file_uploader("Upload your resume", type=["pdf"])
if resume is not None:
st.session_state.resume = resume
st.success("Resume uploaded successfully.")
# Submit button to start the interview simulation
button = st.button("Submit")
try:
if button or "resume" in st.session_state and "input_text" in st.session_state:
initialize_session_state_resume(st.session_state.input_text,st.session_state.resume)
for message in st.session_state.resume_history:
with st.chat_message(message.origin):
st.markdown(message.message)
agree = st.checkbox("Access the Voice Assistant")
if agree:
#record audio input
audio_bytes = audio_recorder(
pause_threshold=2.0, sample_rate=41_000,
text="",
recording_color="#fffff",
neutral_color="#6aa36f",
icon_name="microphone-lines",
icon_size="3x",
)
if audio_bytes:
audio_input = "chatbot/audiofile.wav"
with open(audio_input,"wb") as f:
f.write(audio_bytes)
#convert speech to text
user_input = speech_to_text(audio_input)
with st.chat_message("human"):
st.markdown(user_input)
st.session_state.resume_history.append(Message(origin="human", message=user_input))
st.session_state.token_count += len(user_input.split())
st.audio(audio_bytes, format="audio/wav")
# generate bot response
bot_response = st.session_state.resume_screen.run(user_input)
with st.chat_message("assistant"):
st.markdown(f"Bot: {bot_response}")
st.session_state.resume_history.append(Message(origin="ai", message=bot_response))
# convert text to speech
audio_output = "chatbot/audiofileout.wav"
text_to_speech(speech_file_path=audio_output,chat_format=bot_response)
st.audio(audio_output)
else:
# for message in st.session_state.resume_history:
# with st.chat_message(message.origin):
# st.markdown(message.message)
if user_input := st.chat_input("Chat with me!"):
# Display user message in chat message container
with st.chat_message("human"):
st.markdown(user_input)
# Add user message to chat history
st.session_state.resume_history.append(Message(origin="human", message=user_input))
st.session_state.token_count += len(user_input.split())
# Generate bot response
bot_response = st.session_state.resume_screen.run(user_input)
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(f"Bot: {bot_response}")
# Add assistant response to chat history
st.session_state.resume_history.append(Message(origin="ai", message=bot_response))
else:
st.warning("Please upload your resume and provide a job description before submitting.")
except AttributeError:
# raise "Please upload before "
pass
if st.button("Show feedback"):
show_feedback()
# call the function
if __name__ == "__main__":
main()