Spaces:
Paused
Paused
Update frontend/app.py
Browse files- frontend/app.py +22 -4
frontend/app.py
CHANGED
|
@@ -6,6 +6,7 @@
|
|
| 6 |
|
| 7 |
import os
|
| 8 |
import chainlit as cl
|
|
|
|
| 9 |
import httpx
|
| 10 |
import asyncio
|
| 11 |
import json
|
|
@@ -200,14 +201,14 @@ def render_pipeline(fase_status: dict, detalhe: str = "", concluido: bool = Fals
|
|
| 200 |
# ──────────────────────────────────────────────────────────────────────
|
| 201 |
# STREAMING ASSÍNCRONO (usa BACKEND_API_URL)
|
| 202 |
# ──────────────────────────────────────────────────────────────────────
|
| 203 |
-
async def stream_eventos(pergunta: str, session_id: str) -> AsyncGenerator[dict, None]:
|
| 204 |
try:
|
| 205 |
async with httpx.AsyncClient(
|
| 206 |
timeout=httpx.Timeout(300.0, connect=5.0)
|
| 207 |
) as client:
|
| 208 |
async with client.stream(
|
| 209 |
"POST", f"{BACKEND_API_URL}/chat",
|
| 210 |
-
json={"content": pergunta, "session_id": session_id},
|
| 211 |
headers={"Accept": "application/x-ndjson", **headers},
|
| 212 |
) as resp:
|
| 213 |
if resp.status_code != 200:
|
|
@@ -730,6 +731,17 @@ async def on_download_pdf(action: cl.Action):
|
|
| 730 |
|
| 731 |
@cl.on_chat_start
|
| 732 |
async def start():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
if cl.user_session.get("_welcomed"):
|
| 734 |
return
|
| 735 |
cl.user_session.set("_welcomed", True)
|
|
@@ -782,9 +794,15 @@ async def start():
|
|
| 782 |
|
| 783 |
|
| 784 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
@cl.on_message
|
| 786 |
async def main(message: cl.Message):
|
| 787 |
-
session_id
|
|
|
|
| 788 |
|
| 789 |
welcome_msg = cl.user_session.get("welcome_msg")
|
| 790 |
if welcome_msg:
|
|
@@ -823,7 +841,7 @@ async def main(message: cl.Message):
|
|
| 823 |
pipeline_step.input = message.content
|
| 824 |
pipeline_step.output = render_pipeline(fase_status, ultimo_detalhe)
|
| 825 |
|
| 826 |
-
async for evento in stream_eventos(message.content, session_id):
|
| 827 |
tipo = evento.get("type", "")
|
| 828 |
|
| 829 |
if tipo == "status":
|
|
|
|
| 6 |
|
| 7 |
import os
|
| 8 |
import chainlit as cl
|
| 9 |
+
from chainlit.input_widget import Switch
|
| 10 |
import httpx
|
| 11 |
import asyncio
|
| 12 |
import json
|
|
|
|
| 201 |
# ──────────────────────────────────────────────────────────────────────
|
| 202 |
# STREAMING ASSÍNCRONO (usa BACKEND_API_URL)
|
| 203 |
# ──────────────────────────────────────────────────────────────────────
|
| 204 |
+
async def stream_eventos(pergunta: str, session_id: str, deep_search: bool = False) -> AsyncGenerator[dict, None]:
|
| 205 |
try:
|
| 206 |
async with httpx.AsyncClient(
|
| 207 |
timeout=httpx.Timeout(300.0, connect=5.0)
|
| 208 |
) as client:
|
| 209 |
async with client.stream(
|
| 210 |
"POST", f"{BACKEND_API_URL}/chat",
|
| 211 |
+
json={"content": pergunta, "session_id": session_id, "deep_search": deep_search},
|
| 212 |
headers={"Accept": "application/x-ndjson", **headers},
|
| 213 |
) as resp:
|
| 214 |
if resp.status_code != 200:
|
|
|
|
| 731 |
|
| 732 |
@cl.on_chat_start
|
| 733 |
async def start():
|
| 734 |
+
await cl.ChatSettings(
|
| 735 |
+
[
|
| 736 |
+
Switch(
|
| 737 |
+
id="busca_profunda",
|
| 738 |
+
label="🔍 Busca Profunda",
|
| 739 |
+
description="Ativa análise mais detalhada (mais lenta)",
|
| 740 |
+
initial=False,
|
| 741 |
+
)
|
| 742 |
+
]
|
| 743 |
+
).send()
|
| 744 |
+
|
| 745 |
if cl.user_session.get("_welcomed"):
|
| 746 |
return
|
| 747 |
cl.user_session.set("_welcomed", True)
|
|
|
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
+
@cl.on_settings_update
|
| 798 |
+
async def on_settings_update(settings: dict):
|
| 799 |
+
cl.user_session.set("busca_profunda", settings.get("busca_profunda", False))
|
| 800 |
+
|
| 801 |
+
|
| 802 |
@cl.on_message
|
| 803 |
async def main(message: cl.Message):
|
| 804 |
+
session_id = cl.user_session.get("id") or "default"
|
| 805 |
+
busca_profunda = cl.user_session.get("busca_profunda", False)
|
| 806 |
|
| 807 |
welcome_msg = cl.user_session.get("welcome_msg")
|
| 808 |
if welcome_msg:
|
|
|
|
| 841 |
pipeline_step.input = message.content
|
| 842 |
pipeline_step.output = render_pipeline(fase_status, ultimo_detalhe)
|
| 843 |
|
| 844 |
+
async for evento in stream_eventos(message.content, session_id, busca_profunda):
|
| 845 |
tipo = evento.get("type", "")
|
| 846 |
|
| 847 |
if tipo == "status":
|