Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,74 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
from summarize import mmr_summarize
|
| 5 |
-
from rag_store import add_text, search
|
| 6 |
-
from llm import answer
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
return "No audio provided.", ""
|
| 11 |
-
t0 = time.time()
|
| 12 |
-
text = transcribe_file(audio_path) or ""
|
| 13 |
-
if not text.strip():
|
| 14 |
-
return "Couldn't transcribe. Try speaking closer to the mic.", ""
|
| 15 |
-
summary = mmr_summarize(text, max_sentences=4)
|
| 16 |
-
meta = {
|
| 17 |
-
"id": str(uuid.uuid4()),
|
| 18 |
-
"ts": dt.datetime.utcnow().isoformat(),
|
| 19 |
-
"tags": [t.strip() for t in (notes or "").split(",") if t.strip()]
|
| 20 |
-
}
|
| 21 |
-
add_text(summary, meta)
|
| 22 |
-
dt_ms = int((time.time()-t0)*1000)
|
| 23 |
-
return f"Indexed summary in {dt_ms} ms (text-only).", summary
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
hits = search(query, k=5)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
demo = build_demo()
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
#
|
| 64 |
demo.launch(server_name="0.0.0.0", show_api=False)
|
| 65 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from rag_store import add_memory, search
|
| 3 |
+
from llm import generate_answer
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
DESCRIPTION = """
|
| 6 |
+
### 🧠 MnemoSense – Tiny External Memory Demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
- **Add memory**: type a short summary of something that happened.
|
| 9 |
+
- **Ask**: query MnemoSense and it will retrieve the most relevant memories
|
| 10 |
+
and answer using only those.
|
| 11 |
+
- This demo stores **only text summaries** on-disk, no audio or video.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
def ingest_memory(text: str):
|
| 15 |
+
text = (text or "").strip()
|
| 16 |
+
if not text:
|
| 17 |
+
return "⚠️ Please type something to remember.", ""
|
| 18 |
+
add_memory(text)
|
| 19 |
+
return "✅ Saved to MnemoSense memory.", text
|
| 20 |
+
|
| 21 |
+
def ask_question(query: str):
|
| 22 |
+
query = (query or "").strip()
|
| 23 |
+
if not query:
|
| 24 |
+
return "⚠️ Please ask a question."
|
| 25 |
hits = search(query, k=5)
|
| 26 |
+
contexts = [h["text"] for h in hits]
|
| 27 |
+
answer = generate_answer(query, contexts)
|
| 28 |
+
return answer
|
| 29 |
+
|
| 30 |
+
def build_demo():
|
| 31 |
+
with gr.Blocks(title="MnemoSense", theme="soft") as demo:
|
| 32 |
+
gr.Markdown("# 🧠 MnemoSense\nYour tiny external memory assistant.")
|
| 33 |
+
gr.Markdown(DESCRIPTION)
|
| 34 |
+
|
| 35 |
+
with gr.Tab("Add memory"):
|
| 36 |
+
mem_in = gr.Textbox(
|
| 37 |
+
label="What happened?",
|
| 38 |
+
lines=4,
|
| 39 |
+
placeholder="Example: I met Felix at the lab and we discussed MRI-compatible ECG patches…",
|
| 40 |
+
)
|
| 41 |
+
save_btn = gr.Button("Save memory")
|
| 42 |
+
status = gr.Markdown()
|
| 43 |
+
echo = gr.Textbox(label="Saved snippet", interactive=False)
|
| 44 |
|
| 45 |
+
save_btn.click(
|
| 46 |
+
ingest_memory,
|
| 47 |
+
inputs=mem_in,
|
| 48 |
+
outputs=[status, echo],
|
| 49 |
+
)
|
| 50 |
|
| 51 |
+
with gr.Tab("Ask"):
|
| 52 |
+
q = gr.Textbox(
|
| 53 |
+
label="Ask MnemoSense",
|
| 54 |
+
lines=2,
|
| 55 |
+
placeholder="Example: What did we say about the mission?",
|
| 56 |
+
)
|
| 57 |
+
ask_btn = gr.Button("Ask")
|
| 58 |
+
ans = gr.Textbox(label="Answer", lines=6)
|
| 59 |
|
| 60 |
+
ask_btn.click(
|
| 61 |
+
ask_question,
|
| 62 |
+
inputs=q,
|
| 63 |
+
outputs=ans,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
gr.Markdown("⚠️ Demo note: this Space keeps only text memories in `memories.jsonl`.")
|
| 67 |
+
|
| 68 |
+
return demo
|
| 69 |
|
| 70 |
demo = build_demo()
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
# HF Spaces: bind to 0.0.0.0 and hide API docs (they were causing a schema crash)
|
| 74 |
demo.launch(server_name="0.0.0.0", show_api=False)
|
|
|