Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,60 @@
|
|
| 1 |
-
from
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
@app.
|
| 33 |
-
def generate_image():
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Convert output image to Base64
|
| 63 |
-
buffered = BytesIO()
|
| 64 |
-
images[0].save(buffered, format="PNG")
|
| 65 |
-
image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 66 |
-
|
| 67 |
-
return jsonify({"image": image_base64})
|
| 68 |
-
except Exception as e:
|
| 69 |
-
return jsonify({"error": str(e)}), 500
|
| 70 |
-
|
| 71 |
-
if __name__ == "__main__":
|
| 72 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
import base64
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
# Initialize FastAPI app
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Load Hugging Face pipeline components
|
| 13 |
+
model_id = "fyp1/sketchToImage"
|
| 14 |
+
controlnet = ControlNetModel.from_pretrained(f"{model_id}/controlnet", torch_dtype=torch.float16)
|
| 15 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 16 |
+
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(f"{model_id}/scheduler")
|
| 17 |
+
|
| 18 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 19 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 20 |
+
controlnet=controlnet,
|
| 21 |
+
vae=vae,
|
| 22 |
+
scheduler=scheduler,
|
| 23 |
+
safety_checker=None,
|
| 24 |
+
torch_dtype=torch.float16,
|
| 25 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 26 |
+
|
| 27 |
+
class GenerateRequest(BaseModel):
|
| 28 |
+
prompt: str
|
| 29 |
+
negative_prompt: str
|
| 30 |
+
sketch: str # Base64 encoded image
|
| 31 |
+
|
| 32 |
+
@app.post("/generate")
|
| 33 |
+
async def generate_image(data: GenerateRequest):
|
| 34 |
+
try:
|
| 35 |
+
# Decode and preprocess the sketch image
|
| 36 |
+
sketch_bytes = base64.b64decode(data.sketch)
|
| 37 |
+
sketch_image = Image.open(BytesIO(sketch_bytes)).convert("L") # Convert to grayscale
|
| 38 |
+
sketch_image = sketch_image.resize((1024, 1024))
|
| 39 |
+
|
| 40 |
+
# Generate the image using the pipeline
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
images = pipe(
|
| 43 |
+
prompt=data.prompt,
|
| 44 |
+
negative_prompt=data.negative_prompt,
|
| 45 |
+
image=sketch_image,
|
| 46 |
+
controlnet_conditioning_scale=1.0,
|
| 47 |
+
width=1024,
|
| 48 |
+
height=1024,
|
| 49 |
+
num_inference_steps=30,
|
| 50 |
+
).images
|
| 51 |
+
|
| 52 |
+
# Convert output image to Base64
|
| 53 |
+
buffered = BytesIO()
|
| 54 |
+
images[0].save(buffered, format="PNG")
|
| 55 |
+
image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 56 |
+
|
| 57 |
+
return {"image": image_base64}
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|