Krishndhola commited on
Commit
b947b1a
·
verified ·
1 Parent(s): 94ceda1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -8
app.py CHANGED
@@ -1,11 +1,65 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- def placeholder(query):
4
- return "LifeSync Lite is initializing..."
5
 
6
- iface = gr.Interface(fn=placeholder,
7
- inputs="text",
8
- outputs="text",
9
- title="LifeSync Lite (Promptless Search)",
10
- description="Upload snippets, notes, or files—search coming soon.")
11
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from sentence_transformers import SentenceTransformer, util
3
+ import faiss
4
+ import os
5
+ import pickle
6
 
7
+ MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
8
+ model = SentenceTransformer(MODEL_NAME)
9
 
10
+ INDEX_FILE = "vector_store.index"
11
+ TEXT_FILE = "texts.pkl"
12
+
13
+ # --- helper: load / save index ----
14
+ def load_index():
15
+ if os.path.exists(INDEX_FILE) and os.path.exists(TEXT_FILE):
16
+ index = faiss.read_index(INDEX_FILE)
17
+ with open(TEXT_FILE, "rb") as f:
18
+ texts = pickle.load(f)
19
+ else:
20
+ index = faiss.IndexFlatIP(model.get_sentence_embedding_dimension())
21
+ texts = []
22
+ return index, texts
23
+
24
+ def save_index(index, texts):
25
+ faiss.write_index(index, INDEX_FILE)
26
+ with open(TEXT_FILE, "wb") as f:
27
+ pickle.dump(texts, f)
28
+
29
+ # --- core logic ----
30
+ def add_text(docs):
31
+ index, texts = load_index()
32
+ embeddings = model.encode(docs, normalize_embeddings=True)
33
+ index.add(embeddings)
34
+ texts.extend(docs)
35
+ save_index(index, texts)
36
+ return f"✅ Added {len(docs)} snippet(s) to memory."
37
+
38
+ def search(query):
39
+ index, texts = load_index()
40
+ if len(texts) == 0:
41
+ return "🪣 Memory empty — add some snippets first."
42
+ q_emb = model.encode([query], normalize_embeddings=True)
43
+ scores, ids = index.search(q_emb, 5)
44
+ results = []
45
+ for i, s in zip(ids[0], scores[0]):
46
+ if i < len(texts):
47
+ results.append(f"• {texts[i]} (score {round(float(s),3)})")
48
+ return "\n\n".join(results)
49
+
50
+ with gr.Blocks(title="LifeSync Lite") as demo:
51
+ gr.Markdown("## 🧠 LifeSync Lite — Promptless Search\nUpload or paste your notes, then ask natural-language questions.")
52
+
53
+ with gr.Tab("Add"):
54
+ docs_box = gr.Textbox(lines=6, label="Paste notes or text")
55
+ add_btn = gr.Button("Add to Memory")
56
+ add_out = gr.Textbox(label="Status")
57
+ add_btn.click(add_text, inputs=[docs_box], outputs=[add_out])
58
+
59
+ with gr.Tab("Search"):
60
+ query_box = gr.Textbox(lines=2, label="Ask something")
61
+ search_btn = gr.Button("Search Memory")
62
+ search_out = gr.Textbox(label="Results")
63
+ search_btn.click(search, inputs=[query_box], outputs=[search_out])
64
+
65
+ demo.launch()