Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
33 |
-
|
34 |
-
|
|
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
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 =
|
76 |
-
|
|
|
|
|
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))
|