Fix: Update API and config for text-to-image task
Browse files- api.py +25 -16
- config.json +17 -29
api.py
CHANGED
|
@@ -1,27 +1,36 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
-
import
|
| 6 |
-
from fastapi import FastAPI, Request, HTTPException
|
| 7 |
-
import uvicorn
|
| 8 |
|
| 9 |
-
# Import the handler
|
| 10 |
-
from handler import
|
| 11 |
|
|
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.post("/")
|
| 15 |
-
async def
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
raise HTTPException(status_code=500, detail=str(e))
|
| 21 |
|
|
|
|
| 22 |
@app.get("/health")
|
| 23 |
-
async def
|
| 24 |
-
return {"status": "ok"}
|
| 25 |
-
|
| 26 |
-
if __name__ == "__main__":
|
| 27 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import Dict, Any, Optional, List, Union
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import torch
|
| 8 |
import os
|
| 9 |
+
import sys
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Import the handler
|
| 12 |
+
from handler import EndpointHandler
|
| 13 |
|
| 14 |
+
# Initialize the app
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
# Initialize the model
|
| 18 |
+
model = EndpointHandler(model_dir="/code")
|
| 19 |
+
|
| 20 |
+
class TextToImageRequest(BaseModel):
|
| 21 |
+
inputs: Union[str, Dict[str, Any]]
|
| 22 |
+
parameters: Optional[Dict[str, Any]] = None
|
| 23 |
+
|
| 24 |
@app.post("/")
|
| 25 |
+
async def text_to_image(request: TextToImageRequest):
|
| 26 |
try:
|
| 27 |
+
# Process the request
|
| 28 |
+
result = model(request.dict())
|
| 29 |
+
return result
|
| 30 |
except Exception as e:
|
| 31 |
raise HTTPException(status_code=500, detail=str(e))
|
| 32 |
|
| 33 |
+
# Add a health check endpoint
|
| 34 |
@app.get("/health")
|
| 35 |
+
async def health():
|
| 36 |
+
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
config.json
CHANGED
|
@@ -1,34 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"model_type": "vector_graphics",
|
| 3 |
-
"name": "DiffSketcher",
|
| 4 |
"task": "text-to-image",
|
| 5 |
-
"
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"guidance_scale": {
|
| 23 |
-
"type": "number",
|
| 24 |
-
"description": "Guidance scale for the diffusion model",
|
| 25 |
-
"required": false,
|
| 26 |
-
"default": 7.5
|
| 27 |
-
},
|
| 28 |
-
"seed": {
|
| 29 |
-
"type": "integer",
|
| 30 |
-
"description": "Random seed for reproducibility",
|
| 31 |
-
"required": false
|
| 32 |
}
|
| 33 |
}
|
| 34 |
}
|
|
|
|
| 1 |
{
|
|
|
|
|
|
|
| 2 |
"task": "text-to-image",
|
| 3 |
+
"framework": "custom",
|
| 4 |
+
"model-type": "text-to-image",
|
| 5 |
+
"inference": {
|
| 6 |
+
"task_type": "text-to-image",
|
| 7 |
+
"text_to_image": {
|
| 8 |
+
"inputs": {
|
| 9 |
+
"text": {
|
| 10 |
+
"type": "string",
|
| 11 |
+
"description": "Input text prompt"
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"outputs": {
|
| 15 |
+
"image": {
|
| 16 |
+
"type": "image",
|
| 17 |
+
"description": "Generated image"
|
| 18 |
+
}
|
| 19 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
}
|
| 22 |
}
|