Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,43 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import os
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import numpy as np
|
| 4 |
+
from memory_store import MemoryStore
|
| 5 |
+
from graph_view import visualize_graph
|
| 6 |
+
|
| 7 |
+
# Initialize
|
| 8 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 9 |
+
memory = MemoryStore()
|
| 10 |
+
|
| 11 |
+
def process_input(user_input, chat_history):
|
| 12 |
+
user_input = user_input.strip()
|
| 13 |
+
|
| 14 |
+
# visualization command
|
| 15 |
+
if user_input.lower() in ["visualize memory", "show graph", "see memory map"]:
|
| 16 |
+
memories = getattr(memory, "memories", [])
|
| 17 |
+
if not memories:
|
| 18 |
+
return chat_history + [[user_input, "No memories yet to visualize."]]
|
| 19 |
+
visualize_graph(memories)
|
| 20 |
+
return chat_history + [[user_input, "🧠 Memory graph generated. Check the output window."]]
|
| 21 |
+
|
| 22 |
+
# embed input
|
| 23 |
+
vector = model.encode(user_input)
|
| 24 |
+
save_status = memory.add_memory(user_input, vector)
|
| 25 |
+
|
| 26 |
+
# retrieve related
|
| 27 |
+
related = memory.retrieve_relevant(vector)
|
| 28 |
+
recall = "\n".join([f"• {txt} (score: {round(score,4)})" for txt, score in related])
|
| 29 |
+
|
| 30 |
+
response = f"{save_status}\nHere’s what I recall most related:\n{recall}"
|
| 31 |
+
chat_history.append([user_input, response])
|
| 32 |
+
return chat_history
|
| 33 |
+
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# 🧠 Aventra Memory Interface")
|
| 36 |
+
chatbot = gr.Chatbot(height=500)
|
| 37 |
+
msg = gr.Textbox(label="Type your message...")
|
| 38 |
+
clear = gr.Button("Clear Chat")
|
| 39 |
+
|
| 40 |
+
msg.submit(process_input, [msg, chatbot], [chatbot])
|
| 41 |
+
clear.click(lambda: [], None, chatbot)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|