# content_utils.py """Content generation and utility functions for PPT Generator""" import re import requests import logging import base64 import time import traceback from io import BytesIO from PIL import Image from typing import List, Dict, Tuple import PyPDF2 import pandas as pd import chardet import replicate from bs4 import BeautifulSoup from urllib.parse import urlparse import urllib.request from datetime import datetime from pptx import Presentation from pptx.util import Inches, Pt from pptx.dml.color import RGBColor from pptx.enum.text import PP_ALIGN, MSO_ANCHOR from pptx.enum.shapes import MSO_SHAPE logger = logging.getLogger(__name__) # Import process flow generator try: from process_flow_generator import generate_process_flow_for_ppt PROCESS_FLOW_AVAILABLE = True except ImportError: PROCESS_FLOW_AVAILABLE = False print("[Warning] Cannot import process_flow_generator. Process flow diagram feature disabled.") def parse_executor_response(executor_response: str, slides_config: List[Dict], language: str = "English") -> Dict[int, Dict]: """실행자 AI의 응답을 파싱하여 슬라이드 데이터로 변환 - 개선된 버전""" parsed_data = {} # 슬라이드 구분 패턴 slide_patterns = [ r'\[슬라이드\s*(\d+)\]', r'\[Slide\s*(\d+)\]', r'Slide\s*(\d+):', r'#\s*Slide\s*(\d+)', r'---\s*\n\s*\[슬라이드\s*(\d+)\]', r'슬라이드\s*(\d+)\.', r'Slide\s+(\d+)\s*-', r'###\s*슬라이드\s*(\d+)', r'##\s*\[슬라이드\s*(\d+)\]', ] # 전체 텍스트에서 슬라이드 찾기 all_matches = [] for pattern in slide_patterns: matches = list(re.finditer(pattern, executor_response, re.IGNORECASE | re.MULTILINE)) for m in matches: all_matches.append((m.start(), m)) # 위치 순으로 정렬 all_matches.sort(key=lambda x: x[0]) slide_matches = [m[1] for m in all_matches] # 슬라이드별로 내용 파싱 for i, match in enumerate(slide_matches): try: start_pos = match.start() end_pos = slide_matches[i + 1].start() if i + 1 < len(slide_matches) else len(executor_response) slide_content = executor_response[start_pos:end_pos] # 슬라이드 번호 추출 slide_num = None for group in match.groups(): if group: slide_num = int(group) break if slide_num is None: continue # 제목 추출 title = "" title_patterns = [ r'제목:\s*(.+?)(?=\n|$)', r'Title:\s*(.+?)(?=\n|$)', r'제목\s*:\s*(.+?)(?=\n|$)', ] for pattern in title_patterns: title_match = re.search(pattern, slide_content, re.MULTILINE) if title_match: title = title_match.group(1).strip() if title: break # 부제목 추출 subtitle = "" subtitle_patterns = [ r'부제목:\s*(.+?)(?=\n|$)', r'Subtitle:\s*(.+?)(?=\n|$)', r'부제\s*:\s*(.+?)(?=\n|$)', ] for pattern in subtitle_patterns: subtitle_match = re.search(pattern, slide_content, re.MULTILINE) if subtitle_match: subtitle = subtitle_match.group(1).strip() if subtitle: break # 불릿 포인트 추출 (이모지가 있는 라인들) bullet_points = [] lines = slide_content.strip().split('\n') for line in lines: line = line.strip() if not line: continue # 이모지로 시작하거나 • 로 시작하는 라인 찾기 if line.startswith('•') or (len(line) > 2 and ord(line[0]) >= 0x1F300): bullet_points.append(line) # 발표자 노트 추출 - 개선된 패턴 speaker_notes = "" notes_patterns = [ r'발표자 노트:\s*(.+?)(?=\n\n|\n발표자 노트:|\n\[슬라이드|\Z)', r'Speaker Notes:\s*(.+?)(?=\n\n|\nSpeaker Notes:|\n\[Slide|\Z)', r'발표자노트:\s*(.+?)(?=\n\n|\n발표자노트:|\n\[슬라이드|\Z)', r'Notes:\s*(.+?)(?=\n\n|\nNotes:|\n\[Slide|\Z)', ] for pattern in notes_patterns: notes_match = re.search(pattern, slide_content, re.DOTALL | re.MULTILINE) if notes_match: speaker_notes = notes_match.group(1).strip() # 여러 줄의 노트를 합치기 speaker_notes = ' '.join(speaker_notes.split('\n')) if speaker_notes and len(speaker_notes) > 20: # 최소 20자 이상의 의미있는 노트 break # 발표자 노트가 너무 짧으면 더 찾기 if len(speaker_notes) < 50: # "발표자 노트:" 다음의 모든 텍스트 찾기 notes_start = slide_content.find("발표자 노트:") if notes_start == -1: notes_start = slide_content.find("Speaker Notes:") if notes_start == -1: notes_start = slide_content.find("발표자노트:") if notes_start > -1: notes_text = slide_content[notes_start:].split(":", 1) if len(notes_text) > 1: speaker_notes = notes_text[1].strip() # 다음 섹션 시작 전까지의 텍스트 가져오기 next_section_markers = ["시각 자료:", "Visual:", "제목:", "Title:", "[슬라이드", "[Slide"] for marker in next_section_markers: marker_pos = speaker_notes.find(marker) if marker_pos > 0: speaker_notes = speaker_notes[:marker_pos].strip() break speaker_notes = ' '.join(speaker_notes.split('\n')).strip() parsed_data[slide_num - 1] = { "title": title, "subtitle": subtitle, "bullet_points": bullet_points, "speaker_notes": speaker_notes } except Exception as e: logger.error(f"[Parser] 슬라이드 파싱 중 오류: {str(e)}") return parsed_data def extract_relevant_content_from_executor(executor_response: str, keywords: List[str], slide_num: int, slide_title: str) -> Dict: """실행자 응답에서 특정 슬라이드와 관련된 내용 추출""" try: # 여러 방법으로 관련 내용 찾기 # 1. 슬라이드 번호로 찾기 slide_patterns = [ f"\\[슬라이드\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[슬라이드|$)", f"\\[Slide\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[Slide|$)", f"Slide\\s*{slide_num}:([\\s\\S]*?)(?=Slide\\s*\\d+:|$)" ] for pattern in slide_patterns: match = re.search(pattern, executor_response, re.IGNORECASE | re.MULTILINE) if match: section = match.group(1) parsed = parse_slide_section(section, slide_title) if parsed and len(parsed.get("bullet_points", [])) > 0: return parsed # 2. 키워드로 찾기 for keyword in keywords: if not keyword: continue # 키워드 주변에서 불릿 포인트 찾기 keyword_pos = executor_response.lower().find(keyword.lower()) if keyword_pos >= 0: # 키워드 전후 1000자 범위에서 찾기 start = max(0, keyword_pos - 300) end = min(len(executor_response), keyword_pos + 1000) context = executor_response[start:end] # 불릿 포인트 추출 bullet_points = [] lines = context.split('\n') for line in lines: line = line.strip() if not line: continue # 불릿 포인트 패턴 if (line.startswith(('•', '-', '*', '·')) or re.match(r'^\d+\.', line) or (len(line) > 10 and not ':' in line and not line.startswith(('[', '제목', 'Title', '부제', 'Subtitle')))): clean_line = re.sub(r'^[•\-\*·\d+\.]', '', line).strip() if clean_line and len(clean_line) > 5 and not any(skip in clean_line for skip in ["Point", "포인트"]): bullet_points.append(f"• {clean_line}") if len(bullet_points) >= 3: return { "subtitle": keyword, "bullet_points": bullet_points[:5], "speaker_notes": f"{slide_title}에 대한 내용입니다." } except Exception as e: logger.error(f"실행자 응답에서 콘텐츠 추출 중 오류: {str(e)}") return None def extract_slide_section_from_executor(executor_response: str, slide_num: int) -> str: """실행자 응답에서 특정 슬라이드 섹션 추출""" try: # 다양한 슬라이드 구분 패턴 patterns = [ f"\\[슬라이드\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[슬라이드\\s*{slide_num+1}\\]|$)", f"\\[Slide\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[Slide\\s*{slide_num+1}\\]|$)", f"Slide\\s*{slide_num}:([\\s\\S]*?)(?=Slide\\s*{slide_num+1}:|$)", f"슬라이드\\s*{slide_num}\\.([\\s\\S]*?)(?=슬라이드\\s*{slide_num+1}\\.|$)" ] for pattern in patterns: match = re.search(pattern, executor_response, re.IGNORECASE | re.MULTILINE) if match: return match.group(0) # 전체 매치 반환 (슬라이드 번호 포함) except Exception as e: logger.error(f"슬라이드 섹션 추출 중 오류: {str(e)}") return "" def parse_slide_section(section: str, default_title: str) -> Dict: """슬라이드 섹션에서 콘텐츠 파싱 - 더 유연하게""" try: content = { "subtitle": default_title, "bullet_points": [] } lines = section.split('\n') for line in lines: line = line.strip() if not line: continue # 부제목 찾기 (더 유연한 패턴) if any(marker in line.lower() for marker in ['부제목:', 'subtitle:', '부제:', '제목:']): if ':' in line: subtitle = line.split(':', 1)[1].strip() if subtitle: content["subtitle"] = subtitle # 불릿 포인트 수집 (더 포괄적으로) elif ( # 이모지로 시작 (len(line) > 0 and ord(line[0]) >= 0x1F300) or # 불릿 기호로 시작 line.startswith(('•', '-', '*', '·', '▪', '▸')) or # 숫자로 시작 re.match(r'^\d+[.)]\s', line) or # "핵심 내용:" 이후의 모든 줄 ('핵심' in section and line and len(line) > 10 and not any(skip in line for skip in [':', '제목', 'Title', '노트', 'Notes'])) ): # 불필요한 기호 제거 clean_line = re.sub(r'^[•\-\*·▪▸\d+.)]\s*', '', line).strip() # 의미 있는 내용인지 확인 if clean_line and len(clean_line) > 5: # 이모지가 있으면 그대로, 없으면 • 추가 if len(line) > 0 and ord(line[0]) >= 0x1F300: content["bullet_points"].append(line) else: content["bullet_points"].append(f"• {clean_line}") return content if len(content["bullet_points"]) > 0 else None except Exception as e: logger.error(f"슬라이드 섹션 파싱 중 오류: {str(e)}") return None def parse_slide_section_improved(section: str, default_title: str, slide_num: int) -> Dict: """슬라이드 섹션에서 콘텐츠 파싱 - 슬라이드 3 문제 해결""" try: content = { "subtitle": default_title, "bullet_points": [] } # 부제목 추출 (더 많은 패턴) subtitle_patterns = [ r'부제목:\s*(.+?)(?=\n|$)', r'Subtitle:\s*(.+?)(?=\n|$)', r'부제:\s*(.+?)(?=\n|$)', r'Sub:\s*(.+?)(?=\n|$)' ] for pattern in subtitle_patterns: match = re.search(pattern, section, re.MULTILINE) if match: subtitle = match.group(1).strip() if subtitle and len(subtitle) > 2: content["subtitle"] = subtitle break # 불릿 포인트 추출 - 더 정확한 패턴 # 1. 명시적인 "핵심 내용:" 섹션 찾기 content_section_match = re.search( r'(?:핵심\s*내용|Key\s*Points|내용):\s*\n(.+?)(?=발표자|Speaker|시각|Visual|$)', section, re.DOTALL | re.IGNORECASE ) if content_section_match: content_text = content_section_match.group(1) lines = content_text.strip().split('\n') for line in lines: line = line.strip() if not line: continue # 이모지로 시작하는 라인 if len(line) > 0 and ord(line[0]) >= 0x1F300: content["bullet_points"].append(line) # 불릿으로 시작하는 라인 elif line.startswith(('•', '-', '*', '·', '▪', '▸')): content["bullet_points"].append(line) # 의미있는 내용인 경우 elif len(line) > 10 and not any(skip in line for skip in [':', '제목', 'Title', '노트']): # 숫자로 시작하면 제거 clean_line = re.sub(r'^\d+[.)]\s*', '', line).strip() if clean_line: content["bullet_points"].append(f"• {clean_line}") # 2. 불릿 포인트가 부족하면 전체 섹션에서 찾기 if len(content["bullet_points"]) < 3: lines = section.split('\n') for line in lines: line = line.strip() if not line or len(content["bullet_points"]) >= 5: continue # 메타 정보 스킵 if any(skip in line.lower() for skip in ['제목:', 'title:', '부제목:', 'subtitle:', '발표자', 'speaker', '시각']): continue # 이모지로 시작하거나 불릿으로 시작 if (len(line) > 0 and ord(line[0]) >= 0x1F300) or line.startswith(('•', '-', '*')): if line not in content["bullet_points"]: content["bullet_points"].append(line) # 일반 텍스트지만 의미있는 내용 elif len(line) > 15 and ':' not in line: formatted_line = f"• {line}" if formatted_line not in content["bullet_points"]: content["bullet_points"].append(formatted_line) # 중복 제거 content["bullet_points"] = list(dict.fromkeys(content["bullet_points"]))[:5] return content if len(content["bullet_points"]) > 0 else None except Exception as e: logger.error(f"슬라이드 {slide_num} 파싱 중 오류: {str(e)}") return None def extract_speaker_notes_from_section(section: str) -> str: """슬라이드 섹션에서 발표자 노트 추출""" try: patterns = [ r'발표자 노트:\s*(.+?)(?=시각 자료:|Visual:|\n제목:|\nTitle:|\n\[슬라이드|\n\[Slide|$)', r'Speaker Notes:\s*(.+?)(?=Visual:|시각 자료:|\nTitle:|\n제목:|\n\[Slide|\n\[슬라이드|$)', r'Notes:\s*(.+?)(?=Visual:|\nTitle:|$)', r'발표자노트:\s*(.+?)(?=시각 자료:|Visual:|$)' ] for pattern in patterns: match = re.search(pattern, section, re.DOTALL | re.MULTILINE) if match: notes = match.group(1).strip() # 여러 줄의 노트를 하나로 합치기 notes = ' '.join(notes.split('\n')).strip() if notes and len(notes) > 20: # 최소 20자 이상의 의미있는 노트 return notes # 패턴 매칭 실패시 더 넓은 범위로 찾기 notes_markers = ["발표자 노트:", "Speaker Notes:", "발표자노트:", "Notes:"] for marker in notes_markers: marker_pos = section.find(marker) if marker_pos > -1: notes_text = section[marker_pos + len(marker):].strip() # 다음 섹션 시작 전까지의 텍스트 가져오기 end_markers = ["시각 자료:", "Visual:", "제목:", "Title:", "[슬라이드", "[Slide", "\n\n"] min_end_pos = len(notes_text) for end_marker in end_markers: end_pos = notes_text.find(end_marker) if end_pos > 0 and end_pos < min_end_pos: min_end_pos = end_pos notes_text = notes_text[:min_end_pos].strip() notes_text = ' '.join(notes_text.split('\n')).strip() if notes_text and len(notes_text) > 20: return notes_text except Exception as e: logger.error(f"발표자 노트 추출 중 오류: {str(e)}") return "" def extract_relevant_content(executor_response: str, keywords: List[str], slide_num: int) -> Dict: """실행자 응답에서 특정 슬라이드와 관련된 내용 추출""" try: # 키워드 기반으로 관련 섹션 찾기 for keyword in keywords: if not keyword: continue # 키워드 주변 텍스트 찾기 keyword_lower = keyword.lower() response_lower = executor_response.lower() if keyword_lower in response_lower: # 키워드 위치 찾기 pos = response_lower.find(keyword_lower) # 주변 텍스트 추출 (앞뒤 500자) start = max(0, pos - 200) end = min(len(executor_response), pos + 800) context = executor_response[start:end] # 불릿 포인트 추출 bullet_points = [] lines = context.split('\n') for line in lines: line = line.strip() if line.startswith(('•', '-', '*')) or re.match(r'^\d+\.', line): clean_line = re.sub(r'^[•\-\*\d+\.]', '', line).strip() if clean_line and len(clean_line) > 5: bullet_points.append(f"• {clean_line}") if bullet_points: return { "subtitle": keyword, "bullet_points": bullet_points[:5], # 최대 5개 "speaker_notes": f"슬라이드 {slide_num}에 대한 내용입니다." } except Exception as e: logger.error(f"관련 콘텐츠 추출 중 오류: {str(e)}") return None def read_uploaded_file(file_path: str) -> str: """Read uploaded file (PDF, CSV, TXT)""" print(f"[File Reading] {file_path}") try: ext = file_path.lower().rsplit('.', 1)[-1] if '.' in file_path else '' if ext == 'pdf': with open(file_path, 'rb') as file: pdf_reader = PyPDF2.PdfReader(file) text = "" for page in pdf_reader.pages: text += page.extract_text() + "\n" return text[:5000] elif ext == 'csv': with open(file_path, 'rb') as file: raw_data = file.read() result = chardet.detect(raw_data) encoding = result['encoding'] or 'utf-8' df = pd.read_csv(file_path, encoding=encoding) return f"CSV Data:\n{df.head(20).to_string()}\n\nSummary: {len(df)} rows, {len(df.columns)} columns" elif ext in ['txt', 'text']: with open(file_path, 'rb') as file: raw_data = file.read() result = chardet.detect(raw_data) encoding = result['encoding'] or 'utf-8' with open(file_path, 'r', encoding=encoding) as file: return file.read()[:5000] else: return "Unsupported file format." except Exception as e: return f"File reading error: {str(e)}" def generate_dynamic_slides(topic: str, template: Dict, slide_count: int) -> List[Dict]: """Dynamically generate slides based on selected count""" core_slides = template.get("core_slides", []) optional_slides = template.get("optional_slides", []) # Content slide count content_slide_count = slide_count # Adjust if core slides exceed requested count if len(core_slides) > content_slide_count: selected_slides = core_slides[:content_slide_count] else: # Core slides + optional slides selected_slides = core_slides.copy() remaining = content_slide_count - len(core_slides) if remaining > 0 and optional_slides: # Add from optional slides additional = optional_slides[:remaining] selected_slides.extend(additional) # Add cover slide (first) slides = [{"title": "Cover", "style": "Title Slide (Hero)", "prompt_hint": "Presentation cover"}] # Add content slides slides.extend(selected_slides) # Add Thank You slide (last) slides.append({"title": "Thank You", "style": "Thank You Slide", "prompt_hint": "Presentation closing and key message"}) return slides def brave_search(query: str, api_token: str = None) -> List[Dict]: """Web search using Brave Search API""" if not api_token: print("[Brave Search] No API token, skipping search.") return [] print(f"[Brave Search] Query: {query}") headers = { "Accept": "application/json", "X-Subscription-Token": api_token } params = { "q": query, "count": 5 } try: response = requests.get( "https://api.search.brave.com/res/v1/web/search", headers=headers, params=params, timeout=10 ) if response.status_code == 200: data = response.json() results = [] for item in data.get("web", {}).get("results", [])[:3]: results.append({ "title": item.get("title", ""), "description": item.get("description", ""), "url": item.get("url", "") }) print(f"[Brave Search] Got {len(results)} results") return results else: print(f"[Brave Search] Error: {response.status_code}") return [] except Exception as e: print(f"[Brave Search] Exception: {str(e)}") return [] def generate_slide_content(topic: str, slide_title: str, slide_context: str, audience_type: str, language: str = "English", uploaded_content: str = None, web_search_results: List[Dict] = None, friendli_token: str = None, api_url: str = None, model_id: str = None, audience_types: Dict = None) -> Dict[str, str]: """Generate text content for each slide""" audience_info = audience_types.get(audience_type, {}) if audience_types else {} system_prompt = f"""당신은 PPT 슬라이드 콘텐츠 작성 전문가입니다. 청중: {audience_type} 언어: {language} 작성 규칙: 1. 부제목: 최대 10단어 2. 각 불릿 포인트는 반드시 관련 이모지로 시작 3. 이모지 다음에 바로 내용 (• 기호 제외) 4. 각 포인트는 8-12단어로 간결하게 5. 명사형 종결 사용 출력 형식: Subtitle: [부제목] 🎯 [첫 번째 포인트 내용] 💡 [두 번째 포인트 내용] 🚀 [세 번째 포인트 내용] ✅ [네 번째 포인트 내용] 📊 [다섯 번째 포인트 내용] 청중별 이모지 가이드: - 경영진: 📊 🎯 💰 🏆 🚀 📈 🔝 💡 - 투자자: 💰 📈 💎 🏦 💸 📊 🚀 🔒 - 기술팀: 🔧 💻 🛠️ ⚙️ 🔐 🌐 📱 🤖 - 일반직원: 🤝 💡 📋 ✅ 🎯 🌟 📅 💪 - 고객: ⭐ 🎁 💝 🛡️ 🌟 ✨ 🏅 👍 - 일반대중: 😊 🏠 🌍 ❤️ 🎉 🌈 ✨ 🎯""" user_message = f"""Topic: {topic} Slide Title: {slide_title} Context: {slide_context} Target Audience: {audience_type} Language: {language}""" if uploaded_content: user_message += f"\n\nReference Material:\n{uploaded_content[:1000]}" if web_search_results: search_context = "\n\nWeb Search Results:\n" try: if isinstance(web_search_results, list) and len(web_search_results) > 0: for i, result in enumerate(web_search_results[:3]): if isinstance(result, dict): search_context += f"- {result.get('title', 'N/A')}: {result.get('description', 'N/A')}\n" user_message += search_context except Exception as e: print(f"[Slide Content] Error processing search results: {str(e)}") user_message += f"\n\nCreate compelling content for this presentation slide specifically tailored for {audience_type} in {language}. Remember to write COMPLETE bullet points, NOT placeholders like 'Point 1'!" headers = { "Authorization": f"Bearer {friendli_token}", "Content-Type": "application/json" } payload = { "model": model_id, "messages": [ { "role": "system", "content": system_prompt }, { "role": "user", "content": user_message } ], "max_tokens": 400, "top_p": 0.8, "temperature": 0.7, "stream": False } try: response = requests.post(api_url, json=payload, headers=headers, timeout=30) if response.status_code == 200: result = response.json() content = result['choices'][0]['message']['content'].strip() print(f"[Slide Content] LLM Response:\n{content}") # 개선된 파싱 로직 lines = content.split('\n') subtitle = "" bullet_points = [] for line in lines: line = line.strip() if not line: continue # Subtitle 파싱 if line.lower().startswith("subtitle:") or line.startswith("Subtitle:") or line.startswith("부제목:"): subtitle = line.split(':', 1)[1].strip() # Bullet point 파싱 elif line.startswith("•") or line.startswith("-") or (len(line) > 2 and line[1] in [' ', '\t'] and ord(line[0]) >= 128): # 이미 • 로 시작하지 않으면 추가 if not line.startswith("•"): line = "• " + line.lstrip("- ") # "Point X" 패턴 체크 및 거부 if not any(placeholder in line for placeholder in ["Point 1", "Point 2", "Point 3", "Point 4", "Point 5", "📌 Point"]): bullet_points.append(line) # Subtitle이 없으면 기본값 if not subtitle: subtitle = f"{slide_title} Overview" # 불릿 포인트가 부족하면 재시도 또는 기본값 if len(bullet_points) < 5: print(f"[Slide Content] Warning: Only {len(bullet_points)} bullet points found. Retrying...") # 재시도를 위한 더 명확한 프롬프트 retry_message = f"""The previous response didn't include 5 complete bullet points. Please provide EXACTLY 5 bullet points for the slide titled "{slide_title}" about "{topic}". Each bullet must be: - A complete, meaningful statement (NOT "Point 1", "Point 2", etc.) - 8-12 words long - Starting with an emoji - Relevant to {audience_type} - In {language} Example format: - 📊 Increased efficiency through automated processes - 💰 30% cost reduction in operations - 🚀 Faster response times for customers - 🔧 Seamless integration with existing systems - 📈 Measurable ROI within 6 months""" retry_payload = { "model": model_id, "messages": [ {"role": "system", "content": system_prompt}, {"role": "user", "content": retry_message} ], "max_tokens": 300, "temperature": 0.8, "stream": False } try: retry_response = requests.post(api_url, json=retry_payload, headers=headers, timeout=30) if retry_response.status_code == 200: retry_result = retry_response.json() retry_content = retry_result['choices'][0]['message']['content'].strip() # 재시도 파싱 retry_lines = retry_content.split('\n') bullet_points = [] for line in retry_lines: line = line.strip() if line and (line.startswith("•") or line.startswith("-") or (len(line) > 2 and ord(line[0]) >= 128)): if not line.startswith("•"): line = "• " + line.lstrip("- ") if not any(placeholder in line for placeholder in ["Point 1", "Point 2", "Point 3", "Point 4", "Point 5"]): bullet_points.append(line) except: pass # 여전히 부족하면 의미있는 기본값 생성 default_bullets = { "English": [ "• 📊 Data-driven insights for better decisions", "• 💡 Innovative solutions to complex challenges", "• 🚀 Accelerated growth through optimization", "• 🎯 Targeted strategies for maximum impact", "• ✅ Proven results with measurable outcomes" ], "Korean": [ "• 📊 데이터 기반 의사결정 지원", "• 💡 복잡한 문제에 대한 혁신적 해결책", "• 🚀 최적화를 통한 성장 가속화", "• 🎯 최대 효과를 위한 타겟 전략", "• ✅ 측정 가능한 검증된 결과" ] } while len(bullet_points) < 5: default_idx = len(bullet_points) if default_idx < len(default_bullets.get(language, default_bullets["English"])): bullet_points.append(default_bullets[language][default_idx]) else: bullet_points.append(f"• ⚡ Key insight {len(bullet_points) + 1}") bullet_points = bullet_points[:5] print(f"[Slide Content] Final subtitle: {subtitle}") print(f"[Slide Content] Final bullets: {bullet_points}") return { "subtitle": subtitle, "bullet_points": bullet_points } else: print(f"[Slide Content] API Error: {response.status_code}") return { "subtitle": slide_title, "bullet_points": [ "• 📊 Strategic insights and analysis", "• 💡 Innovative approaches and solutions", "• 🚀 Growth opportunities identified", "• 🎯 Targeted implementation strategies", "• ✅ Measurable success metrics" ] } except Exception as e: print(f"[Slide Content] Error: {str(e)}") return { "subtitle": slide_title, "bullet_points": [ "• 📊 Key strategic insights", "• 💡 Innovative solutions proposed", "• 🚀 Growth acceleration opportunities", "• 🎯 Targeted action plans", "• ✅ Success measurement framework" ] } def generate_presentation_notes(topic: str, slide_title: str, content: Dict, audience_type: str, language: str = "English", friendli_token: str = None, api_url: str = None, model_id: str = None, audience_types: Dict = None) -> str: """Generate speaker notes for each slide""" print(f"[Speaker Notes] Generating for {slide_title}...") audience_info = audience_types.get(audience_type, {}) if audience_types else {} headers = { "Authorization": f"Bearer {friendli_token}", "Content-Type": "application/json" } if language == "Korean": system_prompt = f"""당신은 자연스러운 한국어 구어체로 발표하는 전문 프레젠터입니다. 청중: {audience_type} - {audience_info.get('description', '')} 언어: 한국어 발표자 노트 작성 규칙: 1. 완전한 구어체로 작성 (마치 실제로 말하는 것처럼) 2. 청중과 소통하는 느낌의 자연스러운 대화체 3. 각 불릿 포인트를 풀어서 설명 4. 전환 문구와 연결어 활용 5. 150-200자로 풍부하게 작성 6. 실제 발표 시나리오처럼 작성 예시: "자, 이제 우리가 주목해야 할 핵심 포인트들을 하나씩 살펴보겠습니다. 첫 번째로 보실 부분은 바로 시장 규모인데요, 현재 3.4조 달러라는 어마어마한 규모로 성장했습니다. 이게 얼마나 큰 숫자인지 감이 안 오시죠? 우리나라 GDP의 두 배가 넘는 규모입니다. 두 번째 포인트는..." 지시문이나 설명 제외, 오직 발표 내용만 작성하세요.""" else: system_prompt = f"""You are a professional presentation coach creating natural, conversational speaker notes. Audience: {audience_type} - {audience_info.get('description', '')} Tone: {audience_info.get('tone', '')} Language: English Create speaker notes that: 1. Sound completely natural and conversational (as if actually speaking) 2. Expand on each bullet point with context and examples 3. Use smooth transitions between points 4. Engage the audience with inclusive language ("we", "let's", "as you can see") 5. Be 100-150 words long with rich detail 6. Include rhetorical questions or audience engagement 7. NO stage directions or meta-commentary - only spoken words Example: "Now, let's dive into the key points that really matter here. First up, we're looking at a market size of 3.4 trillion dollars. That's trillion with a T! To put that in perspective, that's larger than the GDP of most countries. What's even more exciting is the growth rate - we're seeing 16% year-over-year expansion. Moving to our second point about digital adoption..." Write ONLY what the speaker would say out loud, no instructions or descriptions.""" bullet_text = "\n".join(content.get("bullet_points", [])) if language == "Korean": user_message = f"""주제: {topic} 슬라이드 제목: {slide_title} 부제목: {content.get('subtitle', '')} 내용: {bullet_text} 위 슬라이드를 {audience_type} 청중에게 발표할 때 사용할 자연스러운 구어체 발표 스크립트를 작성하세요. 각 포인트를 풀어서 설명하고, 청중과 소통하는 느낌으로 작성해주세요.""" else: user_message = f"""Topic: {topic} Slide Title: {slide_title} Subtitle: {content.get('subtitle', '')} Content: {bullet_text} Write natural, conversational speaker notes for presenting this slide to {audience_type}. Expand on each point with context and examples, using engaging language that connects with the audience.""" payload = { "model": model_id, "messages": [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_message} ], "max_tokens": 400, "temperature": 0.8, "top_p": 0.9, "stream": False } try: response = requests.post(api_url, json=payload, headers=headers, timeout=30) if response.status_code == 200: result = response.json() notes = result['choices'][0]['message']['content'].strip() # 노트가 너무 짧으면 재시도 if len(notes) < 100: print(f"[Speaker Notes] Notes too short ({len(notes)} chars), retrying...") retry_message = user_message + f"\n\n이전 응답이 너무 짧았습니다. 최소 150자 이상으로 각 포인트를 자세히 설명해주세요." if language == "Korean" else "\n\nThe previous response was too short. Please provide at least 100 words, expanding on each point with specific examples and context." retry_payload = { "model": model_id, "messages": [ {"role": "system", "content": system_prompt}, {"role": "user", "content": retry_message} ], "max_tokens": 500, "temperature": 0.85, "stream": False } retry_response = requests.post(api_url, json=retry_payload, headers=headers, timeout=30) if retry_response.status_code == 200: retry_result = retry_response.json() notes = retry_result['choices'][0]['message']['content'].strip() return notes else: print(f"[Speaker Notes] API Error: {response.status_code}") if language == "Korean": return f"자, 이제 {slide_title}에 대해 살펴보겠습니다. 여기서 우리가 주목해야 할 핵심 포인트들이 있는데요, 첫 번째는 {content.get('subtitle', '')}입니다. 이 부분이 왜 중요한지 하나씩 짚어보면서 설명드리겠습니다. 특히 여러분의 비즈니스에 어떤 영향을 미칠 수 있는지 함께 생각해보시면 좋을 것 같습니다." else: return f"Now let's explore {slide_title}. What I want to highlight here are the key points about {content.get('subtitle', '')}. Each of these elements plays a crucial role in our overall strategy, and I'll walk you through why they matter for your business. As we go through these points, think about how they might apply to your specific situation." except Exception as e: print(f"[Speaker Notes] Error: {str(e)}") if language == "Korean": return f"자, 이제 {slide_title}에 대해 살펴보겠습니다. 여기서 우리가 주목해야 할 핵심 포인트들이 있는데요, 첫 번째는 {content.get('subtitle', '')}입니다. 이 부분이 왜 중요한지 하나씩 짚어보면서 설명드리겠습니다. 특히 여러분의 비즈니스에 어떤 영향을 미칠 수 있는지 함께 생각해보시면 좋을 것 같습니다." else: return f"Now let's explore {slide_title}. What I want to highlight here are the key points about {content.get('subtitle', '')}. Each of these elements plays a crucial role in our overall strategy, and I'll walk you through why they matter for your business. As we go through these points, think about how they might apply to your specific situation." def generate_closing_notes(topic: str, conclusion_phrase: str, audience_type: str, language: str = "English", friendli_token: str = None, api_url: str = None, model_id: str = None, audience_types: Dict = None) -> str: """Generate speaker notes for the closing slide""" print(f"[Closing Notes] Generating...") audience_info = audience_types.get(audience_type, {}) if audience_types else {} headers = { "Authorization": f"Bearer {friendli_token}", "Content-Type": "application/json" } if language == "Korean": system_prompt = f"""당신은 프레젠테이션을 마무리하는 전문 발표자입니다. 청중: {audience_type} - {audience_info.get('description', '')} 톤: 따뜻하고 전문적이며 {audience_type}에게 적합한 어조 언어: 한국어 마무리 발표 노트 작성 규칙: 1. 발표 내용 핵심 요약 (2-3문장) 2. 결론 문구를 자연스럽게 활용 3. 청중에게 감사 인사 4. 질문 유도 또는 다음 단계 제안 5. 150-200자로 풍부하게 작성 6. 완전한 구어체로 작성 7. 지시문 제외, 오직 발표 내용만 예시: "오늘 우리가 함께 살펴본 디지털 트랜스포메이션의 핵심은 바로 '기술이 아닌 사람'이라는 점입니다. 시장 규모, 성공 사례, 그리고 실행 전략까지 다양한 관점에서 접근해 보았는데요. 제가 드리고 싶은 메시지는 명확합니다. '함께 만드는 미래' - 이것이 우리가 추구해야 할 방향입니다. 긴 시간 경청해 주셔서 감사합니다. 혹시 궁금하신 점이나 더 논의하고 싶은 부분이 있으시면 편하게 말씀해 주세요." """ else: system_prompt = f"""You are a professional presenter closing a presentation. Audience: {audience_type} - {audience_info.get('description', '')} Tone: Warm, professional, and appropriate for {audience_type} Language: English Create closing remarks that: 1. Briefly summarize the key takeaways (2-3 sentences) 2. Reference the conclusion phrase naturally 3. Thank the audience warmly 4. Invite questions or suggest next steps 5. Be 100-150 words with rich detail 6. Sound completely conversational 7. NO stage directions - only spoken words Example: "So, what we've explored today is truly transformative - from the 3.4 trillion dollar market opportunity to the real-world success stories of Amazon and Starbucks. The key takeaway? Digital transformation isn't about technology, it's about people and culture. That's why our theme 'Building Tomorrow Together' resonates so strongly. Your engagement and questions throughout have been fantastic, and I hope you're as excited about these possibilities as I am. Thank you for your time and attention today. I'd love to hear your thoughts - what aspects resonated most with you? Are there specific areas you'd like to explore further?" Write ONLY what the speaker would say.""" if language == "Korean": user_message = f"""주제: {topic} 청중: {audience_type} 화면의 결론 문구: {conclusion_phrase} 위 정보를 바탕으로 {audience_type} 청중을 위한 자연스럽고 따뜻한 마무리 발표 노트를 작성하세요. 핵심 내용을 요약하고, 결론 문구를 활용하여 강력한 마무리를 만들어주세요.""" else: user_message = f"""Topic: {topic} Audience: {audience_type} Conclusion phrase on screen: {conclusion_phrase} Create natural, warm closing speaker notes for {audience_type} that wrap up the presentation effectively. Summarize key points and use the conclusion phrase to create a powerful ending.""" payload = { "model": model_id, "messages": [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_message} ], "max_tokens": 400, "temperature": 0.8, "stream": False } try: response = requests.post(api_url, json=payload, headers=headers, timeout=30) if response.status_code == 200: result = response.json() notes = result['choices'][0]['message']['content'].strip() # 노트가 너무 짧으면 재시도 if len(notes) < 100: print(f"[Closing Notes] Notes too short ({len(notes)} chars), retrying...") retry_message = user_message + f"\n\n이전 응답이 너무 짧았습니다. 최소 150자 이상으로 풍부하게 작성해주세요." if language == "Korean" else "\n\nThe previous response was too short. Please provide at least 100 words with more detail." retry_payload = { "model": model_id, "messages": [ {"role": "system", "content": system_prompt}, {"role": "user", "content": retry_message} ], "max_tokens": 500, "temperature": 0.85, "stream": False } retry_response = requests.post(api_url, json=retry_payload, headers=headers, timeout=30) if retry_response.status_code == 200: retry_result = retry_response.json() notes = retry_result['choices'][0]['message']['content'].strip() return notes else: print(f"[Closing Notes] API Error: {response.status_code}") if language == "Korean": return f"오늘 우리가 함께 살펴본 {topic}의 핵심은 바로 변화와 혁신입니다. 시장의 기회부터 실행 전략까지 다양한 관점에서 접근해 보았는데요, 제가 드리고 싶은 메시지는 '{conclusion_phrase}'입니다. 이것이 우리가 함께 추구해야 할 방향이라고 생각합니다. 오늘 이 자리에 함께해 주신 모든 분들께 감사드립니다. 혹시 궁금하신 점이나 더 깊이 논의하고 싶은 부분이 있으시면 편하게 질문해 주세요." else: return f"What we've explored today about {topic} represents a significant opportunity for transformation. From market insights to execution strategies, we've covered the essential elements you need to succeed. The message I want to leave you with is simple yet powerful: '{conclusion_phrase}'. This encapsulates everything we've discussed today. Thank you all for your attention and engagement. I'm excited to hear your thoughts and answer any questions you might have. What resonated most with you from today's presentation?" except Exception as e: print(f"[Closing Notes] Error: {str(e)}") if language == "Korean": return f"오늘 우리가 함께 살펴본 {topic}의 핵심은 바로 변화와 혁신입니다. 시장의 기회부터 실행 전략까지 다양한 관점에서 접근해 보았는데요, 제가 드리고 싶은 메시지는 '{conclusion_phrase}'입니다. 이것이 우리가 함께 추구해야 할 방향이라고 생각합니다. 오늘 이 자리에 함께해 주신 모든 분들께 감사드립니다. 혹시 궁금하신 점이나 더 깊이 논의하고 싶은 부분이 있으시면 편하게 질문해 주세요." else: return f"What we've explored today about {topic} represents a significant opportunity for transformation. From market insights to execution strategies, we've covered the essential elements you need to succeed. The message I want to leave you with is simple yet powerful: '{conclusion_phrase}'. This encapsulates everything we've discussed today. Thank you all for your attention and engagement. I'm excited to hear your thoughts and answer any questions you might have. What resonated most with you from today's presentation?" def generate_conclusion_phrase(topic: str, audience_type: str, language: str = "English", friendli_token: str = None, api_url: str = None, model_id: str = None, audience_types: Dict = None) -> str: """Generate a powerful conclusion phrase""" print(f"[Conclusion Phrase] Generating...") audience_info = audience_types.get(audience_type, {}) if audience_types else {} headers = { "Authorization": f"Bearer {friendli_token}", "Content-Type": "application/json" } system_prompt = f"""You are a professional copywriter creating powerful closing statements for presentations. The audience is: {audience_type} - {audience_info.get('description', '')} Focus on: {audience_info.get('focus', '')} Language: {language} Create a concise, impactful closing phrase that: 1. Captures the essence of the presentation topic 2. Resonates with {audience_type} 3. Is memorable and inspirational 4. Maximum 5-7 words 5. Uses powerful, action-oriented language appropriate for {audience_type} 6. Leaves a lasting impression 7. Written in {language} Examples for different audiences: - For executives: "Excellence Through Strategic Innovation" / "전략적 혁신을 통한 탁월함" - For investors: "Maximum Returns, Minimal Risk" / "최대 수익, 최소 리스크" - For technical teams: "Code Today, Transform Tomorrow" / "오늘의 코드, 내일의 변화" - For customers: "Your Success, Our Mission" / "고객의 성공이 우리의 사명" Output only the phrase in {language}, no explanation.""" user_message = f"""Presentation topic: {topic} Target audience: {audience_type} Language: {language} Create a powerful closing phrase in {language} that encapsulates the main message for {audience_type}.""" payload = { "model": model_id, "messages": [ { "role": "system", "content": system_prompt }, { "role": "user", "content": user_message } ], "max_tokens": 50, "temperature": 0.9, "stream": False } try: response = requests.post(api_url, json=payload, headers=headers, timeout=30) if response.status_code == 200: result = response.json() phrase = result['choices'][0]['message']['content'].strip() return phrase else: if language == "Korean": return "함께 만드는 미래" else: return "Building Tomorrow Together" except Exception as e: print(f"[Conclusion Phrase] Error: {str(e)}") if language == "Korean": return "함께 만드는 미래" else: return "Building Tomorrow Together" def translate_to_english(text: str, friendli_token: str = None, api_url: str = None, model_id: str = None) -> str: """Translate Korean text to English""" if not any(ord('가') <= ord(char) <= ord('힣') for char in text): return text print(f"[Translation] Korean detected, translating to English") headers = { "Authorization": f"Bearer {friendli_token}", "Content-Type": "application/json" } payload = { "model": model_id, "messages": [ { "role": "system", "content": "You are a translator. Translate the given Korean text to English. Only return the translation without any explanation." }, { "role": "user", "content": text } ], "max_tokens": 500, "top_p": 0.8, "stream": False } try: response = requests.post(api_url, json=payload, headers=headers, timeout=30) if response.status_code == 200: result = response.json() translated = result['choices'][0]['message']['content'].strip() print(f"[Translation] Complete") return translated else: print(f"[Translation] Failed, using original") return text except Exception as e: print(f"[Translation] Error: {str(e)}, using original") return text def generate_prompt_with_llm(topic: str, style_example: str = None, slide_context: str = None, uploaded_content: str = None, friendli_token: str = None, api_url: str = None, model_id: str = None) -> str: """Generate image prompt using LLM""" print(f"[LLM] Generating prompt: {slide_context}") headers = { "Authorization": f"Bearer {friendli_token}", "Content-Type": "application/json" } system_prompt = """You are an expert image prompt engineer specializing in creating prompts for professional presentation slides. Your task is to create prompts that: 1. Are highly specific and visual, perfect for PPT backgrounds or main visuals 2. Consider the slide's purpose and maintain consistency across a presentation 3. Include style references matching the given example 4. Focus on clean, professional visuals that won't distract from text overlays 5. Ensure high contrast areas for text readability when needed 6. Maintain brand consistency and professional aesthetics Important guidelines: - If given a style example, adapt the topic to match that specific visual style - Consider the slide context to create appropriate visuals - Always output ONLY the prompt without any explanation - Keep prompts between 50-150 words for optimal results - Ensure the visual supports rather than overwhelms the slide content""" user_message = f"Topic: {topic}" if style_example: user_message += f"\n\nStyle reference to follow:\n{style_example}" if slide_context: user_message += f"\n\nSlide context: {slide_context}" if uploaded_content: user_message += f"\n\nAdditional context from document:\n{uploaded_content[:500]}" payload = { "model": model_id, "messages": [ { "role": "system", "content": system_prompt }, { "role": "user", "content": user_message } ], "max_tokens": 300, "top_p": 0.8, "temperature": 0.7, "stream": False } try: response = requests.post(api_url, json=payload, headers=headers, timeout=30) if response.status_code == 200: result = response.json() prompt = result['choices'][0]['message']['content'].strip() print(f"[LLM] Prompt generated: {prompt[:50]}...") return prompt else: error_msg = f"Prompt generation failed: {response.status_code}" print(f"[LLM] {error_msg}") return error_msg except Exception as e: error_msg = f"Prompt generation error: {str(e)}" print(f"[LLM] {error_msg}") return error_msg def generate_image(prompt: str, seed: int = 10, slide_info: str = "", replicate_token: str = None, current_topic: str = "", current_language: str = "English", friendli_token: str = None, api_url: str = None, model_id: str = None) -> Tuple[Image.Image, str]: """Generate image using Replicate API or process flow diagram""" print(f"\n[Image Generation] {slide_info}") print(f"[Image Generation] Prompt: {prompt[:50]}...") # Check if process flow diagram should be generated should_generate_process_flow = False # Extract slide title slide_title = "" if ":" in slide_info: parts = slide_info.split(":") if len(parts) >= 2: slide_title = parts[1].strip() print(f"[Image Generation] Extracted slide title: '{slide_title}'") # Process keywords for both English and Korean process_keywords_en = [ "process", "workflow", "flow", "procedure", "steps", "phases", "overview" ] process_keywords_kr = [ "프로세스", "작동", "플로우", "흐름", "워크플로우", "절차", "단계", "처리", "진행", "개요" ] # Check if workflow style is_workflow_style = False if any(style in prompt for style in ["Business Workflow", "Flowchart", "Business Process"]): is_workflow_style = True print(f"[Image Generation] Business Workflow or Flowchart style detected") # Check title for process keywords title_has_process = any(keyword in slide_title.lower() for keyword in process_keywords_en) or \ any(keyword in slide_title for keyword in process_keywords_kr) prompt_has_process = any(keyword in prompt.lower() for keyword in ["process", "flow", "workflow"]) print(f"[Image Generation] Title has process keyword: {title_has_process}") print(f"[Image Generation] Workflow style: {is_workflow_style}") print(f"[Image Generation] Prompt has process keyword: {prompt_has_process}") if title_has_process and (is_workflow_style or prompt_has_process): should_generate_process_flow = True print(f"[Image Generation] ✅ Process flow diagram conditions met!") # Special case: exclude table of contents if "Contents" in slide_title or "목차" in slide_title: should_generate_process_flow = False print(f"[Image Generation] Table of contents excluded from process flow") # Generate process flow diagram if PROCESS_FLOW_AVAILABLE and should_generate_process_flow: try: print("[Image Generation] 🔧 Generating process flow diagram...") # Use current language setting use_korean = (current_language == "Korean") img = generate_process_flow_for_ppt( topic=current_topic, context=slide_info, style="Business Workflow", use_korean=use_korean ) if isinstance(img, Image.Image): print("[Image Generation] ✅ Process flow diagram generated successfully!") return img, f"Process flow diagram generated with {current_language} support" else: print("[Image Generation] ❌ Process flow generation failed, falling back to regular image") except Exception as e: print(f"[Image Generation] ❌ Process flow generation error: {str(e)}") import traceback traceback.print_exc() else: if not PROCESS_FLOW_AVAILABLE: print("[Image Generation] ⚠️ Process flow generator not available") else: print("[Image Generation] ℹ️ Process flow conditions not met, generating regular image") # Generate regular image try: english_prompt = translate_to_english(prompt, friendli_token, api_url, model_id) if not replicate_token: error_msg = "RAPI_TOKEN environment variable not set." print(f"[Image Generation] Error: {error_msg}") return None, error_msg print(f"[Image Generation] Calling Replicate API...") client = replicate.Client(api_token=replicate_token) input_params = { "seed": seed, "prompt": english_prompt, "speed_mode": "Extra Juiced 🚀 (even more speed)", "output_quality": 100 } start_time = time.time() output = client.run( "prunaai/hidream-l1-fast:17c237d753218fed0ed477cb553902b6b75735f48c128537ab829096ef3d3645", input=input_params ) elapsed = time.time() - start_time print(f"[Image Generation] API response received ({elapsed:.1f}s)") if output: if isinstance(output, str) and output.startswith('http'): print(f"[Image Generation] Downloading image from URL...") response = requests.get(output, timeout=30) img = Image.open(BytesIO(response.content)) print(f"[Image Generation] Complete!") return img, english_prompt else: print(f"[Image Generation] Processing binary data...") img = Image.open(BytesIO(output.read())) print(f"[Image Generation] Complete!") return img, english_prompt else: error_msg = "Image generation failed - empty response" print(f"[Image Generation] {error_msg}") return None, error_msg except Exception as e: error_msg = f"Error: {str(e)}" print(f"[Image Generation] {error_msg}") print(f"[Image Generation] Detailed error:\n{traceback.format_exc()}") return None, error_msg def create_slide_preview_html(slide_data: Dict) -> str: """Create 16:9 ratio slide preview HTML""" # Encode image to base64 img_base64 = "" if slide_data.get("image"): buffered = BytesIO() slide_data["image"].save(buffered, format="PNG") img_base64 = base64.b64encode(buffered.getvalue()).decode() # Get text content subtitle = slide_data.get("subtitle", "") bullet_points = slide_data.get("bullet_points", []) # Generate HTML html = f"""
Slide {slide_data.get('slide_number', '')}: {slide_data.get('title', '')}
""" # Title and Thank You slides use full screen images if slide_data.get('title') in ['Cover', 'Thank You', '표지']: html += f"""
""" if img_base64: html += f""" Slide Image
""" if slide_data.get('title') in ['Cover', '표지']: html += f"""

{slide_data.get('topic', '')}

{subtitle}

""" else: # Thank You html += f"""

{subtitle}

""" html += """
""" html += """
""" else: # Regular slide layout html += f"""

{subtitle}

    """ for point in bullet_points: clean_point = point.replace('•', '').strip() html += f"""
  • {clean_point}
  • """ html += f"""
""" if img_base64: html += f""" Slide Image """ else: html += """
🖼️

Generating image...

""" html += """
""" html += """
""" return html def create_pptx_file(results: List[Dict], topic: str, template_name: str, theme_name: str = "Minimal Light", design_themes: Dict = None) -> str: """Create PPTX file with speaker notes""" print(f"[PPTX] Creating file... Theme: {theme_name}") print(f"[PPTX] Processing {len(results)} slides") # Helper function to convert color values to RGBColor def get_rgb_color(color_value): """Convert various color formats to RGBColor""" if isinstance(color_value, RGBColor): return color_value elif isinstance(color_value, tuple) and len(color_value) == 3: return RGBColor(color_value[0], color_value[1], color_value[2]) elif isinstance(color_value, str) and color_value.startswith('#'): # Convert hex to RGB hex_color = color_value.lstrip('#') r = int(hex_color[0:2], 16) g = int(hex_color[2:4], 16) b = int(hex_color[4:6], 16) return RGBColor(r, g, b) else: # Default color if conversion fails return RGBColor(128, 128, 128) try: # Create presentation (16:9 ratio) prs = Presentation() prs.slide_width = Inches(16) prs.slide_height = Inches(9) # Get selected theme with proper defaults default_theme = { "background": RGBColor(250, 250, 252), "title_color": RGBColor(33, 37, 41), "subtitle_color": RGBColor(52, 58, 64), "text_color": RGBColor(73, 80, 87), "accent_color": RGBColor(0, 123, 255), "box_fill": RGBColor(255, 255, 255), "box_opacity": 0.95, "shadow": True, "gradient": False } if design_themes and theme_name in design_themes: raw_theme = design_themes[theme_name] # Convert all color values to RGBColor objects theme = {} for key, value in raw_theme.items(): if key in ["background", "title_color", "subtitle_color", "text_color", "accent_color", "box_fill"]: theme[key] = get_rgb_color(value) else: theme[key] = value print(f"[PPTX] Using theme: {theme_name}") else: theme = default_theme print(f"[PPTX] Using default theme as {theme_name} not found") # Add slides slide_count = 0 for i, result in enumerate(results): if not result.get("success", False): print(f"[PPTX] Skipping slide {i+1} - not successful") continue slide_data = result.get("slide_data", {}) print(f"[PPTX] Adding slide {i+1}: {slide_data.get('title', 'Unknown')}") # Use blank layout blank_layout = prs.slide_layouts[6] slide = prs.slides.add_slide(blank_layout) slide_count += 1 # Title slide if slide_data.get('title') in ['Cover', '표지']: # Add background image if slide_data.get('image'): try: img_buffer = BytesIO() slide_data['image'].save(img_buffer, format='PNG') img_buffer.seek(0) # Full screen background image pic = slide.shapes.add_picture( img_buffer, 0, 0, width=prs.slide_width, height=prs.slide_height ) # Send to back slide.shapes._spTree.remove(pic._element) slide.shapes._spTree.insert(2, pic._element) print(f"[PPTX] Added title background image") except Exception as e: print(f"[PPTX] Failed to add title image: {str(e)}") # Title background box (semi-transparent) title_bg = slide.shapes.add_shape( MSO_SHAPE.ROUNDED_RECTANGLE, Inches(2), Inches(2.8), Inches(12), Inches(3.2) ) title_bg.fill.solid() title_bg.fill.fore_color.rgb = RGBColor(255, 255, 255) title_bg.fill.transparency = 0.8 title_bg.line.fill.background() # Shadow effect shadow = title_bg.shadow shadow.visible = True shadow.distance = Pt(6) shadow.size = 100 shadow.blur_radius = Pt(12) shadow.transparency = 0.8 shadow.angle = 45 # Title text title_box = slide.shapes.add_textbox( Inches(2), Inches(3.2), Inches(12), Inches(1.5) ) title_frame = title_box.text_frame title_frame.text = topic title_para = title_frame.paragraphs[0] title_para.font.size = Pt(48) title_para.font.bold = True title_para.font.color.rgb = RGBColor(0, 0, 0) title_para.alignment = PP_ALIGN.CENTER # Subtitle subtitle_box = slide.shapes.add_textbox( Inches(2), Inches(4.3), Inches(12), Inches(1.0) ) subtitle_frame = subtitle_box.text_frame subtitle_frame.text = slide_data.get('subtitle', f'{template_name} - AI Presentation') subtitle_para = subtitle_frame.paragraphs[0] subtitle_para.font.size = Pt(28) subtitle_para.font.color.rgb = RGBColor(33, 37, 41) subtitle_para.alignment = PP_ALIGN.CENTER # Thank You slide elif slide_data.get('title') == 'Thank You': # Add background image if slide_data.get('image'): try: img_buffer = BytesIO() slide_data['image'].save(img_buffer, format='PNG') img_buffer.seek(0) # Full screen background image pic = slide.shapes.add_picture( img_buffer, 0, 0, width=prs.slide_width, height=prs.slide_height ) # Send to back slide.shapes._spTree.remove(pic._element) slide.shapes._spTree.insert(2, pic._element) except Exception as e: print(f"[PPTX] Failed to add Thank You image: {str(e)}") # Thank You background box thanks_bg = slide.shapes.add_shape( MSO_SHAPE.ROUNDED_RECTANGLE, Inches(2), Inches(3.5), Inches(12), Inches(2.5) ) thanks_bg.fill.solid() thanks_bg.fill.fore_color.rgb = RGBColor(255, 255, 255) thanks_bg.fill.transparency = 0.8 thanks_bg.line.fill.background() # Shadow effect shadow = thanks_bg.shadow shadow.visible = True shadow.distance = Pt(6) shadow.size = 100 shadow.blur_radius = Pt(12) shadow.transparency = 0.8 shadow.angle = 45 # Thank You text (conclusion phrase) thanks_box = slide.shapes.add_textbox( Inches(2), Inches(4), Inches(12), Inches(1.5) ) thanks_frame = thanks_box.text_frame thanks_frame.text = slide_data.get('subtitle', 'Thank You') thanks_para = thanks_frame.paragraphs[0] thanks_para.font.size = Pt(42) thanks_para.font.bold = True thanks_para.font.color.rgb = RGBColor(0, 0, 0) thanks_para.alignment = PP_ALIGN.CENTER # Regular slides else: # Background color background = slide.background fill = background.fill fill.solid() fill.fore_color.rgb = get_rgb_color(theme.get("background", RGBColor(250, 250, 252))) # Slide title background box title_box_bg = slide.shapes.add_shape( MSO_SHAPE.ROUNDED_RECTANGLE, Inches(0.3), Inches(0.2), Inches(15.4), Inches(1.0) ) title_box_bg.fill.solid() title_box_bg.fill.fore_color.rgb = get_rgb_color(theme.get("box_fill", RGBColor(255, 255, 255))) title_box_bg.fill.transparency = 1 - theme.get("box_opacity", 0.95) # Shadow effect if theme.get("shadow", True): shadow = title_box_bg.shadow shadow.visible = True shadow.distance = Pt(4) shadow.size = 100 shadow.blur_radius = Pt(8) shadow.transparency = 0.75 shadow.angle = 45 title_box_bg.line.fill.background() # Slide title title_box = slide.shapes.add_textbox( Inches(0.5), Inches(0.3), Inches(15), Inches(0.8) ) title_frame = title_box.text_frame title_frame.text = f"{slide_data.get('title', '')}" title_para = title_frame.paragraphs[0] title_para.font.size = Pt(28) title_para.font.bold = True title_para.font.color.rgb = get_rgb_color(theme.get("title_color", RGBColor(33, 37, 41))) # Left text area background box text_box_bg = slide.shapes.add_shape( MSO_SHAPE.ROUNDED_RECTANGLE, Inches(0.3), Inches(1.4), Inches(7.8), Inches(6.8) ) text_box_bg.fill.solid() text_box_bg.fill.fore_color.rgb = get_rgb_color(theme.get("box_fill", RGBColor(255, 255, 255))) text_box_bg.fill.transparency = 1 - theme.get("box_opacity", 0.95) if theme.get("shadow", True): shadow = text_box_bg.shadow shadow.visible = True shadow.distance = Pt(5) shadow.size = 100 shadow.blur_radius = Pt(10) shadow.transparency = 0.7 shadow.angle = 45 text_box_bg.line.fill.background() # Left text area text_box = slide.shapes.add_textbox( Inches(0.8), Inches(1.8), Inches(7.0), Inches(6.0) ) text_frame = text_box.text_frame text_frame.word_wrap = True # Subtitle subtitle_para = text_frame.paragraphs[0] subtitle_para.text = slide_data.get('subtitle', '') subtitle_para.font.size = Pt(20) subtitle_para.font.bold = True subtitle_para.font.color.rgb = get_rgb_color(theme.get("subtitle_color", RGBColor(52, 58, 64))) subtitle_para.space_after = Pt(20) # Bullet points bullet_points = slide_data.get('bullet_points', []) for point in bullet_points: p = text_frame.add_paragraph() # Remove • and add text only (keep emojis) clean_text = point.replace('•', '').strip() p.text = clean_text p.font.size = Pt(16) p.font.color.rgb = get_rgb_color(theme.get("text_color", RGBColor(73, 80, 87))) p.level = 0 p.space_after = Pt(12) p.line_spacing = 1.5 p.left_indent = Pt(0) # Right image if slide_data.get('image'): try: img_buffer = BytesIO() slide_data['image'].save(img_buffer, format='PNG') img_buffer.seek(0) pic = slide.shapes.add_picture( img_buffer, Inches(8.5), Inches(1.6), width=Inches(6.8), height=Inches(6.4) ) pic.line.fill.background() except Exception as e: print(f"[PPTX] Failed to add image: {str(e)}") # Page number page_num = slide.shapes.add_textbox( Inches(15), Inches(8.5), Inches(1), Inches(0.5) ) page_frame = page_num.text_frame page_frame.text = str(i + 1) page_para = page_frame.paragraphs[0] page_para.font.size = Pt(12) page_para.font.color.rgb = get_rgb_color(theme.get("text_color", RGBColor(73, 80, 87))) page_para.alignment = PP_ALIGN.RIGHT # Add speaker notes notes_slide = slide.notes_slide notes_slide.notes_text_frame.text = slide_data.get('speaker_notes', '') print(f"[PPTX] Added speaker notes for slide {i+1}") print(f"[PPTX] Total slides added: {slide_count}") # Save file using tempfile for better compatibility import tempfile import os timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"presentation_{timestamp}.pptx" # Create a temporary file with tempfile.NamedTemporaryFile(mode='wb', suffix='.pptx', delete=False) as tmp_file: prs.save(tmp_file) temp_path = tmp_file.name print(f"[PPTX] File saved to temporary path: {temp_path}") # Verify file exists and has content if os.path.exists(temp_path): file_size = os.path.getsize(temp_path) print(f"[PPTX] File created successfully: {filename}") print(f"[PPTX] File size: {file_size} bytes") print(f"[PPTX] File path: {temp_path}") if file_size > 0: return temp_path else: print(f"[PPTX] ERROR: File created but has 0 bytes") os.remove(temp_path) return None else: print(f"[PPTX] ERROR: File was not created at {temp_path}") return None except Exception as e: print(f"[PPTX] CRITICAL ERROR during file creation: {str(e)}") import traceback traceback.print_exc() return None