amirkhanbloch commited on
Commit
b555e2e
·
verified ·
1 Parent(s): 20cedfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -132
app.py CHANGED
@@ -1,142 +1,38 @@
1
- import gradio as gr
2
- import spaces
3
- import numpy as np
4
- import random
5
  import torch
 
6
  from diffusers import StableDiffusion3Pipeline
7
 
8
- # Set device and data type
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- dtype = torch.float16
11
-
12
- # Load the Stable Diffusion model
13
- repo = "stabilityai/stable-diffusion-3-medium-diffusers"
14
- pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=dtype).to(device)
 
15
 
16
- # Constants
17
- MAX_SEED = np.iinfo(np.int32).max
18
- MAX_IMAGE_SIZE = 1344
 
 
 
 
 
19
 
20
- @spaces.GPU
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
22
- if randomize_seed:
23
- seed = random.randint(0, MAX_SEED)
24
-
25
- generator = torch.Generator().manual_seed(seed)
26
-
27
- try:
28
- image = pipe(
29
- prompt=prompt,
30
- negative_prompt=negative_prompt,
31
- guidance_scale=guidance_scale,
32
- num_inference_steps=num_inference_steps,
33
- width=width,
34
- height=height,
35
- generator=generator
36
- ).images[0]
37
-
38
- return image, seed
39
- except Exception as e:
40
- return str(e), seed # Return error message if any
41
 
42
- # Example prompts
43
- examples = [
44
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
45
- "An astronaut riding a green horse",
46
- "A delicious ceviche cheesecake slice",
47
- ]
48
 
49
- css = """
50
- #col-container {
51
- margin: 0 auto;
52
- max-width: 580px;
53
- }
54
- """
55
 
56
- # Create Gradio interface
57
- with gr.Blocks(css=css) as demo:
58
-
59
- with gr.Column(elem_id="col-container"):
60
- gr.Markdown(f"""
61
- # Demo [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
62
- Learn more about the [Stable Diffusion 3 series](https://stability.ai/news/stable-diffusion-3). Try on [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), [Stable Assistant](https://stability.ai/stable-assistant), or on Discord via [Stable Artisan](https://stability.ai/stable-artisan). Run locally with [ComfyUI](https://github.com/comfyanonymous/ComfyUI) or [diffusers](https://github.com/huggingface/diffusers)
63
- """)
64
-
65
- with gr.Row():
66
- prompt = gr.Text(
67
- label="Prompt",
68
- show_label=False,
69
- max_lines=1,
70
- placeholder="Enter your prompt",
71
- container=False,
72
- )
73
-
74
- run_button = gr.Button("Run", scale=0)
75
-
76
- result = gr.Image(label="Result", show_label=False)
77
 
78
- with gr.Accordion("Advanced Settings", open=False):
79
- negative_prompt = gr.Text(
80
- label="Negative prompt",
81
- max_lines=1,
82
- placeholder="Enter a negative prompt",
83
- )
84
-
85
- seed = gr.Slider(
86
- label="Seed",
87
- minimum=0,
88
- maximum=MAX_SEED,
89
- step=1,
90
- value=0,
91
- )
92
-
93
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
- with gr.Row():
96
- width = gr.Slider(
97
- label="Width",
98
- minimum=256,
99
- maximum=MAX_IMAGE_SIZE,
100
- step=64,
101
- value=1024,
102
- )
103
-
104
- height = gr.Slider(
105
- label="Height",
106
- minimum=256,
107
- maximum=MAX_IMAGE_SIZE,
108
- step=64,
109
- value=1024,
110
- )
111
-
112
- with gr.Row():
113
- guidance_scale = gr.Slider(
114
- label="Guidance scale",
115
- minimum=0.0,
116
- maximum=10.0,
117
- step=0.1,
118
- value=5.0,
119
- )
120
-
121
- num_inference_steps = gr.Slider(
122
- label="Number of inference steps",
123
- minimum=1,
124
- maximum=50,
125
- step=1,
126
- value=28,
127
- )
128
-
129
- gr.Examples(
130
- examples=examples,
131
- inputs=[prompt]
132
- )
133
-
134
- gr.on(
135
- triggers=[run_button.click, prompt.submit, negative_prompt.submit],
136
- fn=infer,
137
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
138
- outputs=[result, seed]
139
- )
140
 
141
- # Launch the app
142
- demo.launch(timeout=10) # Increase timeout if needed
 
 
 
 
 
1
  import torch
2
+ import gradio as gr
3
  from diffusers import StableDiffusion3Pipeline
4
 
5
+ def image_generator(prompt):
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+ pipeline = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers",
8
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
9
+ text_encoder_3=None,
10
+ tokenizer_3 = None)
11
+ #pipeline.enable_model_cpu_offload()
12
+ pipeline.to(device)
13
 
14
+ image = pipeline(
15
+ prompt=prompt,
16
+ negative_prompt="blurred, ugly, watermark, low, resolution, blurry",
17
+ num_inference_steps=40,
18
+ height=1024,
19
+ width=1024,
20
+ guidance_scale=9.0
21
+ ).images[0]
22
 
23
+ #image.show()
24
+ return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ #image_generator("A magical cat doing spell")
 
 
 
 
 
27
 
28
+ interface = gr.Interface(
29
+ fn=image_generator,
30
+ inputs=gr.Textbox(lines=2, placeholder = "Enter your prompt..."),
31
+ outputs=gr.Image(type = "pil"),
32
+ title = "Image Generator App",
33
+ description = "This is a simple image generator app using HuggingFace's Stable Diffusion 3 model.")
34
 
35
+ interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ print(interface)