jeremierostan commited on
Commit
e10a598
·
verified ·
1 Parent(s): 6541b31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -27,24 +27,21 @@ def chat_with_assistant(message, history):
27
  role="user",
28
  content=message
29
  )
30
-
31
- # Run the assistant
32
- run = client.beta.threads.runs.create(
33
- thread_id=thread.id,
34
- assistant_id=assistant_id
 
35
  )
36
 
37
- # Wait for the assistant's response
38
- while True:
39
- run_status = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
40
- if run_status.status == 'completed':
41
- # Retrieve the assistant's response
42
- messages = client.beta.threads.messages.list(thread_id=thread.id)
43
- assistant_response = messages.data[0].content[0].text.value
44
- break
45
- time.sleep(1)
46
-
47
- return assistant_response
48
 
49
  # Custom CSS for chat bubbles and colors
50
  custom_css = """
@@ -72,12 +69,14 @@ with gr.Blocks(css=custom_css) as demo:
72
 
73
  def bot(history):
74
  user_message = history[-1][0]
75
- bot_message = chat_with_assistant(user_message, history)
76
- history[-1][1] = bot_message
 
 
77
  return history
78
 
79
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
80
  bot, chatbot, chatbot
81
  )
82
 
83
- demo.launch(auth=(username, password))
 
27
  role="user",
28
  content=message
29
  )
30
+
31
+ # Run the assistant with streaming
32
+ response = client.chat.completions.create(
33
+ model='gpt-4o-mini',
34
+ messages=[{'role': 'user', 'content': message}],
35
+ stream=True
36
  )
37
 
38
+ # Handle the streaming response
39
+ response_text = ""
40
+ for chunk in response:
41
+ delta = chunk['choices'][0]['delta']
42
+ if 'content' in delta:
43
+ response_text += delta['content']
44
+ yield response_text
 
 
 
 
45
 
46
  # Custom CSS for chat bubbles and colors
47
  custom_css = """
 
69
 
70
  def bot(history):
71
  user_message = history[-1][0]
72
+ bot_message = ""
73
+ for response_text in chat_with_assistant(user_message, history):
74
+ history[-1][1] = response_text
75
+ chatbot.update(history)
76
  return history
77
 
78
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
79
  bot, chatbot, chatbot
80
  )
81
 
82
+ demo.launch(auth=(username, password))