Spaces:
Build error
Build error
Create database_utils.py
Browse files- database_utils.py +28 -0
database_utils.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
def init_db():
|
| 4 |
+
conn = sqlite3.connect('embeddings.db')
|
| 5 |
+
c = conn.cursor()
|
| 6 |
+
c.execute('''CREATE TABLE IF NOT EXISTS embeddings
|
| 7 |
+
(sentence TEXT, embedding BLOB)''')
|
| 8 |
+
conn.commit()
|
| 9 |
+
conn.close()
|
| 10 |
+
return "Database initialized successfully!"
|
| 11 |
+
|
| 12 |
+
def save_embeddings_to_db(sentence, embedding):
|
| 13 |
+
conn = sqlite3.connect('embeddings.db')
|
| 14 |
+
c = conn.cursor()
|
| 15 |
+
embedding_blob = sqlite3.Binary(embedding.tobytes())
|
| 16 |
+
c.execute("INSERT INTO embeddings (sentence, embedding) VALUES (?, ?)", (sentence, embedding_blob))
|
| 17 |
+
conn.commit()
|
| 18 |
+
conn.close()
|
| 19 |
+
|
| 20 |
+
def get_all_embeddings():
|
| 21 |
+
conn = sqlite3.connect('embeddings.db')
|
| 22 |
+
c = conn.cursor()
|
| 23 |
+
c.execute("SELECT sentence, embedding FROM embeddings")
|
| 24 |
+
data = c.fetchall()
|
| 25 |
+
conn.close()
|
| 26 |
+
embeddings = [np.frombuffer(row[1], dtype=np.float32).reshape(-1, 3) for row in data]
|
| 27 |
+
sentences = [row[0] for row in data]
|
| 28 |
+
return embeddings, sentences
|