Gradio Demo
Interactive chat interface with memory using Gradio.
Prerequisites
pip install remina-memory[gemini,postgres,qdrant,redis] gradioImplementation
"""
Interactive chat with Gradio UI and Remina memory.
Run: python gradio_chat.py
"""
import os
import gradio as gr
from google import genai
from remina import Memory
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
REMINA_CONFIG = {
"cache": {
"redis_url": "redis://localhost:6379",
"ttl_seconds": 3600,
"enabled": True,
},
"storage": {
"provider": "postgres",
"config": {
"host": "localhost",
"port": 5432,
"database": "remina",
"user": "remina",
"password": "remina",
}
},
"vector_store": {
"provider": "qdrant",
"config": {
"url": "http://localhost:6333",
"collection_name": "remina_memories",
"embedding_dims": 768,
}
},
"embedder": {
"provider": "gemini",
"config": {
"model": "models/text-embedding-004",
"dimensions": 768,
"api_key": GOOGLE_API_KEY,
}
},
"llm": {
"provider": "gemini",
"config": {
"model": "gemini-2.0-flash",
"temperature": 0.7,
"api_key": GOOGLE_API_KEY,
}
},
}
memory = Memory(REMINA_CONFIG)
client = genai.Client(api_key=GOOGLE_API_KEY)
```python
def get_relevant_memories(user_id: str, message: str, limit: int = 5) -> str:
try:
results = memory.search(query=message, user_id=user_id, limit=limit)
if results["results"]:
return "\n".join([f"- {m['memory']}" for m in results["results"]])
except Exception as e:
print(f"Memory retrieval error: {e}")
return ""
def store_conversation(user_id: str, user_msg: str, assistant_msg: str):
try:
memory.add(
messages=[
{"role": "user", "content": user_msg},
{"role": "assistant", "content": assistant_msg},
],
user_id=user_id,
)
except Exception as e:
print(f"Memory storage error: {e}")
def chat(message: str, history: list, user_id: str) -> str:
if not message.strip():
return ""
memories_context = get_relevant_memories(user_id, message)
system_prompt = """You are an AI assistant with memory capabilities.
Use memories naturally without explicitly mentioning them."""
if memories_context:
system_prompt += f"\n\nUser context:\n{memories_context}"
conversation = f"System: {system_prompt}\n\n"
for h in history[-10:]:
if isinstance(h, (list, tuple)) and len(h) >= 2:
conversation += f"User: {h[0]}\nAssistant: {h[1]}\n"
conversation += f"User: {message}\nAssistant:"
try:
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=conversation,
)
assistant_response = response.text
except Exception as e:
assistant_response = f"Error: {str(e)}"
store_conversation(user_id, message, assistant_response)
return assistant_response
def get_all_memories(user_id: str) -> str:
try:
results = memory.get_all(user_id=user_id)
if results["results"]:
return "\n".join([
f"{i}. {m['memory']}"
for i, m in enumerate(results["results"], 1)
])
return "No memories stored."
except Exception as e:
return f"Error: {str(e)}"
def clear_memories(user_id: str) -> str:
try:
memory.delete_all(user_id=user_id)
return "Memories cleared."
except Exception as e:
return f"Error: {str(e)}"
def create_ui():
with gr.Blocks(title="Remina Chat") as demo:
gr.Markdown("# Remina Chat\nAI assistant with persistent memory.")
with gr.Row():
user_id = gr.Textbox(
value="user_001",
label="User ID",
scale=1,
)
with gr.Tabs():
with gr.Tab("Chat"):
chatbot = gr.Chatbot(height=400)
msg = gr.Textbox(placeholder="Type message...", show_label=False)
with gr.Row():
submit = gr.Button("Send", variant="primary")
clear = gr.Button("Clear Chat")
with gr.Tab("Memories"):
memories_display = gr.Textbox(label="Stored Memories", lines=10, interactive=False)
with gr.Row():
refresh_btn = gr.Button("Refresh")
clear_mem_btn = gr.Button("Clear All", variant="stop")
def respond(message, chat_history, uid):
old_format = []
for m in chat_history:
if isinstance(m, dict):
if m.get("role") == "user":
old_format.append((m.get("content", ""), None))
elif m.get("role") == "assistant" and old_format:
old_format[-1] = (old_format[-1][0], m.get("content", ""))
elif isinstance(m, (list, tuple)) and len(m) >= 2:
old_format.append((m[0], m[1]))
bot_message = chat(message, old_format, uid)
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": bot_message})
return "", chat_history
msg.submit(respond, [msg, chatbot, user_id], [msg, chatbot])
submit.click(respond, [msg, chatbot, user_id], [msg, chatbot])
clear.click(lambda: [], None, chatbot)
refresh_btn.click(get_all_memories, [user_id], [memories_display])
clear_mem_btn.click(clear_memories, [user_id], [memories_display])
user_id.change(get_all_memories, [user_id], [memories_display])
return demo
if __name__ == "__main__":
demo = create_ui()
demo.launch(share=False)Running
# Start infrastructure
docker-compose up -d
# Set API key
export GOOGLE_API_KEY="your-api-key"
# Run
python gradio_chat.pyOpen http://localhost:7860 (opens in a new tab) in browser.
Features
- Real-time chat with memory-augmented responses
- View and manage stored memories
- Switch between user profiles