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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -30,16 +30,22 @@ def render_suggestions(btns):
30
  return updates
31
 
32
  def on_open(chat_history, state):
 
33
  if state["initialized"]:
34
  return chat_history, state, *render_suggestions(flows.default_suggestions())
 
35
  welcome = "Welcome to SysLink Food System 👋 How can I help you today?"
36
- chat_history = chat_history + [("", welcome)]
37
  state["initialized"] = True
38
  return chat_history, state, *render_suggestions(flows.default_suggestions())
39
 
40
  def handle_user_message(user_text, chat_history, state):
 
41
  session_id = state["session_id"]
42
 
 
 
 
43
  try:
44
  flow_result = flows.handle_message(session_id=session_id, user_message=user_text)
45
 
@@ -47,14 +53,25 @@ def handle_user_message(user_text, chat_history, state):
47
  try:
48
  rag_result = rag.answer(user_text, preferred_lang=flow_result.get("lang"))
49
  bot_reply = rag_result.get("answer", "")
 
50
  if rag_result.get("sources"):
51
  src_lines = "\n".join([f"- {s['title']}" for s in rag_result["sources"][:3]])
52
  bot_reply += f"\n\nSources:\n{src_lines}"
 
53
  except Exception:
54
- bot_reply = (
55
- "I ran into a temporary error while generating the answer. "
56
- "Please try again in a moment."
57
- )
 
 
 
 
 
 
 
 
 
58
 
59
  chat_history = chat_history + [(user_text, bot_reply)]
60
  return "", chat_history, state, *render_suggestions(flow_result.get("suggestions", []))
@@ -73,8 +90,7 @@ with gr.Blocks(title="SysLink Food System Chatbot") as demo:
73
  state = gr.State(init_state())
74
  gr.Markdown("## SysLink Food System Assistant")
75
 
76
- chat = gr.Chatbot(height=420)
77
- open_btn = gr.Button("Open Chat / Start")
78
 
79
  with gr.Row():
80
  s1 = gr.Button(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
 
 
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", []))
 
90
  state = gr.State(init_state())
91
  gr.Markdown("## SysLink Food System Assistant")
92
 
93
+ chat = gr.Chatbot(height=420, type="messages")
 
94
 
95
  with gr.Row():
96
  s1 = gr.Button(visible=False)