jree423 commited on
Commit
eaa1a7e
·
verified ·
1 Parent(s): 54f01ed

Fix: Update API and config for text-to-image task

Browse files
Files changed (2) hide show
  1. api.py +25 -16
  2. config.json +17 -29
api.py CHANGED
@@ -1,27 +1,36 @@
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
- # Import the handler directly
10
- from handler import handler
11
 
 
12
  app = FastAPI()
13
 
 
 
 
 
 
 
 
14
  @app.post("/")
15
- async def process_request(request: Request):
16
  try:
17
- json_data = await request.json()
18
- return handler(json_data)
 
19
  except Exception as e:
20
  raise HTTPException(status_code=500, detail=str(e))
21
 
 
22
  @app.get("/health")
23
- async def health_check():
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
- "parameters": {
6
- "prompt": {
7
- "type": "string",
8
- "description": "Text description of the desired output",
9
- "required": true
10
- },
11
- "negative_prompt": {
12
- "type": "string",
13
- "description": "Text to avoid in the generation",
14
- "required": false
15
- },
16
- "num_paths": {
17
- "type": "integer",
18
- "description": "Number of paths in the SVG",
19
- "required": false,
20
- "default": 100
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
  }