Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,13 @@ from context_graph import ContextGraph
|
|
| 6 |
from graph_view import visualize_reasoned_graph
|
| 7 |
from memory_store import MemoryStore
|
| 8 |
|
| 9 |
-
# === Setup ===
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# === Initialize Core Systems ===
|
| 14 |
agent = AgentCore()
|
|
@@ -18,27 +22,29 @@ memory = MemoryStore(path=MEMORY_PATH)
|
|
| 18 |
|
| 19 |
# === Core Chat Function ===
|
| 20 |
def process_input(user_input, history=[]):
|
| 21 |
-
"""
|
| 22 |
-
#
|
| 23 |
vector = agent.embed_text(user_input)
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
memory.add_memory(user_input, vector)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
reasoning = reasoner.reason(user_input)
|
| 30 |
response = f"[Aventra Response]: {reasoning}"
|
| 31 |
|
| 32 |
-
#
|
| 33 |
history.append((user_input, response))
|
| 34 |
return history, history
|
| 35 |
|
| 36 |
-
# === Visualize Memory
|
| 37 |
def visualize_memory():
|
| 38 |
-
"""
|
| 39 |
memories = memory.memories
|
| 40 |
-
|
|
|
|
| 41 |
|
|
|
|
| 42 |
for i, (text1, vec1) in enumerate(memories):
|
| 43 |
for j, (text2, vec2) in enumerate(memories):
|
| 44 |
if i != j:
|
|
@@ -46,11 +52,10 @@ def visualize_memory():
|
|
| 46 |
if similarity > 0.35:
|
| 47 |
relationships.append((text1, text2, float(similarity)))
|
| 48 |
|
| 49 |
-
# Generate visualization
|
| 50 |
output_html = visualize_reasoned_graph(memories, relationships)
|
| 51 |
print(f"✅ Visualization created at: {output_html}")
|
| 52 |
|
| 53 |
-
# Return graph inline
|
| 54 |
if os.path.exists(output_html):
|
| 55 |
with open(output_html, "r") as f:
|
| 56 |
html_content = f.read()
|
|
@@ -65,11 +70,12 @@ def clear_memory():
|
|
| 65 |
return "🧹 Memory cleared successfully."
|
| 66 |
|
| 67 |
# === Build Gradio Interface ===
|
| 68 |
-
with gr.Blocks(title="Aventra OS") as demo:
|
| 69 |
-
gr.Markdown("## 🧠 **Aventra OS —
|
|
|
|
| 70 |
chatbot = gr.Chatbot(label="Aventra Chat")
|
| 71 |
msg = gr.Textbox(label="Type a message")
|
| 72 |
-
send_btn = gr.Button("Send")
|
| 73 |
visualize_btn = gr.Button("Visualize Memory 🕸️")
|
| 74 |
clear_btn = gr.Button("Clear Memory 🧹")
|
| 75 |
graph_html = gr.HTML(label="Memory Graph")
|
|
|
|
| 6 |
from graph_view import visualize_reasoned_graph
|
| 7 |
from memory_store import MemoryStore
|
| 8 |
|
| 9 |
+
# === Lightweight Setup ===
|
| 10 |
+
# Redirects model cache to ephemeral folder (won’t bloat storage)
|
| 11 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
| 12 |
+
os.environ["HF_HOME"] = "/tmp/hf_home"
|
| 13 |
+
|
| 14 |
+
# Memory stored in /tmp so it resets safely if needed
|
| 15 |
+
MEMORY_PATH = "/tmp/memory.json"
|
| 16 |
|
| 17 |
# === Initialize Core Systems ===
|
| 18 |
agent = AgentCore()
|
|
|
|
| 22 |
|
| 23 |
# === Core Chat Function ===
|
| 24 |
def process_input(user_input, history=[]):
|
| 25 |
+
"""Aventra OS conversation loop with memory and reasoning."""
|
| 26 |
+
# 1️⃣ Embed user input
|
| 27 |
vector = agent.embed_text(user_input)
|
| 28 |
+
|
| 29 |
+
# 2️⃣ Add memory safely
|
| 30 |
memory.add_memory(user_input, vector)
|
| 31 |
|
| 32 |
+
# 3️⃣ Reason on new input
|
| 33 |
reasoning = reasoner.reason(user_input)
|
| 34 |
response = f"[Aventra Response]: {reasoning}"
|
| 35 |
|
| 36 |
+
# 4️⃣ Append to chat history
|
| 37 |
history.append((user_input, response))
|
| 38 |
return history, history
|
| 39 |
|
| 40 |
+
# === Visualize Memory ===
|
| 41 |
def visualize_memory():
|
| 42 |
+
"""Inline visualization for memory graph."""
|
| 43 |
memories = memory.memories
|
| 44 |
+
if not memories:
|
| 45 |
+
return gr.HTML.update(value="<b>No memory data yet — start chatting first.</b>")
|
| 46 |
|
| 47 |
+
relationships = []
|
| 48 |
for i, (text1, vec1) in enumerate(memories):
|
| 49 |
for j, (text2, vec2) in enumerate(memories):
|
| 50 |
if i != j:
|
|
|
|
| 52 |
if similarity > 0.35:
|
| 53 |
relationships.append((text1, text2, float(similarity)))
|
| 54 |
|
| 55 |
+
# Generate and show visualization
|
| 56 |
output_html = visualize_reasoned_graph(memories, relationships)
|
| 57 |
print(f"✅ Visualization created at: {output_html}")
|
| 58 |
|
|
|
|
| 59 |
if os.path.exists(output_html):
|
| 60 |
with open(output_html, "r") as f:
|
| 61 |
html_content = f.read()
|
|
|
|
| 70 |
return "🧹 Memory cleared successfully."
|
| 71 |
|
| 72 |
# === Build Gradio Interface ===
|
| 73 |
+
with gr.Blocks(title="Aventra OS - Lightweight Mode") as demo:
|
| 74 |
+
gr.Markdown("## 🧠 **Aventra OS — Memory Engine (Light Mode)**")
|
| 75 |
+
|
| 76 |
chatbot = gr.Chatbot(label="Aventra Chat")
|
| 77 |
msg = gr.Textbox(label="Type a message")
|
| 78 |
+
send_btn = gr.Button("Send 🚀")
|
| 79 |
visualize_btn = gr.Button("Visualize Memory 🕸️")
|
| 80 |
clear_btn = gr.Button("Clear Memory 🧹")
|
| 81 |
graph_html = gr.HTML(label="Memory Graph")
|