Spaces:
Running
Running
feat: added a wait timer
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import aiohttp
|
3 |
import asyncio
|
@@ -58,7 +60,6 @@ async def chat_with_server(chat_history, api_key, base_url, selected_type, selec
|
|
58 |
if assistant_msg is not None and not assistant_msg.startswith("Assistant is thinking"):
|
59 |
api_messages.append({'role': 'assistant', 'content': assistant_msg})
|
60 |
|
61 |
-
# Use custom_model if selected_type is 'custom' or 'open_router', otherwise use selected_model
|
62 |
model = custom_model if selected_type in ['custom', 'open_router'] else selected_model
|
63 |
|
64 |
payload = {
|
@@ -71,35 +72,36 @@ async def chat_with_server(chat_history, api_key, base_url, selected_type, selec
|
|
71 |
'stream': True,
|
72 |
}
|
73 |
|
74 |
-
|
|
|
75 |
if chat_history:
|
76 |
-
chat_history[-1][1] = "Assistant is thinking"
|
77 |
yield chat_history
|
78 |
|
79 |
async with aiohttp.ClientSession() as session:
|
80 |
try:
|
81 |
-
# Start the server request asynchronously
|
82 |
response_task = asyncio.create_task(session.post(
|
83 |
f'{api_url}/v1/chat/completions',
|
84 |
headers=headers,
|
85 |
json=payload,
|
86 |
-
timeout=
|
87 |
))
|
88 |
|
89 |
-
# Typing animation variables
|
90 |
dots_count = 0
|
91 |
assistant_message = ''
|
92 |
|
93 |
while not response_task.done():
|
94 |
-
|
|
|
|
|
|
|
95 |
dots = '.' * ((dots_count % 3) + 1)
|
96 |
dots_count += 1
|
97 |
if chat_history:
|
98 |
-
chat_history[-1][1] = f"Assistant is thinking{dots}"
|
99 |
yield chat_history
|
100 |
-
await asyncio.sleep(0.5)
|
101 |
|
102 |
-
# Now get the response
|
103 |
response = await response_task
|
104 |
if response.status != 200:
|
105 |
error_message = f"Error {response.status}: {await response.text()}"
|
@@ -108,7 +110,6 @@ async def chat_with_server(chat_history, api_key, base_url, selected_type, selec
|
|
108 |
return
|
109 |
|
110 |
assistant_message = ''
|
111 |
-
# Read the streaming response
|
112 |
async for line in response.content:
|
113 |
line = line.decode('utf-8').strip()
|
114 |
if line == '':
|
@@ -124,10 +125,8 @@ async def chat_with_server(chat_history, api_key, base_url, selected_type, selec
|
|
124 |
if 'content' in delta:
|
125 |
content = delta['content']
|
126 |
assistant_message += content
|
127 |
-
# Update the assistant's message in the chat
|
128 |
chat_history[-1][1] = assistant_message
|
129 |
yield chat_history
|
130 |
-
# Final update to the conversation
|
131 |
yield chat_history
|
132 |
|
133 |
except Exception as e:
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
import gradio as gr
|
4 |
import aiohttp
|
5 |
import asyncio
|
|
|
60 |
if assistant_msg is not None and not assistant_msg.startswith("Assistant is thinking"):
|
61 |
api_messages.append({'role': 'assistant', 'content': assistant_msg})
|
62 |
|
|
|
63 |
model = custom_model if selected_type in ['custom', 'open_router'] else selected_model
|
64 |
|
65 |
payload = {
|
|
|
72 |
'stream': True,
|
73 |
}
|
74 |
|
75 |
+
start_time = time.time()
|
76 |
+
|
77 |
if chat_history:
|
78 |
+
chat_history[-1][1] = "Assistant is thinking (0 sec)"
|
79 |
yield chat_history
|
80 |
|
81 |
async with aiohttp.ClientSession() as session:
|
82 |
try:
|
|
|
83 |
response_task = asyncio.create_task(session.post(
|
84 |
f'{api_url}/v1/chat/completions',
|
85 |
headers=headers,
|
86 |
json=payload,
|
87 |
+
timeout=1200
|
88 |
))
|
89 |
|
|
|
90 |
dots_count = 0
|
91 |
assistant_message = ''
|
92 |
|
93 |
while not response_task.done():
|
94 |
+
elapsed_time = time.time() - start_time
|
95 |
+
minutes, seconds = divmod(int(elapsed_time), 60)
|
96 |
+
time_str = f"{minutes} min {seconds} sec" if minutes > 0 else f"{seconds} sec"
|
97 |
+
|
98 |
dots = '.' * ((dots_count % 3) + 1)
|
99 |
dots_count += 1
|
100 |
if chat_history:
|
101 |
+
chat_history[-1][1] = f"Assistant is thinking ({time_str}){dots}"
|
102 |
yield chat_history
|
103 |
+
await asyncio.sleep(0.5)
|
104 |
|
|
|
105 |
response = await response_task
|
106 |
if response.status != 200:
|
107 |
error_message = f"Error {response.status}: {await response.text()}"
|
|
|
110 |
return
|
111 |
|
112 |
assistant_message = ''
|
|
|
113 |
async for line in response.content:
|
114 |
line = line.decode('utf-8').strip()
|
115 |
if line == '':
|
|
|
125 |
if 'content' in delta:
|
126 |
content = delta['content']
|
127 |
assistant_message += content
|
|
|
128 |
chat_history[-1][1] = assistant_message
|
129 |
yield chat_history
|
|
|
130 |
yield chat_history
|
131 |
|
132 |
except Exception as e:
|