Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Hugging Faceのトークン
|
6 |
+
HUGGING_FACE_TOKEN = "あなたのHugging Face APIトークン"
|
7 |
+
|
8 |
+
# モデルのロード
|
9 |
+
def load_model():
|
10 |
+
model_id = "double-negative/hololive-diffusion"
|
11 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
12 |
+
model_id,
|
13 |
+
use_auth_token=HUGGING_FACE_TOKEN
|
14 |
+
)
|
15 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
+
return pipe
|
17 |
+
|
18 |
+
# 画像生成関数
|
19 |
+
def generate_image(prompt, num_inference_steps, guidance_scale):
|
20 |
+
try:
|
21 |
+
result = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale)
|
22 |
+
return result.images[0]
|
23 |
+
except Exception as e:
|
24 |
+
return f"エラーが発生しました: {e}"
|
25 |
+
|
26 |
+
# Gradioインターフェースの定義
|
27 |
+
pipe = load_model()
|
28 |
+
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("## Hololive Diffusion 画像生成")
|
31 |
+
|
32 |
+
with gr.Row():
|
33 |
+
with gr.Column():
|
34 |
+
prompt = gr.Textbox(label="プロンプト", placeholder="例: かわいい猫のイラスト")
|
35 |
+
num_inference_steps = gr.Slider(1, 100, value=50, step=1, label="推論ステップ数")
|
36 |
+
guidance_scale = gr.Slider(1, 20, value=7.5, step=0.1, label="ガイダンススケール")
|
37 |
+
generate_button = gr.Button("画像生成")
|
38 |
+
with gr.Column():
|
39 |
+
output_image = gr.Image(label="生成された画像")
|
40 |
+
|
41 |
+
generate_button.click(
|
42 |
+
fn=generate_image,
|
43 |
+
inputs=[prompt, num_inference_steps, guidance_scale],
|
44 |
+
outputs=[output_image]
|
45 |
+
)
|
46 |
+
|
47 |
+
# アプリの実行
|
48 |
+
demo.launch()
|