# app.py import os import re import json import gradio as gr from llama_cloud_services import LlamaParse from openai import OpenAI from dotenv import load_dotenv from weasyprint import HTML from pathlib import Path import requests MAX_AUDIO_SLIDES = 15 load_dotenv() OUTPUT_DIR = Path("generated_pdf_files") OUTPUT_DIR.mkdir(exist_ok=True) OUTPUT_DIR_AUDIO = Path("generated_audio_files") OUTPUT_DIR_AUDIO.mkdir(exist_ok=True) MODAL_TTS_URL = os.environ.get("MODAL_TTS_URL") SNC_API_KEY = os.environ.get("SNC_API_KEY") # LlamaParse Client for document parsing LLAMA_CLOUD_API = os.environ.get("LLAMA_CLOUD_API_KEY") llama_parser_client = None if LLAMA_CLOUD_API: try: llama_parser_client = LlamaParse(api_key=LLAMA_CLOUD_API, result_type="markdown", num_workers=4, language='en', verbose=False) print("✅ LlamaParse client initialized successfully.") except Exception as e: print(f"❌ Error initializing LlamaParse client: {e}") else: print('⚠️ WARNING: LLAMA_CLOUD_API_KEY not found. Document Parsing will be disabled.') # Nebius Client for AI generation NEBIUS_API_KEY = os.environ.get("NEBIUS_API_KEY") nebius_client = None if NEBIUS_API_KEY: try: nebius_client = OpenAI( base_url="https://api.studio.nebius.com/v1/", api_key=NEBIUS_API_KEY ) print("✅ Nebius client initialized successfully.") except Exception as e: print(f"❌ Error initializing Nebius client: {e}") else: print('⚠️ WARNING: NEBIUS_API_KEY not found. All AI generation tools will be disabled.') sambanova_client = None if SNC_API_KEY: sambanova_client = OpenAI(api_key=SNC_API_KEY, base_url='https://api.sambanova.ai/v1') print("✅ Sambanova client initialized successfully.") else: print('⚠️ WARNING: SNC_API_KEY not found. All AI generation tools will be disabled.') # Tool 1: Document Parser def parse_documents_tool(files_input_from_gradio: list) -> str: """Parses uploaded documents using LlamaParse into clean Markdown text.""" if not llama_parser_client: raise gr.Error("LlamaParse client not initialized.") if not files_input_from_gradio: raise gr.Error("No files provided for parsing.") file_paths = [f.name for f in files_input_from_gradio] try: print(f"Tool 1 (LlamaParse): Parsing {len(file_paths)} documents...") llama_index_documents = llama_parser_client.load_data(file_paths) final_markdown = "\n\n\n\n".join([doc.text for doc in llama_index_documents if doc.text]) print(f"Tool 1 (LlamaParse): Success. Total Markdown length: {len(final_markdown)}") return final_markdown except Exception as e: print(f"❌ Error in parse_documents_tool: {e}") raise gr.Error(f"LlamaParse failed: {str(e)}") # Tool 2: Creative Plan Generator (Augment, Outline, Design) def generate_rich_content_and_theme_tool(parsed_markdown: str, user_topic: str) -> str: """ Analyzes text and a topic to generate a comprehensive JSON 'master plan' for a presentation, including a visual theme, slide structure, augmented content, and visualization ideas. """ if not nebius_client: raise gr.Error("Nebius client not initialized.") # prompt = f""" You are a Chief Creative Officer and an expert AI Art Director. Your task is to take raw text and a presentation topic and create a master plan for a stunning visual report. The output MUST be a single, valid JSON object. **Your Plan (JSON Object) Must Contain:** 1. `visual_theme`: An object describing the aesthetic with `theme_name`, `primary_color`, `secondary_color`, `accent_color`, `font_headings`, and `font_body`. 2. `presentation_title`: A compelling title. 3. `slides`: An array of slide objects. For each slide: - `slide_title`: A clear title. - `key_points`: An array of critical bullet points. - `augmented_info`: Additional context and details. - `visualization_suggestion`: A practical idea for a non-image visual element (e.g., "A pure CSS bar chart..."). - **`image_generation_prompt`**: An **OPTIONAL** key. Only include this key for the slides you select for an image. - **`speaker_notes_text`**: Contains a script for this slide. The text should be engaging, expand on the key points, and guide a presenter on how to deliver the content. **CRITICAL ART DIRECTION RULES:** 1. **Selectivity is Key:** Your goal is to select only a **few (2-4) high-impact slides** to feature an image. Do not add an image prompt to every slide. Prompts must be specific and conceptual. 2. **Title Slide is Mandatory:** The **first (title) slide MUST have an `image_generation_prompt`** to create a strong opening. 3. **Use Color Names, Not Hex Codes:** In the image prompt, you MUST use the descriptive color `name` you generated (e.g., "Dominant colors: Deep Space Blue, Solar Orange"). **DO NOT use hex codes in the image prompt.** 3. **Choose Wisely:** For the other 1-3 images, choose slides that have a strong, singular, and visualizable concept. Slides that are very dense with text or data charts are poor candidates for an image. 4. **Content-Specific Prompts:** The `image_generation_prompt` MUST be highly specific to the content of that slide. It should generate a conceptual, artistic image that represents the slide's core idea. Use descriptive keywords. - **Good Example:** For a slide about financial growth, a good prompt is "Minimalist vector illustration of a green arrow moving upwards through a field of abstract data points, clean background, corporate style." - **Bad Example:** "An image about finance." (Too generic!) - **Text in Images:** When writing an `image_generation_prompt`, AVOID including complex text. If text is necessary, keep it to one or two simple words and specify it clearly, e.g., "with the word 'Growth' elegantly integrated". Image models are bad at rendering lots of text. 5. **Speaker Notes:** The `speaker_notes_text` should NOT simply repeat the on-slide text. It should provide a narrative, give background, and guide the presenter. **Instructions for `image_generation_prompt` for the 'flux-schnell' model:** - Focus on keywords like: "minimalist illustration", "conceptual art", "vector graphic", "isometric design", "abstract shapes", "data visualization art". - Incorporate the `visual_theme` colors into the prompt (e.g., "Dominant colors: [primary_color], [accent_color]"). **Presentation Topic:** "{user_topic}" **Parsed Document Text:** --- {parsed_markdown} --- Begin JSON Output: """ try: # response = nebius_client.chat.completions.create( # model="deepseek-ai/DeepSeek-V3-0324", # messages=[{"role": "user", "content": prompt}], # temperature=0.4, # max_tokens=8192, # response_format={"type": "json_object"}, # ) print("Tool 2 (Creative Plan): Calling Sambanova (DeepSeek-V3-0324)...") response = sambanova_client.chat.completions.create( model="DeepSeek-V3-0324", messages=[{"role": "user", "content": prompt}], temperature=0.4, max_tokens=8192, response_format={"type": "json_object"}, ) rich_content_json = response.choices[0].message.content print("\n--- DEBUG: Raw Creative Plan JSON from Model ---\n", rich_content_json, "\n----------------------------------------\n") json.loads(rich_content_json) print("Tool 2 (Creative Plan): Success.") return rich_content_json except Exception as e: print(f"❌ Error in generate_rich_content_and_theme_tool: {e}") raise gr.Error(f"Creative plan generation failed: {str(e)}") def generate_assets_tool(json_plan_str: str) -> str: """ Takes a JSON plan, generates images for each slide using the provided prompts, and returns an updated JSON plan with image URLs. """ if not nebius_client: raise gr.Error("Nebius client not initialized.") print("Tool 3 (Asset Gen): Starting image generation for all slides...") plan_data = json.loads(json_plan_str) for i, slide in enumerate(plan_data["slides"]): image_prompt = slide.get("image_generation_prompt") if not image_prompt: print(f" - Skipping slide {i+1}, no image prompt found.") plan_data["slides"][i]["image_url"] = None # Explicitly set to null continue print(f" - Generating image for slide {i+1}: '{slide['slide_title']}'...") try: response = nebius_client.images.generate( model="black-forest-labs/flux-schnell", prompt=image_prompt, response_format="url", extra_body={ "width": 1024, "height": 1024, "num_inference_steps": 4, }, ) image_url = response.data[0].url plan_data["slides"][i]["image_url"] = image_url print(f" - Success! Image URL: {image_url}") except Exception as e: print(f" - ❌ FAILED to generate image for slide {i+1}: {e}") plan_data["slides"][i]["image_url"] = None return json.dumps(plan_data) # Tool 4: Audio Asset Generator def generate_audio_assets_tool(json_plan_str: str): """ Takes a JSON plan, generates audio for each slide using the Modal TTS API, and returns an updated JSON plan with audio URLs. """ if not MODAL_TTS_URL: print("⚠️ WARNING: MODAL_TTS_URL not set. Skipping audio generation.") return json_plan_str print("Tool 4 (Audio Gen): Starting TTS for all slides via Modal API...") plan_data = json.loads(json_plan_str) for i, slide in enumerate(plan_data["slides"]): notes_text = slide.get("speaker_notes_text") if not notes_text: print(f" - Skipping audio for slide {i+1}, no speaker notes text found.") plan_data["slides"][i]["audio_url"] = None continue print(f" - Generating audio for slide {i+1}...") try: payload = {"text": notes_text, "voice": "af_heart"} headers = {"Content-Type": "application/json"} response = requests.post(MODAL_TTS_URL, json=payload, headers=headers, timeout=180) if response.status_code == 200: audio_path = OUTPUT_DIR_AUDIO / f"slide_audio_{i}.wav" with open(audio_path, "wb") as f: f.write(response.content) plan_data["slides"][i]["audio_url"] = str(audio_path) print(f" - Success! Audio saved to {audio_path}") else: print(f" - ❌ FAILED to generate audio for slide {i+1}. Status: {response.status_code}, Response: {response.text}") plan_data["slides"][i]["audio_url"] = None except Exception as e: print(f" - ❌ FAILED to generate audio for slide {i+1} with exception: {e}") plan_data["slides"][i]["audio_url"] = None # return json.dumps(plan_data) yield plan_data # Tool 3: HTML Presentation Renderer def convert_rich_content_to_html_tool(rich_content_json: str) -> str: """Takes a rich JSON 'master plan' and renders it as a single, static HTML file.""" if not nebius_client: raise gr.Error("Nebius client not initialized.") prompt = f""" You are a '10x' Staff Front-End Engineer and a world-class Information Designer. You are an expert in creating beautiful, accessible, and high-performance static web reports from data. You have a deep understanding of web standards and know how to interpret technical requirements pragmatically. **Core Mandate:** Your task is to take the provided JSON design brief and transform it into a single, static, scrollable HTML file that is both breathtakingly beautiful and easy to understand. **STRICT TECHNICAL RULES:** 1. **NO JAVASCRIPT.** The final output must be pure HTML and CSS. 2. **NO INTERACTIVE NAVIGATION.** The page is a static report that users will scroll through. 3. **SELF-CONTAINED.** All CSS must be embedded in `