Vineetha00 commited on
Commit
7b5e88a
·
verified ·
1 Parent(s): c57d0e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -53
app.py CHANGED
@@ -1,65 +1,74 @@
1
  import gradio as gr
2
- import time, os, uuid, datetime as dt
3
- from stt import transcribe_file
4
- from summarize import mmr_summarize
5
- from rag_store import add_text, search
6
- from llm import answer
7
 
8
- def ingest(audio_path: str, notes: str):
9
- if not audio_path:
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
- def ask(q: str, audio_q: str):
26
- query = (q or "").strip()
27
- if (not query) and audio_q:
28
- query = transcribe_file(audio_q)
29
- if not query.strip():
30
- return "", "", "Please provide a question (text or audio)."
 
 
 
 
 
 
 
 
 
 
 
31
  hits = search(query, k=5)
32
- ctxs = [h.get("text","") for h in hits]
33
- ans = answer(query, ctxs)
34
- refs = "\n\n".join([f"- {h.get('text','')[:160]}…" for h in hits])
35
- return query, ans, refs if refs else "(no references yet)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- with gr.Blocks(title="MnemoSense — Spaces Demo") as demo:
38
- gr.Markdown("# MnemoSense — Text-only Memory (HF Spaces)")
39
- gr.Markdown("**Privacy-first**: Only summaries are stored. Try the **Ingest** tab, then ask questions.")
 
 
40
 
41
- with gr.Tab("Ingest"):
42
- with gr.Row():
43
- mic = gr.Audio(sources=["microphone","upload"], type="filepath", label="Record or Upload (<= 60s)")
44
- notes = gr.Textbox(label="Optional tags (comma-separated)", placeholder="demo, meeting, idea")
45
- btn_ingest = gr.Button("Transcribe Summarize Index")
46
- status = gr.Textbox(label="Status", interactive=False)
47
- summary = gr.Textbox(label="Summary stored", lines=4, interactive=False)
48
- btn_ingest.click(ingest, inputs=[mic, notes], outputs=[status, summary])
49
 
50
- with gr.Tab("Ask"):
51
- with gr.Row():
52
- q = gr.Textbox(label="Question", placeholder="What did we say about the mission?")
53
- q_audio = gr.Audio(sources=["microphone","upload"], type="filepath", label="Or ask by voice")
54
- btn_ask = gr.Button("Retrieve → Answer")
55
- out_q = gr.Textbox(label="You asked", interactive=False)
56
- out_ans = gr.Textbox(label="Answer", lines=6, interactive=False)
57
- out_refs = gr.Textbox(label="References (summaries)", lines=6, interactive=False)
58
- btn_ask.click(ask, inputs=[q, q_audio], outputs=[out_q, out_ans, out_refs])
59
 
60
  demo = build_demo()
61
 
62
  if __name__ == "__main__":
63
- # On Hugging Face Spaces: bind to 0.0.0.0 and disable API docs
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)