Spaces:
Runtime error
Runtime error
import gradio as gr | |
from diffusers import DiffusionPipeline | |
import torch | |
import os | |
from huggingface_hub import login | |
# 通过环境变量获取 Token | |
hf_token = os.getenv("hf") | |
# 使用 Hugging Face Token 登录 | |
login(token=hf_token) | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
model_repo_id = "stabilityai/stable-diffusion-3.5-large" | |
if torch.cuda.is_available(): | |
torch_dtype = torch.bfloat16 | |
else: | |
torch_dtype = torch.float32 | |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype) | |
pipe.load_lora_weights("prithivMLmods/SD3.5-Large-Photorealistic-LoRA", weight_name="Photorealistic-SD3.5-Large-LoRA.safetensors") | |
pipe = pipe.to(device) | |
pipe.fuse_lora(lora_scale=1.0) | |
# 定义图像生成函数 | |
# @spaces.GPU(duration=65) 这个装饰器指定 函数运行在 GPU 上,并限制运行时间 最多 65 秒。 | |
def generate_image(prompt, negative_prompt=""): | |
print(f"正向提示词: {prompt}") | |
print(f"负向提示词: {negative_prompt}") | |
# • prompt: 正向提示词,用于控制生成的内容。 | |
# • negative_prompt: 负向提示词,用于减少不想要的特性。 | |
# • seed: 随机种子,相同的 seed + prompt 会生成相同的图片。 | |
# • randomize_seed: 是否随机化种子,如果为 True,则随机生成 seed。 | |
# • width / height: 图片宽高,默认为 1024×1024。 | |
# • guidance_scale: 提示词引导强度,越高越严格按 prompt 生成,值太高可能会降低质量。 | |
# • num_inference_steps: 推理步数,一般 40~50 比较好,步数越高质量越好,但生成速度更慢。 | |
# • progress=gr.Progress(track_tqdm=True): Gradio 进度条,用于在前端显示进度。 | |
return pipe( | |
prompt=prompt, | |
negative_prompt=negative_prompt, # 传递负面提示词 | |
num_inference_steps=40, | |
randomize_seed=True, | |
guidance_scale=4.5, | |
width=1024, | |
height=1024, | |
).images[0] | |
# return pipe(prompt).images[0] | |
# 创建 Gradio 界面 | |
# iface = gr.Interface(fn=generate_image, inputs="text", outputs="image") | |
iface = gr.Interface( | |
fn=generate_image, | |
inputs=[ | |
gr.Textbox(label="正向提示词"), | |
gr.Textbox(label="负向提示词", placeholder="(可选)输入你不想要的元素") | |
], | |
outputs="image" | |
) | |
# 启动界面 | |
iface.launch() | |
# import gradio as gr | |
# gr.load("models/prithivMLmods/SD3.5-Large-Photorealistic-LoRA").launch() | |
# import gradio as gr | |
# gr.load( | |
# "models/prithivMLmods/SD3.5-Large-Photorealistic-LoRA", | |
# provider="hf-inference", | |
# ).launch() | |
# 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] | |
# print(type(image)) | |
# 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() |