Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -7,7 +7,8 @@ from flask_cors import CORS
|
|
7 |
import random
|
8 |
import numpy as np
|
9 |
from langchain_chroma import Chroma
|
10 |
-
from chromadb import Documents, EmbeddingFunction, Embeddings
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
@@ -73,15 +74,49 @@ class MyEmbeddingFunction(EmbeddingFunction):
|
|
73 |
except Exception as e:
|
74 |
print("Error in Embeding :",str(e))
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
try:
|
77 |
-
CHROMA_PATH = "chroma"
|
78 |
custom_embeddings = MyEmbeddingFunction()
|
79 |
-
db = Chroma(
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
except Exception as e:
|
84 |
-
print("Error
|
85 |
|
86 |
|
87 |
def embeddingGen(query):
|
|
|
7 |
import random
|
8 |
import numpy as np
|
9 |
from langchain_chroma import Chroma
|
10 |
+
from chromadb import Documents, EmbeddingFunction, Embeddings, Collection
|
11 |
+
import sqlite3
|
12 |
|
13 |
app = Flask(__name__)
|
14 |
CORS(app)
|
|
|
74 |
except Exception as e:
|
75 |
print("Error in Embeding :",str(e))
|
76 |
|
77 |
+
# try:
|
78 |
+
# CHROMA_PATH = "chroma"
|
79 |
+
# custom_embeddings = MyEmbeddingFunction()
|
80 |
+
# db = Chroma(
|
81 |
+
# persist_directory=CHROMA_PATH,embedding_function=custom_embeddings
|
82 |
+
# )
|
83 |
+
# #
|
84 |
+
# except Exception as e:
|
85 |
+
# print("Error in database :",str(e))
|
86 |
+
|
87 |
+
# Initialize the database without persist_directory
|
88 |
try:
|
|
|
89 |
custom_embeddings = MyEmbeddingFunction()
|
90 |
+
db = Chroma(embedding_function=custom_embeddings)
|
91 |
+
|
92 |
+
# Load documents from chroma.sqlite3
|
93 |
+
def load_documents_from_sqlite(db_path="chroma.sqlite3"):
|
94 |
+
conn = sqlite3.connect(db_path)
|
95 |
+
cursor = conn.cursor()
|
96 |
+
|
97 |
+
# Assuming your table structure has "id", "content", and "embedding"
|
98 |
+
cursor.execute("SELECT id, content, embedding FROM documents")
|
99 |
+
rows = cursor.fetchall()
|
100 |
+
|
101 |
+
collection = db.get_or_create_collection("default_collection")
|
102 |
+
|
103 |
+
for row in rows:
|
104 |
+
doc_id = row[0]
|
105 |
+
content = row[1]
|
106 |
+
embedding = json.loads(row[2]) # If embeddings are stored as JSON strings
|
107 |
+
collection.add(
|
108 |
+
ids=[doc_id],
|
109 |
+
documents=[content],
|
110 |
+
embeddings=[embedding]
|
111 |
+
)
|
112 |
+
|
113 |
+
conn.close()
|
114 |
+
print("Loaded documents into Chroma.")
|
115 |
+
|
116 |
+
load_documents_from_sqlite() # Call to load data
|
117 |
+
|
118 |
except Exception as e:
|
119 |
+
print("Error initializing database:", str(e))
|
120 |
|
121 |
|
122 |
def embeddingGen(query):
|