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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
from reportlab.lib.pagesizes import letter
|
| 7 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 8 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 9 |
+
from reportlab.lib.units import inch
|
| 10 |
+
from reportlab.lib import colors
|
| 11 |
+
import io
|
| 12 |
+
import tempfile
|
| 13 |
+
|
| 14 |
+
# Initialize the text generation pipeline and MCP client
|
| 15 |
+
generator = None
|
| 16 |
+
mcp_client = None
|
| 17 |
+
image_generator = None
|
| 18 |
+
img2img_generator = None
|
| 19 |
+
|
| 20 |
+
# MCP client configuration
|
| 21 |
+
MCP_ENDPOINTS = {
|
| 22 |
+
"claude": "https://api.anthropic.com/v1/mcp",
|
| 23 |
+
"openai": "https://api.openai.com/v1/mcp",
|
| 24 |
+
"huggingface": None # Will use local model
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def initialize_model():
|
| 28 |
+
global generator
|
| 29 |
+
try:
|
| 30 |
+
# Use HF Inference API with modern models (no local downloads)
|
| 31 |
+
generator = InferenceClient(model="microsoft/Phi-3-mini-4k-instruct")
|
| 32 |
+
return "Phi-3-mini loaded via Inference API!"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
try:
|
| 35 |
+
# Fallback to Qwen via API
|
| 36 |
+
generator = InferenceClient(model="Qwen/Qwen2.5-1.5B-Instruct")
|
| 37 |
+
return "Qwen 2.5-1.5B loaded via Inference API!"
|
| 38 |
+
except Exception as e2:
|
| 39 |
+
# Final fallback to any available model
|
| 40 |
+
generator = InferenceClient() # Use default model
|
| 41 |
+
return f"Default model loaded via Inference API! Primary error: {str(e)}"
|
| 42 |
+
|
| 43 |
+
def initialize_mcp_client():
|
| 44 |
+
"""Initialize MCP client for external AI services"""
|
| 45 |
+
global mcp_client
|
| 46 |
+
try:
|
| 47 |
+
# Simplified MCP client (no external dependencies)
|
| 48 |
+
mcp_client = {"status": "ready", "type": "local_only"}
|
| 49 |
+
return "MCP client initialized successfully!"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"MCP client initialization failed: {str(e)}"
|
| 52 |
+
|
| 53 |
+
def initialize_image_generator():
|
| 54 |
+
"""Initialize basic image generator (FLUX disabled for dependency issues)"""
|
| 55 |
+
global image_generator
|
| 56 |
+
try:
|
| 57 |
+
# For now, disable image generation to avoid dependency issues
|
| 58 |
+
print('Image generation temporarily disabled due to dependency conflicts...')
|
| 59 |
+
image_generator = None
|
| 60 |
+
return "Image generation disabled - focusing on text generation and PDF export"
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"Image generation initialization failed: {str(e)}"
|
| 64 |
+
|
| 65 |
+
def generate_with_mcp(topic, target_audience, key_points, tone, length, model_choice="local"):
|
| 66 |
+
"""Generate one-pager using MCP client or local model"""
|
| 67 |
+
|
| 68 |
+
if model_choice == "local" or mcp_client is None:
|
| 69 |
+
return generate_onepager(topic, target_audience, key_points, tone, length)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Example of using MCP client to connect to other services
|
| 73 |
+
# This would be where you'd implement actual MCP protocol calls
|
| 74 |
+
prompt = f"""Create a compelling one-page business document about "{topic}" for {target_audience}.
|
| 75 |
+
|
| 76 |
+
Style: {tone.lower()} but action-oriented
|
| 77 |
+
Key points: {key_points}
|
| 78 |
+
Length: {length}
|
| 79 |
+
|
| 80 |
+
Format as a TRUE one-pager with visual elements, benefits, and clear next steps."""
|
| 81 |
+
|
| 82 |
+
# For demonstration, fall back to local generation
|
| 83 |
+
# In practice, this would make MCP calls to external services
|
| 84 |
+
return generate_onepager(topic, target_audience, key_points, tone, length)
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
# Fallback to local generation
|
| 88 |
+
return generate_onepager(topic, target_audience, key_points, tone, length)
|
| 89 |
+
|
| 90 |
+
def generate_onepager(topic, target_audience, key_points, tone, length):
|
| 91 |
+
if generator is None:
|
| 92 |
+
return "Error: Model not initialized. Please wait for the model to load."
|
| 93 |
+
|
| 94 |
+
# Create a structured prompt for one-pager generation
|
| 95 |
+
length_tokens = {"Short": 200, "Medium": 400, "Long": 600}
|
| 96 |
+
max_tokens = length_tokens.get(length, 400)
|
| 97 |
+
|
| 98 |
+
# Create a simple prompt that works well with GPT-2
|
| 99 |
+
prompt = f"""Business Document: {topic}
|
| 100 |
+
|
| 101 |
+
Target Audience: {target_audience}
|
| 102 |
+
Key Points: {key_points}
|
| 103 |
+
Tone: {tone}
|
| 104 |
+
|
| 105 |
+
Professional one-page business summary:
|
| 106 |
+
|
| 107 |
+
{topic.upper()}
|
| 108 |
+
Business Case & Action Plan
|
| 109 |
+
|
| 110 |
+
Executive Summary:
|
| 111 |
+
{topic} represents a strategic opportunity for {target_audience.lower()}. This initiative delivers measurable business value through focused implementation and clear outcomes.
|
| 112 |
+
|
| 113 |
+
Key Benefits:
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
# Generate using HF Inference API
|
| 118 |
+
response = generator.text_generation(
|
| 119 |
+
prompt,
|
| 120 |
+
max_new_tokens=max_tokens,
|
| 121 |
+
temperature=0.7,
|
| 122 |
+
do_sample=True,
|
| 123 |
+
return_full_text=False
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# Extract generated text
|
| 127 |
+
if isinstance(response, str):
|
| 128 |
+
onepager = response.strip()
|
| 129 |
+
else:
|
| 130 |
+
onepager = response.generated_text.strip()
|
| 131 |
+
|
| 132 |
+
# If output is too short, provide a structured fallback
|
| 133 |
+
if len(onepager) < 50:
|
| 134 |
+
onepager = create_structured_onepager(topic, target_audience, key_points, tone)
|
| 135 |
+
|
| 136 |
+
return onepager
|
| 137 |
+
|
| 138 |
+
except Exception as e:
|
| 139 |
+
# Fallback to structured template
|
| 140 |
+
return create_structured_onepager(topic, target_audience, key_points, tone)
|
| 141 |
+
|
| 142 |
+
def create_structured_onepager(topic, target_audience, key_points, tone):
|
| 143 |
+
"""Create a structured one-pager that looks like a real business document"""
|
| 144 |
+
|
| 145 |
+
key_points_list = [point.strip() for point in key_points.split(',') if point.strip()]
|
| 146 |
+
|
| 147 |
+
# Create a visual one-pager that looks professional, not markdown
|
| 148 |
+
template = f"""
|
| 149 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 150 |
+
β {topic.upper()} β
|
| 151 |
+
β Business Case & Action Plan β
|
| 152 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 153 |
+
|
| 154 |
+
TARGET AUDIENCE: {target_audience.title()} DATE: {import_date()}
|
| 155 |
+
|
| 156 |
+
ββ EXECUTIVE SUMMARY ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 157 |
+
β {topic} represents a strategic opportunity to drive significant business β
|
| 158 |
+
β value through focused implementation. This initiative delivers measurable β
|
| 159 |
+
β outcomes with clear ROI and competitive advantages. β
|
| 160 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 161 |
+
|
| 162 |
+
β KEY BENEFITS & VALUE DRIVERS
|
| 163 |
+
|
| 164 |
+
{chr(10).join([f" βͺ {point.strip()}" for point in key_points_list[:4]])}
|
| 165 |
+
|
| 166 |
+
β‘ BUSINESS IMPACT
|
| 167 |
+
|
| 168 |
+
Revenue Growth: 15-30% increase through improved efficiency
|
| 169 |
+
Cost Reduction: 20-25% operational cost savings
|
| 170 |
+
Time to Market: 40-50% faster delivery cycles
|
| 171 |
+
Risk Mitigation: Reduced compliance and operational risks
|
| 172 |
+
|
| 173 |
+
π IMPLEMENTATION ROADMAP
|
| 174 |
+
|
| 175 |
+
Phase 1 (Month 1-2): Assessment & Planning
|
| 176 |
+
Phase 2 (Month 3-4): Core Implementation
|
| 177 |
+
Phase 3 (Month 5-6): Optimization & Scale
|
| 178 |
+
|
| 179 |
+
π΅ INVESTMENT SUMMARY
|
| 180 |
+
|
| 181 |
+
Initial Investment: $XXX,XXX (one-time)
|
| 182 |
+
Annual Operating: $XX,XXX (ongoing)
|
| 183 |
+
Break-even Point: 8-12 months
|
| 184 |
+
3-Year ROI: 250-400%
|
| 185 |
+
|
| 186 |
+
ββ DECISION REQUIRED ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 187 |
+
β APPROVE: Proceed with {topic.lower()} implementation β
|
| 188 |
+
β TIMELINE: Decision needed by [DATE] to meet Q[X] targets β
|
| 189 |
+
β NEXT STEP: Schedule planning session with implementation team β
|
| 190 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 191 |
+
|
| 192 |
+
Contact: [Implementation Team] | Email: [team@company.com] | Ext: XXXX
|
| 193 |
+
"""
|
| 194 |
+
|
| 195 |
+
return template
|
| 196 |
+
|
| 197 |
+
def import_date():
|
| 198 |
+
"""Get current date for the one-pager"""
|
| 199 |
+
from datetime import datetime
|
| 200 |
+
return datetime.now().strftime("%B %d, %Y")
|
| 201 |
+
|
| 202 |
+
def generate_header_image(topic, tone):
|
| 203 |
+
"""Generate optimized header image for business one-pager"""
|
| 204 |
+
global image_generator
|
| 205 |
+
|
| 206 |
+
if image_generator is None:
|
| 207 |
+
return None
|
| 208 |
+
|
| 209 |
+
try:
|
| 210 |
+
# Create business-focused prompt with LoRA triggers
|
| 211 |
+
business_style = {
|
| 212 |
+
"Professional": "corporate office style, business presentation",
|
| 213 |
+
"Casual": "modern startup office, friendly business environment",
|
| 214 |
+
"Academic": "research presentation, educational infographic",
|
| 215 |
+
"Persuasive": "marketing presentation, compelling business visual",
|
| 216 |
+
"Informative": "clean infographic style, data visualization"
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
style_desc = business_style.get(tone, "professional business")
|
| 220 |
+
|
| 221 |
+
# Enhanced prompt for business LoRAs
|
| 222 |
+
image_prompt = f"Professional business infographic header for {topic}, {style_desc}, clean corporate design, business graphics, office environment, high quality, no text, ultra realistic"
|
| 223 |
+
|
| 224 |
+
# Use optimized generation settings
|
| 225 |
+
generator = torch.Generator().manual_seed(42) # Consistent seed for business docs
|
| 226 |
+
|
| 227 |
+
# Generate with optimized settings
|
| 228 |
+
image = image_generator(
|
| 229 |
+
prompt=image_prompt,
|
| 230 |
+
num_inference_steps=1, # Ultra-fast with Schnell + optimizations
|
| 231 |
+
generator=generator,
|
| 232 |
+
height=384, # Better aspect ratio for headers
|
| 233 |
+
width=768,
|
| 234 |
+
).images[0]
|
| 235 |
+
|
| 236 |
+
return image
|
| 237 |
+
|
| 238 |
+
except Exception as e:
|
| 239 |
+
print(f"Optimized image generation failed: {str(e)}")
|
| 240 |
+
return None
|
| 241 |
+
|
| 242 |
+
def export_to_pdf(content, topic, header_image=None):
|
| 243 |
+
"""Export the one-pager content to PDF"""
|
| 244 |
+
try:
|
| 245 |
+
# Create a temporary file for the PDF
|
| 246 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
|
| 247 |
+
pdf_path = tmp_file.name
|
| 248 |
+
|
| 249 |
+
# Create PDF document
|
| 250 |
+
doc = SimpleDocTemplate(pdf_path, pagesize=letter, topMargin=0.5*inch)
|
| 251 |
+
styles = getSampleStyleSheet()
|
| 252 |
+
|
| 253 |
+
# Custom styles
|
| 254 |
+
title_style = ParagraphStyle(
|
| 255 |
+
'CustomTitle',
|
| 256 |
+
parent=styles['Heading1'],
|
| 257 |
+
fontSize=16,
|
| 258 |
+
spaceAfter=20,
|
| 259 |
+
textColor=colors.darkblue,
|
| 260 |
+
alignment=1 # Center alignment
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
body_style = ParagraphStyle(
|
| 264 |
+
'CustomBody',
|
| 265 |
+
parent=styles['Normal'],
|
| 266 |
+
fontSize=10,
|
| 267 |
+
fontName='Courier', # Monospace font to preserve ASCII formatting
|
| 268 |
+
leftIndent=0,
|
| 269 |
+
rightIndent=0
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
# Build PDF content
|
| 273 |
+
story = []
|
| 274 |
+
|
| 275 |
+
# Add header image if available
|
| 276 |
+
if header_image:
|
| 277 |
+
try:
|
| 278 |
+
# Save image temporarily
|
| 279 |
+
img_buffer = io.BytesIO()
|
| 280 |
+
header_image.save(img_buffer, format='PNG')
|
| 281 |
+
img_buffer.seek(0)
|
| 282 |
+
|
| 283 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as img_file:
|
| 284 |
+
img_file.write(img_buffer.getvalue())
|
| 285 |
+
img_path = img_file.name
|
| 286 |
+
|
| 287 |
+
# Add image to PDF
|
| 288 |
+
img = RLImage(img_path, width=6*inch, height=3*inch)
|
| 289 |
+
story.append(img)
|
| 290 |
+
story.append(Spacer(1, 20))
|
| 291 |
+
|
| 292 |
+
# Clean up temp image file
|
| 293 |
+
os.unlink(img_path)
|
| 294 |
+
|
| 295 |
+
except Exception as e:
|
| 296 |
+
print(f"Failed to add image to PDF: {str(e)}")
|
| 297 |
+
|
| 298 |
+
# Add title
|
| 299 |
+
story.append(Paragraph(f"Business Document: {topic}", title_style))
|
| 300 |
+
story.append(Spacer(1, 20))
|
| 301 |
+
|
| 302 |
+
# Add content (preserve formatting)
|
| 303 |
+
content_lines = content.split('\n')
|
| 304 |
+
for line in content_lines:
|
| 305 |
+
if line.strip():
|
| 306 |
+
story.append(Paragraph(line.replace('<', '<').replace('>', '>'), body_style))
|
| 307 |
+
else:
|
| 308 |
+
story.append(Spacer(1, 6))
|
| 309 |
+
|
| 310 |
+
# Build PDF
|
| 311 |
+
doc.build(story)
|
| 312 |
+
|
| 313 |
+
return pdf_path
|
| 314 |
+
|
| 315 |
+
except Exception as e:
|
| 316 |
+
print(f"PDF export failed: {str(e)}")
|
| 317 |
+
return None
|
| 318 |
+
|
| 319 |
+
def generate_complete_onepager(topic, target_audience, key_points, tone, length, model_choice="local", include_image=True):
|
| 320 |
+
"""Generate complete one-pager with optional image and return both content and PDF"""
|
| 321 |
+
|
| 322 |
+
# Generate the text content
|
| 323 |
+
content = generate_with_mcp(topic, target_audience, key_points, tone, length, model_choice)
|
| 324 |
+
|
| 325 |
+
# Generate header image if requested
|
| 326 |
+
header_image = None
|
| 327 |
+
if include_image and image_generator is not None:
|
| 328 |
+
header_image = generate_header_image(topic, tone)
|
| 329 |
+
|
| 330 |
+
# Generate PDF
|
| 331 |
+
pdf_path = export_to_pdf(content, topic, header_image)
|
| 332 |
+
|
| 333 |
+
return content, pdf_path, header_image
|
| 334 |
+
|
| 335 |
+
# Create the Gradio interface
|
| 336 |
+
def create_interface():
|
| 337 |
+
with gr.Blocks(title="One-Pager Generator", theme=gr.themes.Soft()) as demo:
|
| 338 |
+
gr.Markdown("# π AI One-Pager Generator")
|
| 339 |
+
gr.Markdown("Generate professional business documents using modern AI models via Inference API + PDF export!")
|
| 340 |
+
|
| 341 |
+
with gr.Row():
|
| 342 |
+
with gr.Column(scale=1):
|
| 343 |
+
topic_input = gr.Textbox(
|
| 344 |
+
label="Topic",
|
| 345 |
+
placeholder="e.g., Digital Marketing Strategy, Climate Change Solutions, etc.",
|
| 346 |
+
lines=2,
|
| 347 |
+
value="Artificial Intelligence in Healthcare"
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
audience_input = gr.Textbox(
|
| 351 |
+
label="Target Audience",
|
| 352 |
+
placeholder="e.g., Business executives, Students, General public, etc.",
|
| 353 |
+
lines=1,
|
| 354 |
+
value="Healthcare professionals"
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
keypoints_input = gr.Textbox(
|
| 358 |
+
label="Key Points to Cover",
|
| 359 |
+
placeholder="Enter main points separated by commas",
|
| 360 |
+
lines=4,
|
| 361 |
+
value="Machine learning applications, Data privacy, Cost-effectiveness, Implementation challenges"
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
tone_dropdown = gr.Dropdown(
|
| 365 |
+
choices=["Professional", "Casual", "Academic", "Persuasive", "Informative"],
|
| 366 |
+
label="Tone",
|
| 367 |
+
value="Professional"
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
length_dropdown = gr.Dropdown(
|
| 371 |
+
choices=["Short", "Medium", "Long"],
|
| 372 |
+
label="Length",
|
| 373 |
+
value="Medium"
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
model_dropdown = gr.Dropdown(
|
| 377 |
+
choices=["local", "mcp-claude", "mcp-openai"],
|
| 378 |
+
label="AI Model",
|
| 379 |
+
value="local",
|
| 380 |
+
info="Choose between local Qwen model or MCP-connected external services"
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
include_image_checkbox = gr.Checkbox(
|
| 384 |
+
label="Generate Header Image",
|
| 385 |
+
value=True,
|
| 386 |
+
info="Use FLUX Schnell to generate a professional header image"
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
+
generate_btn = gr.Button("π Generate One-Pager", variant="primary")
|
| 390 |
+
|
| 391 |
+
with gr.Column(scale=2):
|
| 392 |
+
with gr.Row():
|
| 393 |
+
output_text = gr.Textbox(
|
| 394 |
+
label="Generated One-Pager",
|
| 395 |
+
lines=20,
|
| 396 |
+
max_lines=30,
|
| 397 |
+
show_copy_button=True,
|
| 398 |
+
placeholder="Your generated one-pager will appear here...",
|
| 399 |
+
scale=2
|
| 400 |
+
)
|
| 401 |
+
generated_image = gr.Image(
|
| 402 |
+
label="Header Image",
|
| 403 |
+
scale=1,
|
| 404 |
+
height=200
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
+
with gr.Row():
|
| 408 |
+
pdf_download = gr.File(
|
| 409 |
+
label="Download PDF",
|
| 410 |
+
visible=False
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
with gr.Row():
|
| 414 |
+
gr.Markdown("""
|
| 415 |
+
### π‘ Tips for Best Results:
|
| 416 |
+
- **Be specific** with your topic for more targeted content
|
| 417 |
+
- **Include 3-5 key points** separated by commas
|
| 418 |
+
- **Choose the right tone** for your intended audience
|
| 419 |
+
- **Use descriptive audience** details (e.g., "C-level executives" vs "executives")
|
| 420 |
+
- **Try different AI models** - Local for privacy, MCP for enhanced capabilities
|
| 421 |
+
""")
|
| 422 |
+
|
| 423 |
+
# Connect the generate button to the function
|
| 424 |
+
def generate_and_display(topic, audience, keypoints, tone, length, model, include_image):
|
| 425 |
+
content, pdf_path, header_image = generate_complete_onepager(
|
| 426 |
+
topic, audience, keypoints, tone, length, model, include_image
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
# Return outputs for all components
|
| 430 |
+
pdf_visible = pdf_path is not None
|
| 431 |
+
return (
|
| 432 |
+
content, # output_text
|
| 433 |
+
header_image, # generated_image
|
| 434 |
+
gr.File(value=pdf_path, visible=pdf_visible) # pdf_download
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
generate_btn.click(
|
| 438 |
+
fn=generate_and_display,
|
| 439 |
+
inputs=[topic_input, audience_input, keypoints_input, tone_dropdown, length_dropdown, model_dropdown, include_image_checkbox],
|
| 440 |
+
outputs=[output_text, generated_image, pdf_download]
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
+
return demo
|
| 444 |
+
|
| 445 |
+
# Initialize model and launch
|
| 446 |
+
if __name__ == "__main__":
|
| 447 |
+
print("π Starting One-Pager Generator with Qwen 2.5-7B, MCP, and FLUX Schnell...")
|
| 448 |
+
print("π₯ Loading AI text model...")
|
| 449 |
+
model_status = initialize_model()
|
| 450 |
+
print(f"β
{model_status}")
|
| 451 |
+
|
| 452 |
+
print("π¨ Loading FLUX Schnell image generator...")
|
| 453 |
+
image_status = initialize_image_generator()
|
| 454 |
+
print(f"β
{image_status}")
|
| 455 |
+
|
| 456 |
+
print("π Initializing MCP client...")
|
| 457 |
+
mcp_status = initialize_mcp_client()
|
| 458 |
+
print(f"β
{mcp_status}")
|
| 459 |
+
|
| 460 |
+
print("π Launching interface...")
|
| 461 |
+
demo = create_interface()
|
| 462 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,16 +1,4 @@
|
|
| 1 |
-
torch
|
| 2 |
-
transformers
|
| 3 |
gradio
|
| 4 |
huggingface_hub
|
| 5 |
-
tokenizers
|
| 6 |
-
accelerate
|
| 7 |
-
psutil
|
| 8 |
-
mcp
|
| 9 |
-
gradio_client
|
| 10 |
reportlab
|
| 11 |
-
|
| 12 |
-
diffusers
|
| 13 |
-
pillow
|
| 14 |
-
spaces
|
| 15 |
-
torchvision
|
| 16 |
-
xformers
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
huggingface_hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
reportlab
|
| 4 |
+
requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|