apollo987 commited on
Commit
4f82cad
·
verified ·
1 Parent(s): 1e30411

Add Stable Diffusion app files

Browse files
Files changed (2) hide show
  1. app.py +95 -0
  2. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
15
+ model_id,
16
+ scheduler=scheduler,
17
+ torch_dtype=torch_dtype,
18
+ safety_checker=None # 処理速度向上のため
19
+ )
20
+ pipe = pipe.to(device)
21
+
22
+ # 可能であればメモリ最適化を有効化
23
+ if device == "cuda":
24
+ pipe.enable_attention_slicing()
25
+
26
+ def generate_image(prompt, negative_prompt, num_steps, guidance_scale, width, height, seed):
27
+ """テキストプロンプトから画像を生成する関数"""
28
+ # シードの設定(再現性のため)
29
+ if seed > 0:
30
+ generator = torch.Generator(device=device).manual_seed(seed)
31
+ else:
32
+ generator = None
33
+
34
+ # 画像生成
35
+ image = pipe(
36
+ prompt=prompt,
37
+ negative_prompt=negative_prompt,
38
+ num_inference_steps=num_steps,
39
+ guidance_scale=guidance_scale,
40
+ width=width,
41
+ height=height,
42
+ generator=generator
43
+ ).images[0]
44
+
45
+ return image
46
+
47
+ # Gradio UIの作成
48
+ with gr.Blocks(title="Simple Stable Diffusion", css="footer {visibility: hidden}") as demo:
49
+ gr.Markdown("# Simple Stable Diffusion")
50
+ gr.Markdown("テキストから画像を生成するシンプルなアプリケーションです。")
51
+
52
+ with gr.Row():
53
+ with gr.Column():
54
+ prompt = gr.Textbox(label="プロンプト", placeholder="画像にしたい内容を入力してください...", lines=3)
55
+ negative_prompt = gr.Textbox(label="ネガティブプロンプト", placeholder="避けたい要素を入力...", lines=2, value="低品質, 粗い, 汚い, ぼやけた, 歪んだ")
56
+
57
+ with gr.Row():
58
+ num_steps = gr.Slider(minimum=5, maximum=50, step=1, value=25, label="ステップ数")
59
+ guidance_scale = gr.Slider(minimum=1, maximum=15, step=0.1, value=7.5, label="ガイダンススケール")
60
+
61
+ with gr.Row():
62
+ width = gr.Slider(minimum=384, maximum=768, step=32, value=512, label="幅")
63
+ height = gr.Slider(minimum=384, maximum=768, step=32, value=512, label="高さ")
64
+
65
+ seed = gr.Number(label="シード値(0=ランダム)", value=0)
66
+
67
+ generate_btn = gr.Button("生成", variant="primary")
68
+
69
+ with gr.Column():
70
+ result = gr.Image(label="生成結果")
71
+
72
+ # ボタンクリック時の処理
73
+ generate_btn.click(
74
+ generate_image,
75
+ inputs=[prompt, negative_prompt, num_steps, guidance_scale, width, height, seed],
76
+ outputs=result
77
+ )
78
+
79
+ # 使用例
80
+ gr.Examples(
81
+ [
82
+ ["美しい日本の風景、富士山、桜の花、春、日の出、超高品質、写実的な写真", "低品質, 漫画調, イラスト", 25, 7.5, 512, 512, 42],
83
+ ["未来的な都市、ネオン、サイバーパンク、夜景、雨、反射、詳細な建物", "ぼやけた, 汚い, 人物", 30, 7.5, 512, 512, 123],
84
+ ["日本のラーメン、スチーミング、美味しそう、トップビュー、プロの食べ物写真、美食、詳細", "変形, 不自然, 低品質", 25, 7.5, 512, 512, 456]
85
+ ],
86
+ inputs=[prompt, negative_prompt, num_steps, guidance_scale, width, height, seed]
87
+ )
88
+
89
+ gr.Markdown("### 使い方")
90
+ gr.Markdown("1. プロンプトに生成したい画像の説明を入力")
91
+ gr.Markdown("2. 必要に応じてネガティブプロンプトや各種設定を調整")
92
+ gr.Markdown("3. 「生成」ボタンをクリック")
93
+
94
+ # アプリの起動
95
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ diffusers==0.19.1
2
+ transformers==4.30.2
3
+ accelerate==0.20.3
4
+ safetensors==0.3.1
5
+ gradio==3.35.2
6
+ torch==2.0.1
7
+ torchvision==0.15.2
8
+ numpy==1.25.1
9
+ Pillow==10.0.0
10
+ scipy==1.11.1