Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
import faiss
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 7 |
+
memory = []
|
| 8 |
+
|
| 9 |
+
index = faiss.IndexFlatL2(384)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def add_message(msg):
|
| 13 |
+
emb = model.encode([msg])
|
| 14 |
+
index.add(np.array(emb, dtype='float32'))
|
| 15 |
+
memory.append(msg)
|
| 16 |
+
return f"Memory saved. Total: {len(memory)}"
|
| 17 |
+
|
| 18 |
+
def recall(query):
|
| 19 |
+
emb = model.encode([query])
|
| 20 |
+
D, I = index.search(np.array(emb, dtype='float32'), 1)
|
| 21 |
+
return memory[I[0][0]] if memory else "Nothing in memory."
|
| 22 |
+
|
| 23 |
+
ui = gr.Interface(fn=recall, inputs="text", outputs="text", title="Memory Recall")
|
| 24 |
+
ui.launch()
|