Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,98 @@
|
|
| 1 |
import os
|
| 2 |
-
import traceback
|
| 3 |
-
import tempfile
|
| 4 |
-
from typing import Optional
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
from PIL import Image
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
token = os.getenv("HF_TOKEN")
|
| 13 |
if not token:
|
| 14 |
-
raise
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
client = InferenceClient(token=token)
|
| 18 |
-
|
| 19 |
-
# Convert PIL image to bytes
|
| 20 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
| 21 |
-
img.save(tmp.name, format="PNG")
|
| 22 |
-
with open(tmp.name, "rb") as f:
|
| 23 |
-
input_bytes = f.read()
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
prompt=prompt,
|
| 29 |
-
|
|
|
|
| 30 |
)
|
| 31 |
-
return
|
| 32 |
except Exception as e:
|
| 33 |
traceback.print_exc()
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
raise RuntimeError("错误402:付费要求。可能是模型需要付费使用或超出免费额度。请检查您的HuggingFace账户状态。")
|
| 37 |
-
raise
|
| 38 |
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
with gr.Row():
|
| 44 |
-
with gr.Column():
|
| 45 |
-
in_img = gr.Image(type="pil", label="上传图片", height=
|
| 46 |
prompt = gr.Textbox(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
lines=
|
| 50 |
-
placeholder="
|
| 51 |
)
|
|
|
|
|
|
|
| 52 |
model = gr.Dropdown(
|
| 53 |
choices=[
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
],
|
| 58 |
-
value="
|
| 59 |
-
label="
|
|
|
|
| 60 |
)
|
| 61 |
-
with gr.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
with gr.Column():
|
| 65 |
-
out_img = gr.Image(type="pil", label="输出结果", height=420)
|
| 66 |
-
download = gr.File(label="下载输出", interactive=False)
|
| 67 |
-
|
| 68 |
-
gr.Markdown("""
|
| 69 |
-
### 使用说明
|
| 70 |
-
1. 上传您想要编辑的图片
|
| 71 |
-
2. 输入中文或英文提示词,描述您想要的变换效果
|
| 72 |
-
3. 选择支持中文的模型(所有列出的模型均为免费使用)
|
| 73 |
-
4. 点击"一键转换"按钮
|
| 74 |
-
|
| 75 |
-
### 提示词示例
|
| 76 |
-
- 将图片变成水彩画风格
|
| 77 |
-
- 给图片添加雪景效果
|
| 78 |
-
- 将图片中的人物变成卡通风格
|
| 79 |
-
- 将图片背景变成樱花盛开的场景
|
| 80 |
-
""")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# 保存为临时文件以供下载
|
| 85 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 86 |
-
result_img.save(tmp.name)
|
| 87 |
-
return result_img, tmp.name
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
|
|
|
| 93 |
if __name__ == "__main__":
|
| 94 |
-
demo.launch(
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from PIL import Image
|
| 5 |
+
import traceback
|
| 6 |
|
| 7 |
+
# --- 核心函数:调用HuggingFace推理API ---
|
| 8 |
+
def run_image_to_image(img: Image.Image, prompt: str, model: str, strength: float, guidance_scale: float) -> Image.Image:
|
| 9 |
+
"""
|
| 10 |
+
使用HuggingFace InferenceClient运行image-to-image任务。
|
| 11 |
|
| 12 |
+
参数:
|
| 13 |
+
- img: 输入的PIL Image对象。
|
| 14 |
+
- prompt: 描述图像变化的文本提示词。
|
| 15 |
+
- model: 要使用的HuggingFace模型库ID。
|
| 16 |
+
- strength: 控制对原始图像的修改程度 (0.0到1.0)。
|
| 17 |
+
- guidance_scale: 控制提示词与输出图像的匹配程度。
|
| 18 |
+
"""
|
| 19 |
token = os.getenv("HF_TOKEN")
|
| 20 |
if not token:
|
| 21 |
+
raise gr.Error("错误:环境变量 HF_TOKEN 未设置。请在HuggingFace Space的Secrets中设置您的Hugging Face Token。")
|
| 22 |
|
| 23 |
+
# 在每次调用时,根据用户选择的模型ID创建客户端
|
| 24 |
+
client = InferenceClient(model=model, token=token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
try:
|
| 27 |
+
# 直接将PIL图像和参数传递给客户端
|
| 28 |
+
# 新版库支持直接传入PIL Image对象
|
| 29 |
+
result_img = client.image_to_image(
|
| 30 |
+
image=img,
|
| 31 |
prompt=prompt,
|
| 32 |
+
strength=strength,
|
| 33 |
+
guidance_scale=guidance_scale,
|
| 34 |
)
|
| 35 |
+
return result_img
|
| 36 |
except Exception as e:
|
| 37 |
traceback.print_exc()
|
| 38 |
+
# 将详细错误反馈给用户
|
| 39 |
+
raise gr.Error(f"模型推理失败,请检查模型是否支持Image-to-Image任务或更换模型尝试。错误详情: {e}")
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
+
# --- Gradio Blocks 界面 ---
|
| 43 |
+
with gr.Blocks(title="图像风格转换", css=".gradio-container {max-width: 1080px}") as demo:
|
| 44 |
+
gr.Markdown("# 图像风格转换工具 🎨")
|
| 45 |
+
gr.Markdown("上传一张图片,输入提示词,选择一个模型,然后点击“一键转换”来生成新的图片。")
|
| 46 |
|
| 47 |
+
with gr.Row(variant="panel"):
|
| 48 |
+
with gr.Column(scale=1):
|
| 49 |
+
in_img = gr.Image(type="pil", label="🖼️ 上传图片", height=512)
|
| 50 |
prompt = gr.Textbox(
|
| 51 |
+
label="📝 提示词",
|
| 52 |
+
value="A beautiful painting of a castle in a forest, fantasy, detailed, epic",
|
| 53 |
+
lines=3,
|
| 54 |
+
placeholder="输入你想要的画面效果,例如:'A cute cat wearing a wizard hat'"
|
| 55 |
)
|
| 56 |
+
# 为了保证成功率,我们选择一个已知可靠的免费模型
|
| 57 |
+
# 您的原始模型列表可以作为备选,但需确认它们在HF上部署了免费的推理API
|
| 58 |
model = gr.Dropdown(
|
| 59 |
choices=[
|
| 60 |
+
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
| 61 |
+
"runwayml/stable-diffusion-v1-5",
|
| 62 |
+
"SG161222/Realistic_Vision_V5.1_noVAE"
|
| 63 |
],
|
| 64 |
+
value="stabilityai/stable-diffusion-xl-refiner-1.0",
|
| 65 |
+
label="🤖 选择模型",
|
| 66 |
+
info="这些是经过验证的免费模型,主要支持英文提示词。"
|
| 67 |
)
|
| 68 |
+
with gr.Accordion("高级参数 (选填)", open=False):
|
| 69 |
+
strength = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.1, label="修改强度", info="值越高,图片变化越大")
|
| 70 |
+
guidance_scale = gr.Slider(minimum=0.0, maximum=20.0, value=7.5, step=0.5, label="引导强度", info="值越高,图片越接近提示词")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
with gr.Row():
|
| 73 |
+
run_btn = gr.Button("🚀 一键转换", variant="primary", scale=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
with gr.Column(scale=1):
|
| 76 |
+
out_img = gr.Image(type="pil", label="✨ 输出结果", height=512, interactive=False)
|
| 77 |
+
gr.Markdown("### 💡 提示词示例 (Examples)")
|
| 78 |
+
gr.Examples(
|
| 79 |
+
examples=[
|
| 80 |
+
["A beautiful oil painting of a landscape."],
|
| 81 |
+
["photo of a cute white cat, cinematic, 8k"],
|
| 82 |
+
["Make it in anime style, detailed, beautiful."],
|
| 83 |
+
["cyberpunk city, neon lights, rainy night"]
|
| 84 |
+
],
|
| 85 |
+
inputs=prompt,
|
| 86 |
+
label="点击示例以填充提示词"
|
| 87 |
+
)
|
| 88 |
|
| 89 |
+
# --- 按钮点击事件 ---
|
| 90 |
+
run_btn.click(
|
| 91 |
+
fn=run_image_to_image,
|
| 92 |
+
inputs=[in_img, prompt, model, strength, guidance_scale],
|
| 93 |
+
outputs=out_img
|
| 94 |
+
)
|
| 95 |
|
| 96 |
+
# --- 启动应用 ---
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
demo.launch(share=True)
|
|
|
|
|
|