Open-GAMMA / content_utils.py
openfree's picture
Update content_utils.py
706ad74 verified
# 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