Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import requests
|
| 4 |
-
import io
|
| 5 |
import random
|
| 6 |
import os
|
| 7 |
-
from
|
| 8 |
-
import base64
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -18,7 +16,7 @@ class ImageRequest(BaseModel):
|
|
| 18 |
negative_prompt: str = "(deformed, distorted, disfigured), poorly drawn, bad anatomy"
|
| 19 |
steps: int = 4
|
| 20 |
cfg_scale: float = 7.0
|
| 21 |
-
sampler: str = "DPM++
|
| 22 |
seed: int = -1
|
| 23 |
strength: float = 0.7
|
| 24 |
|
|
@@ -44,19 +42,12 @@ def query(prompt: str, negative_prompt: str, steps: int, cfg_scale: float,
|
|
| 44 |
if response.status_code != 200:
|
| 45 |
raise HTTPException(status_code=response.status_code, detail=response.text)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
image = Image.open(io.BytesIO(image_bytes))
|
| 49 |
-
|
| 50 |
-
buffered = io.BytesIO()
|
| 51 |
-
image.save(buffered, format="PNG")
|
| 52 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 53 |
-
|
| 54 |
-
return {"image": f"data:image/png;base64,{img_str}"}
|
| 55 |
|
| 56 |
@app.post("/generate")
|
| 57 |
async def generate_image(request: ImageRequest):
|
| 58 |
try:
|
| 59 |
-
|
| 60 |
prompt=request.prompt,
|
| 61 |
negative_prompt=request.negative_prompt,
|
| 62 |
steps=request.steps,
|
|
@@ -65,7 +56,7 @@ async def generate_image(request: ImageRequest):
|
|
| 65 |
seed=request.seed,
|
| 66 |
strength=request.strength
|
| 67 |
)
|
| 68 |
-
return
|
| 69 |
except HTTPException as e:
|
| 70 |
raise e
|
| 71 |
except Exception as e:
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import requests
|
|
|
|
| 4 |
import random
|
| 5 |
import os
|
| 6 |
+
from fastapi.responses import Response
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 16 |
negative_prompt: str = "(deformed, distorted, disfigured), poorly drawn, bad anatomy"
|
| 17 |
steps: int = 4
|
| 18 |
cfg_scale: float = 7.0
|
| 19 |
+
sampler: str = "DPM++ 2M Karras"
|
| 20 |
seed: int = -1
|
| 21 |
strength: float = 0.7
|
| 22 |
|
|
|
|
| 42 |
if response.status_code != 200:
|
| 43 |
raise HTTPException(status_code=response.status_code, detail=response.text)
|
| 44 |
|
| 45 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
@app.post("/generate")
|
| 48 |
async def generate_image(request: ImageRequest):
|
| 49 |
try:
|
| 50 |
+
raw_data = query(
|
| 51 |
prompt=request.prompt,
|
| 52 |
negative_prompt=request.negative_prompt,
|
| 53 |
steps=request.steps,
|
|
|
|
| 56 |
seed=request.seed,
|
| 57 |
strength=request.strength
|
| 58 |
)
|
| 59 |
+
return Response(content=raw_data, media_type="application/octet-stream")
|
| 60 |
except HTTPException as e:
|
| 61 |
raise e
|
| 62 |
except Exception as e:
|