Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,7 @@ import uuid
|
|
| 3 |
from typing import Any, Dict, List, Optional
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
-
from
|
| 7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
|
| 10 |
from backend_app.flows import FlowManager
|
| 11 |
from backend_app.ingest import run_ingestion
|
|
@@ -23,49 +21,7 @@ if not (os.path.exists(FAISS_INDEX_PATH) and os.path.exists(DOCSTORE_PATH)):
|
|
| 23 |
rag = RAGEngineHF()
|
| 24 |
flows = FlowManager()
|
| 25 |
|
| 26 |
-
# ---------
|
| 27 |
-
api = FastAPI(title="SysLink Chat API")
|
| 28 |
-
|
| 29 |
-
api.add_middleware(
|
| 30 |
-
CORSMiddleware,
|
| 31 |
-
allow_origins=["*"], # keep open for testing; lock later
|
| 32 |
-
allow_methods=["*"],
|
| 33 |
-
allow_headers=["*"],
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
class ChatIn(BaseModel):
|
| 37 |
-
session_id: str
|
| 38 |
-
message: str
|
| 39 |
-
|
| 40 |
-
@api.get("/init")
|
| 41 |
-
def init() -> Dict[str, Any]:
|
| 42 |
-
return {
|
| 43 |
-
"bot_name": BOT_NAME,
|
| 44 |
-
"logo_url": BOT_LOGO_URL,
|
| 45 |
-
"welcome": BOT_WELCOME,
|
| 46 |
-
"suggestions": flows.default_suggestions(),
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
@api.post("/chat")
|
| 50 |
-
def chat(payload: ChatIn) -> Dict[str, Any]:
|
| 51 |
-
session_id = payload.session_id
|
| 52 |
-
user_text = (payload.message or "").strip()
|
| 53 |
-
|
| 54 |
-
flow_result = flows.handle_message(session_id=session_id, user_message=user_text)
|
| 55 |
-
|
| 56 |
-
if flow_result["action"] == "rag":
|
| 57 |
-
rag_result = rag.answer(user_text, preferred_lang=flow_result.get("lang"))
|
| 58 |
-
return {
|
| 59 |
-
"answer": rag_result.get("answer", ""),
|
| 60 |
-
"suggestions": flow_result.get("suggestions", []),
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
return {
|
| 64 |
-
"answer": flow_result.get("answer", ""),
|
| 65 |
-
"suggestions": flow_result.get("suggestions", []),
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
# --------- Gradio UI (optional, also hosted at /) ----------
|
| 69 |
def init_state():
|
| 70 |
return {"session_id": "sess-" + uuid.uuid4().hex[:12], "initialized": False}
|
| 71 |
|
|
@@ -81,7 +37,7 @@ def render_suggestions(btns: Optional[List[str]]):
|
|
| 81 |
|
| 82 |
def on_open(chat_history, state):
|
| 83 |
chat_history = chat_history or []
|
| 84 |
-
if not state
|
| 85 |
chat_history.append({"role": "assistant", "content": BOT_WELCOME})
|
| 86 |
state["initialized"] = True
|
| 87 |
return chat_history, state, *render_suggestions(flows.default_suggestions())
|
|
@@ -90,18 +46,30 @@ def handle_user_message(user_text, chat_history, state):
|
|
| 90 |
chat_history = chat_history or []
|
| 91 |
session_id = state["session_id"]
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
chat_history.append({"role": "user", "content": user_text})
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
bot_reply = rag_result.get("answer", "")
|
| 100 |
-
chat_history.append({"role": "assistant", "content": bot_reply})
|
| 101 |
return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
def suggestion_click(text, chat_history, state):
|
| 107 |
return handle_user_message(text, chat_history, state)
|
|
@@ -132,4 +100,5 @@ with gr.Blocks(title="SysLink Food System Chatbot") as demo:
|
|
| 132 |
for b in [s1, s2, s3, s4, s5, s6]:
|
| 133 |
b.click(suggestion_click, inputs=[b, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
| 3 |
from typing import Any, Dict, List, Optional
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
+
from pydantic import BaseModel # ok to keep even if unused
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from backend_app.flows import FlowManager
|
| 9 |
from backend_app.ingest import run_ingestion
|
|
|
|
| 21 |
rag = RAGEngineHF()
|
| 22 |
flows = FlowManager()
|
| 23 |
|
| 24 |
+
# --------- Gradio UI ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def init_state():
|
| 26 |
return {"session_id": "sess-" + uuid.uuid4().hex[:12], "initialized": False}
|
| 27 |
|
|
|
|
| 37 |
|
| 38 |
def on_open(chat_history, state):
|
| 39 |
chat_history = chat_history or []
|
| 40 |
+
if not state.get("initialized", False):
|
| 41 |
chat_history.append({"role": "assistant", "content": BOT_WELCOME})
|
| 42 |
state["initialized"] = True
|
| 43 |
return chat_history, state, *render_suggestions(flows.default_suggestions())
|
|
|
|
| 46 |
chat_history = chat_history or []
|
| 47 |
session_id = state["session_id"]
|
| 48 |
|
| 49 |
+
user_text = (user_text or "").strip()
|
| 50 |
+
if not user_text:
|
| 51 |
+
return "", chat_history, state, *render_suggestions([])
|
| 52 |
+
|
| 53 |
+
# add user message
|
| 54 |
chat_history.append({"role": "user", "content": user_text})
|
| 55 |
|
| 56 |
+
try:
|
| 57 |
+
flow_result = flows.handle_message(session_id=session_id, user_message=user_text)
|
| 58 |
+
|
| 59 |
+
# RAG path
|
| 60 |
+
if flow_result["action"] == "rag":
|
| 61 |
+
rag_result = rag.answer(user_text, preferred_lang=flow_result.get("lang"))
|
| 62 |
+
bot_reply = rag_result.get("answer", "")
|
| 63 |
+
chat_history.append({"role": "assistant", "content": bot_reply})
|
| 64 |
+
return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
|
| 65 |
|
| 66 |
+
# Flow-only reply
|
| 67 |
+
chat_history.append({"role": "assistant", "content": flow_result.get("answer", "")})
|
|
|
|
|
|
|
| 68 |
return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
|
| 69 |
|
| 70 |
+
except Exception:
|
| 71 |
+
chat_history.append({"role": "assistant", "content": "Something went wrong temporarily. Please try again."})
|
| 72 |
+
return "", chat_history, state, *render_suggestions(flows.default_suggestions())
|
| 73 |
|
| 74 |
def suggestion_click(text, chat_history, state):
|
| 75 |
return handle_user_message(text, chat_history, state)
|
|
|
|
| 100 |
for b in [s1, s2, s3, s4, s5, s6]:
|
| 101 |
b.click(suggestion_click, inputs=[b, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
|
| 102 |
|
| 103 |
+
# ✅ Required for Hugging Face Gradio runtime stability
|
| 104 |
+
demo.queue()
|