Acytel commited on
Commit
766d875
·
1 Parent(s): 6ac5492

Switched to requests library to fix Render DNS bug and added wait_for_model flag

Browse files
Files changed (1) hide show
  1. api.py +27 -14
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
- # Fetch embeddings from HuggingFace
41
- async with httpx.AsyncClient() as client:
42
- hf_response = await client.post(
 
 
 
 
43
  hf_url,
44
  headers=headers,
45
- json={"inputs": [query.question]},
46
- timeout=60.0
 
 
 
47
  )
 
48
 
49
- hf_data = hf_response.json()
50
-
51
- # NEW: Handle the Hugging Face "Cold Start" safely
52
- if isinstance(hf_data, dict) and "error" in hf_data:
53
- print(f"HuggingFace is sleeping: {hf_data}")
54
- async def wakeup_message():
55
- yield "The AI Brain is waking up from a deep sleep! Please wait 20 seconds and click Ask AI again."
56
- return StreamingResponse(wakeup_message(), media_type="text/plain")
57
 
58
- query_numbers = hf_data[0]
 
 
 
 
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,