Download memory.py from suraj-ml-projects/AI_video_assistant_summarization: direct link, hf CLI and curl.
- Browser
- Download file 695 Bytes
-
https://huggingface.co/spaces/suraj-ml-projects/AI_video_assistant_summarization/resolve/main/memory.py
- Command line
-
hf download hf://spaces/suraj-ml-projects/AI_video_assistant_summarization/memory.py
-
curl -L -o memory.py https://huggingface.co/spaces/suraj-ml-projects/AI_video_assistant_summarization/resolve/main/memory.py
695 Bytes
| # memory.py | |
| import faiss | |
| import numpy as np | |
| from sentence_transformers import SentenceTransformer | |
| class VideoMemory: | |
| def __init__(self): | |
| self.encoder = SentenceTransformer('all-MiniLM-L6-v2') | |
| self.index = faiss.IndexFlatL2(384) | |
| self.metadata = [] | |
| def add(self, ts, text, src): | |
| v = self.encoder.encode([text]) | |
| self.index.add(np.array(v).astype('float32')) | |
| self.metadata.append(f"[{int(ts)}s] {text}") | |
| def search(self, q): | |
| if not self.metadata: return "" | |
| v = self.encoder.encode([q]) | |
| _, I = self.index.search(np.array(v).astype('float32'), 2) | |
| return " ".join([self.metadata[i] for i in I[0] if i != -1]) | |