jejunepixels commited on
Commit
a012807
·
verified ·
1 Parent(s): b573360

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, HTTPException
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from pydantic import BaseModel
5
+ from typing import List
6
+ import httpx
7
+ import logging
8
+
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
+
12
+ # Get HF token from Space Secrets
13
+ HF_TOKEN = os.environ.get("HF_TOKEN")
14
+ if not HF_TOKEN:
15
+ raise RuntimeError("HF_TOKEN environment variable not set")
16
+
17
+ app = FastAPI(title="HF Inference Proxy")
18
+
19
+ # Enable CORS for Unity
20
+ app.add_middleware(
21
+ CORSMiddleware,
22
+ allow_origins=["*"],
23
+ allow_credentials=True,
24
+ allow_methods=["*"],
25
+ allow_headers=["*"],
26
+ )
27
+
28
+ class Message(BaseModel):
29
+ role: str
30
+ content: str
31
+
32
+ class InferenceRequest(BaseModel):
33
+ model: str
34
+ temperature: float
35
+ messages: List[Message]
36
+ max_tokens: int = 512
37
+
38
+ @app.get("/")
39
+ async def root():
40
+ return {
41
+ "status": "running",
42
+ "message": "HF Inference Proxy Active",
43
+ "endpoints": {
44
+ "extract": "POST /extract"
45
+ }
46
+ }
47
+
48
+ @app.post("/extract")
49
+ async def proxy_inference(request: InferenceRequest):
50
+ try:
51
+ logger.info(f"Request: {request.model}, {len(request.messages)} messages")
52
+
53
+ async with httpx.AsyncClient(timeout=120.0) as client:
54
+ hf_response = await client.post(
55
+ "https://router.huggingface.co/v1/chat/completions",
56
+ json={
57
+ "model": f"{request.model}:fastest",
58
+ "temperature": request.temperature,
59
+ "max_tokens": request.max_tokens,
60
+ "messages": [
61
+ {"role": msg.role, "content": msg.content}
62
+ for msg in request.messages
63
+ ]
64
+ },
65
+ headers={
66
+ "Authorization": f"Bearer {HF_TOKEN}",
67
+ "Content-Type": "application/json"
68
+ }
69
+ )
70
+
71
+ if hf_response.status_code != 200:
72
+ logger.error(f"HF API error: {hf_response.text}")
73
+ raise HTTPException(
74
+ status_code=hf_response.status_code,
75
+ detail=hf_response.text
76
+ )
77
+
78
+ result = hf_response.json()
79
+ logger.info("Request successful")
80
+ return result
81
+
82
+ except httpx.HTTPError as e:
83
+ logger.error(f"HTTP error: {str(e)}")
84
+ raise HTTPException(status_code=500, detail=f"Proxy error: {str(e)}")
85
+ except Exception as e:
86
+ logger.error(f"Error: {str(e)}")
87
+ raise HTTPException(status_code=500, detail=f"Server error: {str(e)}")
88
+
89
+ @app.get("/health")
90
+ async def health():
91
+ return {"status": "healthy", "hf_configured": bool(HF_TOKEN)}