Spaces:
Running
Running
Upload 5 files
Browse files- Dockerfile +30 -0
- README.md +26 -10
- app.py +21 -0
- requirements.txt +6 -0
- utils.py +36 -0
Dockerfile
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10-slim
|
2 |
+
|
3 |
+
# System dependencies
|
4 |
+
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
5 |
+
|
6 |
+
WORKDIR /workspace
|
7 |
+
|
8 |
+
# Install Python dependencies
|
9 |
+
COPY requirements.txt .
|
10 |
+
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
|
11 |
+
|
12 |
+
# Download models and adapters at build time
|
13 |
+
# You can replace MODEL_ID or adapter repos as needed
|
14 |
+
|
15 |
+
# -- Download Stable Diffusion v1-5 weights --
|
16 |
+
RUN python -c "from diffusers import StableDiffusionImg2ImgPipeline; StableDiffusionImg2ImgPipeline.from_pretrained('runwayml/stable-diffusion-v1-5', cache_dir='./models', torch_dtype='float32')"
|
17 |
+
|
18 |
+
# -- Download IP-Adapter weights (official Hugging Face repo) --
|
19 |
+
RUN mkdir -p ./models/ip_adapter && \
|
20 |
+
wget -O ./models/ip_adapter/ip-adapter_sd15.bin https://huggingface.co/h94/IP-Adapter/resolve/main/models/ip-adapter_sd15.bin
|
21 |
+
|
22 |
+
# Copy app code
|
23 |
+
COPY . .
|
24 |
+
|
25 |
+
# Set environment (disable gradio analytics, useful for Spaces)
|
26 |
+
ENV GRADIO_ANALYTICS_ENABLED="False"
|
27 |
+
|
28 |
+
EXPOSE 7860
|
29 |
+
|
30 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
@@ -1,10 +1,26 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AI Sticker Generator (Stable Diffusion + IP-Adapter/InstantID/ControlNet, CPU-ready)
|
2 |
+
|
3 |
+
## 🚀 Features
|
4 |
+
- Generate emoji/sticker-style faces from your own photos
|
5 |
+
- Uses Stable Diffusion + one adapter (IP-Adapter, InstantID, or ControlNet)
|
6 |
+
- Runs on CPU
|
7 |
+
|
8 |
+
## 🖥️ Quickstart: Hugging Face Spaces
|
9 |
+
|
10 |
+
1. **Create a new Space:**
|
11 |
+
Go to [https://huggingface.co/spaces](https://huggingface.co/spaces), click **"Create new Space"**.
|
12 |
+
Select **"Gradio"** as the SDK, choose **CPU** for hardware.
|
13 |
+
|
14 |
+
2. **Upload these files.**
|
15 |
+
|
16 |
+
3. **Add model weights:**
|
17 |
+
- For IP-Adapter: Download weights from the official [IP-Adapter repo](https://github.com/tencent-ailab/IP-Adapter) or Hugging Face model hub.
|
18 |
+
- Place them in the `models/` directory.
|
19 |
+
- Alternatively, adjust the code to download them at runtime.
|
20 |
+
|
21 |
+
4. **Requirements are auto-installed from `requirements.txt` on Hugging Face.**
|
22 |
+
|
23 |
+
5. **Hit "Run".**
|
24 |
+
|
25 |
+
## 🛠️ Local testing
|
26 |
+
- Create a virtual environment and install dependencies:
|
app.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from utils import generate_sticker
|
3 |
+
|
4 |
+
def predict(image, prompt):
|
5 |
+
result_img = generate_sticker(image, prompt)
|
6 |
+
return result_img
|
7 |
+
|
8 |
+
with gr.Blocks() as demo:
|
9 |
+
gr.Markdown("# 🦄 AI Sticker Generator (Stable Diffusion + IP-Adapter)")
|
10 |
+
with gr.Row():
|
11 |
+
image_input = gr.Image(type="pil", label="Upload your photo")
|
12 |
+
prompt_input = gr.Textbox(
|
13 |
+
label="Prompt (style or mood for emoji)",
|
14 |
+
value="cartoon emoji, white outline, clean background",
|
15 |
+
)
|
16 |
+
output_image = gr.Image(label="Sticker Output")
|
17 |
+
run_btn = gr.Button("Generate Sticker")
|
18 |
+
run_btn.click(predict, inputs=[image_input, prompt_input], outputs=output_image)
|
19 |
+
|
20 |
+
if __name__ == "__main__":
|
21 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Only install what's needed for Stable Diffusion, IP-Adapter, and CPU inference
|
2 |
+
diffusers==0.28.0
|
3 |
+
transformers
|
4 |
+
torch # Version will depend on your platform
|
5 |
+
accelerate
|
6 |
+
Pillow
|
utils.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# --- Place any download or path setup here ---
|
6 |
+
MODEL_ID = "runwayml/stable-diffusion-v1-5" # Can swap for custom path if using IP-Adapter
|
7 |
+
DEVICE = "cpu"
|
8 |
+
MODEL_CACHE = "./models"
|
9 |
+
|
10 |
+
# (Optional) Download IP-Adapter weights and patch pipeline if desired
|
11 |
+
|
12 |
+
def generate_sticker(input_image, prompt):
|
13 |
+
"""
|
14 |
+
Given a user image and a prompt, generates a sticker/emoji-style portrait.
|
15 |
+
"""
|
16 |
+
# Load the model (download if not present)
|
17 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
18 |
+
MODEL_ID,
|
19 |
+
torch_dtype=torch.float32,
|
20 |
+
cache_dir=MODEL_CACHE,
|
21 |
+
safety_checker=None, # Disable for demo/testing
|
22 |
+
).to(DEVICE)
|
23 |
+
|
24 |
+
# Preprocess the image (resize, etc)
|
25 |
+
init_image = input_image.convert("RGB").resize((512, 512))
|
26 |
+
|
27 |
+
# Run inference (low strength for identity preservation)
|
28 |
+
result = pipe(
|
29 |
+
prompt=prompt,
|
30 |
+
image=init_image,
|
31 |
+
strength=0.65,
|
32 |
+
guidance_scale=7.5,
|
33 |
+
num_inference_steps=30
|
34 |
+
)
|
35 |
+
# Return the generated image (as PIL)
|
36 |
+
return result.images[0]
|