Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,76 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
|
|
4 |
import spaces #[uncomment to use ZeroGPU]
|
5 |
-
#added DPMSolverSDEScheduler
|
6 |
from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
|
7 |
import torch
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl"
|
11 |
|
12 |
-
if torch.cuda.is_available()
|
13 |
-
torch_dtype = torch.float16
|
14 |
-
else:
|
15 |
-
torch_dtype = torch.float32
|
16 |
|
17 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
18 |
-
#added full line below pipe.scheduler...
|
19 |
pipe.scheduler = DPMSolverSDEScheduler.from_config(pipe.scheduler.config, algorithm_type="dpmsolver++", solver_order=2, use_karras_sigmas=True)
|
20 |
pipe = pipe.to(device)
|
21 |
|
22 |
MAX_SEED = np.iinfo(np.int32).max
|
23 |
MAX_IMAGE_SIZE = 1024
|
24 |
|
25 |
-
|
26 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
if randomize_seed:
|
29 |
seed = random.randint(0, MAX_SEED)
|
30 |
-
|
31 |
generator = torch.Generator().manual_seed(seed)
|
32 |
|
|
|
33 |
image = pipe(
|
34 |
-
prompt
|
35 |
-
negative_prompt
|
36 |
-
guidance_scale
|
37 |
-
num_inference_steps
|
38 |
-
width
|
39 |
-
height
|
40 |
-
generator
|
41 |
-
).images[0]
|
42 |
|
43 |
return image, seed
|
44 |
|
@@ -48,7 +80,7 @@ examples = [
|
|
48 |
"A delicious ceviche cheesecake slice",
|
49 |
]
|
50 |
|
51 |
-
css="""
|
52 |
#col-container {
|
53 |
margin: 0 auto;
|
54 |
max-width: 640px;
|
@@ -58,12 +90,9 @@ css="""
|
|
58 |
with gr.Blocks(css=css) as demo:
|
59 |
|
60 |
with gr.Column(elem_id="col-container"):
|
61 |
-
gr.Markdown(
|
62 |
-
# Text-to-Image Gradio Template
|
63 |
-
""")
|
64 |
|
65 |
with gr.Row():
|
66 |
-
|
67 |
prompt = gr.Text(
|
68 |
label="Prompt",
|
69 |
show_label=False,
|
@@ -77,7 +106,6 @@ with gr.Blocks(css=css) as demo:
|
|
77 |
result = gr.Image(label="Result", show_label=False)
|
78 |
|
79 |
with gr.Accordion("Advanced Settings", open=False):
|
80 |
-
|
81 |
negative_prompt = gr.Text(
|
82 |
label="Negative prompt",
|
83 |
max_lines=1,
|
@@ -96,13 +124,12 @@ with gr.Blocks(css=css) as demo:
|
|
96 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
97 |
|
98 |
with gr.Row():
|
99 |
-
|
100 |
width = gr.Slider(
|
101 |
label="Width",
|
102 |
minimum=256,
|
103 |
maximum=MAX_IMAGE_SIZE,
|
104 |
step=32,
|
105 |
-
value=1024,
|
106 |
)
|
107 |
|
108 |
height = gr.Slider(
|
@@ -110,17 +137,16 @@ with gr.Blocks(css=css) as demo:
|
|
110 |
minimum=256,
|
111 |
maximum=MAX_IMAGE_SIZE,
|
112 |
step=32,
|
113 |
-
value=1024,
|
114 |
)
|
115 |
|
116 |
with gr.Row():
|
117 |
-
|
118 |
guidance_scale = gr.Slider(
|
119 |
label="Guidance scale",
|
120 |
minimum=0.0,
|
121 |
maximum=10.0,
|
122 |
step=0.1,
|
123 |
-
value=0.0,
|
124 |
)
|
125 |
|
126 |
num_inference_steps = gr.Slider(
|
@@ -128,18 +154,20 @@ with gr.Blocks(css=css) as demo:
|
|
128 |
minimum=1,
|
129 |
maximum=50,
|
130 |
step=1,
|
131 |
-
value=2,
|
132 |
)
|
133 |
|
134 |
gr.Examples(
|
135 |
-
examples
|
136 |
-
inputs
|
137 |
)
|
|
|
|
|
138 |
gr.on(
|
139 |
triggers=[run_button.click, prompt.submit],
|
140 |
-
fn
|
141 |
-
inputs
|
142 |
-
outputs
|
143 |
)
|
144 |
|
145 |
-
demo.queue().launch()
|
|
|
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" # Replace with your model ID
|
18 |
|
19 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
|
|
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)
|
23 |
pipe = pipe.to(device)
|
24 |
|
25 |
MAX_SEED = np.iinfo(np.int32).max
|
26 |
MAX_IMAGE_SIZE = 1024
|
27 |
|
28 |
+
# Function to enhance the prompt using Hugging Face's Inference API
|
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=enhanced_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 |
"A delicious ceviche cheesecake slice",
|
81 |
]
|
82 |
|
83 |
+
css = """
|
84 |
#col-container {
|
85 |
margin: 0 auto;
|
86 |
max-width: 640px;
|
|
|
90 |
with gr.Blocks(css=css) as demo:
|
91 |
|
92 |
with gr.Column(elem_id="col-container"):
|
93 |
+
gr.Markdown("# Text-to-Image Gradio Template")
|
|
|
|
|
94 |
|
95 |
with gr.Row():
|
|
|
96 |
prompt = gr.Text(
|
97 |
label="Prompt",
|
98 |
show_label=False,
|
|
|
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 |
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, # Default width for the model
|
133 |
)
|
134 |
|
135 |
height = gr.Slider(
|
|
|
137 |
minimum=256,
|
138 |
maximum=MAX_IMAGE_SIZE,
|
139 |
step=32,
|
140 |
+
value=1024, # Default height for the model
|
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, # Default guidance scale for the model
|
150 |
)
|
151 |
|
152 |
num_inference_steps = gr.Slider(
|
|
|
154 |
minimum=1,
|
155 |
maximum=50,
|
156 |
step=1,
|
157 |
+
value=2, # Default inference steps for the model
|
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()
|