ziheWang commited on
Commit
32c9916
·
verified ·
1 Parent(s): 764ae97

Update generate_image.py

Browse files
Files changed (1) hide show
  1. generate_image.py +18 -12
generate_image.py CHANGED
@@ -1,18 +1,24 @@
 
 
1
  import os
2
- import requests
 
3
  from PIL import Image
4
- from io import BytesIO
5
 
6
- # 替换为你选定的模型 repo(如 stablediffusionapi/anything-v5
7
- API_URL = "https://api-inference.huggingface.co/models/nitrosocke/epiCRealism"
8
- headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
9
 
10
- def generate_ghibli_avatar(prompt, reference_image=None):
11
- # 模拟 ControlNet 的参考图传入能力(实际不支持,参考图可用于引导微调)
12
- payload = {"inputs": prompt}
13
- response = requests.post(API_URL, headers=headers, json=payload)
 
14
 
15
- if response.status_code != 200:
16
- raise Exception(f"生成失败:{response.status_code}, {response.text}")
17
 
18
- return Image.open(BytesIO(response.content))
 
 
 
 
 
 
 
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