File size: 2,304 Bytes
2385429
4cec43c
 
c06bbea
6d0ff10
095f289
 
c06bbea
 
 
4dd0da4
4cec43c
 
 
095f289
 
95aa530
22a2ff4
4cec43c
83c0970
4cec43c
 
 
 
bb3044b
 
 
 
 
4cec43c
bb3044b
 
4cec43c
22a2ff4
bb3044b
 
 
 
 
 
 
 
 
 
 
83c0970
bb3044b
 
22a2ff4
bb3044b
 
2385429
bb3044b
 
4dd0da4
bb3044b
 
4dd0da4
bb3044b
 
 
4dd0da4
bb3044b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
import torch
from diffusers import StableDiffusion3Pipeline
import os
from huggingface_hub import login

# 获取Hugging Face Token
hf_token = os.environ.get("HF_TOKEN")
login(token=hf_token)

# 加载模型并配置
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-large", torch_dtype=torch.bfloat16)
pipe.load_lora_weights("prithivMLmods/SD3.5-Large-Photorealistic-LoRA", weight_name="Photorealistic-SD3.5-Large-LoRA.safetensors")
pipe.fuse_lora(lora_scale=1.0)

# 如果有GPU,转移到GPU
# pipe.to("cuda")

# 定义图像生成函数,添加种子参数
def generate_image(prompt, seed):
    # 设置种子
    generator = torch.manual_seed(seed)
    
    # 使用模型生成图像
    result = pipe(prompt=prompt,
                  num_inference_steps=24, 
                  guidance_scale=4.0,
                  width=960, height=1280,
                  generator=generator)
    
    # 确保返回 PIL 图像
    image = result.images[0]
    return image

# 创建Gradio界面(使用 Interface)
def gradio_interface():
    with gr.Interface(fn=generate_image, 
                      inputs=[gr.Textbox(label="Prompt", value="Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational photography"),
                              gr.Slider(minimum=0, maximum=100000, step=1, label="Seed", value=42)], 
                      outputs=gr.Image(type="pil", label="Generated Image")) as demo:
        demo.launch()

# 启动Gradio应用
gradio_interface()
    
# 创建Gradio界面
# with gr.Blocks() as demo:
#     gr.Markdown("## Stable Diffusion Image Generation with Seed Control")

#     # 输入框:提示文本
#     prompt_input = gr.Textbox(label="Prompt", value="Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational photography")

#     # 滑块:种子
#     seed_input = gr.Slider(minimum=0, maximum=100000, step=1, label="Seed", value=42)

#     # 输出图像
#     output_image = gr.Image(type="pil", label="Generated Image")

#     # 按钮触发事件
#     generate_btn = gr.Button("Generate Image")
#     generate_btn.click(fn=generate_image, inputs=[prompt_input, seed_input], outputs=output_image)

# # 启动Gradio应用
# demo.launch()