Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import json
|
| 2 |
import re
|
| 3 |
-
import os, requests
|
| 4 |
import fitz # PyMuPDF We use PyMuPDF (fitz) to capture hierarchy (section β subsection β subsubsection β content/bullets).
|
| 5 |
from collections import Counter
|
| 6 |
from fastapi import FastAPI
|
|
@@ -591,11 +591,24 @@ def startup_event():
|
|
| 591 |
# return generate_rag_response(req.query, retriever, k=req.top_k)
|
| 592 |
|
| 593 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
# =============================
|
| 595 |
# Step 5: Chat Function
|
| 596 |
# =============================
|
| 597 |
def chat_fn(message, history):
|
| 598 |
global retriever
|
|
|
|
| 599 |
if retriever is None:
|
| 600 |
return "β οΈ Retriever not initialized. Please rebuild or check vector DB."
|
| 601 |
answer = generate_rag_response(message, retriever)
|
|
|
|
| 1 |
import json
|
| 2 |
import re
|
| 3 |
+
import os, requests, time
|
| 4 |
import fitz # PyMuPDF We use PyMuPDF (fitz) to capture hierarchy (section β subsection β subsubsection β content/bullets).
|
| 5 |
from collections import Counter
|
| 6 |
from fastapi import FastAPI
|
|
|
|
| 591 |
# return generate_rag_response(req.query, retriever, k=req.top_k)
|
| 592 |
|
| 593 |
|
| 594 |
+
|
| 595 |
+
|
| 596 |
+
def wait_for_key(key_name="OPENAI_API_KEY", timeout=10):
|
| 597 |
+
for _ in range(timeout):
|
| 598 |
+
if os.getenv(key_name):
|
| 599 |
+
print(f"β
{key_name} available.")
|
| 600 |
+
return True
|
| 601 |
+
print(f"β³ Waiting for {key_name}...")
|
| 602 |
+
time.sleep(1)
|
| 603 |
+
print(f"β {key_name} not found after {timeout} seconds.")
|
| 604 |
+
return False
|
| 605 |
+
|
| 606 |
# =============================
|
| 607 |
# Step 5: Chat Function
|
| 608 |
# =============================
|
| 609 |
def chat_fn(message, history):
|
| 610 |
global retriever
|
| 611 |
+
wait_for_key()
|
| 612 |
if retriever is None:
|
| 613 |
return "β οΈ Retriever not initialized. Please rebuild or check vector DB."
|
| 614 |
answer = generate_rag_response(message, retriever)
|