Letsch22 commited on
Commit
9850e12
·
1 Parent(s): f31e6b4

Clear thread functionality

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- import urllib
3
  import urllib.request
4
  from time import sleep
5
  from typing import Dict, List, Generator
@@ -15,43 +14,38 @@ class MockInterviewer:
15
  def __init__(self) -> None:
16
  self._client = openai.OpenAI(api_key=os.environ['OPENAI_API_KEY'])
17
  self._assistant_id_cache: Dict[str, str] = {}
 
18
 
19
  def chat(self, usr_message: Dict, history: List[List], job_role: str, company: str) -> Generator:
20
- print('Started function')
21
  self._validate_fields(job_role, company)
22
-
23
- thread = self._client.beta.threads.create()
24
- user_input = usr_message.get('text')
25
-
26
  assistant_id = self._init_assistant(job_role, company)
 
27
 
28
- # Add the user's message to the thread
29
- self._client.beta.threads.messages.create(thread_id=thread.id,
30
- role="user",
31
- content=user_input)
32
- print('Client made')
33
- # Run the Assistant
34
- run = self._client.beta.threads.runs.create(thread_id=thread.id,
35
- assistant_id=assistant_id)
36
  print('Run created')
37
 
38
  # Check if the Run requires action (function call)
39
  while True:
40
- run_status = self._client.beta.threads.runs.retrieve(thread_id=thread.id,
41
- run_id=run.id)
42
- print(f"Run status: {run_status.status}")
43
  if run_status.status == 'completed':
44
  break
45
 
46
  sleep(1) # Wait for a second before checking again
47
 
48
  # Retrieve and return the latest message from the assistant
49
- messages = self._client.beta.threads.messages.list(thread_id=thread.id)
50
  response = messages.data[0].content[0].text.value
51
-
52
- print(f"Assistant response: {response}") # Debugging line
53
- #return json.dumps({"response": response})
54
- yield response
55
 
56
  def _validate_fields(self, job_role: str, company: str) -> None:
57
  if not job_role and not company:
@@ -105,7 +99,7 @@ class MockInterviewer:
105
  with gr.Blocks() as demo:
106
  mock_interviewer = MockInterviewer()
107
 
108
- chatbot = gr.ChatInterface(
109
  mock_interviewer.chat,
110
  additional_inputs=[
111
  gr.Textbox(label='Job Role', placeholder='Product Manager'),
@@ -116,6 +110,9 @@ with gr.Blocks() as demo:
116
  multimodal=True,
117
  retry_btn=None,
118
  undo_btn=None).queue()
 
 
 
119
 
120
  if __name__ == '__main__':
121
  demo.launch().queue()
 
1
  import os
 
2
  import urllib.request
3
  from time import sleep
4
  from typing import Dict, List, Generator
 
14
  def __init__(self) -> None:
15
  self._client = openai.OpenAI(api_key=os.environ['OPENAI_API_KEY'])
16
  self._assistant_id_cache: Dict[str, str] = {}
17
+ self.clear_thread()
18
 
19
  def chat(self, usr_message: Dict, history: List[List], job_role: str, company: str) -> Generator:
20
+ print('Started chat')
21
  self._validate_fields(job_role, company)
 
 
 
 
22
  assistant_id = self._init_assistant(job_role, company)
23
+ yield self._send_message(usr_message.get('text'), assistant_id)
24
 
25
+ def clear_thread(self) -> None:
26
+ print('Initializing new thread')
27
+ self._thread = self._client.beta.threads.create()
28
+
29
+ def _send_message(self, message: str, assistant_id: str) -> str:
30
+ self._client.beta.threads.messages.create(thread_id=self._thread.id, role='user', content=message)
31
+ print('Message created')
32
+ run = self._client.beta.threads.runs.create(thread_id=self._thread.id, assistant_id=assistant_id)
33
  print('Run created')
34
 
35
  # Check if the Run requires action (function call)
36
  while True:
37
+ run_status = self._client.beta.threads.runs.retrieve(thread_id=self._thread.id, run_id=run.id)
38
+ print(f'Run status: {run_status.status}')
 
39
  if run_status.status == 'completed':
40
  break
41
 
42
  sleep(1) # Wait for a second before checking again
43
 
44
  # Retrieve and return the latest message from the assistant
45
+ messages = self._client.beta.threads.messages.list(thread_id=self._thread.id)
46
  response = messages.data[0].content[0].text.value
47
+ print(f'Assistant response: {response}')
48
+ return response
 
 
49
 
50
  def _validate_fields(self, job_role: str, company: str) -> None:
51
  if not job_role and not company:
 
99
  with gr.Blocks() as demo:
100
  mock_interviewer = MockInterviewer()
101
 
102
+ chat_interface = gr.ChatInterface(
103
  mock_interviewer.chat,
104
  additional_inputs=[
105
  gr.Textbox(label='Job Role', placeholder='Product Manager'),
 
110
  multimodal=True,
111
  retry_btn=None,
112
  undo_btn=None).queue()
113
+
114
+ chat_interface.load(mock_interviewer.clear_thread)
115
+ chat_interface.clear_btn.click(mock_interviewer.clear_thread)
116
 
117
  if __name__ == '__main__':
118
  demo.launch().queue()