Sakalti commited on
Commit
d3384a1
·
verified ·
1 Parent(s): d11ef48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -7,20 +7,27 @@ from PIL import Image
7
  import requests
8
  from io import BytesIO
9
 
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
- model_repo_id = "dalle-mini/dalle-mini"
12
 
 
13
  pipe = DiffusionPipeline.from_pretrained(model_repo_id)
14
- pipe = pipe.to(device)
15
 
 
16
  MAX_SEED = np.iinfo(np.int32).max
17
 
 
18
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
19
  if randomize_seed:
20
  seed = random.randint(0, MAX_SEED)
21
-
 
22
  generator = torch.Generator().manual_seed(seed)
23
-
 
24
  image = pipe(
25
  prompt=prompt,
26
  guidance_scale=guidance_scale,
@@ -28,19 +35,22 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
28
  width=width,
29
  height=height,
30
  generator=generator
31
- ).images[0]
32
 
33
  # 画像を保存
34
  image.save("generated_image.png")
35
 
 
36
  return image, "generated_image.png", seed
37
 
 
38
  examples = [
39
  "ジャングルの中の宇宙飛行士、寒色のパレット、 muted colors、詳細、8k",
40
  "緑の馬に乗った宇宙飛行士",
41
  "美味しそうなセビーチェチーズケーキスライス",
42
  ]
43
 
 
44
  css = """
45
  #col-container {
46
  margin: 0 auto;
@@ -48,13 +58,16 @@ css = """
48
  }
49
  """
50
 
 
51
  with gr.Blocks(css=css) as demo:
52
 
53
  with gr.Column(elem_id="col-container"):
 
54
  gr.Markdown(f"""
55
  # テキストから画像への生成器
56
  """)
57
 
 
58
  with gr.Row():
59
  prompt = gr.Textbox(
60
  label="プロンプト",
@@ -64,11 +77,12 @@ with gr.Blocks(css=css) as demo:
64
  container=False,
65
  )
66
 
67
- run_button = gr.Button("生成", scale=0)
68
 
69
- result = gr.Image(label="結果", show_label=False)
70
- download_link = gr.File(label="生成された画像をダウンロード")
71
 
 
72
  with gr.Accordion("詳細設定", open=False):
73
  negative_prompt = gr.Textbox(
74
  label="ネガティブプロンプト",
@@ -85,13 +99,14 @@ with gr.Blocks(css=css) as demo:
85
  value=0,
86
  )
87
 
88
- randomize_seed = gr.Checkbox(label="シードをランダム化", value=True)
89
 
 
90
  with gr.Row():
91
  width = gr.Slider(
92
  label="幅",
93
  minimum=256,
94
- maximum=MAX_IMAGE_SIZE,
95
  step=32,
96
  value=1024,
97
  )
@@ -99,11 +114,12 @@ with gr.Blocks(css=css) as demo:
99
  height = gr.Slider(
100
  label="高さ",
101
  minimum=256,
102
- maximum=MAX_IMAGE_SIZE,
103
  step=32,
104
  value=1024,
105
  )
106
 
 
107
  with gr.Row():
108
  guidance_scale = gr.Slider(
109
  label="ガイダンススケール",
@@ -121,11 +137,13 @@ with gr.Blocks(css=css) as demo:
121
  value=20,
122
  )
123
 
 
124
  gr.Examples(
125
  examples=examples,
126
  inputs=[prompt]
127
  )
128
 
 
129
  gr.on(
130
  triggers=[run_button.click, prompt.submit],
131
  fn=infer,
@@ -133,4 +151,5 @@ with gr.Blocks(css=css) as demo:
133
  outputs=[result, download_link, seed]
134
  )
135
 
 
136
  demo.queue().launch()
 
7
  import requests
8
  from io import BytesIO
9
 
10
+ # デバイスを設定(CUDAが利用可能ならGPUを使用)
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model_repo_id = "dalle-mini/dalle-mini" # 使用するモデルのリポジトリID
13
 
14
+ # モデルをロード
15
  pipe = DiffusionPipeline.from_pretrained(model_repo_id)
16
+ pipe = pipe.to(device) # モデルを選択したデバイスに移動
17
 
18
+ # 最大のシード値を定義
19
  MAX_SEED = np.iinfo(np.int32).max
20
 
21
+ # 画像生成関数
22
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
23
+ # シードをランダム化するオプションの処理
24
  if randomize_seed:
25
  seed = random.randint(0, MAX_SEED)
26
+
27
+ # シードを指定して画像生成のための乱数生成器を作成
28
  generator = torch.Generator().manual_seed(seed)
29
+
30
+ # プロンプトから画像を生成
31
  image = pipe(
32
  prompt=prompt,
33
  guidance_scale=guidance_scale,
 
35
  width=width,
36
  height=height,
37
  generator=generator
38
+ ).images[0] # 生成された画像のリストから最初の画像を取得
39
 
40
  # 画像を保存
41
  image.save("generated_image.png")
42
 
43
+ # 生成された画像とダウンロードリンク、シードを返す
44
  return image, "generated_image.png", seed
45
 
46
+ # サンプルプロンプトのリスト
47
  examples = [
48
  "ジャングルの中の宇宙飛行士、寒色のパレット、 muted colors、詳細、8k",
49
  "緑の馬に乗った宇宙飛行士",
50
  "美味しそうなセビーチェチーズケーキスライス",
51
  ]
52
 
53
+ # スタイルシートの設定
54
  css = """
55
  #col-container {
56
  margin: 0 auto;
 
58
  }
59
  """
60
 
61
+ # Gradioのインターフェースを構築
62
  with gr.Blocks(css=css) as demo:
63
 
64
  with gr.Column(elem_id="col-container"):
65
+ # タイトルの表示
66
  gr.Markdown(f"""
67
  # テキストから画像への生成器
68
  """)
69
 
70
+ # プロンプト入力と実行ボタン
71
  with gr.Row():
72
  prompt = gr.Textbox(
73
  label="プロンプト",
 
77
  container=False,
78
  )
79
 
80
+ run_button = gr.Button("生成", scale=0) # 画像生成ボタン
81
 
82
+ result = gr.Image(label="結果", show_label=False) # 生成された画像を表示するための領域
83
+ download_link = gr.File(label="生成された画像をダウンロード") # 画像をダウンロードするリンク
84
 
85
+ # 詳細設定のアコーディオン
86
  with gr.Accordion("詳細設定", open=False):
87
  negative_prompt = gr.Textbox(
88
  label="ネガティブプロンプト",
 
99
  value=0,
100
  )
101
 
102
+ randomize_seed = gr.Checkbox(label="シードをランダム化", value=True) # シードのランダム化オプション
103
 
104
+ # 画像の幅と高さを設定するスライダー
105
  with gr.Row():
106
  width = gr.Slider(
107
  label="幅",
108
  minimum=256,
109
+ maximum=1024,
110
  step=32,
111
  value=1024,
112
  )
 
114
  height = gr.Slider(
115
  label="高さ",
116
  minimum=256,
117
+ maximum=1024,
118
  step=32,
119
  value=1024,
120
  )
121
 
122
+ # ガイダンススケールと推論ステップ数を設定するスライダー
123
  with gr.Row():
124
  guidance_scale = gr.Slider(
125
  label="ガイダンススケール",
 
137
  value=20,
138
  )
139
 
140
+ # サンプルプロンプトを提供する機能
141
  gr.Examples(
142
  examples=examples,
143
  inputs=[prompt]
144
  )
145
 
146
+ # ボタンをクリックまたはプロンプトを送信すると画像生成をトリガー
147
  gr.on(
148
  triggers=[run_button.click, prompt.submit],
149
  fn=infer,
 
151
  outputs=[result, download_link, seed]
152
  )
153
 
154
+ # Gradioアプリを起動
155
  demo.queue().launch()