Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import AutoPipelineForText2Image
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Model configuration
|
7 |
+
MODEL_NAME = "katuni4ka/tiny-random-flex.2-preview"
|
8 |
+
CACHE_DIR = "./model_cache"
|
9 |
+
|
10 |
+
# Create cache directory
|
11 |
+
os.makedirs(CACHE_DIR, exist_ok=True)
|
12 |
+
|
13 |
+
# Load model with optimized settings
|
14 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
15 |
+
MODEL_NAME,
|
16 |
+
torch_dtype=torch.float16,
|
17 |
+
cache_dir=CACHE_DIR
|
18 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
+
|
20 |
+
# Aspect ratio presets
|
21 |
+
ASPECT_RATIOS = {
|
22 |
+
"Square (1:1)": (512, 512),
|
23 |
+
"Landscape (16:9)": (1024, 576),
|
24 |
+
"Portrait (9:16)": (576, 1024),
|
25 |
+
"A4 (3:4)": (864, 1152)
|
26 |
+
}
|
27 |
+
|
28 |
+
def generate_image(prompt, aspect_ratio, num_inference_steps=25, guidance_scale=4.5):
|
29 |
+
"""Generate image with optimized inference settings"""
|
30 |
+
width, height = ASPECT_RATIOS[aspect_ratio]
|
31 |
+
|
32 |
+
with torch.inference_mode():
|
33 |
+
image = pipe(
|
34 |
+
prompt=prompt,
|
35 |
+
width=width,
|
36 |
+
height=height,
|
37 |
+
num_inference_steps=num_inference_steps,
|
38 |
+
guidance_scale=guidance_scale
|
39 |
+
).images[0]
|
40 |
+
|
41 |
+
return image
|
42 |
+
|
43 |
+
# UI Configuration
|
44 |
+
with gr.Blocks(theme="huggingface", analytics_enabled=False) as demo:
|
45 |
+
gr.Markdown("""
|
46 |
+
# Tiny Random Flex Text-to-Image Generator
|
47 |
+
Create images from text prompts using the `katuni4ka/tiny-random-flex.2-preview` model
|
48 |
+
|
49 |
+
💡 Tip: Try descriptive prompts like "A futuristic cityscape at sunset" or "Abstract watercolor patterns"
|
50 |
+
""")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
with gr.Column():
|
54 |
+
prompt = gr.Textbox(
|
55 |
+
label="Prompt",
|
56 |
+
placeholder="Describe your image...",
|
57 |
+
lines=3
|
58 |
+
)
|
59 |
+
aspect_ratio = gr.Dropdown(
|
60 |
+
label="Aspect Ratio",
|
61 |
+
choices=list(ASPECT_RATIOS.keys()),
|
62 |
+
value="Square (1:1)"
|
63 |
+
)
|
64 |
+
generate_btn = gr.Button("🎨 Generate Image", variant="primary")
|
65 |
+
|
66 |
+
with gr.Column():
|
67 |
+
output_image = gr.Image(label="Generated Image", interactive=False)
|
68 |
+
|
69 |
+
generate_btn.click(
|
70 |
+
fn=generate_image,
|
71 |
+
inputs=[prompt, aspect_ratio],
|
72 |
+
outputs=output_image
|
73 |
+
)
|
74 |
+
|
75 |
+
gr.Examples(
|
76 |
+
examples=[
|
77 |
+
["A vibrant neon cityscape at night", "Landscape (16:9)"],
|
78 |
+
["Abstract geometric patterns in pastel colors", "Square (1:1)"],
|
79 |
+
["Mystical forest with glowing plants", "Portrait (9:16)"]
|
80 |
+
],
|
81 |
+
inputs=[prompt, aspect_ratio]
|
82 |
+
)
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
demo.launch()
|