Update app.py
Browse files
app.py
CHANGED
|
@@ -291,15 +291,21 @@ class OpenAIApi:
|
|
| 291 |
stream=True
|
| 292 |
)
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
response_chunk_id = self.memory.add_chunk(full_response, "assistant")
|
| 305 |
logger.info(f"Received response for chunk: {response_chunk_id}, length: {len(full_response)}")
|
|
@@ -415,7 +421,7 @@ def create_ui():
|
|
| 415 |
|
| 416 |
with gr.Tab("Conversation History"):
|
| 417 |
history = gr.Dataframe(
|
| 418 |
-
label="
|
| 419 |
headers=["chunk_id", "text", "role", "timestamp", "intent", "token_count"],
|
| 420 |
datatype=["str", "str", "str", "str", "str", "number"]
|
| 421 |
)
|
|
@@ -440,20 +446,19 @@ def create_ui():
|
|
| 440 |
paste_btn = gr.Button("Paste")
|
| 441 |
prefix_btn = gr.Button("Add Prefix")
|
| 442 |
suffix_btn = gr.Button("Add Suffix")
|
| 443 |
-
|
| 444 |
|
| 445 |
-
cut_btn.click(fn=edit_cut, inputs=[chunk_id, start, end], outputs=[chunk_text,
|
| 446 |
-
copy_btn.click(fn=edit_copy, inputs=[chunk_id, start, end], outputs=[chunk_text,
|
| 447 |
-
paste_btn.click(fn=edit_paste, inputs=[chunk_id, position], outputs=[chunk_text,
|
| 448 |
-
prefix_btn.click(fn=edit_prefix, inputs=[chunk_id, prefix], outputs=[chunk_text,
|
| 449 |
-
suffix_btn.click(fn=edit_suffix, inputs=[chunk_id, suffix], outputs=[chunk_text,
|
| 450 |
|
| 451 |
with gr.Tab("Logs"):
|
| 452 |
-
logs = gr.Textbox(label="Application Logs",
|
| 453 |
-
logs_btn =
|
| 454 |
-
logs_btn.click(fn=get_logs, outputs=logs)
|
| 455 |
|
| 456 |
-
gr.Markdown(f"Current Time: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S
|
| 457 |
|
| 458 |
logger.info("Created Gradio UI")
|
| 459 |
return demo
|
|
|
|
| 291 |
stream=True
|
| 292 |
)
|
| 293 |
|
| 294 |
+
async def process_stream(sync_stream):
|
| 295 |
+
full_response = ""
|
| 296 |
+
tool_calls = []
|
| 297 |
+
for chunk in sync_stream:
|
| 298 |
+
logger.debug(f"Received chunk: {chunk}")
|
| 299 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 300 |
+
full_response += chunk.choices[0].delta.content
|
| 301 |
+
if chunk.choices and chunk.choices[0].delta.tool_calls:
|
| 302 |
+
tool_calls.extend(chunk.choices[0].delta.tool_calls)
|
| 303 |
+
return full_response, tool_calls
|
| 304 |
+
|
| 305 |
+
# Run synchronous stream processing in a separate thread
|
| 306 |
+
logger.debug("Processing stream in separate thread")
|
| 307 |
+
full_response, tool_calls = await asyncio.to_thread(process_stream, response)
|
| 308 |
+
logger.debug("Stream processing completed")
|
| 309 |
|
| 310 |
response_chunk_id = self.memory.add_chunk(full_response, "assistant")
|
| 311 |
logger.info(f"Received response for chunk: {response_chunk_id}, length: {len(full_response)}")
|
|
|
|
| 421 |
|
| 422 |
with gr.Tab("Conversation History"):
|
| 423 |
history = gr.Dataframe(
|
| 424 |
+
label="History",
|
| 425 |
headers=["chunk_id", "text", "role", "timestamp", "intent", "token_count"],
|
| 426 |
datatype=["str", "str", "str", "str", "str", "number"]
|
| 427 |
)
|
|
|
|
| 446 |
paste_btn = gr.Button("Paste")
|
| 447 |
prefix_btn = gr.Button("Add Prefix")
|
| 448 |
suffix_btn = gr.Button("Add Suffix")
|
| 449 |
+
diff = gr.Textbox(label="Diff Output", interactive=False)
|
| 450 |
|
| 451 |
+
cut_btn.click(fn=edit_cut, inputs=[chunk_id, start, end], outputs=[chunk_text, diff])
|
| 452 |
+
copy_btn.click(fn=edit_copy, inputs=[chunk_id, start, end], outputs=[chunk_text, diff])
|
| 453 |
+
paste_btn.click(fn=edit_paste, inputs=[chunk_id, position], outputs=[chunk_text, diff])
|
| 454 |
+
prefix_btn.click(fn=edit_prefix, inputs=[chunk_id, prefix], outputs=[chunk_text, diff])
|
| 455 |
+
suffix_btn.click(fn=edit_suffix, inputs=[chunk_id, suffix], outputs=[chunk_text, diff])
|
| 456 |
|
| 457 |
with gr.Tab("Logs"):
|
| 458 |
+
logs = gr.Textbox(label="Application Logs", placeholder="Logs are loading...")
|
| 459 |
+
logs_btn = click(fn=get_logs, outputs=[logs])
|
|
|
|
| 460 |
|
| 461 |
+
gr.Markdown(f"Current Time: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
| 462 |
|
| 463 |
logger.info("Created Gradio UI")
|
| 464 |
return demo
|