tharunchndrn commited on
Commit
91ce131
·
verified ·
1 Parent(s): 589d766

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -27
app.py CHANGED
@@ -7,6 +7,7 @@ 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
@@ -16,8 +17,13 @@ if not (os.path.exists(FAISS_INDEX_PATH) and os.path.exists(DOCSTORE_PATH)):
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 or [])[:6]
@@ -29,65 +35,98 @@ def render_suggestions(btns):
29
  updates.append(gr.update(value="", visible=False))
30
  return updates
31
 
 
32
  def on_open(chat_history, state):
33
  chat_history = chat_history or []
 
34
  if state["initialized"]:
35
  return chat_history, state, *render_suggestions(flows.default_suggestions())
36
 
37
  welcome = "Welcome to SysLink Food System 👋 How can I help you today?"
38
  chat_history.append({"role": "assistant", "content": welcome})
39
  state["initialized"] = True
 
40
  return chat_history, state, *render_suggestions(flows.default_suggestions())
41
 
 
42
  def handle_user_message(user_text, chat_history, state):
43
  chat_history = chat_history or []
44
  session_id = state["session_id"]
45
 
46
- # add user message
47
  chat_history.append({"role": "user", "content": user_text})
48
 
49
  try:
50
- flow_result = flows.handle_message(session_id=session_id, user_message=user_text)
 
 
 
51
 
 
52
  if flow_result["action"] == "rag":
53
  try:
54
- rag_result = rag.answer(user_text, preferred_lang=flow_result.get("lang"))
 
 
 
 
55
  bot_reply = rag_result.get("answer", "")
56
 
57
  if rag_result.get("sources"):
58
- src_lines = "\n".join([f"- {s['title']}" for s in rag_result["sources"][:3]])
 
 
59
  bot_reply += f"\n\nSources:\n{src_lines}"
60
 
61
  except Exception:
62
- bot_reply = "I ran into a temporary error while generating the answer. Please try again."
 
 
 
63
 
64
  chat_history.append({"role": "assistant", "content": bot_reply})
65
- return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
66
 
67
-
68
- # flow reply
69
- chat_history.append({"role": "assistant", "content": flow_result["answer"]})
70
- return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  except Exception:
73
- chat_history.append({"role": "assistant", "content": "Something went wrong temporarily. Please try again."})
74
- return "", chat_history, state, *render_suggestions(flows.default_suggestions())
 
 
75
 
76
- chat_history = chat_history + [(user_text, bot_reply)]
77
- return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
 
 
 
 
78
 
79
- chat_history = chat_history + [(user_text, flow_result["answer"])]
80
- return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
81
-
82
- except Exception:
83
- chat_history = chat_history + [(user_text, "Something went wrong temporarily. Please try again.")]
84
- return "", chat_history, state, *render_suggestions(flows.default_suggestions())
85
 
86
  def suggestion_click(suggestion_text, chat_history, state):
87
  return handle_user_message(suggestion_text, chat_history, state)
88
 
 
89
  with gr.Blocks(title="SysLink Food System Chatbot") as demo:
90
  state = gr.State(init_state())
 
91
  gr.Markdown("## SysLink Food System Assistant")
92
 
93
  chat = gr.Chatbot(height=420, type="messages")
@@ -100,14 +139,37 @@ with gr.Blocks(title="SysLink Food System Chatbot") as demo:
100
  s5 = gr.Button(visible=False)
101
  s6 = gr.Button(visible=False)
102
 
103
- user_in = gr.Textbox(placeholder="Type your message...", label="Message")
104
- send_btn = gr.Button("Send")
 
 
105
 
106
- open_btn.click(on_open, inputs=[chat, state], outputs=[chat, state, s1, s2, s3, s4, s5, s6])
107
- send_btn.click(handle_user_message, inputs=[user_in, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
108
- user_in.submit(handle_user_message, inputs=[user_in, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  for b in [s1, s2, s3, s4, s5, s6]:
111
- b.click(suggestion_click, inputs=[b, chat, state], outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6])
 
 
 
 
112
 
113
  demo.launch()
 
7
  from backend_app.rag_hf import RAGEngineHF
8
  from backend_app.config import DATA_DIR, FAISS_INDEX_PATH, DOCSTORE_PATH
9
 
10
+ # Ensure data directory exists
11
  os.makedirs(DATA_DIR, exist_ok=True)
12
 
13
  # Build index if missing
 
17
  rag = RAGEngineHF()
18
  flows = FlowManager()
19
 
20
+
21
  def init_state():
22
+ return {
23
+ "session_id": "sess-" + uuid.uuid4().hex[:12],
24
+ "initialized": False
25
+ }
26
+
27
 
28
  def render_suggestions(btns):
29
  btns = (btns or [])[:6]
 
35
  updates.append(gr.update(value="", visible=False))
36
  return updates
37
 
38
+
39
  def on_open(chat_history, state):
40
  chat_history = chat_history or []
41
+
42
  if state["initialized"]:
43
  return chat_history, state, *render_suggestions(flows.default_suggestions())
44
 
45
  welcome = "Welcome to SysLink Food System 👋 How can I help you today?"
46
  chat_history.append({"role": "assistant", "content": welcome})
47
  state["initialized"] = True
48
+
49
  return chat_history, state, *render_suggestions(flows.default_suggestions())
50
 
51
+
52
  def handle_user_message(user_text, chat_history, state):
53
  chat_history = chat_history or []
54
  session_id = state["session_id"]
55
 
56
+ # Add user message
57
  chat_history.append({"role": "user", "content": user_text})
58
 
59
  try:
60
+ flow_result = flows.handle_message(
61
+ session_id=session_id,
62
+ user_message=user_text
63
+ )
64
 
65
+ # RAG path
66
  if flow_result["action"] == "rag":
67
  try:
68
+ rag_result = rag.answer(
69
+ user_text,
70
+ preferred_lang=flow_result.get("lang")
71
+ )
72
+
73
  bot_reply = rag_result.get("answer", "")
74
 
75
  if rag_result.get("sources"):
76
+ src_lines = "\n".join(
77
+ [f"- {s['title']}" for s in rag_result["sources"][:3]]
78
+ )
79
  bot_reply += f"\n\nSources:\n{src_lines}"
80
 
81
  except Exception:
82
+ bot_reply = (
83
+ "I ran into a temporary error while generating the answer. "
84
+ "Please try again."
85
+ )
86
 
87
  chat_history.append({"role": "assistant", "content": bot_reply})
 
88
 
89
+ return (
90
+ "",
91
+ chat_history,
92
+ state,
93
+ *render_suggestions(flow_result.get("suggestions", []))
94
+ )
95
+
96
+ # Flow-only response
97
+ chat_history.append({
98
+ "role": "assistant",
99
+ "content": flow_result["answer"]
100
+ })
101
+
102
+ return (
103
+ "",
104
+ chat_history,
105
+ state,
106
+ *render_suggestions(flow_result.get("suggestions", []))
107
+ )
108
 
109
  except Exception:
110
+ chat_history.append({
111
+ "role": "assistant",
112
+ "content": "Something went wrong temporarily. Please try again."
113
+ })
114
 
115
+ return (
116
+ "",
117
+ chat_history,
118
+ state,
119
+ *render_suggestions(flows.default_suggestions())
120
+ )
121
 
 
 
 
 
 
 
122
 
123
  def suggestion_click(suggestion_text, chat_history, state):
124
  return handle_user_message(suggestion_text, chat_history, state)
125
 
126
+
127
  with gr.Blocks(title="SysLink Food System Chatbot") as demo:
128
  state = gr.State(init_state())
129
+
130
  gr.Markdown("## SysLink Food System Assistant")
131
 
132
  chat = gr.Chatbot(height=420, type="messages")
 
139
  s5 = gr.Button(visible=False)
140
  s6 = gr.Button(visible=False)
141
 
142
+ user_in = gr.Textbox(
143
+ placeholder="Type your message...",
144
+ label="Message"
145
+ )
146
 
147
+ send_btn = gr.Button("Send")
148
+ open_btn = gr.Button("Open Chat")
149
+
150
+ open_btn.click(
151
+ on_open,
152
+ inputs=[chat, state],
153
+ outputs=[chat, state, s1, s2, s3, s4, s5, s6]
154
+ )
155
+
156
+ send_btn.click(
157
+ handle_user_message,
158
+ inputs=[user_in, chat, state],
159
+ outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6]
160
+ )
161
+
162
+ user_in.submit(
163
+ handle_user_message,
164
+ inputs=[user_in, chat, state],
165
+ outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6]
166
+ )
167
 
168
  for b in [s1, s2, s3, s4, s5, s6]:
169
+ b.click(
170
+ suggestion_click,
171
+ inputs=[b, chat, state],
172
+ outputs=[user_in, chat, state, s1, s2, s3, s4, s5, s6]
173
+ )
174
 
175
  demo.launch()