Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,11 +9,33 @@ from embedding_utils import parallel_generate_embeddings, get_embedding
|
|
9 |
# Load environment variables from .env file
|
10 |
load_dotenv()
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
def get_field_names(db_name: str, collection_name: str) -> list[str]:
|
19 |
"""Get list of fields in the collection"""
|
@@ -152,8 +174,29 @@ def vector_search(query_text: str, db_name: str, collection_name: str, embedding
|
|
152 |
with gr.Blocks(title="MongoDB Vector Search Tool") as iface:
|
153 |
gr.Markdown("# MongoDB Vector Search Tool")
|
154 |
|
155 |
-
#
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
with gr.Tab("Generate Embeddings"):
|
159 |
with gr.Row():
|
|
|
9 |
# Load environment variables from .env file
|
10 |
load_dotenv()
|
11 |
|
12 |
+
def check_credentials() -> tuple[bool, str]:
|
13 |
+
"""Check if required credentials are set and valid"""
|
14 |
+
atlas_uri = os.getenv("ATLAS_URI")
|
15 |
+
openai_key = os.getenv("OPENAI_API_KEY")
|
16 |
+
|
17 |
+
if not atlas_uri:
|
18 |
+
return False, """Please set up your MongoDB Atlas credentials:
|
19 |
+
1. Go to Settings tab
|
20 |
+
2. Add ATLAS_URI as a Repository Secret
|
21 |
+
3. Paste your MongoDB connection string (should start with 'mongodb+srv://')"""
|
22 |
+
|
23 |
+
if not openai_key:
|
24 |
+
return False, """Please set up your OpenAI API key:
|
25 |
+
1. Go to Settings tab
|
26 |
+
2. Add OPENAI_API_KEY as a Repository Secret
|
27 |
+
3. Paste your OpenAI API key"""
|
28 |
+
|
29 |
+
return True, ""
|
30 |
|
31 |
+
def init_clients():
|
32 |
+
"""Initialize OpenAI and MongoDB clients"""
|
33 |
+
try:
|
34 |
+
openai_client = OpenAI()
|
35 |
+
db_utils = DatabaseUtils()
|
36 |
+
return openai_client, db_utils
|
37 |
+
except Exception as e:
|
38 |
+
return None, None
|
39 |
|
40 |
def get_field_names(db_name: str, collection_name: str) -> list[str]:
|
41 |
"""Get list of fields in the collection"""
|
|
|
174 |
with gr.Blocks(title="MongoDB Vector Search Tool") as iface:
|
175 |
gr.Markdown("# MongoDB Vector Search Tool")
|
176 |
|
177 |
+
# Check credentials first
|
178 |
+
has_creds, cred_message = check_credentials()
|
179 |
+
if not has_creds:
|
180 |
+
gr.Markdown(f"""
|
181 |
+
## ⚠️ Setup Required
|
182 |
+
|
183 |
+
{cred_message}
|
184 |
+
|
185 |
+
After setting up credentials, refresh this page.
|
186 |
+
""")
|
187 |
+
else:
|
188 |
+
# Initialize clients
|
189 |
+
openai_client, db_utils = init_clients()
|
190 |
+
if not openai_client or not db_utils:
|
191 |
+
gr.Markdown("""
|
192 |
+
## ⚠️ Connection Error
|
193 |
+
|
194 |
+
Failed to connect to MongoDB Atlas or OpenAI. Please check your credentials and try again.
|
195 |
+
""")
|
196 |
+
else:
|
197 |
+
# Get available databases
|
198 |
+
try:
|
199 |
+
databases = db_utils.get_databases()
|
200 |
|
201 |
with gr.Tab("Generate Embeddings"):
|
202 |
with gr.Row():
|