T2IX2 / app.py
DigiP-AI's picture
Update app.py
0d88f6c verified
import gradio as gr
import threading
import os
import torch
from PIL import Image
from fastapi import FastAPI
app = FastAPI()
#----------Start of theme----------
theme = gr.themes.Ocean(
primary_hue="zinc",
secondary_hue="slate",
neutral_hue="neutral",
font=[gr.themes.GoogleFont('Kavivanar'), gr.themes.GoogleFont('Kavivanar'), 'system-ui', 'sans-serif'],
font_mono=[gr.themes.GoogleFont('Source Code Pro'), gr.themes.GoogleFont('Inconsolata'), gr.themes.GoogleFont('Inconsolata'), 'monospace'],
).set(
#Body Settings
body_background_fill='linear-gradient(10deg, *primary_200, *secondary_50)',
body_text_color='secondary_600',
body_text_color_subdued='*primary_500',
body_text_weight='500',
#Background Settings
background_fill_primary='*primary_100',
background_fill_secondary='*secondary_200',
color_accent='*primary_300',
#Border Settings
border_color_accent_subdued='*primary_400',
border_color_primary='*primary_400',
panel_border_color='*primary_400',
input_border_color='*primary_400',
table_border_color='*primary_400',
#Block Settings
block_radius='*radius_md',
block_background_fill='*primary_200',
block_border_color='*primary_500',
block_border_width='*panel_border_width',
block_info_text_color='*primary_700',
block_info_text_size='*text_md',
container_radius='*radius_xl',
panel_background_fill='*primary_200',
accordion_text_color='*primary_600',
checkbox_border_radius='*radius_xl',
slider_color='*primary_500',
table_text_color='*primary_600',
input_background_fill='*primary_50',
input_background_fill_focus='*primary_100',
#Button Settings
button_border_width='1px',
button_transform_hover='scale(1.01)',
button_transition='all 0.1s ease-in-out',
button_transform_active='Scale(0.9)',
button_large_radius='*radius_xl',
button_small_radius='*radius_xl',
button_primary_border_color='*primary_500',
button_secondary_border_color='*primary_400',
button_primary_background_fill_hover='linear-gradient(90deg, *primary_400, *secondary_200, *primary_400)',
button_primary_background_fill='linear-gradient(90deg,*secondary_300 , *primary_500, *secondary_300)',
button_primary_text_color='*primary_100',
button_primary_text_color_hover='*primary_700',
button_cancel_background_fill='*primary_500',
button_cancel_background_fill_hover='*primary_400'
)
#----------End of theme----------
os.environ["OMP_NUM_THREADS"] = str(os.cpu_count())
torch.set_num_threads(os.cpu_count())
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
model2 = gr.load("models/Purz/face-projection")
stop_event = threading.Event()
def generate_images(text, selected_model):
stop_event.clear()
if selected_model == "Model 1 (Turbo Realism)":
model = model1
elif selected_model == "Model 2 (Face Projection)":
model = model2
else:
return ["Invalid model selection."] * 3
results = []
for i in range(3):
if stop_event.is_set():
return ["Image generation stopped by user."] * 3
modified_text = f"{text} variation {i+1}"
result = model(modified_text)
results.append(result)
return results
def stop_generation():
"""Stops the ongoing image generation by setting the stop_event flag."""
stop_event.set()
return ["Generation stopped."] * 3
with gr.Blocks() as interface:#...
gr.Markdown(
"### ⚠ Sorry for the inconvenience. The Space is currently running on the CPU, which might affect performance. We appreciate your understanding."
)
text_prompt = gr.Textbox(label="Image Prompt", placeholder="Enter a prompt here", lines=2, show_copy_button = True, elem_id="prompt-text-input")
model_selector = gr.Radio(
["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
label="Select Model",
value="Model 1 (Turbo Realism)"
)
with gr.Row():
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="((visible hand:1.3), (ugly:1.3), (duplicate:1.2), (morbid:1.1), (mutilated:1.1), out of frame, bad face, extra fingers, mutated hands, (poorly drawn hands:1.1), (poorly drawn face:1.3), (mutation:1.3), (deformed:1.3), blurry, (bad anatomy:1.1), (bad proportions:1.2), (extra limbs:1.1), cloned face, (disfigured:1.2), gross proportions, malformed limbs, (missing arms:1.1), (missing legs:1.1), (extra arms:1.2), (extra legs:1.2), fused fingers, too many fingers, (long neck:1.2), sketched by bad-artist, (bad-image-v2-39000:1.3)", lines=5, elem_id="negative-prompt-text-input")
width = gr.Slider(label="ImageWidth", value=896, minimum=64, maximum=1216, step=32)
height = gr.Slider(label="Image Height", value=1152, minimum=64, maximum=1216, step=32)
with gr.Row():
generate_button = gr.Button("Generate 3 Images 🎨")
stop_button = gr.Button("Stop Image Generation")
with gr.Row():
output1 = gr.Image(type="pil", label="Generated Image 1", format="png")
output2 = gr.Image(type="pil", label="Generated Image 2", format="png")
output3 = gr.Image(type="pil", label="Generated Image 3", format="png")
generate_button.click(generate_images, inputs=[text_prompt, negative_prompt, width, height, model_selector], outputs=[output1, output2, output3])
stop_button.click(stop_generation, inputs=[], outputs=[output1, output2, output3])
interface.launch()