Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
#
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
@@ -14,11 +14,10 @@ app.add_middleware(
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
# Example: "https://api-inference.huggingface.co/models/google/gemma-2b"
|
| 21 |
-
# If your model is danosethrus/EthioDoc, put that here:
|
| 22 |
MODEL_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
|
| 23 |
|
| 24 |
@app.get("/", response_class=HTMLResponse)
|
|
@@ -26,23 +25,34 @@ async def home():
|
|
| 26 |
try:
|
| 27 |
with open("index.html") as f:
|
| 28 |
return f.read()
|
| 29 |
-
except:
|
| 30 |
-
return "index.html not found in Files tab!"
|
| 31 |
|
| 32 |
@app.post("/ask")
|
| 33 |
async def ask_ai(request: Request):
|
| 34 |
-
|
| 35 |
|
| 36 |
headers = {
|
| 37 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 38 |
"Content-Type": "application/json"
|
| 39 |
}
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
print(f"Body: {response.text}")
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# This allows your index.html to talk to your Python backend
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Pulls your secret token from the 'Settings' vault
|
| 18 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 19 |
|
| 20 |
+
# TARGET: Your actual Model (without the 's')
|
|
|
|
|
|
|
| 21 |
MODEL_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
|
| 22 |
|
| 23 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 25 |
try:
|
| 26 |
with open("index.html") as f:
|
| 27 |
return f.read()
|
| 28 |
+
except FileNotFoundError:
|
| 29 |
+
return "Error: index.html not found in Files tab!"
|
| 30 |
|
| 31 |
@app.post("/ask")
|
| 32 |
async def ask_ai(request: Request):
|
| 33 |
+
user_data = await request.json()
|
| 34 |
|
| 35 |
headers = {
|
| 36 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 37 |
"Content-Type": "application/json"
|
| 38 |
}
|
| 39 |
|
| 40 |
+
# 'wait_for_model' is the key: it prevents 503 errors while the brain loads
|
| 41 |
+
payload = {
|
| 42 |
+
"inputs": user_data.get("inputs", ""),
|
| 43 |
+
"options": {"wait_for_model": True}
|
| 44 |
+
}
|
|
|
|
| 45 |
|
| 46 |
+
try:
|
| 47 |
+
response = requests.post(MODEL_URL, headers=headers, json=payload, timeout=60)
|
| 48 |
+
|
| 49 |
+
# This helps you see the result in the 'Logs' tab
|
| 50 |
+
print(f"DEBUG: Status {response.status_code}")
|
| 51 |
+
print(f"DEBUG: Response {response.text}")
|
| 52 |
+
|
| 53 |
+
# If everything is okay, send the AI answer back to the website
|
| 54 |
+
return JSONResponse(content=response.json(), status_code=response.status_code)
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"CRITICAL ERROR: {str(e)}")
|
| 58 |
+
return JSONResponse({"error": "The bridge is broken. Check Logs."}, status_code=500)
|