File size: 1,274 Bytes
17e9562
 
068c2e4
17e9562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
068c2e4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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")

@app.post("/api/ask")
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)