Spaces:
Runtime error
Runtime error
File size: 7,212 Bytes
eaa9650 ba74abd eaa9650 37c41a7 1d3f5e1 37c41a7 ba74abd eaa9650 ba74abd eaa9650 ba74abd eaa9650 ba74abd 1d3f5e1 d8389e8 ba74abd 3f6c118 ba74abd b46eb2a 3f6c118 d8389e8 3f6c118 ba74abd 3f6c118 ba74abd 3f6c118 d8389e8 3f6c118 37c41a7 eaa9650 b15e4f3 0c86bfd cdc2f90 0579504 cdc2f90 1d3f5e1 ba74abd 1d3f5e1 cdc2f90 0c86bfd 4a778bd 0c86bfd 15cf57c 0c86bfd 1d3f5e1 0c86bfd 13d267c 0c86bfd cdc2f90 0c86bfd 0579504 f112aea 0c86bfd cdc2f90 1d3f5e1 cdc2f90 2e14e78 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | import gradio as gr
import os
import requests
import time
import re
from PIL import Image
from gradio_client import Client
# Ensure HF_TOKEN is set
hf_token = os.environ.get("HF_TOKEN")
if not hf_token:
raise ValueError("HF_TOKEN environment variable is not set. Please set it to your Hugging Face token.")
# Verify the repository access
repo_id = "fffiloni/safety-checker-bot"
try:
client = Client(repo_id, hf_token=hf_token)
except Exception as e:
raise ValueError(f"Failed to access repository {repo_id}: {e}")
def safety_check(user_prompt):
try:
response = client.predict(
"consistent-character space", # str source space
user_prompt, # str in 'User sent this' Textbox component
api_name="/infer"
)
return response
except Exception as e:
raise ValueError(f"Safety check failed: {e}")
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
if filename.endswith((".png", ".jpg", ".jpeg")):
images.append(os.path.join(folder, filename))
return images
def generate_variation(original_image_path):
original_image = Image.open(original_image_path)
variation = original_image.transpose(Image.FLIP_LEFT_RIGHT) # Example: Flip the image
output_path = os.path.join("output", os.path.basename(original_image_path))
os.makedirs(os.path.dirname(output_path), exist_ok=True)
variation.save(output_path)
return output_path
def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
prompt = args[0]
if not prompt:
raise gr.Error("You forgot to provide a prompt.")
try:
is_safe = safety_check(prompt)
if "Yes" in is_safe:
raise gr.Error("Do not ask for such things.")
else:
headers = {'Content-Type': 'application/json'}
payload = {"input": {}}
base_url = "http://0.0.0.0:7860"
for i, key in enumerate(names):
value = args[i]
if value and os.path.exists(str(value)):
value = f"{base_url}/file=" + value
if value is not None and value != "":
payload["input"][key] = value
response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
if response.status_code == 201:
follow_up_url = response.json()["urls"]["get"]
response = requests.get(follow_up_url, headers=headers)
while response.json()["status"] != "succeeded":
if response.json()["status"] == "failed":
raise gr.Error("The submission failed!")
response = requests.get(follow_up_url, headers=headers)
time.sleep(1)
if response.status_code == 200:
json_response = response.json()
if(outputs[0].get_config()["name"] == "json"):
return json_response["output"]
predict_outputs = parse_outputs(json_response["output"])
processed_outputs = process_outputs(predict_outputs)
return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
else:
if(response.status_code == 409):
raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
raise gr.Error(f"The submission failed! Error: {response.status_code}")
except Exception as e:
raise gr.Error(f"An error occurred: {e}")
title = "Demo for consistent-character cog image by fofr"
description = "Create images of a given character in different poses • running cog image by fofr"
css="""
#col-container{
margin: 0 auto;
max-width: 1400px;
text-align: left;
}
"""
images = load_images_from_folder("uploaded_images")
with gr.Blocks(css=css) as app:
with gr.Column(elem_id="col-container"):
gr.HTML(f"""
<h2 style="text-align: center;">Consistent Character Workflow</h2>
<p style="text-align: center;">{description}</p>
""")
with gr.Row():
with gr.Column(scale=1):
prompt = gr.Textbox(
label="Prompt", info='''Describe the subject. Include clothes and hairstyle for more consistency.''',
value="a person, darkblue suit, black tie, white pocket"
)
subject = gr.Image(
label="Subject", type="filepath"
)
image_selection = gr.Dropdown(
label="Select an image from directory", choices=images, type="index"
)
submit_btn = gr.Button("Submit")
with gr.Accordion(label="Advanced Settings", open=False):
negative_prompt = gr.Textbox(
label="Negative Prompt", info='''Things you do not want to see in your image''',
value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry"
)
with gr.Row():
number_of_outputs = gr.Slider(
label="Number Of Outputs", info='''The number of images to generate.''', value=2,
minimum=1, maximum=4, step=1,
)
number_of_images_per_pose = gr.Slider(
label="Number Of Images Per Pose", info='''The number of images to generate for each pose.''', value=1,
minimum=1, maximum=4, step=1,
)
with gr.Row():
randomise_poses = gr.Checkbox(
label="Randomise Poses", info='''Randomise the poses used.''', value=True
)
output_format = gr.Dropdown(
choices=['webp', 'jpg', 'png'], label="output_format", info='''Format of the output images''', value="webp"
)
with gr.Row():
output_quality = gr.Number(
label="Output Quality", info='''Quality of the output images, from 0 to 100. 100 is best quality, 0 is lowest quality.''', value=80
)
seed = gr.Number(
label="Seed", info='''Set a seed for reproducibility. Random by default.''', value=None
)
with gr.Column(scale=1.5):
consistent_results = gr.Gallery(label="Consistent Results")
inputs = [prompt, negative_prompt, subject, number_of_outputs, number_of_images_per_pose, randomise_poses, output_format, output_quality, seed]
outputs = [consistent_results]
submit_btn.click(
fn=predict,
inputs=inputs,
outputs=outputs,
show_api=False
)
app.queue(max_size=12, api_open=False).launch(share=False, show_api=False, show_error=True)
|