Spaces:
Running
Running
Update generate_image.py
Browse files- generate_image.py +18 -12
generate_image.py
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
from io import BytesIO
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
if response.status_code != 200:
|
| 16 |
-
raise Exception(f"生成失败:{response.status_code}, {response.text}")
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# generate_image.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
+
import torch
|
| 5 |
+
from diffusers import StableDiffusionPipeline
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
+
# 加载 Hugging Face Token(你需要在 Spaces 的 Secrets 设置中添加 HF_TOKEN)
|
| 9 |
+
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
| 10 |
|
| 11 |
+
# 使用 nitrosocke/Ghibli-Diffusion 模型
|
| 12 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 13 |
+
"nitrosocke/Ghibli-Diffusion",
|
| 14 |
+
use_auth_token=hf_token
|
| 15 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def generate_ghibli_avatar(prompt, reference_image=None):
|
| 19 |
+
"""
|
| 20 |
+
生成吉卜力风格头像,仅使用文本 prompt,不依赖 reference image。
|
| 21 |
+
"""
|
| 22 |
+
final_prompt = f"{prompt}, ghibli style"
|
| 23 |
+
image = pipe(final_prompt, num_inference_steps=30).images[0]
|
| 24 |
+
return image
|