Spaces:
Running
on
Zero
Running
on
Zero
Delete app copy.py
Browse files- app copy.py +0 -149
app copy.py
DELETED
|
@@ -1,149 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from diffusers import AutoPipelineForText2Image
|
| 4 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
import stone
|
| 7 |
-
import requests
|
| 8 |
-
import io
|
| 9 |
-
import os
|
| 10 |
-
from PIL import Image
|
| 11 |
-
import spaces
|
| 12 |
-
|
| 13 |
-
import matplotlib.pyplot as plt
|
| 14 |
-
import numpy as np
|
| 15 |
-
from matplotlib.colors import hex2color
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
pipeline_text2image = AutoPipelineForText2Image.from_pretrained(
|
| 19 |
-
"stabilityai/sdxl-turbo",
|
| 20 |
-
torch_dtype=torch.float16,
|
| 21 |
-
variant="fp16",
|
| 22 |
-
)
|
| 23 |
-
pipeline_text2image = pipeline_text2image.to("cuda")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
@spaces.GPU
|
| 27 |
-
def getimgen(prompt):
|
| 28 |
-
|
| 29 |
-
return pipeline_text2image(
|
| 30 |
-
prompt=prompt,
|
| 31 |
-
guidance_scale=0.0,
|
| 32 |
-
num_inference_steps=2
|
| 33 |
-
).images[0]
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 37 |
-
blip_model = BlipForConditionalGeneration.from_pretrained(
|
| 38 |
-
"Salesforce/blip-image-captioning-large",
|
| 39 |
-
torch_dtype=torch.float16
|
| 40 |
-
).to("cuda")
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
@spaces.GPU
|
| 44 |
-
def blip_caption_image(image, prefix):
|
| 45 |
-
inputs = blip_processor(image, prefix, return_tensors="pt").to("cuda", torch.float16)
|
| 46 |
-
out = blip_model.generate(**inputs)
|
| 47 |
-
return blip_processor.decode(out[0], skip_special_tokens=True)
|
| 48 |
-
|
| 49 |
-
def genderfromcaption(caption):
|
| 50 |
-
cc = caption.split()
|
| 51 |
-
if "man" in cc or "boy" in cc:
|
| 52 |
-
return "Man"
|
| 53 |
-
elif "woman" in cc or "girl" in cc:
|
| 54 |
-
return "Woman"
|
| 55 |
-
return "Unsure"
|
| 56 |
-
|
| 57 |
-
def genderplot(genlist):
|
| 58 |
-
order = ["Man", "Woman", "Unsure"]
|
| 59 |
-
|
| 60 |
-
# Sort the list based on the order of keys
|
| 61 |
-
words = sorted(genlist, key=lambda x: order.index(x))
|
| 62 |
-
|
| 63 |
-
# Define colors for each category
|
| 64 |
-
colors = {"Man": "lightgreen", "Woman": "darkgreen", "Unsure": "lightgrey"}
|
| 65 |
-
|
| 66 |
-
# Map each word to its corresponding color
|
| 67 |
-
word_colors = [colors[word] for word in words]
|
| 68 |
-
|
| 69 |
-
# Plot the colors in a grid with reduced spacing
|
| 70 |
-
fig, axes = plt.subplots(2, 5, figsize=(5,5))
|
| 71 |
-
|
| 72 |
-
# Adjust spacing between subplots
|
| 73 |
-
plt.subplots_adjust(hspace=0.1, wspace=0.1)
|
| 74 |
-
|
| 75 |
-
for i, ax in enumerate(axes.flat):
|
| 76 |
-
ax.set_axis_off()
|
| 77 |
-
ax.add_patch(plt.Rectangle((0, 0), 1, 1, color=word_colors[i]))
|
| 78 |
-
|
| 79 |
-
return fig
|
| 80 |
-
|
| 81 |
-
def skintoneplot(hex_codes):
|
| 82 |
-
# Convert hex codes to RGB values
|
| 83 |
-
rgb_values = [hex2color(hex_code) for hex_code in hex_codes]
|
| 84 |
-
|
| 85 |
-
# Calculate luminance for each color
|
| 86 |
-
luminance_values = [0.299 * r + 0.587 * g + 0.114 * b for r, g, b in rgb_values]
|
| 87 |
-
|
| 88 |
-
# Sort hex codes based on luminance in descending order (dark to light)
|
| 89 |
-
sorted_hex_codes = [code for _, code in sorted(zip(luminance_values, hex_codes), reverse=True)]
|
| 90 |
-
|
| 91 |
-
# Plot the colors in a grid with reduced spacing
|
| 92 |
-
fig, axes = plt.subplots(2, 5, figsize=(5,5))
|
| 93 |
-
|
| 94 |
-
# Adjust spacing between subplots
|
| 95 |
-
plt.subplots_adjust(hspace=0.1, wspace=0.1)
|
| 96 |
-
|
| 97 |
-
for i, ax in enumerate(axes.flat):
|
| 98 |
-
ax.set_axis_off()
|
| 99 |
-
ax.add_patch(plt.Rectangle((0, 0), 1, 1, color=sorted_hex_codes[i]))
|
| 100 |
-
|
| 101 |
-
return fig
|
| 102 |
-
|
| 103 |
-
@spaces.GPU
|
| 104 |
-
def generate_images_plots(prompt):
|
| 105 |
-
foldername = "temp"
|
| 106 |
-
# Generate 10 images
|
| 107 |
-
images = [getimgen(prompt) for _ in range(10)]
|
| 108 |
-
|
| 109 |
-
Path(foldername).mkdir(parents=True, exist_ok=True)
|
| 110 |
-
|
| 111 |
-
genders = []
|
| 112 |
-
skintones = []
|
| 113 |
-
|
| 114 |
-
for image, i in zip(images, range(10)):
|
| 115 |
-
prompt_prefix = "photo of a "
|
| 116 |
-
caption = blip_caption_image(image, prefix=prompt_prefix)
|
| 117 |
-
image.save(f"{foldername}/image_{i}.png")
|
| 118 |
-
try:
|
| 119 |
-
skintoneres = stone.process(f"{foldername}/image_{i}.png", return_report_image=False)
|
| 120 |
-
tone = skintoneres['faces'][0]['dominant_colors'][0]['color']
|
| 121 |
-
skintones.append(tone)
|
| 122 |
-
except:
|
| 123 |
-
skintones.append(None)
|
| 124 |
-
|
| 125 |
-
genders.append(genderfromcaption(caption))
|
| 126 |
-
|
| 127 |
-
print(genders, skintones)
|
| 128 |
-
|
| 129 |
-
return images, skintoneplot(skintones), genderplot(genders)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
with gr.Blocks(title = "Skin Tone and Gender bias in SDXL Demo - Inference API") as demo:
|
| 133 |
-
|
| 134 |
-
gr.Markdown("# Skin Tone and Gender bias in SDXL Demo")
|
| 135 |
-
|
| 136 |
-
prompt = gr.Textbox(label="Enter the Prompt")
|
| 137 |
-
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery",
|
| 138 |
-
columns=[5], rows=[2], object_fit="contain", height="auto")
|
| 139 |
-
btn = gr.Button("Generate images", scale=0)
|
| 140 |
-
with gr.Row(equal_height=True):
|
| 141 |
-
skinplot = gr.Plot(label="Skin Tone")
|
| 142 |
-
genplot = gr.Plot(label="Gender")
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
btn.click(generate_images_plots, inputs = prompt, outputs = [gallery, skinplot, genplot])
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|