Phoenix21 commited on
Commit
87cd864
·
verified ·
1 Parent(s): 0dad17d

UPDATE APP.PY WITH SESSION ID

Browse files
Files changed (1) hide show
  1. app.py +29 -57
app.py CHANGED
@@ -1,63 +1,35 @@
1
- import os
2
  import gradio as gr
 
3
 
4
- # Suppose 'run_with_chain' is your pipeline function from pipeline.py
5
- from pipeline import run_with_chain
6
-
7
- # Suppose 'memory' and 'restatement_chain' come from my_memory_logic.py
8
- from my_memory_logic import memory, restatement_chain
9
-
10
- def chat_history_fn(user_input, history):
11
  """
12
- Rely on LangChain memory to store the entire conversation across calls.
13
- DO NOT re-add old messages from 'history' each time.
14
- Also, handle potential None or invalid strings for user_input/answer
15
- to avoid Pydantic validation errors.
16
  """
17
- # -- 0) Sanitize user_input to ensure it's a valid string
18
- if not user_input or not isinstance(user_input, str):
19
- user_input = "" if user_input is None else str(user_input)
20
-
21
- # -- 1) Restate the new user question using existing LangChain memory
22
- reformulated_q = restatement_chain.run({
23
- "chat_history": memory.chat_memory.messages,
24
- "input": user_input
25
- })
26
-
27
- # -- 2) Pass the reformulated question into your pipeline
28
- answer = run_with_chain(reformulated_q)
29
- # also sanitize if needed
30
- if answer is None or not isinstance(answer, str):
31
- answer = "" if answer is None else str(answer)
32
-
33
- # -- 3) Add this new user->assistant turn to memory
34
- memory.chat_memory.add_user_message(user_input)
35
- memory.chat_memory.add_ai_message(answer)
36
-
37
- # -- 4) Update Gradio’s 'history' so the UI shows the new turn
38
- history.append((user_input, answer))
39
-
40
- # -- 5) Convert the entire 'history' to message dictionaries:
41
- # [{"role":"user","content":...},{"role":"assistant","content":...},...]
42
  message_dicts = []
43
- for usr_msg, ai_msg in history:
44
- if not isinstance(usr_msg, str):
45
- usr_msg = str(usr_msg) if usr_msg else ""
46
- if not isinstance(ai_msg, str):
47
- ai_msg = str(ai_msg) if ai_msg else ""
48
-
49
- message_dicts.append({"role": "user", "content": usr_msg})
50
  message_dicts.append({"role": "assistant", "content": ai_msg})
51
-
52
- # -- 6) Return the message dictionary list
53
- return message_dicts
54
-
55
- # Build your ChatInterface with the function
56
- demo = gr.ChatInterface(
57
- fn=chat_history_fn,
58
- title="DailyWellnessAI with Memory",
59
- description="A chat bot that remembers context using memory + question restatement."
60
- )
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
1
+ # app.py
2
  import gradio as gr
3
+ from my_memory_logic import run_with_session_memory
4
 
5
+ def chat_interface_fn(message, history, session_id):
 
 
 
 
 
 
6
  """
7
+ A single-turn chat function for Gradio's ChatInterface.
8
+ We rely on session_id to store the conversation in our my_memory_logic store.
 
 
9
  """
10
+ # 1) We call run_with_session_memory with user message and session_id
11
+ answer = run_with_session_memory(message, session_id)
12
+
13
+ # 2) Append the turn to the 'history' so Gradio UI displays it
14
+ history.append((message, answer))
15
+
16
+ # 3) Convert into message dicts if ChatInterface is using openai-style messages
17
+ # or we can just return a single string. Let's do openai-style message dicts:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  message_dicts = []
19
+ for user_msg, ai_msg in history:
20
+ message_dicts.append({"role": "user", "content": user_msg})
 
 
 
 
 
21
  message_dicts.append({"role": "assistant", "content": ai_msg})
22
+ return message_dicts, history
23
+
24
+ # We'll define a small Gradio Blocks or ChatInterface
25
+ with gr.Blocks() as demo:
26
+ session_id_box = gr.Textbox(label="Session ID", value="abc123", interactive=True)
27
+ chat_interface = gr.ChatInterface(
28
+ fn=lambda message, history: chat_interface_fn(
29
+ message, history, session_id_box.value
30
+ ),
31
+ title="DailyWellnessAI (Session-based Memory)",
32
+ description="Ask your questions. The session_id determines your stored memory."
33
+ )
34
+
35
+ demo.launch()