Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Votepurchase-AnythingXL Xl
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Votepurchase-AnythingXL Xl
|
| 3 |
+
emoji: 🖼
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.26.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import PIL.Image
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import random
|
| 6 |
+
from diffusers import ControlNetModel, StableDiffusionXLPipeline, AutoencoderKL
|
| 7 |
+
from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
|
| 8 |
+
import cv2
|
| 9 |
+
import torch
|
| 10 |
+
import spaces
|
| 11 |
+
from hf import upload_image
|
| 12 |
+
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
|
| 15 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 16 |
+
"votepurchase/AnythingXL_xl",
|
| 17 |
+
torch_dtype=torch.float16,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 21 |
+
pipe.to(device)
|
| 22 |
+
|
| 23 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 24 |
+
MAX_IMAGE_SIZE = 1216
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@spaces.GPU
|
| 28 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, filename=""):
|
| 29 |
+
|
| 30 |
+
if randomize_seed:
|
| 31 |
+
seed = random.randint(0, MAX_SEED)
|
| 32 |
+
|
| 33 |
+
generator = torch.Generator().manual_seed(seed)
|
| 34 |
+
|
| 35 |
+
output_image = pipe(
|
| 36 |
+
prompt=prompt,
|
| 37 |
+
negative_prompt=negative_prompt,
|
| 38 |
+
guidance_scale=guidance_scale,
|
| 39 |
+
num_inference_steps=num_inference_steps,
|
| 40 |
+
width=width,
|
| 41 |
+
height=height,
|
| 42 |
+
generator=generator
|
| 43 |
+
).images[0]
|
| 44 |
+
|
| 45 |
+
if filename: upload_image(output_image, filename)
|
| 46 |
+
|
| 47 |
+
return output_image
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
css = """
|
| 51 |
+
#col-container {
|
| 52 |
+
margin: 0 auto;
|
| 53 |
+
max-width: 520px;
|
| 54 |
+
}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
with gr.Blocks(css=css) as demo:
|
| 58 |
+
|
| 59 |
+
with gr.Column(elem_id="col-container"):
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
prompt = gr.Text(
|
| 63 |
+
label="Prompt",
|
| 64 |
+
show_label=False,
|
| 65 |
+
max_lines=1,
|
| 66 |
+
placeholder="Enter your prompt",
|
| 67 |
+
container=False,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
run_button = gr.Button("Run", scale=0)
|
| 71 |
+
|
| 72 |
+
result = gr.Image(label="Result", show_label=False)
|
| 73 |
+
|
| 74 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 75 |
+
|
| 76 |
+
negative_prompt = gr.Text(
|
| 77 |
+
label="Negative prompt",
|
| 78 |
+
max_lines=1,
|
| 79 |
+
placeholder="Enter a negative prompt",
|
| 80 |
+
value="nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
seed = gr.Slider(
|
| 84 |
+
label="Seed",
|
| 85 |
+
minimum=0,
|
| 86 |
+
maximum=MAX_SEED,
|
| 87 |
+
step=1,
|
| 88 |
+
value=0,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
width = gr.Slider(
|
| 95 |
+
label="Width",
|
| 96 |
+
minimum=256,
|
| 97 |
+
maximum=MAX_IMAGE_SIZE,
|
| 98 |
+
step=32,
|
| 99 |
+
value=1024,#832,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
height = gr.Slider(
|
| 103 |
+
label="Height",
|
| 104 |
+
minimum=256,
|
| 105 |
+
maximum=MAX_IMAGE_SIZE,
|
| 106 |
+
step=32,
|
| 107 |
+
value=1024,#1216,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with gr.Row():
|
| 111 |
+
guidance_scale = gr.Slider(
|
| 112 |
+
label="Guidance scale",
|
| 113 |
+
minimum=0.0,
|
| 114 |
+
maximum=20.0,
|
| 115 |
+
step=0.1,
|
| 116 |
+
value=7,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
num_inference_steps = gr.Slider(
|
| 120 |
+
label="Number of inference steps",
|
| 121 |
+
minimum=1,
|
| 122 |
+
maximum=28,
|
| 123 |
+
step=1,
|
| 124 |
+
value=28,
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
run_button.click(#lambda x: None, inputs=None, outputs=result).then(
|
| 128 |
+
fn=infer,
|
| 129 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 130 |
+
outputs=[result]
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
demo.queue().launch()
|
hf.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
os.environ['HF_OUTPUT_REPO'] = 'votepurchase/pony' # The name of the 'model' repo to upload to. private repo recommended.
|
| 6 |
+
# Set HF write token to HF_TOKEN as SECRET (never as environment)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def upload_to_repo(path, filename):
|
| 10 |
+
from huggingface_hub import HfApi, hf_hub_url
|
| 11 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 12 |
+
repo_id = os.environ.get("HF_OUTPUT_REPO")
|
| 13 |
+
api = HfApi()
|
| 14 |
+
try:
|
| 15 |
+
api.upload_file(path_or_fileobj=path, path_in_repo=filename, repo_id=repo_id, token=hf_token)
|
| 16 |
+
url = hf_hub_url(repo_id=repo_id, filename=filename)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Error: Failed to upload to {repo_id}. ")
|
| 19 |
+
return None
|
| 20 |
+
return url
|
| 21 |
+
|
| 22 |
+
def upload_image(path, filename):
|
| 23 |
+
url = upload_to_repo(path, filename)
|
| 24 |
+
print(url) # for debug
|
| 25 |
+
return url
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def compose_image_url(repo_id, filename): # Can be used in local environment
|
| 29 |
+
from huggingface_hub import hf_hub_url
|
| 30 |
+
url = hf_hub_url(repo_id=repo_id, filename=filename)
|
| 31 |
+
return url
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def is_exists_image(filename): # Can be used in local environment
|
| 35 |
+
from huggingface_hub import HfApi
|
| 36 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 37 |
+
repo_id = os.environ.get("HF_OUTPUT_REPO")
|
| 38 |
+
api = HfApi()
|
| 39 |
+
try:
|
| 40 |
+
is_exists = api.file_exists(repo_id=repo_id, filename=filename, token=hf_token)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return None
|
| 43 |
+
return is_exists
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def download_image(filename): # Can be used in local environment
|
| 47 |
+
from huggingface_hub import HfApi
|
| 48 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 49 |
+
repo_id = os.environ.get("HF_OUTPUT_REPO")
|
| 50 |
+
api = HfApi()
|
| 51 |
+
try:
|
| 52 |
+
if is_exists_image:
|
| 53 |
+
api.hf_hub_download(repo_id=repo_id, filename=filename, local_dir=".", token=hf_token)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return
|
| 56 |
+
|
| 57 |
+
# HfApi Client Document: https://huggingface.co/docs/huggingface_hub/package_reference/hf_api
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate
|
| 2 |
+
diffusers
|
| 3 |
+
invisible_watermark
|
| 4 |
+
torch
|
| 5 |
+
transformers
|
| 6 |
+
xformers
|