Upload folder using huggingface_hub
Browse files- README.md +7 -5
- app.py +101 -0
- deploy_space.py +53 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: pink
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 6.9.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: wode
|
| 3 |
+
emoji: 🌻
|
| 4 |
colorFrom: pink
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# wode
|
| 13 |
+
|
| 14 |
+
Anime image generation Space powered by Diffusers.
|
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gc
|
| 2 |
+
import os
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from diffusers import AutoPipelineForText2Image
|
| 8 |
+
|
| 9 |
+
MODEL_ID = os.getenv("MODEL_ID", "Asahina2K/animagine-xl-3.1")
|
| 10 |
+
DEFAULT_NEGATIVE = os.getenv(
|
| 11 |
+
"DEFAULT_NEGATIVE",
|
| 12 |
+
"lowres, blurry, bad anatomy, extra fingers, cropped, worst quality, low quality",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
_pipe = None
|
| 16 |
+
_device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
+
_dtype = torch.float16 if _device == "cuda" else torch.float32
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def load_pipe():
|
| 21 |
+
global _pipe
|
| 22 |
+
if _pipe is None:
|
| 23 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 24 |
+
MODEL_ID,
|
| 25 |
+
torch_dtype=_dtype,
|
| 26 |
+
use_safetensors=True,
|
| 27 |
+
)
|
| 28 |
+
if hasattr(pipe, "enable_attention_slicing"):
|
| 29 |
+
pipe.enable_attention_slicing()
|
| 30 |
+
if hasattr(pipe, "vae") and hasattr(pipe.vae, "enable_tiling"):
|
| 31 |
+
pipe.vae.enable_tiling()
|
| 32 |
+
pipe = pipe.to(_device)
|
| 33 |
+
_pipe = pipe
|
| 34 |
+
return _pipe
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def unload_pipe():
|
| 38 |
+
global _pipe
|
| 39 |
+
if _pipe is not None:
|
| 40 |
+
_pipe = None
|
| 41 |
+
gc.collect()
|
| 42 |
+
if torch.cuda.is_available():
|
| 43 |
+
torch.cuda.empty_cache()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def generate(
|
| 47 |
+
prompt: str,
|
| 48 |
+
negative_prompt: str,
|
| 49 |
+
steps: int,
|
| 50 |
+
guidance_scale: float,
|
| 51 |
+
width: int,
|
| 52 |
+
height: int,
|
| 53 |
+
seed: int,
|
| 54 |
+
):
|
| 55 |
+
if not prompt or not prompt.strip():
|
| 56 |
+
raise gr.Error("Prompt 不能为空")
|
| 57 |
+
|
| 58 |
+
pipe = load_pipe()
|
| 59 |
+
generator: Optional[torch.Generator] = None
|
| 60 |
+
if seed >= 0:
|
| 61 |
+
generator = torch.Generator(device=_device).manual_seed(int(seed))
|
| 62 |
+
|
| 63 |
+
image = pipe(
|
| 64 |
+
prompt=prompt,
|
| 65 |
+
negative_prompt=negative_prompt,
|
| 66 |
+
num_inference_steps=int(steps),
|
| 67 |
+
guidance_scale=float(guidance_scale),
|
| 68 |
+
width=int(width),
|
| 69 |
+
height=int(height),
|
| 70 |
+
generator=generator,
|
| 71 |
+
).images[0]
|
| 72 |
+
return image
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
with gr.Blocks(title="wode") as demo:
|
| 76 |
+
gr.Markdown(f"# wode\n\nModel: `{MODEL_ID}`")
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
prompt = gr.Textbox(label="Prompt", lines=5, placeholder="masterpiece, best quality, 1girl, sunflower field, anime style")
|
| 80 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", lines=3, value=DEFAULT_NEGATIVE)
|
| 81 |
+
with gr.Row():
|
| 82 |
+
steps = gr.Slider(10, 50, value=28, step=1, label="Steps")
|
| 83 |
+
guidance_scale = gr.Slider(1, 12, value=7, step=0.5, label="CFG")
|
| 84 |
+
with gr.Row():
|
| 85 |
+
width = gr.Slider(512, 1024, value=832, step=64, label="Width")
|
| 86 |
+
height = gr.Slider(512, 1536, value=1216, step=64, label="Height")
|
| 87 |
+
seed = gr.Number(label="Seed (-1 = random)", value=-1, precision=0)
|
| 88 |
+
run = gr.Button("Generate", variant="primary")
|
| 89 |
+
unload = gr.Button("Unload model from memory")
|
| 90 |
+
with gr.Column():
|
| 91 |
+
out = gr.Image(label="Image", type="pil")
|
| 92 |
+
|
| 93 |
+
run.click(
|
| 94 |
+
fn=generate,
|
| 95 |
+
inputs=[prompt, negative_prompt, steps, guidance_scale, width, height, seed],
|
| 96 |
+
outputs=out,
|
| 97 |
+
)
|
| 98 |
+
unload.click(fn=unload_pipe, outputs=[])
|
| 99 |
+
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
demo.queue(max_size=8).launch()
|
deploy_space.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
from huggingface_hub import HfApi
|
| 7 |
+
|
| 8 |
+
SPACE_NAME = os.environ.get("SPACE_NAME", "wode")
|
| 9 |
+
PRIVATE = os.environ.get("PRIVATE", "false").lower() == "true"
|
| 10 |
+
FOLDER = Path(os.environ.get("FOLDER", ".")).resolve()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
token = sys.stdin.read().strip()
|
| 15 |
+
if not token:
|
| 16 |
+
raise SystemExit("missing token on stdin")
|
| 17 |
+
|
| 18 |
+
api = HfApi(token=token)
|
| 19 |
+
me = api.whoami()
|
| 20 |
+
username = me.get("name") or me.get("fullname") or me.get("email") or "unknown"
|
| 21 |
+
|
| 22 |
+
repo_url = api.create_repo(
|
| 23 |
+
repo_id=SPACE_NAME,
|
| 24 |
+
repo_type="space",
|
| 25 |
+
private=PRIVATE,
|
| 26 |
+
exist_ok=True,
|
| 27 |
+
space_sdk="gradio",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
full_repo_id = repo_url.repo_id if hasattr(repo_url, "repo_id") else str(repo_url).replace("https://huggingface.co/", "")
|
| 31 |
+
|
| 32 |
+
api.upload_folder(
|
| 33 |
+
repo_id=full_repo_id,
|
| 34 |
+
repo_type="space",
|
| 35 |
+
folder_path=str(FOLDER),
|
| 36 |
+
ignore_patterns=["*.pyc", "__pycache__/*", ".git/*"],
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
api.restart_space(repo_id=full_repo_id)
|
| 41 |
+
except Exception:
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
+
out = {
|
| 45 |
+
"username": username,
|
| 46 |
+
"repo_id": full_repo_id,
|
| 47 |
+
"url": f"https://huggingface.co/spaces/{full_repo_id}",
|
| 48 |
+
}
|
| 49 |
+
print(json.dumps(out, ensure_ascii=False))
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
diffusers
|
| 4 |
+
accelerate
|
| 5 |
+
safetensors
|
| 6 |
+
gradio>=4.44.0
|
| 7 |
+
Pillow
|