Spaces:
Paused
Paused
Update app/main.py
Browse files4 unmodified lines56789103 unmodified lines1415161718192021222324252627284 unmodified linesapp = FastAPI()HF_TOKEN = os.environ.get("HF_TOKEN")HF_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"3 unmodified lines@app.post("/predict")async def predict(query: Query):"""Relay endpoint: forwards text to Hugging Face Inference API."""headers = {"Authorization": f"Bearer {HF_TOKEN}"}url = f"https://api-inference.huggingface.co/models/{HF_MODEL}"async with httpx.AsyncClient() as client:resp = await client.post(url, headers=headers, json=query.dict())return resp.json()@app.get("/")def home():"""Simple health check endpoint."""return {"status": "ok", "message": "FastAPI relay is running!"}4 unmodified lines5678910113 unmodified lines151617181920212223242526272829303132333435363738394041424344454647484950515253545556574 unmodified linesapp = FastAPI()# Get the Hugging Face token from environment variablesHF_TOKEN = os.environ.get("HF_TOKEN")HF_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"3 unmodified lines@app.post("/predict")async def predict(query: Query):"""Relay endpoint: forwards text to Hugging Face Inference API."""# Check if HF_TOKEN is setif not HF_TOKEN:return {"error": "Hugging Face token not found. Please set the HF_TOKEN environment variable."}headers = {"Authorization": f"Bearer {HF_TOKEN}"}url = f"https://api-inference.huggingface.co/models/{HF_MODEL}"try:async with httpx.AsyncClient(timeout=30.0) as client:resp = await client.post(url, headers=headers, json=query.dict())# Print the status code and response body for debuggingprint(f"Hugging Face API Status Code: {resp.status_code}")print(f"Hugging Face API Response Body: {resp.text}")# Check for a successful response (status code 200)resp.raise_for_status() # This will raise an exception for 4xx/5xx responsesreturn resp.json()except httpx.HTTPStatusError as e:# This block catches 4xx and 5xx errors from the Hugging Face APIprint(f"HTTP error occurred: {e.response.status_code} - {e.response.text}")return {"error": "Failed to get a successful response from Hugging Face.", "details": e.response.text}except httpx.RequestError as e:# This block catches network-related errorsprint(f"An error occurred while requesting {e.request.url!r}.")return {"error": "A network error occurred.", "details": str(e)}except Exception as e:# This is a generic catch-all for any other errorsprint(f"An unexpected error occurred: {e}")return {"error": "An unexpected server error occurred.", "details": str(e)}@app.get("/")def home():"""Simple health check endpoint."""return {"status": "ok", "message": "FastAPI relay is running!"}
app/main.py+33-4