GGPENG commited on
Commit
435643f
·
verified ·
1 Parent(s): efbaeaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -58
app.py CHANGED
@@ -12,75 +12,117 @@ import os
12
 
13
 
14
 
15
- API_URL = "https://api-inference.huggingface.co/models/GGPENG/StyleDiffusion" # 替换为你上传的模型仓库
16
- API_TOKEN = os.getenv("HF_TOKEN")
17
 
18
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
19
 
20
- # ----------------------------
21
- # Streamlit 页面设置
22
- # ----------------------------
23
- st.set_page_config(page_title="Fine-tuning style diffusion (API)", layout="wide")
24
 
25
- st.title("Fine-tuning style diffusion 推理 Demo (API)")
26
- st.write("只是训练了一个提示词 'A <new1> reference.'")
27
- st.write("示例:A <new1> reference. New Year image with a rabbit as the main element, in a 2D or anime style, and a festive background")
28
 
29
- # ----------------------------
30
- # Prompt 输入
31
- # ----------------------------
32
- prompt = st.text_input(
33
- "Prompt",
34
- "A <new1> reference. New Year image with a rabbit as the main element, in a 2D or anime style, and a festive background"
35
- )
36
 
37
- # ----------------------------
38
- # 参数调节
39
- # ----------------------------
40
- steps = st.slider("Steps", 10, 320, 100)
41
- guidance = st.slider("Guidance", 1.0, 18.0, 6.0)
42
 
43
- # ----------------------------
44
- # 生成函数(调用 API)
45
- # ----------------------------
46
- def generate(prompt):
47
- payload = {
48
- "inputs": prompt,
49
- "parameters": {
50
- "num_inference_steps": steps,
51
- "guidance_scale": guidance,
52
- # "height": 512,
53
- # "width": 512
54
- }
55
- }
56
-
57
- response = requests.post(API_URL, headers=headers, json=payload)
58
 
59
- if response.status_code != 200:
60
- st.error(f"API请求失败:{response.status_code}, {response.text}")
61
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- # 将返回的字节流或 Base64 数据转换为 PIL Image
64
- try:
65
- image = Image.open(BytesIO(response.content))
66
- except:
67
- st.error("生成图像失败,请检查模型是否支持图像输出。")
68
- return None
69
- return image
 
 
 
 
 
 
 
 
 
70
 
71
  # ----------------------------
72
- # 生成按钮
 
 
 
 
 
73
  # ----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  if st.button("Generate"):
75
- with st.spinner("Generating via Hugging Face API..."):
76
- image = generate(prompt)
77
-
78
- if image:
79
- st.image(image, caption="Result", width=512)
80
  buf = io.BytesIO()
81
  image.save(buf, format="PNG")
82
- st.download_button(
83
- "Download",
84
- buf.getvalue(),
85
- "result.png"
86
- )
 
12
 
13
 
14
 
15
+ # API_URL = "https://api-inference.huggingface.co/models/GGPENG/StyleDiffusion" # 替换为你上传的模型仓库
16
+ # API_TOKEN = os.getenv("HF_TOKEN")
17
 
18
+ # headers = {"Authorization": f"Bearer {API_TOKEN}"}
19
 
20
+ # # ----------------------------
21
+ # # Streamlit 页面设置
22
+ # # ----------------------------
23
+ # st.set_page_config(page_title="Fine-tuning style diffusion (API)", layout="wide")
24
 
25
+ # st.title("Fine-tuning style diffusion 推理 Demo (API)")
26
+ # st.write("只是训练了一个提示词 'A <new1> reference.'")
27
+ # st.write("示例:A <new1> reference. New Year image with a rabbit as the main element, in a 2D or anime style, and a festive background")
28
 
29
+ # # ----------------------------
30
+ # # Prompt 输入
31
+ # # ----------------------------
32
+ # prompt = st.text_input(
33
+ # "Prompt",
34
+ # "A <new1> reference. New Year image with a rabbit as the main element, in a 2D or anime style, and a festive background"
35
+ # )
36
 
37
+ # # ----------------------------
38
+ # # 参数调节
39
+ # # ----------------------------
40
+ # steps = st.slider("Steps", 10, 320, 100)
41
+ # guidance = st.slider("Guidance", 1.0, 18.0, 6.0)
42
 
43
+ # # ----------------------------
44
+ # # 生成函数(调用 API)
45
+ # # ----------------------------
46
+ # def generate(prompt):
47
+ # payload = {
48
+ # "inputs": prompt,
49
+ # "parameters": {
50
+ # "num_inference_steps": steps,
51
+ # "guidance_scale": guidance,
52
+ # # "height": 512,
53
+ # # "width": 512
54
+ # }
55
+ # }
56
+
57
+ # response = requests.post(API_URL, headers=headers, json=payload)
58
 
59
+ # if response.status_code != 200:
60
+ # st.error(f"API请求失败:{response.status_code}, {response.text}")
61
+ # return None
62
+
63
+ # # 将返回的字节流或 Base64 数据转换为 PIL Image
64
+ # try:
65
+ # image = Image.open(BytesIO(response.content))
66
+ # except:
67
+ # st.error("生成图像失败,请检查模型是否支持图像输出。")
68
+ # return None
69
+ # return image
70
+
71
+ # # ----------------------------
72
+ # # 生成按钮
73
+ # # ----------------------------
74
+ # if st.button("Generate"):
75
+ # with st.spinner("Generating via Hugging Face API..."):
76
+ # image = generate(prompt)
77
 
78
+ # if image:
79
+ # st.image(image, caption="Result", width=512)
80
+ # buf = io.BytesIO()
81
+ # image.save(buf, format="PNG")
82
+ # st.download_button(
83
+ # "Download",
84
+ # buf.getvalue(),
85
+ # "result.png"
86
+ # )
87
+
88
+
89
+
90
+ from diffusers import StableDiffusionPipeline
91
+ import torch
92
+ from PIL import Image
93
+ import io
94
 
95
  # ----------------------------
96
+ # 加载基础模型
97
+ # ----------------------------
98
+ base_model = "runwayml/stable-diffusion-v1-5"
99
+ pipe = StableDiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)
100
+ pipe = pipe.to("cuda") # 有GPU加速
101
+
102
  # ----------------------------
103
+ # 加载自定义微调权重
104
+ # ----------------------------
105
+ ckpt_path = "./pytorch_custom_diffusion_weights.bin"
106
+
107
+ # 假设你用的是 Diffusers 支持的 UNet 权重增量加载
108
+ pipe.unet.load_attn_procs(ckpt_path)
109
+
110
+
111
+ import streamlit as st
112
+
113
+ st.set_page_config(page_title="Custom Style Diffusion Demo", layout="wide")
114
+ st.title("Custom Style Diffusion 本地推理 Demo")
115
+
116
+ prompt = st.text_input("Prompt", "A <new1> reference. New Year image with a rabbit in 2D anime style")
117
+ steps = st.slider("Steps", 10, 320, 50)
118
+ guidance = st.slider("Guidance Scale", 1.0, 20.0, 7.5)
119
+
120
  if st.button("Generate"):
121
+ with st.spinner("Generating image..."):
122
+ result = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance)
123
+ image = result.images[0]
124
+ st.image(image, caption="Result", use_column_width=True)
125
+
126
  buf = io.BytesIO()
127
  image.save(buf, format="PNG")
128
+ st.download_button("Download Image", buf.getvalue(), "result.png")