Spaces:
Sleeping
Sleeping
Commit
Β·
85ac081
1
Parent(s):
ddc5c21
update the App.py file
Browse files
app.py
CHANGED
|
@@ -42,7 +42,13 @@ def can_view_analytics(role: str) -> bool:
|
|
| 42 |
def chat_with_agent(message, tenant_id, role, history):
|
| 43 |
"""
|
| 44 |
Send a message to the backend MCP agent and return the response.
|
| 45 |
-
Uses streaming for real-time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
Args:
|
| 48 |
message: User's message text
|
|
@@ -50,7 +56,7 @@ def chat_with_agent(message, tenant_id, role, history):
|
|
| 50 |
history: Chat history (Gradio messages format)
|
| 51 |
|
| 52 |
Yields:
|
| 53 |
-
Updated chat history with agent response (streaming)
|
| 54 |
"""
|
| 55 |
if not message or not message.strip():
|
| 56 |
yield history
|
|
@@ -101,7 +107,8 @@ def chat_with_agent(message, tenant_id, role, history):
|
|
| 101 |
history.append({"role": "assistant", "content": assistant_message})
|
| 102 |
yield history # Yield initial empty message
|
| 103 |
|
| 104 |
-
# Stream tokens -
|
|
|
|
| 105 |
for line_bytes in response.iter_lines():
|
| 106 |
if line_bytes:
|
| 107 |
try:
|
|
@@ -123,13 +130,13 @@ def chat_with_agent(message, tenant_id, role, history):
|
|
| 123 |
yield history
|
| 124 |
continue
|
| 125 |
|
| 126 |
-
# Handle tokens
|
| 127 |
token = data.get('token', '')
|
| 128 |
if token:
|
| 129 |
assistant_message += token
|
| 130 |
# Update the last message in history
|
| 131 |
history[-1] = {"role": "assistant", "content": assistant_message}
|
| 132 |
-
yield history # Yield updated history immediately
|
| 133 |
|
| 134 |
if data.get('done', False):
|
| 135 |
break
|
|
@@ -1452,16 +1459,15 @@ with gr.Blocks(
|
|
| 1452 |
box-shadow: 0 8px 26px rgba(15, 23, 42, 0.9);
|
| 1453 |
display: flex;
|
| 1454 |
flex-direction: column;
|
| 1455 |
-
gap: 10px;
|
| 1456 |
transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
|
| 1457 |
}
|
| 1458 |
|
| 1459 |
-
.tenant-card:hover,
|
| 1460 |
-
.role-card:hover {
|
| 1461 |
-
|
| 1462 |
-
|
| 1463 |
-
|
| 1464 |
-
}
|
| 1465 |
|
| 1466 |
.field-label-pill {
|
| 1467 |
display: inline-flex;
|
|
@@ -1483,10 +1489,21 @@ with gr.Blocks(
|
|
| 1483 |
}
|
| 1484 |
|
| 1485 |
.field-label-subtitle {
|
| 1486 |
-
margin-top:
|
|
|
|
| 1487 |
color: #9ca3af;
|
| 1488 |
font-size: 0.8rem;
|
| 1489 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1490 |
|
| 1491 |
/* Left/right columns in Chat tab */
|
| 1492 |
.chat-row > .col:nth-child(1) {
|
|
@@ -1650,6 +1667,19 @@ with gr.Blocks(
|
|
| 1650 |
box-shadow: 0 0 0 1px rgba(6, 182, 212, 0.65) !important;
|
| 1651 |
background: rgba(15, 23, 42, 1) !important;
|
| 1652 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1653 |
|
| 1654 |
/* Generic section card */
|
| 1655 |
.section-card {
|
|
@@ -1810,14 +1840,18 @@ with gr.Blocks(
|
|
| 1810 |
3. The MCP agent will automatically select tools (RAG, Web, etc.)
|
| 1811 |
|
| 1812 |
### β‘ Features
|
| 1813 |
-
- β¨ Real-time streaming responses
|
|
|
|
|
|
|
|
|
|
| 1814 |
- π§ Multi-step planning & reasoning
|
| 1815 |
- π Automatic tool selection with latency prediction
|
| 1816 |
- π§ Context-aware routing (intelligent tool skipping)
|
| 1817 |
- πΎ Conversation memory
|
| 1818 |
- π Reasoning visualization (see Debug tab)
|
| 1819 |
- β‘ Per-tool latency estimates (RAG: 60-120ms, Web: 400-1800ms)
|
| 1820 |
-
- π Schema-validated tool outputs
|
|
|
|
| 1821 |
"""
|
| 1822 |
)
|
| 1823 |
|
|
|
|
| 42 |
def chat_with_agent(message, tenant_id, role, history):
|
| 43 |
"""
|
| 44 |
Send a message to the backend MCP agent and return the response.
|
| 45 |
+
Uses streaming for real-time character-by-character updates for smooth UX.
|
| 46 |
+
|
| 47 |
+
Features:
|
| 48 |
+
- Character-by-character streaming for smooth animation
|
| 49 |
+
- Query caching for faster repeated queries
|
| 50 |
+
- Enhanced error handling with actionable messages
|
| 51 |
+
- Multi-query web search for better results
|
| 52 |
|
| 53 |
Args:
|
| 54 |
message: User's message text
|
|
|
|
| 56 |
history: Chat history (Gradio messages format)
|
| 57 |
|
| 58 |
Yields:
|
| 59 |
+
Updated chat history with agent response (streaming character-by-character)
|
| 60 |
"""
|
| 61 |
if not message or not message.strip():
|
| 62 |
yield history
|
|
|
|
| 107 |
history.append({"role": "assistant", "content": assistant_message})
|
| 108 |
yield history # Yield initial empty message
|
| 109 |
|
| 110 |
+
# Stream tokens character-by-character for smooth UX
|
| 111 |
+
# Backend now streams character-by-character instead of word-by-word
|
| 112 |
for line_bytes in response.iter_lines():
|
| 113 |
if line_bytes:
|
| 114 |
try:
|
|
|
|
| 130 |
yield history
|
| 131 |
continue
|
| 132 |
|
| 133 |
+
# Handle tokens (now character-by-character for smoother streaming)
|
| 134 |
token = data.get('token', '')
|
| 135 |
if token:
|
| 136 |
assistant_message += token
|
| 137 |
# Update the last message in history
|
| 138 |
history[-1] = {"role": "assistant", "content": assistant_message}
|
| 139 |
+
yield history # Yield updated history immediately for smooth character-by-character display
|
| 140 |
|
| 141 |
if data.get('done', False):
|
| 142 |
break
|
|
|
|
| 1459 |
box-shadow: 0 8px 26px rgba(15, 23, 42, 0.9);
|
| 1460 |
display: flex;
|
| 1461 |
flex-direction: column;
|
|
|
|
| 1462 |
transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
|
| 1463 |
}
|
| 1464 |
|
| 1465 |
+
# .tenant-card:hover,
|
| 1466 |
+
# .role-card:hover {
|
| 1467 |
+
# border-color: #38bdf8;
|
| 1468 |
+
# box-shadow: 0 12px 36px rgba(56, 189, 248, 0.35);
|
| 1469 |
+
# transform: translateY(-1px);
|
| 1470 |
+
# }
|
| 1471 |
|
| 1472 |
.field-label-pill {
|
| 1473 |
display: inline-flex;
|
|
|
|
| 1489 |
}
|
| 1490 |
|
| 1491 |
.field-label-subtitle {
|
| 1492 |
+
margin-top: 4px;
|
| 1493 |
+
margin-bottom: 4px;
|
| 1494 |
color: #9ca3af;
|
| 1495 |
font-size: 0.8rem;
|
| 1496 |
}
|
| 1497 |
+
|
| 1498 |
+
/* Reduce spacing for dropdown in role card */
|
| 1499 |
+
.role-card .field-label-subtitle {
|
| 1500 |
+
margin-bottom: 6px;
|
| 1501 |
+
}
|
| 1502 |
+
|
| 1503 |
+
.role-card select,
|
| 1504 |
+
.role-card .gradio-dropdown {
|
| 1505 |
+
margin-top: 2px;
|
| 1506 |
+
}
|
| 1507 |
|
| 1508 |
/* Left/right columns in Chat tab */
|
| 1509 |
.chat-row > .col:nth-child(1) {
|
|
|
|
| 1667 |
box-shadow: 0 0 0 1px rgba(6, 182, 212, 0.65) !important;
|
| 1668 |
background: rgba(15, 23, 42, 1) !important;
|
| 1669 |
}
|
| 1670 |
+
|
| 1671 |
+
/* Reduce spacing in dropdown menu items */
|
| 1672 |
+
.gradio-dropdown ul,
|
| 1673 |
+
.gradio-dropdown .dropdown-menu,
|
| 1674 |
+
select option {
|
| 1675 |
+
padding: 4px 8px !important;
|
| 1676 |
+
margin: 0 !important;
|
| 1677 |
+
}
|
| 1678 |
+
|
| 1679 |
+
/* Reduce gap between dropdown and label */
|
| 1680 |
+
.role-card .gradio-dropdown {
|
| 1681 |
+
margin-top: 4px !important;
|
| 1682 |
+
}
|
| 1683 |
|
| 1684 |
/* Generic section card */
|
| 1685 |
.section-card {
|
|
|
|
| 1840 |
3. The MCP agent will automatically select tools (RAG, Web, etc.)
|
| 1841 |
|
| 1842 |
### β‘ Features
|
| 1843 |
+
- β¨ Real-time character-by-character streaming responses
|
| 1844 |
+
- π Query caching for faster repeated queries
|
| 1845 |
+
- π Query expansion for ambiguous terms (AlβAI, MLβmachine learning)
|
| 1846 |
+
- π Multi-query web search with parallel execution
|
| 1847 |
- π§ Multi-step planning & reasoning
|
| 1848 |
- π Automatic tool selection with latency prediction
|
| 1849 |
- π§ Context-aware routing (intelligent tool skipping)
|
| 1850 |
- πΎ Conversation memory
|
| 1851 |
- π Reasoning visualization (see Debug tab)
|
| 1852 |
- β‘ Per-tool latency estimates (RAG: 60-120ms, Web: 400-1800ms)
|
| 1853 |
+
- π Schema-validated tool outputs
|
| 1854 |
+
- π‘οΈ Enhanced error handling with actionable messages
|
| 1855 |
"""
|
| 1856 |
)
|
| 1857 |
|