Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,93 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import uuid
|
| 3 |
-
import gradio as gr
|
| 4 |
-
|
| 5 |
-
from backend_app.flows import FlowManager
|
| 6 |
-
from backend_app.ingest import run_ingestion
|
| 7 |
-
from backend_app.rag_hf import RAGEngineHF
|
| 8 |
-
from backend_app.config import DATA_DIR, FAISS_INDEX_PATH, DOCSTORE_PATH
|
| 9 |
-
|
| 10 |
-
os.makedirs(DATA_DIR, exist_ok=True)
|
| 11 |
-
|
| 12 |
-
# Build index if missing
|
| 13 |
-
if not (os.path.exists(FAISS_INDEX_PATH) and os.path.exists(DOCSTORE_PATH)):
|
| 14 |
-
run_ingestion()
|
| 15 |
-
|
| 16 |
-
rag = RAGEngineHF()
|
| 17 |
-
flows = FlowManager()
|
| 18 |
-
|
| 19 |
-
def init_state():
|
| 20 |
-
return {"session_id": "sess-" + uuid.uuid4().hex[:12], "initialized": False}
|
| 21 |
-
|
| 22 |
-
def render_suggestions(btns):
|
| 23 |
-
btns = btns[:6]
|
| 24 |
-
updates = [gr.Button.update(visible=True, value=b) for b in btns]
|
| 25 |
-
updates += [gr.Button.update(visible=False) for _ in range(6 - len(btns))]
|
| 26 |
-
return updates
|
| 27 |
-
|
| 28 |
-
def on_open(chat_history, state):
|
| 29 |
-
if state["initialized"]:
|
| 30 |
-
return chat_history, state, *render_suggestions(flows.default_suggestions())
|
| 31 |
-
welcome = "Welcome to SysLink Food System 👋 How can I help you today?"
|
| 32 |
-
chat_history = chat_history + [("", welcome)]
|
| 33 |
-
state["initialized"] = True
|
| 34 |
-
return chat_history, state, *render_suggestions(flows.default_suggestions())
|
| 35 |
-
|
| 36 |
-
def handle_user_message(user_text, chat_history, state):
|
| 37 |
-
session_id = state["session_id"]
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from backend_app.flows import FlowManager
|
| 6 |
+
from backend_app.ingest import run_ingestion
|
| 7 |
+
from backend_app.rag_hf import RAGEngineHF
|
| 8 |
+
from backend_app.config import DATA_DIR, FAISS_INDEX_PATH, DOCSTORE_PATH
|
| 9 |
+
|
| 10 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# Build index if missing
|
| 13 |
+
if not (os.path.exists(FAISS_INDEX_PATH) and os.path.exists(DOCSTORE_PATH)):
|
| 14 |
+
run_ingestion()
|
| 15 |
+
|
| 16 |
+
rag = RAGEngineHF()
|
| 17 |
+
flows = FlowManager()
|
| 18 |
+
|
| 19 |
+
def init_state():
|
| 20 |
+
return {"session_id": "sess-" + uuid.uuid4().hex[:12], "initialized": False}
|
| 21 |
+
|
| 22 |
+
def render_suggestions(btns):
|
| 23 |
+
btns = btns[:6]
|
| 24 |
+
updates = [gr.Button.update(visible=True, value=b) for b in btns]
|
| 25 |
+
updates += [gr.Button.update(visible=False) for _ in range(6 - len(btns))]
|
| 26 |
+
return updates
|
| 27 |
+
|
| 28 |
+
def on_open(chat_history, state):
|
| 29 |
+
if state["initialized"]:
|
| 30 |
+
return chat_history, state, *render_suggestions(flows.default_suggestions())
|
| 31 |
+
welcome = "Welcome to SysLink Food System 👋 How can I help you today?"
|
| 32 |
+
chat_history = chat_history + [("", welcome)]
|
| 33 |
+
state["initialized"] = True
|
| 34 |
+
return chat_history, state, *render_suggestions(flows.default_suggestions())
|
| 35 |
+
|
| 36 |
+
def handle_user_message(user_text, chat_history, state):
|
| 37 |
+
session_id = state["session_id"]
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
flow_result = flows.handle_message(session_id=session_id, user_message=user_text)
|
| 41 |
+
|
| 42 |
+
if flow_result["action"] == "rag":
|
| 43 |
+
try:
|
| 44 |
+
rag_result = rag.answer(user_text, preferred_lang=flow_result.get("lang"))
|
| 45 |
+
bot_reply = rag_result.get("answer", "")
|
| 46 |
+
if rag_result.get("sources"):
|
| 47 |
+
src_lines = "\n".join([f"- {s['title']}" for s in rag_result["sources"][:3]])
|
| 48 |
+
bot_reply += f"\n\nSources:\n{src_lines}"
|
| 49 |
+
except Exception:
|
| 50 |
+
bot_reply = (
|
| 51 |
+
"I ran into a temporary error while generating the answer. "
|
| 52 |
+
"Please try again in a moment."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
chat_history = chat_history + [(user_text, bot_reply)]
|
| 56 |
+
return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
|
| 57 |
+
|
| 58 |
+
chat_history = chat_history + [(user_text, flow_result["answer"])]
|
| 59 |
+
return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
|
| 60 |
+
|
| 61 |
+
except Exception:
|
| 62 |
+
chat_history = chat_history + [(user_text, "Something went wrong temporarily. Please try again.")]
|
| 63 |
+
return "", chat_history, state, *render_suggestions(flows.default_suggestions())
|
| 64 |
+
|
| 65 |
+
def suggestion_click(suggestion_text, chat_history, state):
|
| 66 |
+
return handle_user_message(suggestion_text, chat_history, state)
|
| 67 |
+
|
| 68 |
+
with gr.Blocks(title="SysLink Food System Chatbot") as demo:
|
| 69 |
+
state = gr.State(init_state())
|
| 70 |
+
gr.Markdown("## SysLink Food System Assistant")
|
| 71 |
+
|
| 72 |
+
chat = gr.Chatbot(height=420)
|
| 73 |
+
open_btn = gr.Button("Open Chat / Start")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
s1 = gr.Button(visible=False)
|
| 77 |
+
s2 = gr.Button(visible=False)
|
| 78 |
+
s3 = gr.Button(visible=False)
|
| 79 |
+
s4 = gr.Button(visible=False)
|
| 80 |
+
s5 = gr.Button(visible=False)
|
| 81 |
+
s6 = gr.Button(visible=False)
|
| 82 |
+
|
| 83 |
+
user_in = gr.Textbox(placeholder="Type your message...", label="Message")
|
| 84 |
+
send_btn = gr.Button("Send")
|
| 85 |
+
|
| 86 |
+
open_btn.click(on_open, inputs=[chat, state], outputs=[chat, state, s1, s2, s3, s4, s5, s6])
|
| 87 |
+
send_btn.click(handle_user_message, inputs=[user_in, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
|
| 88 |
+
user_in.submit(handle_user_message, inputs=[user_in, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
|
| 89 |
+
|
| 90 |
+
for b in [s1, s2, s3, s4, s5, s6]:
|
| 91 |
+
b.click(suggestion_click, inputs=[b, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
|
| 92 |
+
|
| 93 |
demo.launch()
|