Letsch22 commited on
Commit
ebd0b81
·
1 Parent(s): cb12085
Files changed (2) hide show
  1. .env.example +1 -0
  2. 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
- client = openai.OpenAI(api_key=os.environ['OPENAI_API_KEY'])
13
-
14
- def create_files(company):
15
- if company.lower() == 'amazon':
16
- url = 'https://www.aboutamazon.com/about-us/leadership-principles'
17
- filename = 'leadership_principles.html'
18
- else:
19
- return []
20
-
21
- filename, headers = urllib.request.urlretrieve(url, filename)
22
- with open(filename, 'rb') as file:
23
- assistant_file = client.files.create(file=file, purpose='assistants')
24
- file_ids = [assistant_file.id]
25
- os.remove(filename)
26
- return file_ids
27
-
28
- # Assistant Creation function
29
- def create_assistant(job_role, company):
30
- file_ids = create_files(company)
31
-
32
- assistant = client.beta.assistants.create(
33
- name='Mock Interviewer',
34
- 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.',
35
- model='gpt-4-0125-preview',
36
- tools=[
37
- {
38
- 'type': 'retrieval' # This adds the knowledge base as a tool
39
- }
40
- ],
41
- file_ids=file_ids)
42
-
43
- return assistant.id
44
-
45
- def chat(usr_message, history, job_role, company):
46
- print('Started function')
47
- thread = client.beta.threads.create()
48
- user_input = usr_message
49
-
50
- # TODO: this creates the assistant every single chat interaction, need to
51
- # cache this if role/company haven't changed
52
- assistant_id = create_assistant(job_role, company)
53
-
54
- # Add the user's message to the thread
55
- client.beta.threads.messages.create(thread_id=thread.id,
56
- role="user",
57
- content=user_input)
58
- print('Client made')
59
- # Run the Assistant
60
- run = client.beta.threads.runs.create(thread_id=thread.id,
61
- assistant_id=assistant_id)
62
- print('Run created')
63
-
64
- # Check if the Run requires action (function call)
65
- while True:
66
- run_status = client.beta.threads.runs.retrieve(thread_id=thread.id,
67
- run_id=run.id)
68
- print(f"Run status: {run_status.status}")
69
- if run_status.status == 'completed':
70
- break
71
-
72
- sleep(1) # Wait for a second before checking again
73
-
74
- # Retrieve and return the latest message from the assistant
75
- messages = client.beta.threads.messages.list(thread_id=thread.id)
76
- response = messages.data[0].content[0].text.value
77
-
78
- print(f"Assistant response: {response}") # Debugging line
79
- #return json.dumps({"response": response})
80
- yield response
 
 
 
 
 
 
 
 
 
 
 
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
- theme='soft',
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()