import gradio as gr from transformers import pipeline import re from fpdf import FPDF import tempfile # -------------------------------------- # Load Model # -------------------------------------- generator = pipeline("text2text-generation", model="google/flan-t5-xl") # -------------------------------------- # Brand Memory (simple key-value storage) # -------------------------------------- brand_memory = { "tone": "Professional, modern, emotionally resonant", "style": "Story-driven, cinematic, customer-first", } # -------------------------------------- # Core Campaign Generation # -------------------------------------- def generate_campaign(brand_name, product_type, goal, tone, audience, region): combined_tone = f"{tone}. The brand historically sounds like this: {brand_memory['tone']}." prompt = f""" You are an expert creative strategist hired to design a world-class campaign. Your output must read like a premium brand deck. Brand Name: {brand_name} Product Type: {product_type} Goal: {goal} Tone: {combined_tone} Target Audience: {audience} Region: {region} Write: **Creative Direction** – 3–5 line vision. **Emotional Tone** – emotional personality + brand resonance. **Ad Script** – cinematic script with Intro, Body, CTA. **Poster Prompt** – visually detailed description for an AI image generator. **Taglines** – two powerful options. Write in rich natural language, with clear section titles. """ llm_output = generator(prompt, max_new_tokens=650, temperature=0.8)[0]["generated_text"] # SECTION EXTRACTION (Regex) def extract(section): pattern = rf"{section}([\s\S]*?)(?=\n[A-Z][a-zA-Z ]+:|$)" match = re.search(pattern, llm_output, re.IGNORECASE) return match.group(1).strip() if match else "Not found." creative_direction = extract("Creative Direction") emotional_tone = extract("Emotional Tone") ad_script = extract("Ad Script") poster_prompt = extract("Poster Prompt") taglines = extract("Taglines") # FORMATTED MARKDOWN OUTPUT md = f""" # 🎨 **Enterprise Creative Campaign Output** --- ## ✨ Brand: **{brand_name}** ### 🌍 Region: {region} --- ## 🎯 **Creative Direction** {creative_direction} --- ## ❤️ **Emotional Tone** {emotional_tone} --- ## 🎬 **Ad Script** {ad_script} --- ## 🖼 **Poster Prompt** {poster_prompt} --- ## 🔥 **Taglines** {taglines} --- """ return md, creative_direction, emotional_tone, ad_script, poster_prompt, taglines # -------------------------------------- # POSTER PROMPT ONLY # -------------------------------------- def generate_poster_prompt(brand, mood, theme, audience): prompt = f""" Create a cinematic AI poster prompt for Stable Diffusion. Brand: {brand} Mood: {mood} Theme: {theme} Audience: {audience} Describe lighting, color palette, environment, camera angle, people, typography. """ result = generator(prompt, max_new_tokens=300, temperature=0.8)[0]["generated_text"] return result # -------------------------------------- # UPDATE BRAND MEMORY # -------------------------------------- def update_brand_memory(new_tone, new_style): if new_tone: brand_memory["tone"] = new_tone if new_style: brand_memory["style"] = new_style return f"✅ Brand memory updated successfully!", str(brand_memory) # -------------------------------------- # Export as PDF # -------------------------------------- def export_pdf(text): pdf = FPDF() pdf.add_page() pdf.set_auto_page_break(auto=True, margin=15) pdf.set_font("Arial", size=12) for line in text.split("\n"): pdf.multi_cell(0, 10, line) tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") pdf.output(tmp.name) return tmp.name # -------------------------------------- # Gradio UI # -------------------------------------- with gr.Blocks(theme=gr.themes.Monochrome()) as demo: gr.HTML("""
Powered by HuggingFace + Rookus Reasoning Core