apollo987 commited on
Commit
38d91e9
·
verified ·
1 Parent(s): a8c117e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -23
app.py CHANGED
@@ -1,14 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
4
 
5
- # モデルの設定
6
  model_id = "runwayml/stable-diffusion-v1-5"
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
9
 
10
  # スケジューラー読み込み
11
- scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
 
 
12
 
13
  # パイプライン読み込み
14
  pipe = StableDiffusionPipeline.from_pretrained(
@@ -18,59 +33,94 @@ pipe = StableDiffusionPipeline.from_pretrained(
18
  safety_checker=None
19
  ).to(device)
20
 
21
- # メモリ節約
22
  if device == "cuda":
23
  pipe.enable_attention_slicing()
24
 
25
  # 画像生成関数
26
- def generate_image(prompt, negative_prompt, num_steps, guidance_scale, width, height, seed):
27
- generator = torch.Generator(device=device).manual_seed(seed) if seed > 0 else None
28
- image = pipe(
 
 
 
 
 
 
29
  prompt=prompt,
30
  negative_prompt=negative_prompt,
31
  num_inference_steps=num_steps,
32
  guidance_scale=guidance_scale,
33
  width=width,
34
  height=height,
35
- generator=generator
36
  ).images[0]
37
- return image
38
 
39
  # Gradio UI
40
  with gr.Blocks(css="footer {visibility: hidden}") as demo:
41
  gr.Markdown("# Simple Stable Diffusion")
42
- gr.Markdown("テキストから画像を生成するシンプルなアプリケーションです。")
43
 
44
  with gr.Row():
45
  with gr.Column():
46
- prompt = gr.Textbox(label="プロンプト", lines=3, placeholder="画像にしたい内容を入力してください...")
47
- negative_prompt = gr.Textbox(label="ネガティブプロンプト", lines=2, value="低品質, 粗い, 汚い, ぼやけた, 歪んだ")
48
- num_steps = gr.Slider(5, 50, value=25, step=1, label="ステップ数")
49
- guidance_scale = gr.Slider(1, 15, value=7.5, step=0.1, label="ガイダンススケール")
50
- width = gr.Slider(384, 768, value=512, step=32, label="幅")
51
- height = gr.Slider(384, 768, value=512, step=32, label="高さ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  seed = gr.Number(label="シード値(0=ランダム)", value=0)
53
  generate_btn = gr.Button("生成", variant="primary")
54
-
55
  with gr.Column():
56
  result = gr.Image(label="生成結果")
57
 
58
  generate_btn.click(
59
  generate_image,
60
- inputs=[prompt, negative_prompt, num_steps, guidance_scale, width, height, seed],
61
- outputs=result
 
 
 
 
 
 
 
 
62
  )
63
 
64
  gr.Examples(
65
  examples=[
66
  ["富士山と桜、春の風景", "低品質, イラスト", 25, 7.5, 512, 512, 42],
67
- ["サイバーパンクな都市、ネオン、夜景", "人物, 低解像度", 30, 7.5, 512, 512, 123],
68
- ["ラーメン、湯気、写実的、食べ物写真", "変形, ぼやけた", 25, 7.5, 512, 512, 456]
 
 
 
 
 
 
 
 
 
69
  ],
70
- inputs=[prompt, negative_prompt, num_steps, guidance_scale, width, height, seed]
71
  )
72
 
73
- gr.Markdown("### 使い方\n1. プロンプト入力\n2. オプション調整\n3. 「生成」ボタン")
74
 
75
- # アプリ起動
76
  demo.launch()
 
1
+ # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2
+ # ┃ Step0: モンキーパッチ (絶対に一番上) ┃
3
+ # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
4
+ import huggingface_hub
5
+ # dynamic_modules_utils が cached_download を import してくるので、
6
+ # hf_hub_download を alias しておく
7
+ if not hasattr(huggingface_hub, "cached_download"):
8
+ huggingface_hub.cached_download = huggingface_hub.hf_hub_download
9
+
10
+
11
+ # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
12
+ # ┃ 以下、これまで通りのコード ┃
13
+ # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
14
  import gradio as gr
15
  import torch
16
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
17
 
18
+ # モデル設定
19
  model_id = "runwayml/stable-diffusion-v1-5"
20
  device = "cuda" if torch.cuda.is_available() else "cpu"
21
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
22
 
23
  # スケジューラー読み込み
24
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(
25
+ model_id, subfolder="scheduler"
26
+ )
27
 
28
  # パイプライン読み込み
29
  pipe = StableDiffusionPipeline.from_pretrained(
 
33
  safety_checker=None
34
  ).to(device)
35
 
36
+ # メモリ最適化
37
  if device == "cuda":
38
  pipe.enable_attention_slicing()
39
 
40
  # 画像生成関数
41
+ def generate_image(
42
+ prompt, negative_prompt, num_steps, guidance_scale, width, height, seed
43
+ ):
44
+ generator = (
45
+ torch.Generator(device=device).manual_seed(seed)
46
+ if seed and seed > 0
47
+ else None
48
+ )
49
+ return pipe(
50
  prompt=prompt,
51
  negative_prompt=negative_prompt,
52
  num_inference_steps=num_steps,
53
  guidance_scale=guidance_scale,
54
  width=width,
55
  height=height,
56
+ generator=generator,
57
  ).images[0]
 
58
 
59
  # Gradio UI
60
  with gr.Blocks(css="footer {visibility: hidden}") as demo:
61
  gr.Markdown("# Simple Stable Diffusion")
62
+ gr.Markdown("テキストから画像を生成するシンプルなアプリです。")
63
 
64
  with gr.Row():
65
  with gr.Column():
66
+ prompt = gr.Textbox(
67
+ label="プロンプト",
68
+ lines=3,
69
+ placeholder="画像にしたい内容を入力してください…",
70
+ )
71
+ negative_prompt = gr.Textbox(
72
+ label="ネガティブプロンプト",
73
+ lines=2,
74
+ value="低品質, 汚い, 粗い, ぼやけた, 歪んだ",
75
+ )
76
+ num_steps = gr.Slider(
77
+ minimum=5, maximum=50, step=1, value=25, label="ステップ数"
78
+ )
79
+ guidance_scale = gr.Slider(
80
+ minimum=1, maximum=15, step=0.1, value=7.5, label="ガイダンススケール"
81
+ )
82
+ width = gr.Slider(
83
+ minimum=384, maximum=768, step=32, value=512, label="幅"
84
+ )
85
+ height = gr.Slider(
86
+ minimum=384, maximum=768, step=32, value=512, label="高さ"
87
+ )
88
  seed = gr.Number(label="シード値(0=ランダム)", value=0)
89
  generate_btn = gr.Button("生成", variant="primary")
 
90
  with gr.Column():
91
  result = gr.Image(label="生成結果")
92
 
93
  generate_btn.click(
94
  generate_image,
95
+ inputs=[
96
+ prompt,
97
+ negative_prompt,
98
+ num_steps,
99
+ guidance_scale,
100
+ width,
101
+ height,
102
+ seed,
103
+ ],
104
+ outputs=result,
105
  )
106
 
107
  gr.Examples(
108
  examples=[
109
  ["富士山と桜、春の風景", "低品質, イラスト", 25, 7.5, 512, 512, 42],
110
+ ["サイバーパンク都市、夜景、ネオン", "人物, ぼやけ", 30, 7.5, 512, 512, 123],
111
+ ["ラーメン、湯気の食べ物写真", "変形, 不自然", 25, 7.5, 512, 512, 456],
112
+ ],
113
+ inputs=[
114
+ prompt,
115
+ negative_prompt,
116
+ num_steps,
117
+ guidance_scale,
118
+ width,
119
+ height,
120
+ seed,
121
  ],
 
122
  )
123
 
124
+ gr.Markdown("### 使い方\n1. プロンプト入力\n2. 設定調整\n3. 「生成」クリック")
125
 
 
126
  demo.launch()