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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import replicate
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import asyncio
|
7 |
+
import concurrent.futures
|
8 |
+
from io import BytesIO
|
9 |
+
from PIL import Image
|
10 |
+
from typing import List, Tuple, Dict
|
11 |
+
import zipfile
|
12 |
+
from datetime import datetime
|
13 |
+
import time
|
14 |
+
import traceback
|
15 |
+
|
16 |
+
# νκ²½ λ³μμμ ν ν° κ°μ Έμ€κΈ°
|
17 |
+
REPLICATE_API_TOKEN = os.getenv("RAPI_TOKEN")
|
18 |
+
FRIENDLI_TOKEN = os.getenv("FRIENDLI_TOKEN")
|
19 |
+
|
20 |
+
# μ€νμΌ μ μ
|
21 |
+
STYLE_TEMPLATES = {
|
22 |
+
"3D Style (Pixar-like)": {
|
23 |
+
"name": "3D Style",
|
24 |
+
"description": "Pixar-esque 3D render with volumetric lighting",
|
25 |
+
"use_case": "νμ§, λΉμ , λ―Έλ 컨μ
",
|
26 |
+
"example": "A fluffy ginger cat wearing a tiny spacesuit, floating amidst a vibrant nebula in a 3D render. The cat is gazing curiously at a swirling planet with rings made of candy. Background is filled with sparkling stars and colorful gas clouds, lit with soft, volumetric lighting. Style: Pixar-esque, highly detailed, playful. Colors: Deep blues, purples, oranges, and pinks. Rendered in Octane, 8k resolution."
|
27 |
+
},
|
28 |
+
"Elegant SWOT Quadrant": {
|
29 |
+
"name": "SWOT Analysis",
|
30 |
+
"description": "Flat-design 4-grid layout with minimal shadows",
|
31 |
+
"use_case": "νν© λΆμ, μ λ΅ νκ°",
|
32 |
+
"example": "Elegant SWOT quadrant: flat-design 4-grid on matte-white backdrop, thin pastel separators, top-left 'Strengths' panel shows glowing shield icon and subtle motif, top-right 'Weaknesses' panel with cracked chain icon in soft crimson, bottom-left 'Opportunities' panel with sunrise-over-horizon icon in optimistic teal, bottom-right 'Threats' panel with storm-cloud & lightning icon in deep indigo, minimal shadows, no text, no watermark, 16:9, 4K"
|
33 |
+
},
|
34 |
+
"Colorful Mind Map": {
|
35 |
+
"name": "Mind Map",
|
36 |
+
"description": "Hand-drawn educational style with vibrant colors",
|
37 |
+
"use_case": "λΈλ μΈμ€ν λ°, μμ΄λμ΄ μ 리",
|
38 |
+
"example": "A handrawn colorful mind map diagram: educational style, vibrant colors, clear hierarchy, golden ratio layout. Central concept with branching sub-topics, each branch with unique color coding, organic flowing connections, doodle-style icons for each node"
|
39 |
+
},
|
40 |
+
"Business Workflow": {
|
41 |
+
"name": "Business Process",
|
42 |
+
"description": "End-to-end business workflow with clear phases",
|
43 |
+
"use_case": "νλ‘μΈμ€ μ€λͺ
, λ¨κ³λ³ μ§ν",
|
44 |
+
"example": "A detailed hand-drawn diagram illustrating an end-to-end business workflow with Market Analysis, Strategy Development, Product Design, Implementation, and Post-Launch Review phases. Clear directional arrows, iconography for each component, vibrant educational yet professional style"
|
45 |
+
},
|
46 |
+
"Industrial Design": {
|
47 |
+
"name": "Product Design",
|
48 |
+
"description": "Sleek industrial design concept sketch",
|
49 |
+
"use_case": "μ ν μκ°, 컨μ
λμμΈ",
|
50 |
+
"example": "A sleek industrial design concept: Curved metallic body with minimal bezel, Touchscreen panel for settings, Modern matte black finish, Hand-drawn concept sketch style with annotations and dimension lines"
|
51 |
+
},
|
52 |
+
"3D Bubble Chart": {
|
53 |
+
"name": "Bubble Chart",
|
54 |
+
"description": "Clean 3D bubble visualization",
|
55 |
+
"use_case": "λΉκ΅ λΆμ, ν¬μ§μ
λ",
|
56 |
+
"example": "3-D bubble chart on clean white 2Γ2 grid, quadrant titles hidden, four translucent spheres in lime, azure, amber, magenta, gentle depth-of-field, modern consulting aesthetic, no text, 4K"
|
57 |
+
},
|
58 |
+
"Timeline Ribbon": {
|
59 |
+
"name": "Timeline",
|
60 |
+
"description": "Horizontal ribbon timeline with cyber-futuristic vibe",
|
61 |
+
"use_case": "μΌμ , λ‘λλ§΅, λ§μΌμ€ν€",
|
62 |
+
"example": "Horizontal ribbon timeline, milestone pins glowing hot pink on charcoal, year markers as circles, faint motion streaks, cyber-futuristic vibe, no text, 1920Γ1080"
|
63 |
+
},
|
64 |
+
"Risk Heat Map": {
|
65 |
+
"name": "Heat Map",
|
66 |
+
"description": "Risk assessment heat map with gradient colors",
|
67 |
+
"use_case": "리μ€ν¬ λΆμ, μ°μ μμ",
|
68 |
+
"example": "Risk Heat Map: square grid, smooth gradient from mint to fire-red, cells beveled, simple legend strip hidden, long subtle shadow, sterile white frame, no text"
|
69 |
+
},
|
70 |
+
"Pyramid/Funnel": {
|
71 |
+
"name": "Funnel Chart",
|
72 |
+
"description": "Multi-layer gradient funnel visualization",
|
73 |
+
"use_case": "λ¨κ³λ³ μΆμ, ν΅μ¬ λμΆ",
|
74 |
+
"example": "Pyramid / Funnel: 5-layer gradient funnel narrowing downwards, top vivid sky-blue, mid mint-green, bottom sunset-orange, glass reflection, minimal background, no text"
|
75 |
+
},
|
76 |
+
"KPI Dashboard": {
|
77 |
+
"name": "Dashboard",
|
78 |
+
"description": "Dark-mode analytics dashboard with sci-fi interface",
|
79 |
+
"use_case": "μ±κ³Ό μ§ν, μ€μ λμ보λ",
|
80 |
+
"example": "KPI Dashboard: Dark-mode analytic dashboard, three glass speedometers glowing neon lime, two sparkline charts under, black glass background, sci-fi interface, no text, 4K"
|
81 |
+
},
|
82 |
+
"Value Chain": {
|
83 |
+
"name": "Value Chain",
|
84 |
+
"description": "Horizontal value chain with industrial look",
|
85 |
+
"use_case": "κ°μΉ μ¬μ¬, λΉμ¦λμ€ λͺ¨λΈ",
|
86 |
+
"example": "Value Chain Diagram: Horizontal value chain blocks, steel-blue gradient bars with subtle bevel, small gear icons above each segment, sleek industrial look, shadow cast, no text"
|
87 |
+
},
|
88 |
+
"Gantt Chart": {
|
89 |
+
"name": "Gantt Chart",
|
90 |
+
"description": "Hand-drawn style Gantt chart with playful colors",
|
91 |
+
"use_case": "νλ‘μ νΈ μΌμ , μμ
κ΄λ¦¬",
|
92 |
+
"example": "Gantt Chart: Hand-drawn style Gantt bars sketched with vibrant markers on dotted grid notebook page, sticky-note color palette, playful yet organized, perspective tilt, no text"
|
93 |
+
},
|
94 |
+
"Mobile App Mockup": {
|
95 |
+
"name": "App Mockup",
|
96 |
+
"description": "Clean wireframe for mobile app design",
|
97 |
+
"use_case": "μ±/μΉ UI, νλ©΄ μ€κ³",
|
98 |
+
"example": "MOCKUP DESIGN: A clean hand-drawn style wireframe for a mobile app with Title screen, Login screen, Dashboard with sections, Bottom navigation bar, minimalist design with annotations"
|
99 |
+
},
|
100 |
+
"Flowchart": {
|
101 |
+
"name": "Flowchart",
|
102 |
+
"description": "Vibrant flowchart with minimalistic icons",
|
103 |
+
"use_case": "μμ¬κ²°μ , νλ‘μΈμ€ νλ¦",
|
104 |
+
"example": "FLOWCHART DESIGN: A hand-drawn style flowchart, vibrant colors, minimalistic icons showing process flow from START to END with decision points, branches, and clear directional arrows"
|
105 |
+
}
|
106 |
+
}
|
107 |
+
|
108 |
+
# PPT ν
νλ¦Ώ μ μ
|
109 |
+
PPT_TEMPLATES = {
|
110 |
+
"λΉμ¦λμ€ μ μμ": {
|
111 |
+
"description": "ν¬μ μ μΉ, μ¬μ
μ μμ©",
|
112 |
+
"slides": [
|
113 |
+
{"title": "νμ§", "style": "3D Style (Pixar-like)", "prompt_hint": "νμ¬ λΉμ κ³Ό λ―Έλ"},
|
114 |
+
{"title": "λͺ©μ°¨", "style": "Flowchart", "prompt_hint": "νλ μ ν
μ΄μ
ꡬ쑰"},
|
115 |
+
{"title": "λ¬Έμ μ μ", "style": "Colorful Mind Map", "prompt_hint": "νμ¬ μμ₯μ λ¬Έμ μ "},
|
116 |
+
{"title": "νν© λΆμ", "style": "Elegant SWOT Quadrant", "prompt_hint": "κ°μ , μ½μ , κΈ°ν, μν"},
|
117 |
+
{"title": "μ루μ
", "style": "Industrial Design", "prompt_hint": "μ ν/μλΉμ€ 컨μ
"},
|
118 |
+
{"title": "νλ‘μΈμ€", "style": "Business Workflow", "prompt_hint": "μ€ν λ¨κ³"},
|
119 |
+
{"title": "μΌμ ", "style": "Timeline Ribbon", "prompt_hint": "μ£Όμ λ§μΌμ€ν€"},
|
120 |
+
{"title": "μ±κ³Ό μμΈ‘", "style": "KPI Dashboard", "prompt_hint": "μμ μ±κ³Ό μ§ν"},
|
121 |
+
{"title": "ν¬μ μμ²", "style": "Pyramid/Funnel", "prompt_hint": "ν¬μ κ·λͺ¨μ νμ©"}
|
122 |
+
]
|
123 |
+
},
|
124 |
+
"μ ν μκ°": {
|
125 |
+
"description": "μ μ ν λ°μΉ, μλΉμ€ μκ°μ©",
|
126 |
+
"slides": [
|
127 |
+
{"title": "μ ν 컨μ
", "style": "Industrial Design", "prompt_hint": "μ ν λμμΈ"},
|
128 |
+
{"title": "μ¬μ©μ λμ¦", "style": "Colorful Mind Map", "prompt_hint": "κ³ κ° νμΈν¬μΈνΈ"},
|
129 |
+
{"title": "κΈ°λ₯ μκ°", "style": "Mobile App Mockup", "prompt_hint": "UI/UX νλ©΄"},
|
130 |
+
{"title": "μλ μ리", "style": "Flowchart", "prompt_hint": "κΈ°λ₯ νλ‘μ°"},
|
131 |
+
{"title": "μμ₯ ν¬μ§μ
", "style": "3D Bubble Chart", "prompt_hint": "κ²½μμ¬ λΉκ΅"},
|
132 |
+
{"title": "μΆμ μΌμ ", "style": "Timeline Ribbon", "prompt_hint": "λ°μΉ λ‘λλ§΅"}
|
133 |
+
]
|
134 |
+
},
|
135 |
+
"νλ‘μ νΈ λ³΄κ³ ": {
|
136 |
+
"description": "μ§ν μν©, μ±κ³Ό λ³΄κ³ μ©",
|
137 |
+
"slides": [
|
138 |
+
{"title": "νλ‘μ νΈ κ°μ", "style": "Business Workflow", "prompt_hint": "μ 체 νλ‘μΈμ€"},
|
139 |
+
{"title": "μ§ν νν©", "style": "Gantt Chart", "prompt_hint": "μμ
μΌμ "},
|
140 |
+
{"title": "리μ€ν¬ κ΄λ¦¬", "style": "Risk Heat Map", "prompt_hint": "μν μμ"},
|
141 |
+
{"title": "μ±κ³Ό μ§ν", "style": "KPI Dashboard", "prompt_hint": "λ¬μ± μ€μ "},
|
142 |
+
{"title": "ν₯ν κ³ν", "style": "Timeline Ribbon", "prompt_hint": "λ€μ λ¨κ³"}
|
143 |
+
]
|
144 |
+
},
|
145 |
+
"μ λ΅ κΈ°ν": {
|
146 |
+
"description": "μ€μ₯κΈ° μ λ΅, λΉμ μ립μ©",
|
147 |
+
"slides": [
|
148 |
+
{"title": "λΉμ ", "style": "3D Style (Pixar-like)", "prompt_hint": "λ―Έλ λΉμ "},
|
149 |
+
{"title": "νκ²½ λΆμ", "style": "Elegant SWOT Quadrant", "prompt_hint": "λ΄μΈλΆ νκ²½"},
|
150 |
+
{"title": "μ λ΅ μ²΄κ³", "style": "Colorful Mind Map", "prompt_hint": "μ λ΅ κ΅¬μ‘°"},
|
151 |
+
{"title": "κ°μΉ μ¬μ¬", "style": "Value Chain", "prompt_hint": "λΉμ¦λμ€ λͺ¨λΈ"},
|
152 |
+
{"title": "μ€ν λ‘λλ§΅", "style": "Timeline Ribbon", "prompt_hint": "λ¨κ³λ³ κ³ν"},
|
153 |
+
{"title": "λͺ©ν μ§ν", "style": "KPI Dashboard", "prompt_hint": "KPI λͺ©ν"}
|
154 |
+
]
|
155 |
+
},
|
156 |
+
"μ¬μ©μ μ μ": {
|
157 |
+
"description": "μ§μ ꡬμ±νκΈ°",
|
158 |
+
"slides": []
|
159 |
+
}
|
160 |
+
}
|
161 |
+
|
162 |
+
def generate_prompt_with_llm(topic: str, style_example: str = None, slide_context: str = None) -> str:
|
163 |
+
"""μ£Όμ μ μ€νμΌ μμ λ₯Ό λ°μμ LLMμ μ¬οΏ½οΏ½ν΄ μ΄λ―Έμ§ ν둬ννΈλ₯Ό μμ±"""
|
164 |
+
print(f"[LLM] ν둬ννΈ μμ± μμ: {slide_context}")
|
165 |
+
|
166 |
+
url = "https://api.friendli.ai/dedicated/v1/chat/completions"
|
167 |
+
headers = {
|
168 |
+
"Authorization": f"Bearer {FRIENDLI_TOKEN}",
|
169 |
+
"Content-Type": "application/json"
|
170 |
+
}
|
171 |
+
|
172 |
+
system_prompt = """You are an expert image prompt engineer specializing in creating prompts for professional presentation slides.
|
173 |
+
|
174 |
+
Your task is to create prompts that:
|
175 |
+
1. Are highly specific and visual, perfect for PPT backgrounds or main visuals
|
176 |
+
2. Consider the slide's purpose and maintain consistency across a presentation
|
177 |
+
3. Include style references matching the given example
|
178 |
+
4. Focus on clean, professional visuals that won't distract from text overlays
|
179 |
+
5. Ensure high contrast areas for text readability when needed
|
180 |
+
6. Maintain brand consistency and professional aesthetics
|
181 |
+
|
182 |
+
Important guidelines:
|
183 |
+
- If given a style example, adapt the topic to match that specific visual style
|
184 |
+
- Consider the slide context (e.g., "νμ§", "νν© λΆμ") to create appropriate visuals
|
185 |
+
- Always output ONLY the prompt without any explanation
|
186 |
+
- Keep prompts between 50-150 words for optimal results
|
187 |
+
- Ensure the visual supports rather than overwhelms the slide content"""
|
188 |
+
|
189 |
+
user_message = f"Topic: {topic}"
|
190 |
+
if style_example:
|
191 |
+
user_message += f"\n\nStyle reference to follow:\n{style_example}"
|
192 |
+
if slide_context:
|
193 |
+
user_message += f"\n\nSlide context: {slide_context}"
|
194 |
+
|
195 |
+
payload = {
|
196 |
+
"model": "dep89a2fld32mcm",
|
197 |
+
"messages": [
|
198 |
+
{
|
199 |
+
"role": "system",
|
200 |
+
"content": system_prompt
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"role": "user",
|
204 |
+
"content": user_message
|
205 |
+
}
|
206 |
+
],
|
207 |
+
"max_tokens": 300,
|
208 |
+
"top_p": 0.8,
|
209 |
+
"temperature": 0.7,
|
210 |
+
"stream": False
|
211 |
+
}
|
212 |
+
|
213 |
+
try:
|
214 |
+
response = requests.post(url, json=payload, headers=headers, timeout=30)
|
215 |
+
if response.status_code == 200:
|
216 |
+
result = response.json()
|
217 |
+
prompt = result['choices'][0]['message']['content'].strip()
|
218 |
+
print(f"[LLM] ν둬ννΈ μμ± μλ£: {prompt[:50]}...")
|
219 |
+
return prompt
|
220 |
+
else:
|
221 |
+
error_msg = f"ν둬ννΈ μμ± μ€ν¨: {response.status_code}"
|
222 |
+
print(f"[LLM] {error_msg}")
|
223 |
+
return error_msg
|
224 |
+
except Exception as e:
|
225 |
+
error_msg = f"ν둬ννΈ μμ± μ€ μ€λ₯ λ°μ: {str(e)}"
|
226 |
+
print(f"[LLM] {error_msg}")
|
227 |
+
return error_msg
|
228 |
+
|
229 |
+
def translate_to_english(text: str) -> str:
|
230 |
+
"""νκΈ ν
μ€νΈλ₯Ό μμ΄λ‘ λ²μ (LLM μ¬μ©)"""
|
231 |
+
if not any(ord('κ°') <= ord(char) <= ord('ν£') for char in text):
|
232 |
+
return text
|
233 |
+
|
234 |
+
print(f"[λ²μ] νκΈ κ°μ§, μμ΄λ‘ λ²μ μμ")
|
235 |
+
|
236 |
+
url = "https://api.friendli.ai/dedicated/v1/chat/completions"
|
237 |
+
headers = {
|
238 |
+
"Authorization": f"Bearer {FRIENDLI_TOKEN}",
|
239 |
+
"Content-Type": "application/json"
|
240 |
+
}
|
241 |
+
|
242 |
+
payload = {
|
243 |
+
"model": "dep89a2fld32mcm",
|
244 |
+
"messages": [
|
245 |
+
{
|
246 |
+
"role": "system",
|
247 |
+
"content": "You are a translator. Translate the given Korean text to English. Only return the translation without any explanation."
|
248 |
+
},
|
249 |
+
{
|
250 |
+
"role": "user",
|
251 |
+
"content": text
|
252 |
+
}
|
253 |
+
],
|
254 |
+
"max_tokens": 500,
|
255 |
+
"top_p": 0.8,
|
256 |
+
"stream": False
|
257 |
+
}
|
258 |
+
|
259 |
+
try:
|
260 |
+
response = requests.post(url, json=payload, headers=headers, timeout=30)
|
261 |
+
if response.status_code == 200:
|
262 |
+
result = response.json()
|
263 |
+
translated = result['choices'][0]['message']['content'].strip()
|
264 |
+
print(f"[λ²μ] μλ£")
|
265 |
+
return translated
|
266 |
+
else:
|
267 |
+
print(f"[λ²μ] μ€ν¨, μλ³Έ μ¬μ©")
|
268 |
+
return text
|
269 |
+
except Exception as e:
|
270 |
+
print(f"[λ²μ] μ€λ₯: {str(e)}, μλ³Έ μ¬μ©")
|
271 |
+
return text
|
272 |
+
|
273 |
+
def generate_image(prompt: str, seed: int = 10, slide_info: str = "") -> Tuple[Image.Image, str]:
|
274 |
+
"""Replicate APIλ₯Ό μ¬μ©ν΄ μ΄λ―Έμ§ μμ±"""
|
275 |
+
print(f"\n[μ΄λ―Έμ§ μμ±] {slide_info}")
|
276 |
+
print(f"[μ΄λ―Έμ§ μμ±] ν둬ννΈ: {prompt[:50]}...")
|
277 |
+
|
278 |
+
try:
|
279 |
+
english_prompt = translate_to_english(prompt)
|
280 |
+
|
281 |
+
if not REPLICATE_API_TOKEN:
|
282 |
+
error_msg = "RAPI_TOKEN νκ²½λ³μκ° μ€μ λμ§ μμμ΅λλ€."
|
283 |
+
print(f"[μ΄λ―Έμ§ μμ±] μ€λ₯: {error_msg}")
|
284 |
+
return None, error_msg
|
285 |
+
|
286 |
+
print(f"[μ΄λ―Έμ§ μμ±] Replicate API νΈμΆ μ€...")
|
287 |
+
client = replicate.Client(api_token=REPLICATE_API_TOKEN)
|
288 |
+
|
289 |
+
input_params = {
|
290 |
+
"seed": seed,
|
291 |
+
"prompt": english_prompt,
|
292 |
+
"speed_mode": "Extra Juiced π (even more speed)",
|
293 |
+
"output_quality": 80
|
294 |
+
}
|
295 |
+
|
296 |
+
start_time = time.time()
|
297 |
+
output = client.run(
|
298 |
+
"prunaai/hidream-l1-fast:17c237d753218fed0ed477cb553902b6b75735f48c128537ab829096ef3d3645",
|
299 |
+
input=input_params
|
300 |
+
)
|
301 |
+
|
302 |
+
elapsed = time.time() - start_time
|
303 |
+
print(f"[μ΄λ―Έμ§ μμ±] API μλ΅ λ°μ ({elapsed:.1f}μ΄)")
|
304 |
+
|
305 |
+
if output:
|
306 |
+
if isinstance(output, str) and output.startswith('http'):
|
307 |
+
print(f"[μ΄λ―Έμ§ μμ±] URLμμ μ΄λ―Έμ§ λ€μ΄λ‘λ μ€...")
|
308 |
+
response = requests.get(output, timeout=30)
|
309 |
+
img = Image.open(BytesIO(response.content))
|
310 |
+
print(f"[μ΄λ―Έμ§ μμ±] μλ£!")
|
311 |
+
return img, english_prompt
|
312 |
+
else:
|
313 |
+
print(f"[μ΄λ―Έμ§ μμ±] λ°μ΄λ리 λ°μ΄ν° μ²λ¦¬ μ€...")
|
314 |
+
img = Image.open(BytesIO(output.read()))
|
315 |
+
print(f"[μ΄λ―Έμ§ μμ±] μλ£!")
|
316 |
+
return img, english_prompt
|
317 |
+
else:
|
318 |
+
error_msg = "μ΄λ―Έμ§ μμ± μ€ν¨ - λΉ μλ΅"
|
319 |
+
print(f"[μ΄λ―Έμ§ μμ±] {error_msg}")
|
320 |
+
return None, error_msg
|
321 |
+
|
322 |
+
except Exception as e:
|
323 |
+
error_msg = f"μ€λ₯: {str(e)}"
|
324 |
+
print(f"[μ΄λ―Έμ§ μμ±] {error_msg}")
|
325 |
+
print(f"[μ΄λ―Έμ§ μμ±] μμΈ μ€λ₯:\n{traceback.format_exc()}")
|
326 |
+
return None, error_msg
|
327 |
+
|
328 |
+
def generate_ppt_images_sequential(topic: str, template_name: str, custom_slides: List[Dict], seed: int, progress=gr.Progress()):
|
329 |
+
"""PPT ν
νλ¦Ώμ λ°λΌ μ΄λ―Έμ§λ₯Ό μμ°¨μ μΌλ‘ μμ±νλ©° μ§ν μν© νμ"""
|
330 |
+
results = []
|
331 |
+
images_for_gallery = []
|
332 |
+
|
333 |
+
# ν
νλ¦Ώ μ ν
|
334 |
+
if template_name == "μ¬μ©μ μ μ" and custom_slides:
|
335 |
+
slides = custom_slides
|
336 |
+
else:
|
337 |
+
slides = PPT_TEMPLATES[template_name]["slides"]
|
338 |
+
|
339 |
+
if not slides:
|
340 |
+
yield [], "μ¬λΌμ΄λκ° μ μλμ§ μμμ΅λλ€."
|
341 |
+
return
|
342 |
+
|
343 |
+
total_slides = len(slides)
|
344 |
+
print(f"\n[PPT μμ±] μμ - μ΄ {total_slides}κ° μ¬λΌμ΄λ")
|
345 |
+
print(f"[PPT μμ±] μ£Όμ : {topic}")
|
346 |
+
print(f"[PPT μμ±] ν
νλ¦Ώ: {template_name}")
|
347 |
+
|
348 |
+
# κ° μ¬λΌμ΄λ μμ°¨ μ²λ¦¬
|
349 |
+
for i, slide in enumerate(slides):
|
350 |
+
progress((i + 1) / (total_slides + 1), f"μ¬λΌμ΄λ {i+1}/{total_slides} μ²λ¦¬ μ€...")
|
351 |
+
|
352 |
+
slide_info = f"μ¬λΌμ΄λ {i+1}: {slide['title']}"
|
353 |
+
status_msg = f"\n### π {slide_info} μμ± μ€...\n"
|
354 |
+
|
355 |
+
# νμ¬κΉμ§μ μν μ
λ°μ΄νΈ
|
356 |
+
yield images_for_gallery, status_msg + format_results_status(results)
|
357 |
+
|
358 |
+
style_key = slide["style"]
|
359 |
+
if style_key in STYLE_TEMPLATES:
|
360 |
+
style_info = STYLE_TEMPLATES[style_key]
|
361 |
+
slide_context = f"{slide['title']} - {slide.get('prompt_hint', '')}"
|
362 |
+
|
363 |
+
# ν둬ννΈ μμ±
|
364 |
+
prompt = generate_prompt_with_llm(topic, style_info["example"], slide_context)
|
365 |
+
|
366 |
+
# μ΄λ―Έμ§ μμ±
|
367 |
+
slide_seed = seed + i
|
368 |
+
img, used_prompt = generate_image(prompt, slide_seed, slide_info)
|
369 |
+
|
370 |
+
# κ²°κ³Ό μ μ₯
|
371 |
+
result = {
|
372 |
+
"slide_title": slide["title"],
|
373 |
+
"style": slide["style"],
|
374 |
+
"image": img,
|
375 |
+
"prompt": prompt,
|
376 |
+
"used_prompt": used_prompt,
|
377 |
+
"success": img is not None
|
378 |
+
}
|
379 |
+
results.append(result)
|
380 |
+
|
381 |
+
# μ±κ³΅ν μ΄λ―Έμ§λ κ°€λ¬λ¦¬μ μΆκ°
|
382 |
+
if img is not None:
|
383 |
+
caption = f"{i+1}. {slide['title']} ({style_info['name']})"
|
384 |
+
images_for_gallery.append((img, caption))
|
385 |
+
|
386 |
+
status_msg = f"\n### β
{slide_info} μλ£!\n"
|
387 |
+
else:
|
388 |
+
status_msg = f"\n### β {slide_info} μ€ν¨: {used_prompt}\n"
|
389 |
+
|
390 |
+
# μ¦μ μ
λ°μ΄νΈ
|
391 |
+
yield images_for_gallery, status_msg + format_results_status(results)
|
392 |
+
|
393 |
+
# μ΅μ’
κ²°κ³Ό
|
394 |
+
progress(1.0, "μλ£!")
|
395 |
+
successful = sum(1 for r in results if r["success"])
|
396 |
+
final_status = f"\n## π μμ± μλ£!\nμ΄ {total_slides}κ° μ¬λΌμ΄λ μ€ {successful}κ° μ±κ³΅\n\n"
|
397 |
+
final_status += format_results_status(results)
|
398 |
+
|
399 |
+
yield images_for_gallery, final_status
|
400 |
+
|
401 |
+
def format_results_status(results: List[Dict]) -> str:
|
402 |
+
"""κ²°κ³Όλ₯Ό μν λ©μμ§λ‘ ν¬λ§·"""
|
403 |
+
if not results:
|
404 |
+
return ""
|
405 |
+
|
406 |
+
status_lines = ["### π μμ± κ²°κ³Ό:\n"]
|
407 |
+
for i, result in enumerate(results):
|
408 |
+
if result["success"]:
|
409 |
+
status_lines.append(f"β
**μ¬λΌμ΄λ {i+1}: {result['slide_title']}**")
|
410 |
+
status_lines.append(f" - μ€νμΌ: {result['style'].split('(')[0].strip()}")
|
411 |
+
status_lines.append(f" - ν둬ννΈ: {result['prompt'][:60]}...\n")
|
412 |
+
else:
|
413 |
+
status_lines.append(f"β **μ¬λΌμ΄λ {i+1}: {result['slide_title']}** - μ€ν¨")
|
414 |
+
status_lines.append(f" - μ€λ₯: {result['used_prompt']}\n")
|
415 |
+
|
416 |
+
return "\n".join(status_lines)
|
417 |
+
|
418 |
+
def create_custom_slides_ui():
|
419 |
+
"""μ¬μ©μ μ μ μ¬λΌμ΄λ κ΅¬μ± UI"""
|
420 |
+
slides = []
|
421 |
+
for i in range(10):
|
422 |
+
with gr.Row():
|
423 |
+
with gr.Column(scale=2):
|
424 |
+
title = gr.Textbox(
|
425 |
+
label=f"μ¬λΌμ΄λ {i+1} μ λͺ©",
|
426 |
+
placeholder="μ: νμ§, λͺ©μ°¨, νν© λΆμ...",
|
427 |
+
visible=(i < 3)
|
428 |
+
)
|
429 |
+
with gr.Column(scale=3):
|
430 |
+
style = gr.Dropdown(
|
431 |
+
choices=list(STYLE_TEMPLATES.keys()),
|
432 |
+
label=f"μ€νμΌ μ ν",
|
433 |
+
visible=(i < 3)
|
434 |
+
)
|
435 |
+
with gr.Column(scale=3):
|
436 |
+
hint = gr.Textbox(
|
437 |
+
label=f"ν둬ννΈ ννΈ",
|
438 |
+
placeholder="μ΄ μ¬λΌμ΄λμμ νννκ³ μΆμ λ΄μ©",
|
439 |
+
visible=(i < 3)
|
440 |
+
)
|
441 |
+
slides.append({"title": title, "style": style, "hint": hint})
|
442 |
+
return slides
|
443 |
+
|
444 |
+
# Gradio μΈν°νμ΄μ€ μμ±
|
445 |
+
with gr.Blocks(title="PPT μ΄λ―Έμ§ μμ±κΈ°", theme=gr.themes.Soft()) as demo:
|
446 |
+
gr.Markdown("""
|
447 |
+
# π― AI κΈ°λ° PPT μ΄λ―Έμ§ μμ±κΈ°
|
448 |
+
|
449 |
+
### μ λ¬Έμ μΈ νλ μ ν
μ΄μ
μ μν λ§μΆ€ν μ΄λ―Έμ§λ₯Ό ν λ²μ μμ±νμΈμ!
|
450 |
+
""")
|
451 |
+
|
452 |
+
# API ν ν° μν νμΈ
|
453 |
+
if not REPLICATE_API_TOKEN:
|
454 |
+
gr.Markdown("β οΈ **κ²½κ³ **: RAPI_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
455 |
+
if not FRIENDLI_TOKEN:
|
456 |
+
gr.Markdown("β οΈ **κ²½κ³ **: FRIENDLI_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
457 |
+
|
458 |
+
with gr.Row():
|
459 |
+
with gr.Column(scale=1):
|
460 |
+
# κΈ°λ³Έ μ
λ ₯
|
461 |
+
topic_input = gr.Textbox(
|
462 |
+
label="νλ μ ν
μ΄μ
μ£Όμ ",
|
463 |
+
placeholder="μ: AI μ€ννΈμ
ν¬μ μ μΉ, μ μ ν λ°μΉ, λμ§νΈ μ ν μ λ΅",
|
464 |
+
lines=2
|
465 |
+
)
|
466 |
+
|
467 |
+
# PPT ν
νλ¦Ώ μ ν
|
468 |
+
template_select = gr.Dropdown(
|
469 |
+
choices=list(PPT_TEMPLATES.keys()),
|
470 |
+
label="PPT ν
νλ¦Ώ μ ν",
|
471 |
+
value="λΉμ¦λμ€ μ μμ",
|
472 |
+
info="λͺ©μ μ λ§λ ν
νλ¦Ώμ μ ννμΈμ"
|
473 |
+
)
|
474 |
+
|
475 |
+
# ν
νλ¦Ώ μ€λͺ
|
476 |
+
template_info = gr.Markdown()
|
477 |
+
|
478 |
+
# μλ κ°
|
479 |
+
seed_input = gr.Slider(
|
480 |
+
minimum=1,
|
481 |
+
maximum=100,
|
482 |
+
value=10,
|
483 |
+
step=1,
|
484 |
+
label="μλ κ°"
|
485 |
+
)
|
486 |
+
|
487 |
+
generate_btn = gr.Button("π PPT μ΄λ―Έμ§ μΈνΈ μμ±", variant="primary", size="lg")
|
488 |
+
|
489 |
+
# μ¬μ©μ μ μ μΉμ
|
490 |
+
with gr.Accordion("π μ¬μ©μ μ μ μ¬λΌμ΄λ ꡬμ±", open=False) as custom_accordion:
|
491 |
+
gr.Markdown("ν
νλ¦Ώμ μ¬μ©νμ§ μκ³ μ§μ μ¬λΌμ΄λλ₯Ό ꡬμ±νμΈμ.")
|
492 |
+
custom_slides_components = create_custom_slides_ui()
|
493 |
+
|
494 |
+
with gr.Column(scale=2):
|
495 |
+
# μ§ν μν© νμ
|
496 |
+
status_output = gr.Markdown(
|
497 |
+
value="### π ν
νλ¦Ώμ μ ννκ³ μμ± λ²νΌμ ν΄λ¦νμΈμ!\n\nμμ± μ§ν μν©μ΄ μ€μκ°μΌλ‘ νμλ©λλ€."
|
498 |
+
)
|
499 |
+
|
500 |
+
# κ²°κ³Ό κ°€λ¬λ¦¬
|
501 |
+
output_gallery = gr.Gallery(
|
502 |
+
label="μμ±λ PPT μ΄λ―Έμ§",
|
503 |
+
show_label=True,
|
504 |
+
elem_id="gallery",
|
505 |
+
columns=3,
|
506 |
+
rows=3,
|
507 |
+
object_fit="contain",
|
508 |
+
height="auto"
|
509 |
+
)
|
510 |
+
|
511 |
+
# μ€νμΌ κ°μ΄λ
|
512 |
+
with gr.Accordion("π μ€νμΌλ³ νμ© κ°μ΄λ", open=False):
|
513 |
+
style_guide = "### PPT μ μμ μν μ€νμΌ κ°μ΄λ\n\n"
|
514 |
+
for style_name, style_info in STYLE_TEMPLATES.items():
|
515 |
+
style_guide += f"**{style_name}**\n"
|
516 |
+
style_guide += f"- μ©λ: {style_info['use_case']}\n"
|
517 |
+
style_guide += f"- νΉμ§: {style_info['description']}\n\n"
|
518 |
+
gr.Markdown(style_guide)
|
519 |
+
|
520 |
+
# νμ© ν
|
521 |
+
gr.Markdown("""
|
522 |
+
---
|
523 |
+
### π‘ PPT μ μ ν:
|
524 |
+
|
525 |
+
1. **ν
νλ¦Ώ νμ©**: λͺ©μ μ λ§λ ν
νλ¦Ώμ μ ννλ©΄ μ΅μ νλ μ¬λΌμ΄λ ꡬμ±μ μ 곡ν©λλ€
|
526 |
+
2. **μΌκ΄μ±**: νλμ μ£Όμ λ‘ μ 체 μ¬λΌμ΄λλ₯Ό μμ±νμ¬ μκ°μ μΌκ΄μ±μ μ μ§ν©λλ€
|
527 |
+
3. **μ€μκ° νμΈ**: κ° μ¬λΌμ΄λκ° μμ±λ λλ§λ€ μ§ν μν©μ νμΈν μ μμ΅λλ€
|
528 |
+
4. **κ³ νμ§**: λͺ¨λ μ΄λ―Έμ§λ νλ μ ν
μ΄μ
μ μ ν©ν κ³ ν΄μλλ‘ μμ±λ©λλ€
|
529 |
+
""")
|
530 |
+
|
531 |
+
# μ΄λ²€νΈ νΈλ€λ¬
|
532 |
+
def update_template_info(template_name):
|
533 |
+
if template_name in PPT_TEMPLATES:
|
534 |
+
template = PPT_TEMPLATES[template_name]
|
535 |
+
info = f"**{template['description']}**\n\nν¬ν¨λ μ¬λΌμ΄λ:\n"
|
536 |
+
for i, slide in enumerate(template['slides']):
|
537 |
+
info += f"{i+1}. {slide['title']} - {STYLE_TEMPLATES[slide['style']]['use_case']}\n"
|
538 |
+
return info
|
539 |
+
return ""
|
540 |
+
|
541 |
+
def generate_ppt_images_handler(topic, template_name, seed, progress=gr.Progress(), *custom_inputs):
|
542 |
+
if not topic.strip():
|
543 |
+
yield [], "β μ£Όμ λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ."
|
544 |
+
return
|
545 |
+
|
546 |
+
# μ¬μ©μ μ μ μ¬λΌμ΄λ μ²λ¦¬
|
547 |
+
custom_slides = []
|
548 |
+
if template_name == "μ¬μ©μ μ μ":
|
549 |
+
for i in range(0, len(custom_inputs), 3):
|
550 |
+
title = custom_inputs[i]
|
551 |
+
style = custom_inputs[i+1] if i+1 < len(custom_inputs) else None
|
552 |
+
hint = custom_inputs[i+2] if i+2 < len(custom_inputs) else ""
|
553 |
+
|
554 |
+
if title and style:
|
555 |
+
custom_slides.append({
|
556 |
+
"title": title,
|
557 |
+
"style": style,
|
558 |
+
"prompt_hint": hint
|
559 |
+
})
|
560 |
+
|
561 |
+
# μμ°¨μ μΌλ‘ μ΄λ―Έμ§ μμ±νλ©° μ§ν μν© νμ
|
562 |
+
for gallery, status in generate_ppt_images_sequential(topic, template_name, custom_slides, seed, progress):
|
563 |
+
yield gallery, status
|
564 |
+
|
565 |
+
# μ΄λ²€νΈ μ°κ²°
|
566 |
+
template_select.change(
|
567 |
+
fn=update_template_info,
|
568 |
+
inputs=[template_select],
|
569 |
+
outputs=[template_info]
|
570 |
+
)
|
571 |
+
|
572 |
+
# μ¬μ©μ μ μ μ
λ ₯ μμ§
|
573 |
+
all_custom_inputs = []
|
574 |
+
for slide_components in custom_slides_components:
|
575 |
+
all_custom_inputs.extend([
|
576 |
+
slide_components["title"],
|
577 |
+
slide_components["style"],
|
578 |
+
slide_components["hint"]
|
579 |
+
])
|
580 |
+
|
581 |
+
generate_btn.click(
|
582 |
+
fn=generate_ppt_images_handler,
|
583 |
+
inputs=[topic_input, template_select, seed_input] + all_custom_inputs,
|
584 |
+
outputs=[output_gallery, status_output]
|
585 |
+
)
|
586 |
+
|
587 |
+
# μ΄κΈ° ν
νλ¦Ώ μ 보 νμ
|
588 |
+
demo.load(
|
589 |
+
fn=update_template_info,
|
590 |
+
inputs=[template_select],
|
591 |
+
outputs=[template_info]
|
592 |
+
)
|
593 |
+
|
594 |
+
# μ± μ€ν
|
595 |
+
if __name__ == "__main__":
|
596 |
+
print("\n" + "="*50)
|
597 |
+
print("π PPT μ΄λ―Έμ§ μμ±κΈ° μμ!")
|
598 |
+
print("="*50)
|
599 |
+
|
600 |
+
# νκ²½ λ³μ νμΈ
|
601 |
+
if not REPLICATE_API_TOKEN:
|
602 |
+
print("β οΈ κ²½κ³ : RAPI_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
603 |
+
else:
|
604 |
+
print("β
RAPI_TOKEN νμΈλ¨")
|
605 |
+
|
606 |
+
if not FRIENDLI_TOKEN:
|
607 |
+
print("β οΈ κ²½κ³ : FRIENDLI_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
608 |
+
else:
|
609 |
+
print("β
FRIENDLI_TOKEN νμΈλ¨")
|
610 |
+
|
611 |
+
print("="*50 + "\n")
|
612 |
+
|
613 |
+
demo.launch()
|