apollo987 commited on
Commit
b7d25ba
·
verified ·
1 Parent(s): aa0378b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -3,11 +3,11 @@ 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
  # パイプラインのロード
@@ -15,7 +15,7 @@ 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
 
@@ -23,8 +23,8 @@ pipe = pipe.to(device)
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
  if seed > 0:
29
  generator = torch.Generator(device=device).manual_seed(seed)
30
  else:
@@ -42,10 +42,10 @@ def generate_image(prompt, negative_prompt, num_steps, guidance_scale, width, he
42
 
43
  return image
44
 
45
- # Gradio UIの作成(titleはv3.35.2では使えないので削除)
46
  with gr.Blocks(css="footer {visibility: hidden}") as demo:
47
  gr.Markdown("# Simple Stable Diffusion")
48
- gr.Markdown("テキストから画像を生成するシンプルなアプリケーションです。")
49
 
50
  with gr.Row():
51
  with gr.Column():
@@ -67,12 +67,14 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
67
  with gr.Column():
68
  result = gr.Image(label="生成結果")
69
 
 
70
  generate_btn.click(
71
  generate_image,
72
  inputs=[prompt, negative_prompt, num_steps, guidance_scale, width, height, seed],
73
  outputs=result
74
  )
75
 
 
76
  gr.Examples(
77
  [
78
  ["美しい日本の風景、富士山、桜の花、春、日の出、超高品質、写実的な写真", "低品質, 漫画調, イラスト", 25, 7.5, 512, 512, 42],
 
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
  # パイプラインのロード
 
15
  model_id,
16
  scheduler=scheduler,
17
  torch_dtype=torch_dtype,
18
+ safety_checker=None # 処理速度を優先するため無効化
19
  )
20
  pipe = pipe.to(device)
21
 
 
23
  if device == "cuda":
24
  pipe.enable_attention_slicing()
25
 
26
+ # 画像生成関数
27
  def generate_image(prompt, negative_prompt, num_steps, guidance_scale, width, height, seed):
 
28
  if seed > 0:
29
  generator = torch.Generator(device=device).manual_seed(seed)
30
  else:
 
42
 
43
  return image
44
 
45
+ # Gradio UI の構築(タイトル削除:gradio==3.35.2 では未サポート)
46
  with gr.Blocks(css="footer {visibility: hidden}") as demo:
47
  gr.Markdown("# Simple Stable Diffusion")
48
+ gr.Markdown("テキストから画像を生成するシンプルなアプリです。")
49
 
50
  with gr.Row():
51
  with gr.Column():
 
67
  with gr.Column():
68
  result = gr.Image(label="生成結果")
69
 
70
+ # 生成ボタン押下時の処理
71
  generate_btn.click(
72
  generate_image,
73
  inputs=[prompt, negative_prompt, num_steps, guidance_scale, width, height, seed],
74
  outputs=result
75
  )
76
 
77
+ # 使用例
78
  gr.Examples(
79
  [
80
  ["美しい日本の風景、富士山、桜の花、春、日の出、超高品質、写実的な写真", "低品質, 漫画調, イラスト", 25, 7.5, 512, 512, 42],