text-to-image / app.py
Zaiiida's picture
Update app.py
087fdc1 verified
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-medium-diffusers"
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_IMAGE_SIZE = 1024
examples = [
"A stylized 3D Kazakh-themed panda dressed in a colorful chapan with simple gold and turquoise patterns. The panda wears a small tyubeteika hat and holds a dombra in one paw, sitting in a cute and relaxed pose. The design is minimalistic, with smooth textures and a focus on soft shapes and earthy tones.",
"A stylized 3D anthropomorphic bear character with a muscular build, dressed in simple yet rugged armor made of leather and metal. The character wields a large axe with intricate carvings on the blade. The colors are muted with earthy greens and browns, complemented by silver armor accents. Shadows emphasize the character's strength and bold pose, perfect for a medieval fantasy setting.",
"A detailed 3D model of a traditional Kazakh yurt with intricate gold and turquoise ornamentation along the fabric and wooden framework. The yurt is adorned with a fur-lined entrance and traditional steppe patterns on the walls and roof. The structure is surrounded by simple props like a traditional wooden saddle and a dombra, giving a sense of nomadic life.",
]
@spaces.GPU(duration=30)
def infer(
prompt,
width=1024,
height=1024,
num_inference_steps=20,
guidance_scale=7.5,
):
# Генерация изображения
image = pipe(
prompt=prompt,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
width=width,
height=height,
).images[0]
return image
class CustomTheme(gr.themes.Base):
def __init__(self):
super().__init__()
self.primary_hue = "#191a1e"
self.background_fill_primary = "#191a1e"
self.background_fill_secondary = "#191a1e"
self.background_fill_tertiary = "#191a1e"
self.text_color_primary = "#FFFFFF"
self.text_color_secondary = "#FFFFFF"
self.text_color_tertiary = "#FFFFFF"
self.input_background_fill = "#191a1e"
self.input_text_color = "#FFFFFF"
css = """
/* Скрываем нижний колонтитул */
footer {
visibility: hidden;
height: 0;
margin: 0;
padding: 0;
overflow: hidden;
}
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap');
/* Применяем шрифты */
body, input, button, textarea, select, .gr-button {
font-family: 'Poppins', sans-serif;
background-color: #191a1e !important;
color: #FFFFFF;
}
/* Настройки заголовков */
h1, h2, h3, h4, h5, h6 {
font-family: 'Poppins', sans-serif;
font-weight: 700;
color: #FFFFFF;
}
/* Стиль для текстовых полей и кнопок */
input[type="text"], textarea {
background-color: #191a1e !important;
color: #FFFFFF;
border: 1px solid #FFFFFF;
}
/* Цвет кнопки Generate */
.generate-button {
background-color: #5271FF !important;
color: #FFFFFF !important;
border: none;
font-weight: bold;
}
.generate-button:hover {
background-color: #405BBF !important; /* Цвет при наведении */
}
/* Выделяем текст для Prompt */
.prompt-text {
font-weight: bold;
color: #FFFFFF;
}
"""
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
with gr.Column(elem_id="col-container"):
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)
gr.Examples(
examples=examples,
inputs=[prompt],
outputs=[result],
fn=infer,
cache_examples=True,
cache_mode="lazy",
)
run_button.click(
fn=infer,
inputs=[prompt],
outputs=[result],
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_api=False,
)