File size: 3,878 Bytes
1b47363
 
 
 
 
f89ca89
1b47363
 
026bd59
1b47363
 
58c0646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
33
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: CompVis Stable Diffusion V1 4
emoji: πŸƒ
colorFrom: pink
colorTo: purple
sdk: gradio
pinned: false
license: bigscience-openrail-m
sdk_version: 5.12.0
---

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118  # GPU support
pip install diffusers transformers flask pillow accelerate
from diffusers import StableDiffusionPipeline
import torch

# Authenticate Hugging Face
from huggingface_hub import login
login(token="your_hugging_face_token")

# Load Stable Diffusion v1-4
model_id = "CompVis/stable-diffusion-v1-4"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")  # Use GPU for faster performance
prompt = "A luxurious futuristic bathroom with marble walls and golden accents, panoramic views of a tropical jungle, ultra-realistic, 32k resolution"
num_steps = 50  # Number of diffusion steps
guidance_scale = 7.5  # Higher = more faithful to the prompt

# Generate an image
image = pipe(prompt, num_inference_steps=num_steps, guidance_scale=guidance_scale).images[0]

# Save the image
image.save("generated_image.png")
from flask import Flask, request, jsonify, send_file
from diffusers import StableDiffusionPipeline
import torch

app = Flask(__name__)

# Load Stable Diffusion v1-4
model_id = "CompVis/stable-diffusion-v1-4"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")

@app.route("/generate", methods=["POST"])
def generate_image():
    data = request.json
    prompt = data.get("prompt", "A beautiful fantasy landscape")
    num_steps = data.get("steps", 50)
    guidance_scale = data.get("guidance_scale", 7.5)

    # Generate image
    image = pipe(prompt, num_inference_steps=num_steps, guidance_scale=guidance_scale).images[0]
    output_path = "output.png"
    image.save(output_path)
    
    return send_file(output_path, mimetype="image/png")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stable Diffusion Generator</title>
</head>
<body>
    <h1>Stable Diffusion v1-4 Image Generator</h1>
    <form id="image-form">
        <label for="prompt">Prompt:</label><br>
        <input type="text" id="prompt" name="prompt" required><br><br>
        <label for="steps">Inference Steps:</label><br>
        <input type="number" id="steps" name="steps" value="50"><br><br>
        <label for="guidance_scale">Guidance Scale:</label><br>
        <input type="number" id="guidance_scale" name="guidance_scale" value="7.5"><br><br>
        <button type="submit">Generate Image</button>
    </form>

    <h2>Generated Image:</h2>
    <img id="generated-image" alt="Generated Image" style="max-width: 100%;">

    <script>
        document.getElementById("image-form").addEventListener("submit", async (event) => {
            event.preventDefault();

            const prompt = document.getElementById("prompt").value;
            const steps = document.getElementById("steps").value;
            const guidanceScale = document.getElementById("guidance_scale").value;

            const response = await fetch("http://localhost:5000/generate", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({ prompt, steps, guidance_scale: guidanceScale }),
            });

            if (response.ok) {
                const blob = await response.blob();
                const url = URL.createObjectURL(blob);
                document.getElementById("generated-image").src = url;
            } else {
                console.error("Error generating image");
            }
        });
    </script>
</body>
</html>