tharunchndrn commited on
Commit
e0b48b1
·
verified ·
1 Parent(s): 2ec017c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -81
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
- flow_result = flows.handle_message(session_id=session_id, user_message=user_text)
39
-
40
- if flow_result["action"] == "rag":
41
- rag_result = rag.answer(user_text, preferred_lang=flow_result.get("lang"))
42
- bot_reply = rag_result["answer"]
43
-
44
- if rag_result.get("sources"):
45
- src_lines = "\n".join([f"- {s['title']}" for s in rag_result["sources"][:3]])
46
- bot_reply += f"\n\nSources:\n{src_lines}"
47
-
48
- chat_history = chat_history + [(user_text, bot_reply)]
49
- return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
50
-
51
- chat_history = chat_history + [(user_text, flow_result["answer"])]
52
- return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
53
-
54
- def suggestion_click(suggestion_text, chat_history, state):
55
- return handle_user_message(suggestion_text, chat_history, state)
56
-
57
- with gr.Blocks(title="SysLink Food System Chatbot") as demo:
58
- state = gr.State(init_state())
59
- gr.Markdown("## SysLink Food System Assistant")
60
-
61
- chat = gr.Chatbot(height=420)
62
- open_btn = gr.Button("Open Chat / Start")
63
-
64
- with gr.Row():
65
- s1 = gr.Button(visible=False)
66
- s2 = gr.Button(visible=False)
67
- s3 = gr.Button(visible=False)
68
- s4 = gr.Button(visible=False)
69
- s5 = gr.Button(visible=False)
70
- s6 = gr.Button(visible=False)
71
-
72
- user_in = gr.Textbox(placeholder="Type your message...", label="Message")
73
- send_btn = gr.Button("Send")
74
-
75
- open_btn.click(on_open, inputs=[chat, state], outputs=[chat, state, s1, s2, s3, s4, s5, s6])
76
- send_btn.click(handle_user_message, inputs=[user_in, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
77
- user_in.submit(handle_user_message, inputs=[user_in, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
78
-
79
- for b in [s1, s2, s3, s4, s5, s6]:
80
- b.click(suggestion_click, inputs=[b, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
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()