text-to-image / app.py
Zaiiida's picture
Update app.py
c66f90b verified
raw
history blame
4.82 kB
import gradio as gr
import numpy as np
import random
import spaces
from diffusers import DiffusionPipeline
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
if torch.cuda.is_available():
torch_dtype = torch.bfloat16
else:
torch_dtype = torch.float32
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
pipe = pipe.to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024
@spaces.GPU(duration=45)
def infer(
prompt,
negative_prompt="",
seed=42,
randomize_seed=False,
width=768,
height=768,
guidance_scale=4.5,
num_inference_steps=20,
progress=gr.Progress(track_tqdm=True),
):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
width=width,
height=height,
generator=generator,
).images[0]
return image, seed
examples = [
"A full-body depiction of a futuristic cyberpunk warrior wearing neon armor with intricate details, holding a sleek glowing katana in a ready stance. The character stands confidently with glowing eyes and a dynamic pose that exudes strength and mystery. Isolated on a clean, transparent, or neutral background to emphasize the character's design and details, with subtle reflections and lighting to enhance the neon glow and textures.",
]
# Define the custom theme with input styles
class CustomTheme(gr.themes.Base):
def __init__(self):
super().__init__()
self.primary_hue = "#5271FF"
self.background_fill_primary = "#17181B"
self.background_fill_secondary = "#17181B"
self.background_fill_tertiary = "#17181B"
self.text_color_primary = "#AEB3B8"
self.text_color_secondary = "#AEB3B8"
self.text_color_tertiary = "#AEB3B8"
self.input_background_fill = "#17181B" # Set input background color
self.input_text_color = "#AEB3B8" # Set input text color
# Custom CSS to hide the footer, set fonts, and adjust font weights
css = """
/* Hide the footer */
footer {
visibility: hidden;
height: 0;
margin: 0;
padding: 0;
overflow: hidden;
}
/* Import Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Montserrat:wght@500;700&display=swap');
/* Apply fonts to different elements */
body, input, button, textarea, select, .gr-button {
font-family: 'Roboto', sans-serif;
}
/* Make button text normal weight */
.generate-button, .generate-button .gr-button {
font-weight: normal !important;
}
/* Ensure headings use Montserrat */
h1, h2, h3, h4, h5, h6 {
font-family: 'Montserrat', sans-serif;
font-weight: 700;
}
/* Additional styling for sliders and checkboxes if needed */
input[type="range"]::-webkit-slider-thumb {
background: #5271FF;
}
input[type="range"]::-moz-range-thumb {
background: #5271FF;
}
input[type="range"]::-ms-thumb {
background: #5271FF;
}
input[type="checkbox"]:checked {
background-color: #5271FF;
}
/* Make Prompt text bold */
.prompt-text {
font-weight: bold;
}
"""
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
with gr.Column(elem_id="col-container"):
# Make "Prompt" bold using Markdown syntax and assign a class
gr.Markdown("**Prompt**", elem_classes="prompt-text")
with gr.Row():
prompt = gr.Text(
label="Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
container=False,
)
run_button = gr.Button(
"Generate",
scale=0,
variant="primary",
elem_classes="generate-button"
)
result = gr.Image(label="Result", show_label=False)
# Removed Advanced Settings block
gr.Examples(
examples=examples,
inputs=[prompt],
outputs=[result],
fn=infer,
cache_examples=True,
cache_mode="lazy"
)
# Use click method for the button and submit for the text field
run_button.click(
fn=infer,
inputs=[prompt], # Only the prompt as input
outputs=[result], # Only result as output
)
prompt.submit(
fn=infer,
inputs=[prompt], # Only the prompt as input
outputs=[result], # Only result as output
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_api=False
)