Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,27 @@
|
|
| 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 |
-
|
| 8 |
-
|
| 9 |
-
Deduplicates consecutive repeated Q&A pairs to avoid repetition.
|
| 10 |
"""
|
| 11 |
# 1) Get answer from the session-based memory pipeline
|
| 12 |
answer = run_with_session_memory(message, session_id)
|
| 13 |
|
| 14 |
-
# 2)
|
| 15 |
-
|
| 16 |
-
history.append((message, answer))
|
| 17 |
|
| 18 |
-
# 3) Convert
|
| 19 |
message_dicts = []
|
| 20 |
-
for user_msg, ai_msg in history:
|
| 21 |
message_dicts.append({"role": "user", "content": user_msg})
|
| 22 |
message_dicts.append({"role": "assistant", "content": ai_msg})
|
| 23 |
-
|
| 24 |
# Return the message dicts and updated history
|
| 25 |
-
return message_dicts
|
|
|
|
|
|
|
| 26 |
my_chat_css = """
|
| 27 |
.gradio-container {
|
| 28 |
margin: auto;
|
|
@@ -35,6 +34,7 @@ my_chat_css = """
|
|
| 35 |
}
|
| 36 |
"""
|
| 37 |
|
|
|
|
| 38 |
with gr.Blocks(css=my_chat_css) as demo:
|
| 39 |
gr.Markdown("### DailyWellnessAI (User on right, Assistant on left)")
|
| 40 |
session_id_box = gr.Textbox(label="Session ID", value="abc123", interactive=True)
|
|
@@ -45,5 +45,5 @@ with gr.Blocks(css=my_chat_css) as demo:
|
|
| 45 |
description="Ask your questions. The session_id determines your stored memory."
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
demo.launch()
|
| 49 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from my_memory_logic import run_with_session_memory
|
| 3 |
|
| 4 |
def chat_interface_fn(message, history, session_id):
|
| 5 |
"""
|
| 6 |
+
Simple chat function for Gradio's ChatInterface that only shows the most recent question and answer pair.
|
| 7 |
+
The session_id is used to maintain session-based memory.
|
|
|
|
| 8 |
"""
|
| 9 |
# 1) Get answer from the session-based memory pipeline
|
| 10 |
answer = run_with_session_memory(message, session_id)
|
| 11 |
|
| 12 |
+
# 2) Append only the new question and answer pair to history
|
| 13 |
+
history.append((message, answer))
|
|
|
|
| 14 |
|
| 15 |
+
# 3) Convert the most recent question-answer pair to message dictionaries for display
|
| 16 |
message_dicts = []
|
| 17 |
+
for user_msg, ai_msg in history[-1:]: # Only use the latest Q&A
|
| 18 |
message_dicts.append({"role": "user", "content": user_msg})
|
| 19 |
message_dicts.append({"role": "assistant", "content": ai_msg})
|
| 20 |
+
|
| 21 |
# Return the message dicts and updated history
|
| 22 |
+
return message_dicts, history
|
| 23 |
+
|
| 24 |
+
# Custom CSS for chat interface
|
| 25 |
my_chat_css = """
|
| 26 |
.gradio-container {
|
| 27 |
margin: auto;
|
|
|
|
| 34 |
}
|
| 35 |
"""
|
| 36 |
|
| 37 |
+
# Set up Gradio interface
|
| 38 |
with gr.Blocks(css=my_chat_css) as demo:
|
| 39 |
gr.Markdown("### DailyWellnessAI (User on right, Assistant on left)")
|
| 40 |
session_id_box = gr.Textbox(label="Session ID", value="abc123", interactive=True)
|
|
|
|
| 45 |
description="Ask your questions. The session_id determines your stored memory."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Launch the Gradio interface
|
| 49 |
demo.launch()
|
|
|