humut commited on
Commit
cff9dc4
·
verified ·
1 Parent(s): e8dbeb7

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """CAD_LLM2 — HF Space Proxy: humut/CAD_LLM2
2
+ Játék + Film/VFX modellek"""
3
+ import os
4
+ import json
5
+ import httpx
6
+ from fastapi import FastAPI, HTTPException
7
+ from fastapi.middleware.cors import CORSMiddleware
8
+ from pydantic import BaseModel
9
+
10
+ app = FastAPI(title="CAD_LLM2")
11
+ app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
12
+
13
+ HF_SPACE = os.getenv("HF_SPACE", "humut/CAD_LLM2")
14
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
15
+ MODEL_NAME = os.getenv("MODEL_NAME", "cad_llm2")
16
+
17
+ with open("config.json") as f:
18
+ CONFIG = json.load(f)
19
+
20
+ class ChatRequest(BaseModel):
21
+ message: str
22
+ history: list[dict] = []
23
+
24
+ @app.get("/health")
25
+ async def health():
26
+ return {"status": "ok", "model": MODEL_NAME, "hf_space": HF_SPACE}
27
+
28
+ @app.get("/config")
29
+ async def config():
30
+ return CONFIG
31
+
32
+ @app.post("/chat")
33
+ async def chat(req: ChatRequest):
34
+ if not HF_TOKEN:
35
+ return {"response": f"[{MODEL_NAME}] No HF_TOKEN configured. Prompt: {req.message}", "dsl": ""}
36
+ try:
37
+ async with httpx.AsyncClient(timeout=120) as client:
38
+ resp = await client.post(
39
+ f"https://api-inference.huggingface.co/models/{HF_SPACE}",
40
+ headers={"Authorization": f"Bearer {HF_TOKEN}"},
41
+ json={"inputs": req.message, "parameters": {"max_new_tokens": 512, "temperature": 0.7}}
42
+ )
43
+ if resp.status_code == 200:
44
+ data = resp.json()
45
+ raw = data[0].get("generated_text", "") if isinstance(data, list) else str(data)
46
+ return {"response": raw, "model": MODEL_NAME}
47
+ except Exception as e:
48
+ return {"response": f"[{MODEL_NAME}] Error: {str(e)}", "dsl": ""}
49
+ return {"response": f"[{MODEL_NAME}] No response from HF", "dsl": ""}
50
+
51
+ if __name__ == "__main__":
52
+ import uvicorn
53
+ uvicorn.run(app, host="0.0.0.0", port=8000)