Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,17 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
-
import os
|
5 |
-
import requests # For calling Hugging Face's Inference API
|
6 |
import spaces #[uncomment to use ZeroGPU]
|
7 |
from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
|
8 |
import torch
|
9 |
|
10 |
-
# Get Hugging Face API key from environment variable
|
11 |
-
huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
|
12 |
-
|
13 |
-
if huggingface_api_key is None:
|
14 |
-
raise ValueError("Hugging Face API key is not set. Please set the 'HUGGINGFACE_API_KEY' environment variable.")
|
15 |
-
|
16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
-
model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl"
|
18 |
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
22 |
pipe.scheduler = DPMSolverSDEScheduler.from_config(pipe.scheduler.config, algorithm_type="dpmsolver++", solver_order=2, use_karras_sigmas=True)
|
@@ -25,52 +20,23 @@ pipe = pipe.to(device)
|
|
25 |
MAX_SEED = np.iinfo(np.int32).max
|
26 |
MAX_IMAGE_SIZE = 1024
|
27 |
|
28 |
-
#
|
29 |
-
def enhance_prompt(prompt):
|
30 |
-
hf_model_id = "EleutherAI/gpt-neo-1.3B" # You can choose a different model
|
31 |
-
api_url = f"https://api-inference.huggingface.co/models/{hf_model_id}"
|
32 |
-
|
33 |
-
headers = {
|
34 |
-
"Authorization": f"Bearer {huggingface_api_key}"
|
35 |
-
}
|
36 |
-
|
37 |
-
payload = {
|
38 |
-
"inputs": f"Enhance this prompt: {prompt}",
|
39 |
-
"parameters": {"max_new_tokens": 50, "temperature": 0.7}
|
40 |
-
}
|
41 |
-
|
42 |
-
response = requests.post(api_url, headers=headers, json=payload)
|
43 |
-
|
44 |
-
if response.status_code != 200:
|
45 |
-
raise Exception(f"Failed to enhance prompt: {response.text}")
|
46 |
-
|
47 |
-
result = response.json()
|
48 |
-
enhanced_prompt = result[0]['generated_text']
|
49 |
-
|
50 |
-
return enhanced_prompt
|
51 |
-
|
52 |
-
# Inference function with automatic prompt enhancement
|
53 |
-
@spaces.GPU # [uncomment to use ZeroGPU]
|
54 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
55 |
-
|
56 |
-
# Automatically enhance the prompt using Hugging Face's API
|
57 |
-
enhanced_prompt = enhance_prompt(prompt)
|
58 |
-
|
59 |
if randomize_seed:
|
60 |
seed = random.randint(0, MAX_SEED)
|
61 |
-
|
62 |
generator = torch.Generator().manual_seed(seed)
|
63 |
|
64 |
-
# Generate the image using the enhanced prompt
|
65 |
image = pipe(
|
66 |
-
prompt=
|
67 |
-
negative_prompt=negative_prompt,
|
68 |
-
guidance_scale=guidance_scale,
|
69 |
-
num_inference_steps=num_inference_steps,
|
70 |
-
width=width,
|
71 |
-
height=height,
|
72 |
-
generator=generator
|
73 |
-
).images[0]
|
74 |
|
75 |
return image, seed
|
76 |
|
@@ -80,7 +46,7 @@ examples = [
|
|
80 |
"A delicious ceviche cheesecake slice",
|
81 |
]
|
82 |
|
83 |
-
css
|
84 |
#col-container {
|
85 |
margin: 0 auto;
|
86 |
max-width: 640px;
|
@@ -90,9 +56,12 @@ css = """
|
|
90 |
with gr.Blocks(css=css) as demo:
|
91 |
|
92 |
with gr.Column(elem_id="col-container"):
|
93 |
-
gr.Markdown("
|
|
|
|
|
94 |
|
95 |
with gr.Row():
|
|
|
96 |
prompt = gr.Text(
|
97 |
label="Prompt",
|
98 |
show_label=False,
|
@@ -106,6 +75,7 @@ with gr.Blocks(css=css) as demo:
|
|
106 |
result = gr.Image(label="Result", show_label=False)
|
107 |
|
108 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
109 |
negative_prompt = gr.Text(
|
110 |
label="Negative prompt",
|
111 |
max_lines=1,
|
@@ -124,12 +94,13 @@ with gr.Blocks(css=css) as demo:
|
|
124 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
125 |
|
126 |
with gr.Row():
|
|
|
127 |
width = gr.Slider(
|
128 |
label="Width",
|
129 |
minimum=256,
|
130 |
maximum=MAX_IMAGE_SIZE,
|
131 |
step=32,
|
132 |
-
value=1024,
|
133 |
)
|
134 |
|
135 |
height = gr.Slider(
|
@@ -137,16 +108,17 @@ with gr.Blocks(css=css) as demo:
|
|
137 |
minimum=256,
|
138 |
maximum=MAX_IMAGE_SIZE,
|
139 |
step=32,
|
140 |
-
value=1024,
|
141 |
)
|
142 |
|
143 |
with gr.Row():
|
|
|
144 |
guidance_scale = gr.Slider(
|
145 |
label="Guidance scale",
|
146 |
minimum=0.0,
|
147 |
maximum=10.0,
|
148 |
step=0.1,
|
149 |
-
value=0.0,
|
150 |
)
|
151 |
|
152 |
num_inference_steps = gr.Slider(
|
@@ -154,20 +126,18 @@ with gr.Blocks(css=css) as demo:
|
|
154 |
minimum=1,
|
155 |
maximum=50,
|
156 |
step=1,
|
157 |
-
value=2,
|
158 |
)
|
159 |
|
160 |
gr.Examples(
|
161 |
-
examples=examples,
|
162 |
-
inputs=[prompt]
|
163 |
)
|
164 |
-
|
165 |
-
# Handle button clicks and prompt submission
|
166 |
gr.on(
|
167 |
triggers=[run_button.click, prompt.submit],
|
168 |
-
fn=infer,
|
169 |
-
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
170 |
-
outputs=[result, seed]
|
171 |
)
|
172 |
|
173 |
-
demo.queue().launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
|
|
4 |
import spaces #[uncomment to use ZeroGPU]
|
5 |
from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
|
6 |
import torch
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl" #Replace to the model you would like to use
|
10 |
|
11 |
+
if torch.cuda.is_available():
|
12 |
+
torch_dtype = torch.float16
|
13 |
+
else:
|
14 |
+
torch_dtype = torch.float32
|
15 |
|
16 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
17 |
pipe.scheduler = DPMSolverSDEScheduler.from_config(pipe.scheduler.config, algorithm_type="dpmsolver++", solver_order=2, use_karras_sigmas=True)
|
|
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
+
@spaces.GPU #[uncomment to use ZeroGPU]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
25 |
+
|
|
|
|
|
|
|
26 |
if randomize_seed:
|
27 |
seed = random.randint(0, MAX_SEED)
|
28 |
+
|
29 |
generator = torch.Generator().manual_seed(seed)
|
30 |
|
|
|
31 |
image = pipe(
|
32 |
+
prompt = prompt,
|
33 |
+
negative_prompt = negative_prompt,
|
34 |
+
guidance_scale = guidance_scale,
|
35 |
+
num_inference_steps = num_inference_steps,
|
36 |
+
width = width,
|
37 |
+
height = height,
|
38 |
+
generator = generator
|
39 |
+
).images[0]
|
40 |
|
41 |
return image, seed
|
42 |
|
|
|
46 |
"A delicious ceviche cheesecake slice",
|
47 |
]
|
48 |
|
49 |
+
css="""
|
50 |
#col-container {
|
51 |
margin: 0 auto;
|
52 |
max-width: 640px;
|
|
|
56 |
with gr.Blocks(css=css) as demo:
|
57 |
|
58 |
with gr.Column(elem_id="col-container"):
|
59 |
+
gr.Markdown(f"""
|
60 |
+
# Text-to-Image Gradio Template
|
61 |
+
""")
|
62 |
|
63 |
with gr.Row():
|
64 |
+
|
65 |
prompt = gr.Text(
|
66 |
label="Prompt",
|
67 |
show_label=False,
|
|
|
75 |
result = gr.Image(label="Result", show_label=False)
|
76 |
|
77 |
with gr.Accordion("Advanced Settings", open=False):
|
78 |
+
|
79 |
negative_prompt = gr.Text(
|
80 |
label="Negative prompt",
|
81 |
max_lines=1,
|
|
|
94 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
95 |
|
96 |
with gr.Row():
|
97 |
+
|
98 |
width = gr.Slider(
|
99 |
label="Width",
|
100 |
minimum=256,
|
101 |
maximum=MAX_IMAGE_SIZE,
|
102 |
step=32,
|
103 |
+
value=1024, #Replace with defaults that work for your model
|
104 |
)
|
105 |
|
106 |
height = gr.Slider(
|
|
|
108 |
minimum=256,
|
109 |
maximum=MAX_IMAGE_SIZE,
|
110 |
step=32,
|
111 |
+
value=1024, #Replace with defaults that work for your model
|
112 |
)
|
113 |
|
114 |
with gr.Row():
|
115 |
+
|
116 |
guidance_scale = gr.Slider(
|
117 |
label="Guidance scale",
|
118 |
minimum=0.0,
|
119 |
maximum=10.0,
|
120 |
step=0.1,
|
121 |
+
value=0.0, #Replace with defaults that work for your model
|
122 |
)
|
123 |
|
124 |
num_inference_steps = gr.Slider(
|
|
|
126 |
minimum=1,
|
127 |
maximum=50,
|
128 |
step=1,
|
129 |
+
value=2, #Replace with defaults that work for your model
|
130 |
)
|
131 |
|
132 |
gr.Examples(
|
133 |
+
examples = examples,
|
134 |
+
inputs = [prompt]
|
135 |
)
|
|
|
|
|
136 |
gr.on(
|
137 |
triggers=[run_button.click, prompt.submit],
|
138 |
+
fn = infer,
|
139 |
+
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
140 |
+
outputs = [result, seed]
|
141 |
)
|
142 |
|
143 |
+
demo.queue().launch()
|