Upload api.py with huggingface_hub
Browse files
api.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 7 |
+
import uvicorn
|
| 8 |
+
|
| 9 |
+
# Determine which handler to use based on environment variable
|
| 10 |
+
model_id = os.environ.get("MODEL_ID", "")
|
| 11 |
+
|
| 12 |
+
if "diffsketcher_edit" in model_id:
|
| 13 |
+
from diffsketcher_edit_handler import Handler
|
| 14 |
+
elif "svgdreamer" in model_id:
|
| 15 |
+
from svgdreamer_handler import Handler
|
| 16 |
+
elif "diffsketcher" in model_id:
|
| 17 |
+
from diffsketcher_handler import Handler
|
| 18 |
+
else:
|
| 19 |
+
raise ValueError(f"Unknown model ID: {model_id}")
|
| 20 |
+
|
| 21 |
+
app = FastAPI()
|
| 22 |
+
handler = Handler()
|
| 23 |
+
|
| 24 |
+
@app.post("/")
|
| 25 |
+
async def process_request(request: Request):
|
| 26 |
+
try:
|
| 27 |
+
json_data = await request.json()
|
| 28 |
+
return handler(json_data)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 31 |
+
|
| 32 |
+
@app.get("/health")
|
| 33 |
+
async def health_check():
|
| 34 |
+
return {"status": "ok"}
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|