hamdibenjarrar commited on
Commit
c9f5d10
·
verified ·
1 Parent(s): bbb043b
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import os
4
+ import requests
5
+
6
+ app = FastAPI()
7
+
8
+ HF_ROUTER = os.environ.get("HF_ROUTER_URL")
9
+ HF_KEY = os.environ.get("HF_TOKEN_MARCUS")
10
+
11
+ class Msg(BaseModel):
12
+ message: str
13
+
14
+ @app.post("/chat")
15
+ def chat(msg: Msg):
16
+ if HF_ROUTER and HF_KEY:
17
+ payload = {"inputs": msg.message, "parameters":{"max_new_tokens":300,"temperature":0.7}}
18
+ headers = {"Authorization": f"Bearer {HF_KEY}", "Content-Type": "application/json"}
19
+ r = requests.post(HF_ROUTER, json=payload, headers=headers, timeout=60)
20
+ if r.status_code != 200:
21
+ raise HTTPException(status_code=502, detail=f"HF call failed: {r.status_code} {r.text[:300]}")
22
+ data = r.json()
23
+ reply = data[0]["generated_text"] if isinstance(data,list) else data.get("generated_text", str(data))
24
+ return {"reply": reply}
25
+ return {"reply": "Space ready as proxy. No hosted model configured."}
26
+
27
+ @app.get("/")
28
+ def root():
29
+ return {"status": "ok"}