Spaces:
Sleeping
Sleeping
Switched to requests library to fix Render DNS bug and added wait_for_model flag
Browse files
api.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import httpx
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.responses import StreamingResponse
|
|
@@ -37,25 +38,37 @@ async def rag_query(query: SearchQuery):
|
|
| 37 |
hf_url = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
|
| 38 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
hf_url,
|
| 44 |
headers=headers,
|
| 45 |
-
json={
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
result = supabase.rpc("match_documents", {
|
| 61 |
"query_embedding": query_numbers,
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
import httpx
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.responses import StreamingResponse
|
|
|
|
| 38 |
hf_url = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
|
| 39 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 40 |
|
| 41 |
+
# NEW: Bulletproof synchronous network call (Bypasses Render DNS bugs)
|
| 42 |
+
hf_url = "https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-MiniLM-L6-v2"
|
| 43 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 44 |
+
|
| 45 |
+
print("Pinging Hugging Face...")
|
| 46 |
+
try:
|
| 47 |
+
hf_response = requests.post(
|
| 48 |
hf_url,
|
| 49 |
headers=headers,
|
| 50 |
+
json={
|
| 51 |
+
"inputs": [query.question],
|
| 52 |
+
"options": {"wait_for_model": True} # Forces HF to wake up automatically
|
| 53 |
+
},
|
| 54 |
+
timeout=120.0 # Gives the model plenty of time to boot up
|
| 55 |
)
|
| 56 |
+
hf_data = hf_response.json()
|
| 57 |
|
| 58 |
+
# Safety check if HF is completely overloaded
|
| 59 |
+
if isinstance(hf_data, dict) and "error" in hf_data:
|
| 60 |
+
print(f"HuggingFace Error: {hf_data}")
|
| 61 |
+
async def error_msg():
|
| 62 |
+
yield "The AI Brain is experiencing heavy traffic. Please try asking again in 15 seconds!"
|
| 63 |
+
return StreamingResponse(error_msg(), media_type="text/plain")
|
| 64 |
+
|
| 65 |
+
query_numbers = hf_data[0]
|
| 66 |
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Critical Network Error: {e}")
|
| 69 |
+
async def crash_msg():
|
| 70 |
+
yield "The AI Brain is temporarily disconnected from the network. Please try again."
|
| 71 |
+
return StreamingResponse(crash_msg(), media_type="text/plain")
|
| 72 |
|
| 73 |
result = supabase.rpc("match_documents", {
|
| 74 |
"query_embedding": query_numbers,
|