Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,28 +26,11 @@ st.markdown("""
|
|
| 26 |
border: 1px solid #e1e4e8;
|
| 27 |
border-radius: 6px;
|
| 28 |
padding: 16px;
|
| 29 |
-
|
| 30 |
}
|
| 31 |
.stCodeBlock pre {
|
| 32 |
margin: 0;
|
| 33 |
padding: 0;
|
| 34 |
-
white-space: pre-wrap;
|
| 35 |
-
word-break: break-word;
|
| 36 |
-
}
|
| 37 |
-
.copyButton {
|
| 38 |
-
position: absolute;
|
| 39 |
-
top: 5px;
|
| 40 |
-
right: 5px;
|
| 41 |
-
padding: 5px 10px;
|
| 42 |
-
background-color: #0366d6;
|
| 43 |
-
color: white;
|
| 44 |
-
border: none;
|
| 45 |
-
border-radius: 3px;
|
| 46 |
-
cursor: pointer;
|
| 47 |
-
font-size: 12px;
|
| 48 |
-
}
|
| 49 |
-
.copyButton:hover {
|
| 50 |
-
background-color: #0056b3;
|
| 51 |
}
|
| 52 |
code {
|
| 53 |
padding: 2px 4px;
|
|
@@ -391,34 +374,16 @@ else:
|
|
| 391 |
)
|
| 392 |
|
| 393 |
def format_ai_response(response):
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
code = re.sub(r'^(python|typescript|javascript)\n', '', code, flags=re.IGNORECASE)
|
| 397 |
-
return f'<div class="stCodeBlock"><button class="copyButton" onclick="copyCode(this)">Copy</button><pre><code>{code}</code></pre></div>'
|
| 398 |
-
|
| 399 |
-
formatted_response = re.sub(r'```(.*?)```', replace_code_block, response, flags=re.DOTALL)
|
| 400 |
-
formatted_response = re.sub(r'`([^`\n]+)`', r'<code>\1</code>', formatted_response)
|
| 401 |
|
| 402 |
-
#
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
range.selectNode(code);
|
| 410 |
-
window.getSelection().removeAllRanges();
|
| 411 |
-
window.getSelection().addRange(range);
|
| 412 |
-
document.execCommand('copy');
|
| 413 |
-
window.getSelection().removeAllRanges();
|
| 414 |
-
button.textContent = 'Copied!';
|
| 415 |
-
setTimeout(() => {
|
| 416 |
-
button.textContent = 'Copy';
|
| 417 |
-
}, 2000);
|
| 418 |
-
}
|
| 419 |
-
</script>
|
| 420 |
-
"""
|
| 421 |
-
return js + formatted_response
|
| 422 |
|
| 423 |
async def run_github_editor(query: str, thread_id: str = "default"):
|
| 424 |
inputs = {"messages": [HumanMessage(content=query)]}
|
|
@@ -429,37 +394,35 @@ else:
|
|
| 429 |
|
| 430 |
st.write(f"Human: {query}\n")
|
| 431 |
|
| 432 |
-
current_thought = ""
|
| 433 |
full_response = ""
|
|
|
|
| 434 |
|
| 435 |
graph = task_graph if mode == "Task" else qa_graph
|
| 436 |
|
| 437 |
async for event in graph.astream_events(inputs, config=config, version="v2"):
|
| 438 |
kind = event["event"]
|
| 439 |
if kind == "on_chat_model_start":
|
| 440 |
-
|
| 441 |
elif kind == "on_chat_model_stream":
|
| 442 |
data = event["data"]
|
| 443 |
if data["chunk"].content:
|
| 444 |
content = data["chunk"].content
|
| 445 |
if isinstance(content, list) and content and isinstance(content[0], dict):
|
| 446 |
text = content[0].get('text', '')
|
| 447 |
-
current_thought += text
|
| 448 |
full_response += text
|
| 449 |
-
if text.endswith(('.', '?', '!')):
|
| 450 |
-
st.write(current_thought.strip())
|
| 451 |
-
current_thought = ""
|
| 452 |
else:
|
| 453 |
full_response += content
|
| 454 |
-
|
|
|
|
|
|
|
| 455 |
elif kind == "on_tool_start" and mode == "Task":
|
| 456 |
-
|
| 457 |
elif kind == "on_tool_end" and mode == "Task":
|
| 458 |
-
|
| 459 |
|
| 460 |
-
#
|
| 461 |
-
|
| 462 |
-
|
| 463 |
|
| 464 |
# Create a session state variable to store the chat messages. This ensures that the
|
| 465 |
# messages persist across reruns.
|
|
|
|
| 26 |
border: 1px solid #e1e4e8;
|
| 27 |
border-radius: 6px;
|
| 28 |
padding: 16px;
|
| 29 |
+
margin-bottom: 16px;
|
| 30 |
}
|
| 31 |
.stCodeBlock pre {
|
| 32 |
margin: 0;
|
| 33 |
padding: 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
code {
|
| 36 |
padding: 2px 4px;
|
|
|
|
| 374 |
)
|
| 375 |
|
| 376 |
def format_ai_response(response):
|
| 377 |
+
# Remove any existing HTML tags that might interfere with markdown rendering
|
| 378 |
+
response = re.sub(r'<[^>]+>', '', response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
+
# Ensure code blocks are properly formatted for markdown
|
| 381 |
+
response = re.sub(r'```(\w+)?\n(.*?)```', r'```\1\n\2\n```', response, flags=re.DOTALL)
|
| 382 |
+
|
| 383 |
+
# Ensure inline code is properly formatted
|
| 384 |
+
response = re.sub(r'`([^`\n]+)`', r'`\1`', response)
|
| 385 |
+
|
| 386 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
async def run_github_editor(query: str, thread_id: str = "default"):
|
| 389 |
inputs = {"messages": [HumanMessage(content=query)]}
|
|
|
|
| 394 |
|
| 395 |
st.write(f"Human: {query}\n")
|
| 396 |
|
|
|
|
| 397 |
full_response = ""
|
| 398 |
+
response_container = st.empty()
|
| 399 |
|
| 400 |
graph = task_graph if mode == "Task" else qa_graph
|
| 401 |
|
| 402 |
async for event in graph.astream_events(inputs, config=config, version="v2"):
|
| 403 |
kind = event["event"]
|
| 404 |
if kind == "on_chat_model_start":
|
| 405 |
+
response_container.write("AI is thinking...")
|
| 406 |
elif kind == "on_chat_model_stream":
|
| 407 |
data = event["data"]
|
| 408 |
if data["chunk"].content:
|
| 409 |
content = data["chunk"].content
|
| 410 |
if isinstance(content, list) and content and isinstance(content[0], dict):
|
| 411 |
text = content[0].get('text', '')
|
|
|
|
| 412 |
full_response += text
|
|
|
|
|
|
|
|
|
|
| 413 |
else:
|
| 414 |
full_response += content
|
| 415 |
+
|
| 416 |
+
formatted_response = format_ai_response(full_response)
|
| 417 |
+
response_container.markdown(formatted_response)
|
| 418 |
elif kind == "on_tool_start" and mode == "Task":
|
| 419 |
+
response_container.write(f"\nUsing tool: {event['name']}")
|
| 420 |
elif kind == "on_tool_end" and mode == "Task":
|
| 421 |
+
response_container.write(f"Tool result: {event['data']['output']}\n")
|
| 422 |
|
| 423 |
+
# Final formatted response
|
| 424 |
+
final_formatted_response = format_ai_response(full_response)
|
| 425 |
+
response_container.markdown(final_formatted_response)
|
| 426 |
|
| 427 |
# Create a session state variable to store the chat messages. This ensures that the
|
| 428 |
# messages persist across reruns.
|