Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- app.py +147 -29
- custom_pipeline.py +8 -0
- requirements.txt +4 -1
app.py
CHANGED
|
@@ -10,15 +10,20 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as RL
|
|
| 10 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 11 |
from reportlab.lib.units import inch
|
| 12 |
from reportlab.lib import colors
|
| 13 |
-
from diffusers import
|
|
|
|
|
|
|
| 14 |
from PIL import Image
|
| 15 |
import io
|
| 16 |
import tempfile
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Initialize the text generation pipeline and MCP client
|
| 19 |
generator = None
|
| 20 |
mcp_client = None
|
| 21 |
image_generator = None
|
|
|
|
| 22 |
|
| 23 |
# MCP client configuration
|
| 24 |
MCP_ENDPOINTS = {
|
|
@@ -83,30 +88,128 @@ def initialize_mcp_client():
|
|
| 83 |
return f"MCP client initialization failed: {str(e)}"
|
| 84 |
|
| 85 |
def initialize_image_generator():
|
| 86 |
-
"""Initialize FLUX Schnell
|
| 87 |
global image_generator
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
# Fallback to
|
| 100 |
try:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
torch_dtype=torch.float32,
|
| 105 |
-
safety_checker=None,
|
| 106 |
-
requires_safety_checker=False
|
| 107 |
)
|
| 108 |
-
image_generator
|
| 109 |
-
return "
|
| 110 |
except Exception as e2:
|
| 111 |
return f"Image generation initialization failed: {str(e)}, Fallback: {str(e2)}"
|
| 112 |
|
|
@@ -263,30 +366,45 @@ def import_date():
|
|
| 263 |
from datetime import datetime
|
| 264 |
return datetime.now().strftime("%B %d, %Y")
|
| 265 |
|
|
|
|
| 266 |
def generate_header_image(topic, tone):
|
| 267 |
-
"""Generate
|
| 268 |
global image_generator
|
| 269 |
|
| 270 |
if image_generator is None:
|
| 271 |
return None
|
| 272 |
|
| 273 |
try:
|
| 274 |
-
# Create
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
-
# Generate
|
| 278 |
image = image_generator(
|
| 279 |
prompt=image_prompt,
|
| 280 |
-
num_inference_steps=
|
| 281 |
-
|
| 282 |
-
height=
|
| 283 |
-
width=
|
| 284 |
).images[0]
|
| 285 |
|
| 286 |
return image
|
| 287 |
|
| 288 |
except Exception as e:
|
| 289 |
-
print(f"
|
| 290 |
return None
|
| 291 |
|
| 292 |
def export_to_pdf(content, topic, header_image=None):
|
|
@@ -386,7 +504,7 @@ def generate_complete_onepager(topic, target_audience, key_points, tone, length,
|
|
| 386 |
def create_interface():
|
| 387 |
with gr.Blocks(title="One-Pager Generator", theme=gr.themes.Soft()) as demo:
|
| 388 |
gr.Markdown("# 📄 AI One-Pager Generator")
|
| 389 |
-
gr.Markdown("Generate professional business documents with FLUX Schnell
|
| 390 |
|
| 391 |
with gr.Row():
|
| 392 |
with gr.Column(scale=1):
|
|
|
|
| 10 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 11 |
from reportlab.lib.units import inch
|
| 12 |
from reportlab.lib import colors
|
| 13 |
+
from diffusers import DiffusionPipeline, AutoencoderTiny, FluxImg2ImgPipeline
|
| 14 |
+
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 15 |
+
from custom_pipeline import FluxWithCFGPipeline
|
| 16 |
from PIL import Image
|
| 17 |
import io
|
| 18 |
import tempfile
|
| 19 |
+
import numpy as np
|
| 20 |
+
import spaces
|
| 21 |
|
| 22 |
# Initialize the text generation pipeline and MCP client
|
| 23 |
generator = None
|
| 24 |
mcp_client = None
|
| 25 |
image_generator = None
|
| 26 |
+
img2img_generator = None
|
| 27 |
|
| 28 |
# MCP client configuration
|
| 29 |
MCP_ENDPOINTS = {
|
|
|
|
| 88 |
return f"MCP client initialization failed: {str(e)}"
|
| 89 |
|
| 90 |
def initialize_image_generator():
|
| 91 |
+
"""Initialize optimized FLUX Schnell with LoRAs and TinyVAE"""
|
| 92 |
global image_generator
|
| 93 |
try:
|
| 94 |
+
print('Initializing optimized FLUX pipeline...')
|
| 95 |
+
|
| 96 |
+
# Enable optimizations
|
| 97 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 98 |
+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 99 |
+
|
| 100 |
+
# Load base FLUX pipeline with optimizations
|
| 101 |
+
image_generator = FluxWithCFGPipeline.from_pretrained(
|
| 102 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 103 |
+
torch_dtype=dtype
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Load TinyAutoencoder for faster VAE (major speed boost)
|
| 107 |
+
print('Loading optimized TinyVAE...')
|
| 108 |
+
image_generator.vae = AutoencoderTiny.from_pretrained(
|
| 109 |
+
"madebyollin/taef1",
|
| 110 |
+
torch_dtype=dtype
|
| 111 |
)
|
| 112 |
+
|
| 113 |
+
if torch.cuda.is_available():
|
| 114 |
+
image_generator.to("cuda")
|
| 115 |
+
|
| 116 |
+
# MISSING: Set attention processor for memory efficiency
|
| 117 |
+
image_generator.transformer.set_attn_processor(AttnProcessor2_0())
|
| 118 |
+
|
| 119 |
+
# MISSING: Enable memory efficient attention
|
| 120 |
+
try:
|
| 121 |
+
image_generator.enable_model_cpu_offload()
|
| 122 |
+
print('CPU offload enabled for memory efficiency.')
|
| 123 |
+
except:
|
| 124 |
+
pass
|
| 125 |
+
|
| 126 |
+
# MISSING: Enable xformers if available
|
| 127 |
+
try:
|
| 128 |
+
image_generator.enable_xformers_memory_efficient_attention()
|
| 129 |
+
print('XFormers memory efficient attention enabled.')
|
| 130 |
+
except:
|
| 131 |
+
pass
|
| 132 |
+
|
| 133 |
+
print('Pipeline loaded to CUDA with optimizations.')
|
| 134 |
+
|
| 135 |
+
# Load business-focused LoRAs
|
| 136 |
+
print('Loading business-focused LoRAs...')
|
| 137 |
+
try:
|
| 138 |
+
image_generator.load_lora_weights(
|
| 139 |
+
'Shakker-Labs/FLUX.1-dev-LoRA-add-details',
|
| 140 |
+
weight_name='FLUX-dev-lora-add_details.safetensors',
|
| 141 |
+
adapter_name='detail'
|
| 142 |
+
)
|
| 143 |
+
image_generator.load_lora_weights(
|
| 144 |
+
'its-magick/merlin-test-loras',
|
| 145 |
+
weight_name='Canopus-LoRA-Flux-UltraRealism.safetensors',
|
| 146 |
+
adapter_name='ultrarealism'
|
| 147 |
+
)
|
| 148 |
+
image_generator.load_lora_weights(
|
| 149 |
+
'its-magick/merlin-logos',
|
| 150 |
+
weight_name='merlin-logos.safetensors',
|
| 151 |
+
adapter_name='logos'
|
| 152 |
+
)
|
| 153 |
+
image_generator.load_lora_weights(
|
| 154 |
+
'its-magick/merlin-infographic',
|
| 155 |
+
weight_name='lora.safetensors',
|
| 156 |
+
adapter_name='infographic'
|
| 157 |
+
)
|
| 158 |
+
image_generator.load_lora_weights(
|
| 159 |
+
'its-magick/merlin-office',
|
| 160 |
+
weight_name='lora.safetensors',
|
| 161 |
+
adapter_name='office'
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# MISSING: Set adapters first (like your code)
|
| 165 |
+
print('Setting adapters...')
|
| 166 |
+
image_generator.set_adapters(["detail"], adapter_weights=[0.6])
|
| 167 |
+
image_generator.set_adapters(["ultrarealism"], adapter_weights=[0.6])
|
| 168 |
+
image_generator.set_adapters(["logos"], adapter_weights=[0.4])
|
| 169 |
+
image_generator.set_adapters(["infographic"], adapter_weights=[0.6])
|
| 170 |
+
image_generator.set_adapters(["office"], adapter_weights=[0.6])
|
| 171 |
+
|
| 172 |
+
# Then fuse them for speed (like your code)
|
| 173 |
+
print('Fusing LoRAs for business optimization...')
|
| 174 |
+
image_generator.fuse_lora(adapter_name=["detail"], lora_scale=0.6)
|
| 175 |
+
image_generator.fuse_lora(adapter_name=["ultrarealism"], lora_scale=0.6)
|
| 176 |
+
image_generator.fuse_lora(adapter_name=["logos"], lora_scale=0.4)
|
| 177 |
+
image_generator.fuse_lora(adapter_name=["infographic"], lora_scale=0.6)
|
| 178 |
+
image_generator.fuse_lora(adapter_name=["office"], lora_scale=0.6)
|
| 179 |
+
|
| 180 |
+
print('Business LoRAs fused successfully!')
|
| 181 |
+
|
| 182 |
+
# MISSING: Initialize img2img pipeline (like your code)
|
| 183 |
+
print('Initializing img2img pipeline...')
|
| 184 |
+
global img2img_generator
|
| 185 |
+
img2img_generator = FluxImg2ImgPipeline(
|
| 186 |
+
transformer=image_generator.transformer,
|
| 187 |
+
scheduler=image_generator.scheduler,
|
| 188 |
+
vae=image_generator.vae,
|
| 189 |
+
text_encoder=image_generator.text_encoder,
|
| 190 |
+
text_encoder_2=image_generator.text_encoder_2,
|
| 191 |
+
tokenizer=image_generator.tokenizer,
|
| 192 |
+
tokenizer_2=image_generator.tokenizer_2,
|
| 193 |
+
).to("cuda")
|
| 194 |
+
print('Img2Img pipeline initialized.')
|
| 195 |
+
|
| 196 |
+
except Exception as lora_e:
|
| 197 |
+
print(f"LoRA loading failed, continuing with base model: {str(lora_e)}")
|
| 198 |
+
else:
|
| 199 |
+
image_generator.to("cpu")
|
| 200 |
+
print('Pipeline loaded to CPU.')
|
| 201 |
+
|
| 202 |
+
return "Optimized FLUX Schnell with business LoRAs loaded successfully!"
|
| 203 |
+
|
| 204 |
except Exception as e:
|
| 205 |
+
# Fallback to basic FLUX
|
| 206 |
try:
|
| 207 |
+
image_generator = DiffusionPipeline.from_pretrained(
|
| 208 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 209 |
+
torch_dtype=torch.float32
|
|
|
|
|
|
|
|
|
|
| 210 |
)
|
| 211 |
+
image_generator.to("cpu")
|
| 212 |
+
return "Basic FLUX Schnell loaded as fallback!"
|
| 213 |
except Exception as e2:
|
| 214 |
return f"Image generation initialization failed: {str(e)}, Fallback: {str(e2)}"
|
| 215 |
|
|
|
|
| 366 |
from datetime import datetime
|
| 367 |
return datetime.now().strftime("%B %d, %Y")
|
| 368 |
|
| 369 |
+
@spaces.GPU(duration=15)
|
| 370 |
def generate_header_image(topic, tone):
|
| 371 |
+
"""Generate optimized header image for business one-pager"""
|
| 372 |
global image_generator
|
| 373 |
|
| 374 |
if image_generator is None:
|
| 375 |
return None
|
| 376 |
|
| 377 |
try:
|
| 378 |
+
# Create business-focused prompt with LoRA triggers
|
| 379 |
+
business_style = {
|
| 380 |
+
"Professional": "corporate office style, business presentation",
|
| 381 |
+
"Casual": "modern startup office, friendly business environment",
|
| 382 |
+
"Academic": "research presentation, educational infographic",
|
| 383 |
+
"Persuasive": "marketing presentation, compelling business visual",
|
| 384 |
+
"Informative": "clean infographic style, data visualization"
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
style_desc = business_style.get(tone, "professional business")
|
| 388 |
+
|
| 389 |
+
# Enhanced prompt for business LoRAs
|
| 390 |
+
image_prompt = f"Professional business infographic header for {topic}, {style_desc}, clean corporate design, business graphics, office environment, high quality, no text, ultra realistic"
|
| 391 |
+
|
| 392 |
+
# Use optimized generation settings
|
| 393 |
+
generator = torch.Generator().manual_seed(42) # Consistent seed for business docs
|
| 394 |
|
| 395 |
+
# Generate with optimized settings
|
| 396 |
image = image_generator(
|
| 397 |
prompt=image_prompt,
|
| 398 |
+
num_inference_steps=1, # Ultra-fast with Schnell + optimizations
|
| 399 |
+
generator=generator,
|
| 400 |
+
height=384, # Better aspect ratio for headers
|
| 401 |
+
width=768,
|
| 402 |
).images[0]
|
| 403 |
|
| 404 |
return image
|
| 405 |
|
| 406 |
except Exception as e:
|
| 407 |
+
print(f"Optimized image generation failed: {str(e)}")
|
| 408 |
return None
|
| 409 |
|
| 410 |
def export_to_pdf(content, topic, header_image=None):
|
|
|
|
| 504 |
def create_interface():
|
| 505 |
with gr.Blocks(title="One-Pager Generator", theme=gr.themes.Soft()) as demo:
|
| 506 |
gr.Markdown("# 📄 AI One-Pager Generator")
|
| 507 |
+
gr.Markdown("Generate professional business documents with optimized FLUX Schnell (TinyVAE + Business LoRAs) and PDF export! Ultra-fast, high-quality images.")
|
| 508 |
|
| 509 |
with gr.Row():
|
| 510 |
with gr.Column(scale=1):
|
custom_pipeline.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Custom FLUX pipeline with CFG support
|
| 2 |
+
# This is a simplified version - for full implementation, see the original repo
|
| 3 |
+
|
| 4 |
+
from diffusers import FluxPipeline
|
| 5 |
+
|
| 6 |
+
class FluxWithCFGPipeline(FluxPipeline):
|
| 7 |
+
"""FLUX pipeline with CFG support for better control"""
|
| 8 |
+
pass # Use base FluxPipeline for now - can be extended later
|
requirements.txt
CHANGED
|
@@ -10,4 +10,7 @@ gradio_client
|
|
| 10 |
reportlab
|
| 11 |
fpdf2
|
| 12 |
diffusers
|
| 13 |
-
pillow
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
reportlab
|
| 11 |
fpdf2
|
| 12 |
diffusers
|
| 13 |
+
pillow
|
| 14 |
+
spaces
|
| 15 |
+
torchvision
|
| 16 |
+
xformers
|