humut commited on
Commit
ff6cfaa
·
verified ·
1 Parent(s): 9fd7a24

Upload app.py with huggingface_hub

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