Update app.py
Browse files
app.py
CHANGED
|
@@ -7,59 +7,62 @@ import os
|
|
| 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
|
| 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
|
| 21 |
ASPECT_RATIOS = {
|
| 22 |
-
"Square (
|
| 23 |
-
"Landscape (
|
| 24 |
-
"Portrait (
|
| 25 |
-
"A4 (
|
| 26 |
}
|
| 27 |
|
| 28 |
-
def generate_image(prompt, aspect_ratio
|
| 29 |
-
"""Generate image with
|
| 30 |
width, height = ASPECT_RATIOS[aspect_ratio]
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
""")
|
| 51 |
-
|
| 52 |
with gr.Row():
|
| 53 |
with gr.Column():
|
| 54 |
prompt = gr.Textbox(
|
| 55 |
label="Prompt",
|
| 56 |
-
placeholder="
|
| 57 |
-
lines=
|
| 58 |
)
|
| 59 |
aspect_ratio = gr.Dropdown(
|
| 60 |
label="Aspect Ratio",
|
| 61 |
choices=list(ASPECT_RATIOS.keys()),
|
| 62 |
-
value="Square (
|
| 63 |
)
|
| 64 |
generate_btn = gr.Button("🎨 Generate Image", variant="primary")
|
| 65 |
|
|
@@ -71,15 +74,6 @@ with gr.Blocks(theme="huggingface", analytics_enabled=False) as demo:
|
|
| 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()
|
|
|
|
| 7 |
MODEL_NAME = "katuni4ka/tiny-random-flex.2-preview"
|
| 8 |
CACHE_DIR = "./model_cache"
|
| 9 |
|
|
|
|
| 10 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 11 |
|
| 12 |
+
# Load model with Flux-specific settings
|
| 13 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 14 |
MODEL_NAME,
|
| 15 |
torch_dtype=torch.float16,
|
| 16 |
+
cache_dir=CACHE_DIR,
|
| 17 |
+
max_seq_length=512 # Critical parameter for Flux models
|
| 18 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
|
| 20 |
+
# Aspect ratios using multiples of 16 for Flux compatibility
|
| 21 |
ASPECT_RATIOS = {
|
| 22 |
+
"Square (512x512)": (512, 512),
|
| 23 |
+
"Landscape (1024x512)": (1024, 512),
|
| 24 |
+
"Portrait (512x1024)": (512, 1024),
|
| 25 |
+
"A4 (768x1024)": (768, 1024)
|
| 26 |
}
|
| 27 |
|
| 28 |
+
def generate_image(prompt, aspect_ratio):
|
| 29 |
+
"""Generate image with Flux-specific parameters"""
|
| 30 |
width, height = ASPECT_RATIOS[aspect_ratio]
|
| 31 |
|
| 32 |
+
try:
|
| 33 |
+
with torch.inference_mode():
|
| 34 |
+
image = pipe(
|
| 35 |
+
prompt=prompt,
|
| 36 |
+
width=width,
|
| 37 |
+
height=height,
|
| 38 |
+
num_inference_steps=20,
|
| 39 |
+
guidance_scale=4.5,
|
| 40 |
+
generator=torch.Generator(device="cuda").manual_seed(42) # Fixed seed
|
| 41 |
+
).images[0]
|
| 42 |
+
return image
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"Error: {str(e)}"
|
| 45 |
|
| 46 |
# UI Configuration
|
| 47 |
with gr.Blocks(theme="huggingface", analytics_enabled=False) as demo:
|
| 48 |
gr.Markdown("""
|
| 49 |
# Tiny Random Flex Text-to-Image Generator
|
| 50 |
+
Experimental Flux-based model with critical fixes for tensor shape errors
|
| 51 |
|
| 52 |
+
🔧 Important: This model requires specific input dimensions and has limited capabilities
|
| 53 |
""")
|
| 54 |
+
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column():
|
| 57 |
prompt = gr.Textbox(
|
| 58 |
label="Prompt",
|
| 59 |
+
placeholder="Try simple prompts like 'a colorful pattern'",
|
| 60 |
+
lines=2
|
| 61 |
)
|
| 62 |
aspect_ratio = gr.Dropdown(
|
| 63 |
label="Aspect Ratio",
|
| 64 |
choices=list(ASPECT_RATIOS.keys()),
|
| 65 |
+
value="Square (512x512)"
|
| 66 |
)
|
| 67 |
generate_btn = gr.Button("🎨 Generate Image", variant="primary")
|
| 68 |
|
|
|
|
| 74 |
inputs=[prompt, aspect_ratio],
|
| 75 |
outputs=output_image
|
| 76 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
demo.launch()
|