weijiang2024 commited on
Commit
92da9c1
·
verified ·
1 Parent(s): 060fd1d

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +7 -2
  2. app.py +132 -32
  3. requirements.txt +4 -4
README.md CHANGED
@@ -1,8 +1,13 @@
1
  ---
2
  title: Suanfamama_AIGC_alg2
3
- app_file: app.py
 
 
4
  sdk: gradio
5
- sdk_version: 4.29.0
 
 
 
6
  ---
7
 
8
  # alg 算法
 
1
  ---
2
  title: Suanfamama_AIGC_alg2
3
+ emoji: 🎨
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.36.1
8
+ app_file: app.py
9
+ pinned: true
10
+ license: mit
11
  ---
12
 
13
  # alg 算法
app.py CHANGED
@@ -1,37 +1,137 @@
1
  import gradio as gr
 
 
2
  import torch
3
- from diffusers import StableDiffusion3Pipeline
 
4
 
5
- # Load the model
6
- pipe = StableDiffusion3Pipeline.from_pretrained(
7
- "stable-diffusion-3-medium-diffusers",
8
- torch_dtype=torch.float16
9
- )
10
- pipe = pipe.to("cuda:1")
11
 
12
- def generate_image(prompt, negative_prompt, num_inference_steps, guidance_scale):
13
- # Generate the image
 
 
 
 
 
 
 
 
 
 
 
 
14
  image = pipe(
15
- prompt,
16
- negative_prompt=negative_prompt,
17
- num_inference_steps=num_inference_steps,
18
- guidance_scale=guidance_scale
19
- ).images[0]
20
- return image
21
-
22
- # Create the Gradio interface
23
- interface = gr.Interface(
24
- fn=generate_image,
25
- inputs=[
26
- gr.Textbox(label="正向提示词"),
27
- gr.Textbox(label="负向提示词", placeholder="Optional"),
28
- gr.Slider(step=1, minimum=1, maximum=100, value=28, label="推理步数"),
29
- gr.Slider(minimum=1.0, maximum=20.0, step=0.1, value=7.0, label="Guidance Scale")
30
- ],
31
- outputs="image",
32
- title="Stable Diffusion 3 Image Generator",
33
- description="Generate images with Stable Diffusion 3. Type a prompt and see the magic!"
34
- )
35
-
36
- # Launch the interface
37
- interface.launch(server_name="0.0.0.0", server_port=8912, inbrowser=True, share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import random
4
  import torch
5
+ from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
6
+ import spaces
7
 
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ dtype = torch.float16
 
 
 
 
10
 
11
+ repo = "stabilityai/stable-diffusion-3-medium-diffusers"
12
+ pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to(device)
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 1344
16
+
17
+ @spaces.GPU
18
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
19
+
20
+ if randomize_seed:
21
+ seed = random.randint(0, MAX_SEED)
22
+
23
+ generator = torch.Generator().manual_seed(seed)
24
+
25
  image = pipe(
26
+ prompt = prompt,
27
+ negative_prompt = negative_prompt,
28
+ guidance_scale = guidance_scale,
29
+ num_inference_steps = num_inference_steps,
30
+ width = width,
31
+ height = height,
32
+ generator = generator
33
+ ).images[0]
34
+
35
+ return image, seed
36
+
37
+ examples = [
38
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
39
+ "An astronaut riding a green horse",
40
+ "A delicious ceviche cheesecake slice",
41
+ ]
42
+
43
+ css="""
44
+ #col-container {
45
+ margin: 0 auto;
46
+ max-width: 580px;
47
+ }
48
+ """
49
+
50
+ with gr.Blocks(css=css) as demo:
51
+
52
+ with gr.Column(elem_id="col-container"):
53
+ gr.Markdown(f"""
54
+ # Demo [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
55
+ 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)
56
+ """)
57
+
58
+ with gr.Row():
59
+
60
+ prompt = gr.Text(
61
+ label="Prompt",
62
+ show_label=False,
63
+ max_lines=1,
64
+ placeholder="Enter your prompt",
65
+ container=False,
66
+ )
67
+
68
+ run_button = gr.Button("Run", scale=0)
69
+
70
+ result = gr.Image(label="Result", show_label=False)
71
+
72
+ with gr.Accordion("Advanced Settings", open=False):
73
+
74
+ negative_prompt = gr.Text(
75
+ label="Negative prompt",
76
+ max_lines=1,
77
+ placeholder="Enter a negative prompt",
78
+ )
79
+
80
+ seed = gr.Slider(
81
+ label="Seed",
82
+ minimum=0,
83
+ maximum=MAX_SEED,
84
+ step=1,
85
+ value=0,
86
+ )
87
+
88
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
89
+
90
+ with gr.Row():
91
+
92
+ width = gr.Slider(
93
+ label="Width",
94
+ minimum=256,
95
+ maximum=MAX_IMAGE_SIZE,
96
+ step=64,
97
+ value=1024,
98
+ )
99
+
100
+ height = gr.Slider(
101
+ label="Height",
102
+ minimum=256,
103
+ maximum=MAX_IMAGE_SIZE,
104
+ step=64,
105
+ value=1024,
106
+ )
107
+
108
+ with gr.Row():
109
+
110
+ guidance_scale = gr.Slider(
111
+ label="Guidance scale",
112
+ minimum=0.0,
113
+ maximum=10.0,
114
+ step=0.1,
115
+ value=5.0,
116
+ )
117
+
118
+ num_inference_steps = gr.Slider(
119
+ label="Number of inference steps",
120
+ minimum=1,
121
+ maximum=50,
122
+ step=1,
123
+ value=28,
124
+ )
125
+
126
+ gr.Examples(
127
+ examples = examples,
128
+ inputs = [prompt]
129
+ )
130
+ gr.on(
131
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
132
+ fn = infer,
133
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
134
+ outputs = [result, seed]
135
+ )
136
+
137
+ demo.launch()
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio
2
- torch
3
- diffusers
4
- transformers
 
1
+ git+https://github.com/huggingface/diffusers.git
2
+ transformers
3
+ accelerate
4
+ sentencepiece