Spaces:
Runtime error
Runtime error
| import os | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse | |
| from fastapi.staticfiles import StaticFiles | |
| import httpx | |
| app = FastAPI() | |
| app.mount("/", StaticFiles(directory="static", html=True), name="static") | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| async def ask(request: Request): | |
| data = await request.json() | |
| question = data.get("question") | |
| if not question: | |
| return JSONResponse({"error": "No question provided"}, status_code=400) | |
| headers = {"Authorization": f"Bearer {HF_TOKEN}"} | |
| payload = { | |
| "messages": [{"role": "user", "content": question}], | |
| "max_tokens": 256 | |
| } | |
| async with httpx.AsyncClient() as client: | |
| resp = await client.post( | |
| "https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B-Instruct", | |
| headers=headers, | |
| json=payload, | |
| timeout=60 | |
| ) | |
| if resp.status_code == 200: | |
| data = resp.json() | |
| try: | |
| answer = data["choices"][0]["message"]["content"] | |
| except Exception: | |
| answer = str(data) | |
| return {"answer": answer} | |
| else: | |
| return JSONResponse({"error": "Failed to get AI response"}, status_code= | |
| 500) |