Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_methods=["*"],
|
| 13 |
+
allow_headers=["*"],
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# NO MORE MANUALLY PASTED TOKENS!
|
| 17 |
+
# Hugging Face provides this automatically if the Space is yours.
|
| 18 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 19 |
+
API_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
|
| 20 |
+
|
| 21 |
+
@app.post("/ask")
|
| 22 |
+
async def ask_ai(request: Request):
|
| 23 |
+
data = await request.json()
|
| 24 |
+
|
| 25 |
+
# If the secret isn't set, this will tell us in the logs
|
| 26 |
+
if not HF_TOKEN:
|
| 27 |
+
return JSONResponse(content={"error": "Secret HF_TOKEN not found in Settings"}, status_code=500)
|
| 28 |
+
|
| 29 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 30 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
| 31 |
+
return JSONResponse(content=response.json())
|