Spaces:
Running
Running
# 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""" | |
<div class="slide-container" style=" | |
width: 100%; | |
max-width: 1200px; | |
margin: 20px auto; | |
background: white; | |
border-radius: 12px; | |
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1); | |
overflow: hidden; | |
"> | |
<div class="slide-header" style=" | |
background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%); | |
color: white; | |
padding: 15px 30px; | |
font-size: 18px; | |
font-weight: bold; | |
"> | |
Slide {slide_data.get('slide_number', '')}: {slide_data.get('title', '')} | |
</div> | |
<div class="slide-content" style=" | |
display: flex; | |
height: 0; | |
padding-bottom: 56.25%; /* 16:9 ratio */ | |
position: relative; | |
background: #fafbfc; | |
"> | |
<div class="slide-inner" style=" | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
display: flex; | |
padding: 20px; | |
gap: 20px; | |
"> | |
""" | |
# Title and Thank You slides use full screen images | |
if slide_data.get('title') in ['Cover', 'Thank You', 'ํ์ง']: | |
html += f""" | |
<!-- Full screen image --> | |
<div style=" | |
width: 100%; | |
height: 100%; | |
position: relative; | |
background: #e9ecef; | |
border-radius: 12px; | |
overflow: hidden; | |
"> | |
""" | |
if img_base64: | |
html += f""" | |
<img src="data:image/png;base64,{img_base64}" style=" | |
width: 100%; | |
height: 100%; | |
object-fit: cover; | |
" alt="Slide Image"> | |
<div style=" | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
text-align: center; | |
background: rgba(255, 255, 255, 0.9); | |
padding: 30px 60px; | |
border-radius: 20px; | |
box-shadow: 0 10px 40px rgba(0,0,0,0.3); | |
backdrop-filter: blur(10px); | |
"> | |
""" | |
if slide_data.get('title') in ['Cover', 'ํ์ง']: | |
html += f""" | |
<h1 style="font-size: 48px; margin-bottom: 10px; color: #000000; font-weight: 700;">{slide_data.get('topic', '')}</h1> | |
<h2 style="font-size: 24px; margin-top: 0; color: #212529; font-weight: 400;">{subtitle}</h2> | |
""" | |
else: # Thank You | |
html += f""" | |
<h1 style="font-size: 42px; color: #000000; font-weight: 700; line-height: 1.2;">{subtitle}</h1> | |
""" | |
html += """ | |
</div> | |
""" | |
html += """ | |
</div> | |
""" | |
else: | |
# Regular slide layout | |
html += f""" | |
<!-- Text area (left) --> | |
<div class="text-area" style=" | |
flex: 1; | |
padding: 30px; | |
display: flex; | |
flex-direction: column; | |
justify-content: center; | |
background: rgba(255, 255, 255, 0.95); | |
border-radius: 12px; | |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.08); | |
"> | |
<h2 style=" | |
color: #212529; | |
font-size: 28px; | |
margin-bottom: 30px; | |
font-weight: 600; | |
">{subtitle}</h2> | |
<ul style=" | |
list-style: none; | |
padding: 0; | |
margin: 0; | |
"> | |
""" | |
for point in bullet_points: | |
clean_point = point.replace('โข', '').strip() | |
html += f""" | |
<li style=" | |
margin-bottom: 16px; | |
padding-left: 28px; | |
position: relative; | |
color: #495057; | |
font-size: 16px; | |
line-height: 1.6; | |
"> | |
<span style=" | |
position: absolute; | |
left: 0; | |
color: #007bff; | |
font-size: 18px; | |
">โข</span> | |
{clean_point} | |
</li> | |
""" | |
html += f""" | |
</ul> | |
</div> | |
<!-- Image area (right) --> | |
<div class="image-area" style=" | |
flex: 1; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
padding: 20px; | |
"> | |
""" | |
if img_base64: | |
html += f""" | |
<img src="data:image/png;base64,{img_base64}" style=" | |
max-width: 100%; | |
max-height: 100%; | |
object-fit: contain; | |
border-radius: 8px; | |
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); | |
" alt="Slide Image"> | |
""" | |
else: | |
html += """ | |
<div style=" | |
color: #6c757d; | |
text-align: center; | |
"> | |
<div style="font-size: 48px;">๐ผ๏ธ</div> | |
<p>Generating image...</p> | |
</div> | |
""" | |
html += """ | |
</div> | |
""" | |
html += """ | |
</div> | |
</div> | |
</div> | |
""" | |
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 |