Spaces:
Sleeping
Sleeping
Create app3.py
Browse files
app3.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import random
|
| 5 |
+
from diffusers import DiffusionPipeline
|
| 6 |
+
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 9 |
+
|
| 10 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 11 |
+
MAX_IMAGE_SIZE = 1024
|
| 12 |
+
|
| 13 |
+
# 預設可選模型
|
| 14 |
+
available_models = [
|
| 15 |
+
"digiplay/AM-mix1",
|
| 16 |
+
"digiplay/pan04",
|
| 17 |
+
"digiplay/2K"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
def load_model(selected_model_id, custom_model_id):
|
| 21 |
+
model_id = custom_model_id.strip() if custom_model_id.strip() else selected_model_id
|
| 22 |
+
try:
|
| 23 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
|
| 24 |
+
return pipe, model_id, f"✅ Model '{model_id}' loaded successfully!"
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return None, "", f"❌ Failed to load model: {e}"
|
| 27 |
+
|
| 28 |
+
def generate_image(pipe, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 29 |
+
if pipe is None:
|
| 30 |
+
raise ValueError("No model loaded. Please load a model first.")
|
| 31 |
+
|
| 32 |
+
if randomize_seed:
|
| 33 |
+
seed = random.randint(0, MAX_SEED)
|
| 34 |
+
|
| 35 |
+
generator = torch.Generator().manual_seed(seed)
|
| 36 |
+
image = pipe(
|
| 37 |
+
prompt=prompt,
|
| 38 |
+
negative_prompt=negative_prompt,
|
| 39 |
+
width=width,
|
| 40 |
+
height=height,
|
| 41 |
+
guidance_scale=guidance_scale,
|
| 42 |
+
num_inference_steps=num_inference_steps,
|
| 43 |
+
generator=generator,
|
| 44 |
+
).images[0]
|
| 45 |
+
|
| 46 |
+
return image, seed
|
| 47 |
+
|
| 48 |
+
with gr.Blocks(css="#container { max-width: 700px; margin: auto; }") as demo:
|
| 49 |
+
gr.Markdown("## Text-to-Image Generator with Model Selector")
|
| 50 |
+
|
| 51 |
+
pipe_state = gr.State(None)
|
| 52 |
+
model_id_state = gr.State("")
|
| 53 |
+
|
| 54 |
+
with gr.Column(elem_id="container"):
|
| 55 |
+
gr.Markdown("### 1. Choose or Enter Model")
|
| 56 |
+
with gr.Row():
|
| 57 |
+
selected_model = gr.Dropdown(label="Choose a model", choices=available_models, value=available_models[0])
|
| 58 |
+
custom_model = gr.Textbox(label="Or enter custom model ID", placeholder="e.g. runwayml/stable-diffusion-v1-5")
|
| 59 |
+
|
| 60 |
+
load_button = gr.Button("Load Model")
|
| 61 |
+
load_status = gr.Textbox(label="Model Load Status", interactive=False)
|
| 62 |
+
|
| 63 |
+
load_button.click(
|
| 64 |
+
fn=load_model,
|
| 65 |
+
inputs=[selected_model, custom_model],
|
| 66 |
+
outputs=[pipe_state, model_id_state, load_status]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
gr.Markdown("### 2. Generate Image")
|
| 70 |
+
prompt = gr.Textbox(label="Prompt", placeholder="e.g. A futuristic city at night")
|
| 71 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="(optional)", value="", visible=True)
|
| 72 |
+
with gr.Row():
|
| 73 |
+
width = gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=512, label="Width")
|
| 74 |
+
height = gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=512, label="Height")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
guidance_scale = gr.Slider(0.0, 10.0, step=0.1, value=7.5, label="Guidance Scale")
|
| 77 |
+
num_inference_steps = gr.Slider(1, 50, step=1, value=25, label="Inference Steps")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
seed = gr.Slider(0, MAX_SEED, step=1, value=0, label="Seed")
|
| 80 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 81 |
+
|
| 82 |
+
generate_button = gr.Button("Generate Image")
|
| 83 |
+
output_image = gr.Image(label="Result")
|
| 84 |
+
final_seed = gr.Number(label="Used Seed", precision=0)
|
| 85 |
+
|
| 86 |
+
generate_button.click(
|
| 87 |
+
fn=generate_image,
|
| 88 |
+
inputs=[pipe_state, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 89 |
+
outputs=[output_image, final_seed]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
demo.launch()
|