Spaces:
Runtime error
Runtime error
Refactor
Browse files- .env.example +1 -0
- app.py +86 -75
.env.example
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY=PASTE-KEY-HERE
|
app.py
CHANGED
|
@@ -9,91 +9,102 @@ from dotenv import load_dotenv
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def
|
| 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 |
# Creating the Gradio interface
|
| 83 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 84 |
chatbot = gr.ChatInterface(
|
| 85 |
-
chat,
|
| 86 |
additional_inputs=[
|
| 87 |
gr.Textbox(label='Job Role'),
|
| 88 |
gr.Textbox(label='Company')
|
| 89 |
],
|
| 90 |
-
title='I am your AI mock interviewer
|
| 91 |
description='Make your selections above to configure me.',
|
| 92 |
-
|
| 93 |
-
fill_height=True,
|
| 94 |
retry_btn=None,
|
| 95 |
-
undo_btn=None
|
| 96 |
-
clear_btn='Clear').queue()
|
| 97 |
|
| 98 |
if __name__ == '__main__':
|
| 99 |
demo.launch().queue()
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
+
class MockInterviewer:
|
| 13 |
+
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.client = openai.OpenAI(api_key=os.environ['OPENAI_API_KEY'])
|
| 16 |
+
self.job_role = ''
|
| 17 |
+
self.company = ''
|
| 18 |
+
self.assistant_id = ''
|
| 19 |
+
|
| 20 |
+
def create_files(self, company):
|
| 21 |
+
if company.lower() == 'amazon':
|
| 22 |
+
url = 'https://www.aboutamazon.com/about-us/leadership-principles'
|
| 23 |
+
filename = 'leadership_principles.html'
|
| 24 |
+
else:
|
| 25 |
+
return []
|
| 26 |
+
|
| 27 |
+
filename, headers = urllib.request.urlretrieve(url, filename)
|
| 28 |
+
with open(filename, 'rb') as file:
|
| 29 |
+
assistant_file = self.client.files.create(file=file, purpose='assistants')
|
| 30 |
+
file_ids = [assistant_file.id]
|
| 31 |
+
os.remove(filename)
|
| 32 |
+
return file_ids
|
| 33 |
+
|
| 34 |
+
def init_assistant(self, job_role, company):
|
| 35 |
+
if not job_role and not company:
|
| 36 |
+
raise gr.Error('Job Role and Company are requried fields.')
|
| 37 |
+
if not job_role:
|
| 38 |
+
raise gr.Error('Job Role is a required field.')
|
| 39 |
+
if not company:
|
| 40 |
+
raise gr.Error('Company is a required field.')
|
| 41 |
+
|
| 42 |
+
if job_role != self.job_role or company != self.company:
|
| 43 |
+
file_ids = self.create_files(company)
|
| 44 |
+
|
| 45 |
+
assistant = self.client.beta.assistants.create(
|
| 46 |
+
name='Mock Interviewer',
|
| 47 |
+
instructions=f'You are an AI mock interviewer for {job_role} roles at {company}. If you have been provided a file, use it as an interview guide.',
|
| 48 |
+
model='gpt-4-0125-preview',
|
| 49 |
+
tools=[
|
| 50 |
+
{
|
| 51 |
+
'type': 'retrieval' # This adds the knowledge base as a tool
|
| 52 |
+
}
|
| 53 |
+
],
|
| 54 |
+
file_ids=file_ids)
|
| 55 |
+
|
| 56 |
+
self.assistant_id = assistant.id
|
| 57 |
+
|
| 58 |
+
def chat(self, usr_message, history, job_role, company):
|
| 59 |
+
print('Started function')
|
| 60 |
+
thread = self.client.beta.threads.create()
|
| 61 |
+
user_input = usr_message['text']
|
| 62 |
+
|
| 63 |
+
self.init_assistant(job_role, company)
|
| 64 |
+
|
| 65 |
+
# Add the user's message to the thread
|
| 66 |
+
self.client.beta.threads.messages.create(thread_id=thread.id,
|
| 67 |
+
role="user",
|
| 68 |
+
content=user_input)
|
| 69 |
+
print('Client made')
|
| 70 |
+
# Run the Assistant
|
| 71 |
+
run = self.client.beta.threads.runs.create(thread_id=thread.id,
|
| 72 |
+
assistant_id=self.assistant_id)
|
| 73 |
+
print('Run created')
|
| 74 |
+
|
| 75 |
+
# Check if the Run requires action (function call)
|
| 76 |
+
while True:
|
| 77 |
+
run_status = self.client.beta.threads.runs.retrieve(thread_id=thread.id,
|
| 78 |
+
run_id=run.id)
|
| 79 |
+
print(f"Run status: {run_status.status}")
|
| 80 |
+
if run_status.status == 'completed':
|
| 81 |
+
break
|
| 82 |
+
|
| 83 |
+
sleep(1) # Wait for a second before checking again
|
| 84 |
+
|
| 85 |
+
# Retrieve and return the latest message from the assistant
|
| 86 |
+
messages = self.client.beta.threads.messages.list(thread_id=thread.id)
|
| 87 |
+
response = messages.data[0].content[0].text.value
|
| 88 |
+
|
| 89 |
+
print(f"Assistant response: {response}") # Debugging line
|
| 90 |
+
#return json.dumps({"response": response})
|
| 91 |
+
yield response
|
| 92 |
|
| 93 |
# Creating the Gradio interface
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
+
mock_interviewer = MockInterviewer()
|
| 96 |
+
|
| 97 |
chatbot = gr.ChatInterface(
|
| 98 |
+
mock_interviewer.chat,
|
| 99 |
additional_inputs=[
|
| 100 |
gr.Textbox(label='Job Role'),
|
| 101 |
gr.Textbox(label='Company')
|
| 102 |
],
|
| 103 |
+
title='I am your AI mock interviewer',
|
| 104 |
description='Make your selections above to configure me.',
|
| 105 |
+
multimodal=True,
|
|
|
|
| 106 |
retry_btn=None,
|
| 107 |
+
undo_btn=None).queue()
|
|
|
|
| 108 |
|
| 109 |
if __name__ == '__main__':
|
| 110 |
demo.launch().queue()
|