Spaces:
Paused
Paused
Upload 3 files
Browse files- app.py +72 -0
- dockerfile +22 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import base64
|
6 |
+
from io import BytesIO
|
7 |
+
from huggingface_hub import login
|
8 |
+
|
9 |
+
# Authenticate with Hugging Face Hub (ensure you replace 'your_token_here')
|
10 |
+
import os
|
11 |
+
login(os.environ["HF_TOKEN"])
|
12 |
+
|
13 |
+
# Initialize Flask app
|
14 |
+
app = Flask(__name__)
|
15 |
+
|
16 |
+
# Load Hugging Face pipeline components
|
17 |
+
model_id = "fyp1/sketchToImage"
|
18 |
+
controlnet = ControlNetModel.from_pretrained(f"{model_id}/controlnet", torch_dtype=torch.float16)
|
19 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
20 |
+
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(f"{model_id}/scheduler")
|
21 |
+
|
22 |
+
# Initialize Stable Diffusion XL ControlNet Pipeline
|
23 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
24 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
25 |
+
controlnet=controlnet,
|
26 |
+
vae=vae,
|
27 |
+
scheduler=scheduler,
|
28 |
+
safety_checker=None,
|
29 |
+
torch_dtype=torch.float16,
|
30 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
31 |
+
|
32 |
+
@app.route("/generate", methods=["POST"])
|
33 |
+
def generate_image():
|
34 |
+
data = request.json
|
35 |
+
|
36 |
+
# Extract prompt, sketch image (Base64), and optional parameters
|
37 |
+
prompt = data.get("prompt", "A default prompt")
|
38 |
+
negative_prompt = data.get("negative_prompt", "low quality, blurry, bad details")
|
39 |
+
sketch_base64 = data.get("sketch", None)
|
40 |
+
|
41 |
+
if not sketch_base64:
|
42 |
+
return jsonify({"error": "Sketch image is required."}), 400
|
43 |
+
|
44 |
+
try:
|
45 |
+
# Decode and preprocess the sketch image
|
46 |
+
sketch_bytes = base64.b64decode(sketch_base64)
|
47 |
+
sketch_image = Image.open(BytesIO(sketch_bytes)).convert("L") # Convert to grayscale
|
48 |
+
sketch_image = sketch_image.resize((1024, 1024))
|
49 |
+
|
50 |
+
# Generate the image using the pipeline
|
51 |
+
with torch.no_grad():
|
52 |
+
images = pipe(
|
53 |
+
prompt=prompt,
|
54 |
+
negative_prompt=negative_prompt,
|
55 |
+
image=sketch_image,
|
56 |
+
controlnet_conditioning_scale=1.0,
|
57 |
+
width=1024,
|
58 |
+
height=1024,
|
59 |
+
num_inference_steps=30,
|
60 |
+
).images
|
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)
|
dockerfile
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python image
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# Set the working directory
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy all files from the local directory to the container
|
8 |
+
COPY . /app
|
9 |
+
|
10 |
+
# Install system dependencies
|
11 |
+
RUN apt-get update && apt-get install -y \
|
12 |
+
libgl1-mesa-glx \
|
13 |
+
&& rm -rf /var/lib/apt/lists/*
|
14 |
+
|
15 |
+
# Install Python dependencies
|
16 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
17 |
+
|
18 |
+
# Expose port for the FastAPI server
|
19 |
+
EXPOSE 7860
|
20 |
+
|
21 |
+
# Run the FastAPI app
|
22 |
+
CMD ["python", "app.py"]
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.95.2
|
2 |
+
uvicorn==0.21.1
|
3 |
+
torch==2.0.1
|
4 |
+
diffusers==0.22.1
|
5 |
+
transformers==4.33.0
|
6 |
+
pillow==9.5.0
|
7 |
+
huggingface_hub==0.17.2
|