Spaces:
Running
on
Zero
Running
on
Zero
Avijit Ghosh
commited on
Commit
·
f1aa060
1
Parent(s):
df3866a
add api key
Browse files
app.py
CHANGED
|
@@ -12,8 +12,11 @@ import stone
|
|
| 12 |
import os
|
| 13 |
import spaces
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Define model initialization functions
|
| 16 |
-
def load_model(model_name
|
| 17 |
if model_name == "stabilityai/sdxl-turbo":
|
| 18 |
pipeline = DiffusionPipeline.from_pretrained(
|
| 19 |
model_name,
|
|
@@ -45,12 +48,12 @@ def load_model(model_name, token=None):
|
|
| 45 |
variant="fp16"
|
| 46 |
).to("cuda")
|
| 47 |
elif model_name == "stabilityai/stable-diffusion-3-medium-diffusers":
|
| 48 |
-
if
|
| 49 |
raise ValueError("Hugging Face token is required to access this model")
|
| 50 |
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
| 51 |
model_name,
|
| 52 |
torch_dtype=torch.float16,
|
| 53 |
-
use_auth_token=
|
| 54 |
).to("cuda")
|
| 55 |
else:
|
| 56 |
raise ValueError("Unknown model name")
|
|
@@ -119,7 +122,7 @@ def skintoneplot(hex_codes):
|
|
| 119 |
return fig
|
| 120 |
|
| 121 |
@spaces.GPU
|
| 122 |
-
def generate_images_plots(prompt, model_name
|
| 123 |
global pipeline_text2image
|
| 124 |
pipeline_text2image = load_model(model_name, token)
|
| 125 |
foldername = "temp"
|
|
@@ -163,13 +166,10 @@ with gr.Blocks(title="Skin Tone and Gender bias in Text to Image Models") as dem
|
|
| 163 |
object_fit="contain",
|
| 164 |
height="auto"
|
| 165 |
)
|
| 166 |
-
gr.LoginButton()
|
| 167 |
-
token = gr.OAuthToken()
|
| 168 |
-
gr.Markdown('### You need to log in to your Hugging Face account to run Stable Diffusion 3')
|
| 169 |
btn = gr.Button("Generate images", scale=0)
|
| 170 |
with gr.Row(equal_height=True):
|
| 171 |
skinplot = gr.Plot(label="Skin Tone")
|
| 172 |
genplot = gr.Plot(label="Gender")
|
| 173 |
-
btn.click(generate_images_plots, inputs=[prompt, model_dropdown
|
| 174 |
|
| 175 |
demo.launch(debug=True)
|
|
|
|
| 12 |
import os
|
| 13 |
import spaces
|
| 14 |
|
| 15 |
+
|
| 16 |
+
api_key = os.getenv("AccessTokenSD3")
|
| 17 |
+
|
| 18 |
# Define model initialization functions
|
| 19 |
+
def load_model(model_name):
|
| 20 |
if model_name == "stabilityai/sdxl-turbo":
|
| 21 |
pipeline = DiffusionPipeline.from_pretrained(
|
| 22 |
model_name,
|
|
|
|
| 48 |
variant="fp16"
|
| 49 |
).to("cuda")
|
| 50 |
elif model_name == "stabilityai/stable-diffusion-3-medium-diffusers":
|
| 51 |
+
if api_key is None:
|
| 52 |
raise ValueError("Hugging Face token is required to access this model")
|
| 53 |
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
| 54 |
model_name,
|
| 55 |
torch_dtype=torch.float16,
|
| 56 |
+
use_auth_token=api_key
|
| 57 |
).to("cuda")
|
| 58 |
else:
|
| 59 |
raise ValueError("Unknown model name")
|
|
|
|
| 122 |
return fig
|
| 123 |
|
| 124 |
@spaces.GPU
|
| 125 |
+
def generate_images_plots(prompt, model_name):
|
| 126 |
global pipeline_text2image
|
| 127 |
pipeline_text2image = load_model(model_name, token)
|
| 128 |
foldername = "temp"
|
|
|
|
| 166 |
object_fit="contain",
|
| 167 |
height="auto"
|
| 168 |
)
|
|
|
|
|
|
|
|
|
|
| 169 |
btn = gr.Button("Generate images", scale=0)
|
| 170 |
with gr.Row(equal_height=True):
|
| 171 |
skinplot = gr.Plot(label="Skin Tone")
|
| 172 |
genplot = gr.Plot(label="Gender")
|
| 173 |
+
btn.click(generate_images_plots, inputs=[prompt, model_dropdown], outputs=[gallery, skinplot, genplot])
|
| 174 |
|
| 175 |
demo.launch(debug=True)
|