vincentkyo commited on
Commit
e8c85aa
·
verified ·
1 Parent(s): c8ee83a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -99
app.py CHANGED
@@ -8,7 +8,7 @@ from huggingface_hub import InferenceClient
8
  from PIL import Image
9
 
10
 
11
- def run_image_to_image(img: Image.Image, prompt: str, model: str) -> Image.Image:
12
  token = os.getenv("HF_TOKEN")
13
  if not token:
14
  raise RuntimeError("HF_TOKEN is not set. Please set your Hugging Face token in environment variables.")
@@ -23,7 +23,7 @@ def run_image_to_image(img: Image.Image, prompt: str, model: str) -> Image.Image
23
  input_bytes = f.read()
24
 
25
  try:
26
- result = client.image_to_image(
27
  input_bytes,
28
  prompt=prompt,
29
  model=model,
@@ -79,8 +79,8 @@ with gr.Blocks(title="图像编辑工具", css=".gradio-container {max-width: 98
79
  - 将图片背景变成樱花盛开的场景
80
  """)
81
 
82
- def _run_and_pack(img: Image.Image, p: str, m: str):
83
- result_img = run_image_to_image(img, p, m)
84
  # 保存为临时文件以供下载
85
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
86
  result_img.save(tmp.name)
@@ -94,98 +94,3 @@ if __name__ == "__main__":
94
  demo.launch(server_name="0.0.0.0", server_port=7860)
95
 
96
 
97
- import os
98
- import traceback
99
- from typing import Optional
100
-
101
- import gradio as gr
102
- from huggingface_hub import InferenceClient
103
- from PIL import Image
104
-
105
-
106
- def run_image_to_image(img: Image.Image, prompt: str, model: str) -> Image.Image:
107
- token = os.getenv("HF_TOKEN")
108
- if not token:
109
- raise RuntimeError("HF_TOKEN is not set. Please set your Hugging Face token in environment variables.")
110
-
111
- # 直接使用Hugging Face的推理服务
112
- client = InferenceClient(token=token)
113
-
114
- # Convert PIL image to bytes
115
- with gr.processing_utils.TempFile(suffix=".png") as tmp:
116
- img.save(tmp.name, format="PNG")
117
- with open(tmp.name, "rb") as f:
118
- input_bytes = f.read()
119
-
120
- try:
121
- result = client.image_to_image(
122
- input_bytes,
123
- prompt=prompt,
124
- model=model,
125
- )
126
- return result
127
- except Exception as e:
128
- traceback.print_exc()
129
- error_msg = str(e)
130
- if "402" in error_msg:
131
- raise RuntimeError("错误402:付费要求。可能是模型需要付费使用或超出免费额度。请检查您的HuggingFace账户状态。")
132
- raise
133
-
134
-
135
- with gr.Blocks(title="图像编辑工具", css=".gradio-container {max-width: 980px}") as demo:
136
- gr.Markdown("**图像编辑工具** · 上传图片,输入中文或英文提示词,一键完成图像变换")
137
-
138
- with gr.Row():
139
- with gr.Column():
140
- in_img = gr.Image(type="pil", label="上传图片", height=420)
141
- prompt = gr.Textbox(
142
- value="将图片中的人物变成卡通风格",
143
- label="提示词(支持中文和英文)",
144
- lines=2,
145
- placeholder="请输入您想要的图像变换效果,例如:'将图片变成水彩画风格'、'给图片添加雪景效果'等"
146
- )
147
- model = gr.Dropdown(
148
- choices=[
149
- "svjack/Stable-Diffusion-FineTuned-zh-v2",
150
- "Tencent-Hunyuan/HunyuanDiT-v1.1-Diffusers-Distilled",
151
- "tencent/HunyuanImage-3.0"
152
- ],
153
- value="svjack/Stable-Diffusion-FineTuned-zh-v2",
154
- label="模型(均支持中文输入)",
155
- )
156
- with gr.Row():
157
- run_btn = gr.Button("一键转换", variant="primary")
158
- clear_btn = gr.Button("清空")
159
- with gr.Column():
160
- out_img = gr.Image(type="pil", label="输出结果", height=420)
161
- download = gr.File(label="下载输出", interactive=False)
162
-
163
- gr.Markdown("""
164
- ### 使用说明
165
- 1. 上传您想要编辑的图片
166
- 2. 输入中文或英文提示词,描述您想要的变换效果
167
- 3. 选择支持中文的模型(所有列出的模型均为免费使用)
168
- 4. 点击"一键转换"按钮
169
-
170
- ### 提示词示例
171
- - 将图片变成水彩画风格
172
- - 给图片添加雪景效果
173
- - 将图片中的人物变成卡通风格
174
- - 将图片背景变成樱花盛开的场景
175
- """)
176
-
177
- def _run_and_pack(img: Image.Image, p: str, m: str):
178
- result_img = run_image_to_image(img, p, m)
179
- # 保存为临时文件以供下载
180
- with gr.processing_utils.TempFile(suffix=".png") as tmp:
181
- result_img.save(tmp.name)
182
- return result_img, tmp.name
183
-
184
- run_btn.click(fn=_run_and_pack, inputs=[in_img, prompt, model], outputs=[out_img, download])
185
- clear_btn.click(lambda: (None, "", None, None), outputs=[in_img, prompt, out_img, download])
186
-
187
-
188
- if __name__ == "__main__":
189
- demo.launch(server_name="0.0.0.0", server_port=7860)
190
-
191
-
 
8
  from PIL import Image
9
 
10
 
11
+ async def run_image_to_image(img: Image.Image, prompt: str, model: str) -> Image.Image:
12
  token = os.getenv("HF_TOKEN")
13
  if not token:
14
  raise RuntimeError("HF_TOKEN is not set. Please set your Hugging Face token in environment variables.")
 
23
  input_bytes = f.read()
24
 
25
  try:
26
+ result = await client.image_to_image(
27
  input_bytes,
28
  prompt=prompt,
29
  model=model,
 
79
  - 将图片背景变成樱花盛开的场景
80
  """)
81
 
82
+ async def _run_and_pack(img: Image.Image, p: str, m: str):
83
+ result_img = await run_image_to_image(img, p, m)
84
  # 保存为临时文件以供下载
85
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
86
  result_img.save(tmp.name)
 
94
  demo.launch(server_name="0.0.0.0", server_port=7860)
95
 
96