ginipick commited on
Commit
a9ea17e
·
verified ·
1 Parent(s): 8d9cd4b

Delete app-BACKUP-LAST.py

Browse files
Files changed (1) hide show
  1. app-BACKUP-LAST.py +0 -1004
app-BACKUP-LAST.py DELETED
@@ -1,1004 +0,0 @@
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
- import base64
16
- from pptx import Presentation
17
- from pptx.util import Inches, Pt
18
- from pptx.dml.color import RGBColor
19
- from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
20
-
21
- # 환경 변수에서 토큰 가져오기
22
- REPLICATE_API_TOKEN = os.getenv("RAPI_TOKEN")
23
- FRIENDLI_TOKEN = os.getenv("FRIENDLI_TOKEN")
24
-
25
- # 스타일 정의
26
- STYLE_TEMPLATES = {
27
- "3D Style (Pixar-like)": {
28
- "name": "3D Style",
29
- "description": "Pixar-esque 3D render with volumetric lighting",
30
- "use_case": "표지, 비전, 미래 컨셉",
31
- "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."
32
- },
33
- "Elegant SWOT Quadrant": {
34
- "name": "SWOT Analysis",
35
- "description": "Flat-design 4-grid layout with minimal shadows",
36
- "use_case": "현황 분석, 전략 평가",
37
- "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"
38
- },
39
- "Colorful Mind Map": {
40
- "name": "Mind Map",
41
- "description": "Hand-drawn educational style with vibrant colors",
42
- "use_case": "브레인스토밍, 아이디어 정리",
43
- "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"
44
- },
45
- "Business Workflow": {
46
- "name": "Business Process",
47
- "description": "End-to-end business workflow with clear phases",
48
- "use_case": "프로세스 설명, 단계별 진행",
49
- "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"
50
- },
51
- "Industrial Design": {
52
- "name": "Product Design",
53
- "description": "Sleek industrial design concept sketch",
54
- "use_case": "제품 소개, 컨셉 디자인",
55
- "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"
56
- },
57
- "3D Bubble Chart": {
58
- "name": "Bubble Chart",
59
- "description": "Clean 3D bubble visualization",
60
- "use_case": "비교 분석, 포지셔닝",
61
- "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"
62
- },
63
- "Timeline Ribbon": {
64
- "name": "Timeline",
65
- "description": "Horizontal ribbon timeline with cyber-futuristic vibe",
66
- "use_case": "일정, 로드맵, 마일스톤",
67
- "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"
68
- },
69
- "Risk Heat Map": {
70
- "name": "Heat Map",
71
- "description": "Risk assessment heat map with gradient colors",
72
- "use_case": "리스크 분석, 우선순위",
73
- "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"
74
- },
75
- "Pyramid/Funnel": {
76
- "name": "Funnel Chart",
77
- "description": "Multi-layer gradient funnel visualization",
78
- "use_case": "단계별 축소, 핵심 도출",
79
- "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"
80
- },
81
- "KPI Dashboard": {
82
- "name": "Dashboard",
83
- "description": "Dark-mode analytics dashboard with sci-fi interface",
84
- "use_case": "성과 지표, 실적 대시보드",
85
- "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"
86
- },
87
- "Value Chain": {
88
- "name": "Value Chain",
89
- "description": "Horizontal value chain with industrial look",
90
- "use_case": "가치 사슬, 비즈니스 모델",
91
- "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"
92
- },
93
- "Gantt Chart": {
94
- "name": "Gantt Chart",
95
- "description": "Hand-drawn style Gantt chart with playful colors",
96
- "use_case": "프로젝트 일정, 작업 관리",
97
- "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"
98
- },
99
- "Mobile App Mockup": {
100
- "name": "App Mockup",
101
- "description": "Clean wireframe for mobile app design",
102
- "use_case": "앱/웹 UI, 화면 설계",
103
- "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"
104
- },
105
- "Flowchart": {
106
- "name": "Flowchart",
107
- "description": "Vibrant flowchart with minimalistic icons",
108
- "use_case": "의사결정, 프로세스 흐름",
109
- "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"
110
- }
111
- }
112
-
113
- # PPT 템플릿 정의
114
- PPT_TEMPLATES = {
115
- "비즈니스 제안서": {
116
- "description": "투자 유치, 사업 제안용",
117
- "slides": [
118
- {"title": "표지", "style": "3D Style (Pixar-like)", "prompt_hint": "회사 비전과 미래"},
119
- {"title": "목차", "style": "Flowchart", "prompt_hint": "프레젠테이션 구조"},
120
- {"title": "문제 정의", "style": "Colorful Mind Map", "prompt_hint": "현재 시장의 문제점"},
121
- {"title": "현황 분석", "style": "Elegant SWOT Quadrant", "prompt_hint": "강점, 약점, 기회, 위협"},
122
- {"title": "솔루션", "style": "Industrial Design", "prompt_hint": "제품/서비스 컨셉"},
123
- {"title": "프로세스", "style": "Business Workflow", "prompt_hint": "실행 단계"},
124
- {"title": "일정", "style": "Timeline Ribbon", "prompt_hint": "주요 마일스톤"},
125
- {"title": "성과 예측", "style": "KPI Dashboard", "prompt_hint": "예상 성과 지표"},
126
- {"title": "투자 요청", "style": "Pyramid/Funnel", "prompt_hint": "투자 규모와 활용"}
127
- ]
128
- },
129
- "제품 소개": {
130
- "description": "신제품 런칭, 서비스 소개용",
131
- "slides": [
132
- {"title": "제품 컨셉", "style": "Industrial Design", "prompt_hint": "제품 디자인"},
133
- {"title": "사용자 니즈", "style": "Colorful Mind Map", "prompt_hint": "고객 페인포인트"},
134
- {"title": "기능 소개", "style": "Mobile App Mockup", "prompt_hint": "UI/UX 화면"},
135
- {"title": "작동 원리", "style": "Flowchart", "prompt_hint": "기능 플로우"},
136
- {"title": "시장 포지션", "style": "3D Bubble Chart", "prompt_hint": "경쟁사 비교"},
137
- {"title": "출시 일정", "style": "Timeline Ribbon", "prompt_hint": "런칭 로드맵"}
138
- ]
139
- },
140
- "프로젝트 보고": {
141
- "description": "진행 상황, 성과 보고용",
142
- "slides": [
143
- {"title": "프로젝트 개요", "style": "Business Workflow", "prompt_hint": "전체 프로세스"},
144
- {"title": "진행 현황", "style": "Gantt Chart", "prompt_hint": "작업 일정"},
145
- {"title": "리스크 관리", "style": "Risk Heat Map", "prompt_hint": "위험 요소"},
146
- {"title": "성과 지표", "style": "KPI Dashboard", "prompt_hint": "달성 실적"},
147
- {"title": "향후 계획", "style": "Timeline Ribbon", "prompt_hint": "다음 단계"}
148
- ]
149
- },
150
- "전략 기획": {
151
- "description": "중장기 전략, 비전 수립용",
152
- "slides": [
153
- {"title": "비전", "style": "3D Style (Pixar-like)", "prompt_hint": "미래 비전"},
154
- {"title": "환경 분석", "style": "Elegant SWOT Quadrant", "prompt_hint": "내외부 환경"},
155
- {"title": "전략 체계", "style": "Colorful Mind Map", "prompt_hint": "전략 구조"},
156
- {"title": "가치 사슬", "style": "Value Chain", "prompt_hint": "비즈니스 모델"},
157
- {"title": "실행 로드맵", "style": "Timeline Ribbon", "prompt_hint": "단계별 계획"},
158
- {"title": "목표 지표", "style": "KPI Dashboard", "prompt_hint": "KPI 목표"}
159
- ]
160
- },
161
- "사용자 정의": {
162
- "description": "직접 구성하기",
163
- "slides": []
164
- }
165
- }
166
-
167
- def generate_slide_content(topic: str, slide_title: str, slide_context: str) -> Dict[str, str]:
168
- """각 슬라이드의 텍스트 내용 생성"""
169
- print(f"[슬라이드 내용] {slide_title} 텍스트 생성 중...")
170
-
171
- url = "https://api.friendli.ai/dedicated/v1/chat/completions"
172
- headers = {
173
- "Authorization": f"Bearer {FRIENDLI_TOKEN}",
174
- "Content-Type": "application/json"
175
- }
176
-
177
- system_prompt = """You are a professional presentation content writer specializing in creating concise, impactful slide content.
178
-
179
- Your task is to create:
180
- 1. A compelling subtitle (max 10 words)
181
- 2. Exactly 5 bullet points, each being a complete, concise sentence
182
- 3. Each bullet point should be 10-15 words
183
-
184
- Guidelines:
185
- - Be specific and actionable
186
- - Use professional business language
187
- - Include relevant data points or metrics when appropriate
188
- - Ensure content aligns with the slide's purpose
189
- - Make each point distinct and valuable
190
- - Use active voice and strong verbs
191
-
192
- Output format:
193
- Subtitle: [subtitle here]
194
- • [Point 1]
195
- • [Point 2]
196
- • [Point 3]
197
- • [Point 4]
198
- • [Point 5]"""
199
-
200
- user_message = f"""Topic: {topic}
201
- Slide Title: {slide_title}
202
- Context: {slide_context}
203
-
204
- Create compelling content for this presentation slide."""
205
-
206
- payload = {
207
- "model": "dep89a2fld32mcm",
208
- "messages": [
209
- {
210
- "role": "system",
211
- "content": system_prompt
212
- },
213
- {
214
- "role": "user",
215
- "content": user_message
216
- }
217
- ],
218
- "max_tokens": 300,
219
- "top_p": 0.8,
220
- "temperature": 0.7,
221
- "stream": False
222
- }
223
-
224
- try:
225
- response = requests.post(url, json=payload, headers=headers, timeout=30)
226
- if response.status_code == 200:
227
- result = response.json()
228
- content = result['choices'][0]['message']['content'].strip()
229
-
230
- # Parse content
231
- lines = content.split('\n')
232
- subtitle = ""
233
- bullet_points = []
234
-
235
- for line in lines:
236
- if line.startswith("Subtitle:"):
237
- subtitle = line.replace("Subtitle:", "").strip()
238
- elif line.strip().startswith("•"):
239
- bullet_points.append(line.strip())
240
-
241
- # 한글로 번역이 필요한 경우
242
- if any(ord('가') <= ord(char) <= ord('힣') for char in topic):
243
- subtitle = translate_content_to_korean(subtitle)
244
- bullet_points = [translate_content_to_korean(point) for point in bullet_points]
245
-
246
- return {
247
- "subtitle": subtitle,
248
- "bullet_points": bullet_points[:5] # 최대 5개
249
- }
250
- else:
251
- return {
252
- "subtitle": slide_title,
253
- "bullet_points": ["내용을 생성할 수 없습니다."] * 5
254
- }
255
- except Exception as e:
256
- print(f"[슬라이드 내용] 오류: {str(e)}")
257
- return {
258
- "subtitle": slide_title,
259
- "bullet_points": ["내용을 생성할 수 없습니다."] * 5
260
- }
261
-
262
- def translate_content_to_korean(text: str) -> str:
263
- """영어 텍스트를 한글로 번역"""
264
- url = "https://api.friendli.ai/dedicated/v1/chat/completions"
265
- headers = {
266
- "Authorization": f"Bearer {FRIENDLI_TOKEN}",
267
- "Content-Type": "application/json"
268
- }
269
-
270
- payload = {
271
- "model": "dep89a2fld32mcm",
272
- "messages": [
273
- {
274
- "role": "system",
275
- "content": "You are a translator. Translate the given English text to Korean. Maintain professional business tone. Only return the translation without any explanation."
276
- },
277
- {
278
- "role": "user",
279
- "content": text
280
- }
281
- ],
282
- "max_tokens": 200,
283
- "top_p": 0.8,
284
- "stream": False
285
- }
286
-
287
- try:
288
- response = requests.post(url, json=payload, headers=headers, timeout=30)
289
- if response.status_code == 200:
290
- result = response.json()
291
- return result['choices'][0]['message']['content'].strip()
292
- else:
293
- return text
294
- except Exception as e:
295
- return text
296
-
297
- def generate_prompt_with_llm(topic: str, style_example: str = None, slide_context: str = None) -> str:
298
- """주제와 스타일 예제를 받아서 LLM을 사용해 이미지 프롬프트를 생성"""
299
- print(f"[LLM] 프롬프트 생성 시작: {slide_context}")
300
-
301
- url = "https://api.friendli.ai/dedicated/v1/chat/completions"
302
- headers = {
303
- "Authorization": f"Bearer {FRIENDLI_TOKEN}",
304
- "Content-Type": "application/json"
305
- }
306
-
307
- system_prompt = """You are an expert image prompt engineer specializing in creating prompts for professional presentation slides.
308
-
309
- Your task is to create prompts that:
310
- 1. Are highly specific and visual, perfect for PPT backgrounds or main visuals
311
- 2. Consider the slide's purpose and maintain consistency across a presentation
312
- 3. Include style references matching the given example
313
- 4. Focus on clean, professional visuals that won't distract from text overlays
314
- 5. Ensure high contrast areas for text readability when needed
315
- 6. Maintain brand consistency and professional aesthetics
316
-
317
- Important guidelines:
318
- - If given a style example, adapt the topic to match that specific visual style
319
- - Consider the slide context (e.g., "표지", "현황 분석") to create appropriate visuals
320
- - Always output ONLY the prompt without any explanation
321
- - Keep prompts between 50-150 words for optimal results
322
- - Ensure the visual supports rather than overwhelms the slide content"""
323
-
324
- user_message = f"Topic: {topic}"
325
- if style_example:
326
- user_message += f"\n\nStyle reference to follow:\n{style_example}"
327
- if slide_context:
328
- user_message += f"\n\nSlide context: {slide_context}"
329
-
330
- payload = {
331
- "model": "dep89a2fld32mcm",
332
- "messages": [
333
- {
334
- "role": "system",
335
- "content": system_prompt
336
- },
337
- {
338
- "role": "user",
339
- "content": user_message
340
- }
341
- ],
342
- "max_tokens": 300,
343
- "top_p": 0.8,
344
- "temperature": 0.7,
345
- "stream": False
346
- }
347
-
348
- try:
349
- response = requests.post(url, json=payload, headers=headers, timeout=30)
350
- if response.status_code == 200:
351
- result = response.json()
352
- prompt = result['choices'][0]['message']['content'].strip()
353
- print(f"[LLM] 프롬프트 생성 완료: {prompt[:50]}...")
354
- return prompt
355
- else:
356
- error_msg = f"프롬프트 생성 실패: {response.status_code}"
357
- print(f"[LLM] {error_msg}")
358
- return error_msg
359
- except Exception as e:
360
- error_msg = f"프롬프트 생성 중 오류 발생: {str(e)}"
361
- print(f"[LLM] {error_msg}")
362
- return error_msg
363
-
364
- def translate_to_english(text: str) -> str:
365
- """한글 텍스트를 영어로 번역 (LLM 사용)"""
366
- if not any(ord('가') <= ord(char) <= ord('힣') for char in text):
367
- return text
368
-
369
- print(f"[번역] 한글 감지, 영어로 번역 시작")
370
-
371
- url = "https://api.friendli.ai/dedicated/v1/chat/completions"
372
- headers = {
373
- "Authorization": f"Bearer {FRIENDLI_TOKEN}",
374
- "Content-Type": "application/json"
375
- }
376
-
377
- payload = {
378
- "model": "dep89a2fld32mcm",
379
- "messages": [
380
- {
381
- "role": "system",
382
- "content": "You are a translator. Translate the given Korean text to English. Only return the translation without any explanation."
383
- },
384
- {
385
- "role": "user",
386
- "content": text
387
- }
388
- ],
389
- "max_tokens": 500,
390
- "top_p": 0.8,
391
- "stream": False
392
- }
393
-
394
- try:
395
- response = requests.post(url, json=payload, headers=headers, timeout=30)
396
- if response.status_code == 200:
397
- result = response.json()
398
- translated = result['choices'][0]['message']['content'].strip()
399
- print(f"[번역] 완료")
400
- return translated
401
- else:
402
- print(f"[번역] 실패, 원본 사용")
403
- return text
404
- except Exception as e:
405
- print(f"[번역] 오류: {str(e)}, 원본 사용")
406
- return text
407
-
408
- def generate_image(prompt: str, seed: int = 10, slide_info: str = "") -> Tuple[Image.Image, str]:
409
- """Replicate API를 사용해 이미지 생성"""
410
- print(f"\n[이미지 생성] {slide_info}")
411
- print(f"[이미지 생성] 프롬프트: {prompt[:50]}...")
412
-
413
- try:
414
- english_prompt = translate_to_english(prompt)
415
-
416
- if not REPLICATE_API_TOKEN:
417
- error_msg = "RAPI_TOKEN 환경변수가 설정되지 않았습니다."
418
- print(f"[이미지 생성] 오류: {error_msg}")
419
- return None, error_msg
420
-
421
- print(f"[이미지 생성] Replicate API 호출 중...")
422
- client = replicate.Client(api_token=REPLICATE_API_TOKEN)
423
-
424
- input_params = {
425
- "seed": seed,
426
- "prompt": english_prompt,
427
- "speed_mode": "Extra Juiced 🚀 (even more speed)",
428
- "output_quality": 100
429
- }
430
-
431
- start_time = time.time()
432
- output = client.run(
433
- "prunaai/hidream-l1-fast:17c237d753218fed0ed477cb553902b6b75735f48c128537ab829096ef3d3645",
434
- input=input_params
435
- )
436
-
437
- elapsed = time.time() - start_time
438
- print(f"[이미지 생성] API 응답 받음 ({elapsed:.1f}초)")
439
-
440
- if output:
441
- if isinstance(output, str) and output.startswith('http'):
442
- print(f"[이미지 생성] URL에서 이미지 다운로드 중...")
443
- response = requests.get(output, timeout=30)
444
- img = Image.open(BytesIO(response.content))
445
- print(f"[이미지 생성] 완료!")
446
- return img, english_prompt
447
- else:
448
- print(f"[이미지 생성] 바이너리 데이터 처리 중...")
449
- img = Image.open(BytesIO(output.read()))
450
- print(f"[이미지 생성] 완료!")
451
- return img, english_prompt
452
- else:
453
- error_msg = "이미지 생성 실패 - 빈 응답"
454
- print(f"[이미지 생성] {error_msg}")
455
- return None, error_msg
456
-
457
- except Exception as e:
458
- error_msg = f"오류: {str(e)}"
459
- print(f"[이미지 생성] {error_msg}")
460
- print(f"[이미지 생성] 상세 오류:\n{traceback.format_exc()}")
461
- return None, error_msg
462
-
463
- def create_slide_preview_html(slide_data: Dict) -> str:
464
- """16:9 비율의 슬라이드 프리뷰 HTML 생성"""
465
-
466
- # 이미지를 base64로 인코딩
467
- img_base64 = ""
468
- if slide_data.get("image"):
469
- buffered = BytesIO()
470
- slide_data["image"].save(buffered, format="PNG")
471
- img_base64 = base64.b64encode(buffered.getvalue()).decode()
472
-
473
- # 텍스트 내용 가져오기
474
- subtitle = slide_data.get("subtitle", "")
475
- bullet_points = slide_data.get("bullet_points", [])
476
-
477
- # HTML 생성
478
- html = f"""
479
- <div class="slide-container" style="
480
- width: 100%;
481
- max-width: 1200px;
482
- margin: 20px auto;
483
- background: white;
484
- border-radius: 8px;
485
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
486
- overflow: hidden;
487
- ">
488
- <div class="slide-header" style="
489
- background: #2c3e50;
490
- color: white;
491
- padding: 15px 30px;
492
- font-size: 18px;
493
- font-weight: bold;
494
- ">
495
- 슬라이드 {slide_data.get('slide_number', '')}: {slide_data.get('title', '')}
496
- </div>
497
-
498
- <div class="slide-content" style="
499
- display: flex;
500
- height: 0;
501
- padding-bottom: 56.25%; /* 16:9 비율 */
502
- position: relative;
503
- ">
504
- <div class="slide-inner" style="
505
- position: absolute;
506
- top: 0;
507
- left: 0;
508
- width: 100%;
509
- height: 100%;
510
- display: flex;
511
- ">
512
- <!-- 텍스트 영역 (좌측) -->
513
- <div class="text-area" style="
514
- flex: 1;
515
- padding: 40px;
516
- display: flex;
517
- flex-direction: column;
518
- justify-content: center;
519
- background: #f8f9fa;
520
- ">
521
- <h2 style="
522
- color: #2c3e50;
523
- font-size: 28px;
524
- margin-bottom: 30px;
525
- font-weight: 600;
526
- ">{subtitle}</h2>
527
-
528
- <ul style="
529
- list-style: none;
530
- padding: 0;
531
- margin: 0;
532
- ">
533
- """
534
-
535
- for point in bullet_points:
536
- html += f"""
537
- <li style="
538
- margin-bottom: 15px;
539
- padding-left: 25px;
540
- position: relative;
541
- color: #34495e;
542
- font-size: 16px;
543
- line-height: 1.6;
544
- ">
545
- <span style="
546
- position: absolute;
547
- left: 0;
548
- color: #3498db;
549
- ">▶</span>
550
- {point.replace('•', '').strip()}
551
- </li>
552
- """
553
-
554
- html += f"""
555
- </ul>
556
- </div>
557
-
558
- <!-- 이미지 영역 (우측) -->
559
- <div class="image-area" style="
560
- flex: 1;
561
- background: #e9ecef;
562
- display: flex;
563
- align-items: center;
564
- justify-content: center;
565
- padding: 20px;
566
- ">
567
- """
568
-
569
- if img_base64:
570
- html += f"""
571
- <img src="data:image/png;base64,{img_base64}" style="
572
- max-width: 100%;
573
- max-height: 100%;
574
- object-fit: contain;
575
- border-radius: 4px;
576
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
577
- " alt="Slide Image">
578
- """
579
- else:
580
- html += """
581
- <div style="
582
- color: #6c757d;
583
- text-align: center;
584
- ">
585
- <div style="font-size: 48px;">🖼️</div>
586
- <p>이미지 생성 중...</p>
587
- </div>
588
- """
589
-
590
- html += """
591
- </div>
592
- </div>
593
- </div>
594
- </div>
595
- """
596
-
597
- return html
598
-
599
- def create_pptx_file(results: List[Dict], topic: str, template_name: str) -> str:
600
- """생성된 결과를 PPTX 파일로 변환"""
601
- print("[PPTX] 파일 생성 시작...")
602
-
603
- # 프레젠테이션 생성 (16:9 비율)
604
- prs = Presentation()
605
- prs.slide_width = Inches(16)
606
- prs.slide_height = Inches(9)
607
-
608
- # 타이틀 슬라이드 레이아웃
609
- title_layout = prs.slide_layouts[0]
610
-
611
- # 표지 슬라이드 추가
612
- slide = prs.slides.add_slide(title_layout)
613
- title = slide.shapes.title
614
- subtitle = slide.placeholders[1]
615
-
616
- title.text = topic
617
- subtitle.text = f"{template_name} - AI로 생성된 프레젠테이션"
618
-
619
- # 각 결과 슬라이드 추가
620
- for i, result in enumerate(results):
621
- if not result.get("success", False):
622
- continue
623
-
624
- slide_data = result.get("slide_data", {})
625
-
626
- # 빈 레이아웃 사용
627
- blank_layout = prs.slide_layouts[5]
628
- slide = prs.slides.add_slide(blank_layout)
629
-
630
- # 슬라이드 제목 추가 (상단)
631
- title_box = slide.shapes.add_textbox(
632
- Inches(0.5), Inches(0.3),
633
- Inches(15), Inches(0.8)
634
- )
635
- title_frame = title_box.text_frame
636
- title_frame.text = f"{slide_data.get('title', '')}"
637
- title_para = title_frame.paragraphs[0]
638
- title_para.font.size = Pt(28)
639
- title_para.font.bold = True
640
- title_para.font.color.rgb = RGBColor(44, 62, 80) # 진한 파란색
641
-
642
- # 좌측 텍스트 영역
643
- text_box = slide.shapes.add_textbox(
644
- Inches(0.5), Inches(1.5),
645
- Inches(7.5), Inches(6.5)
646
- )
647
- text_frame = text_box.text_frame
648
- text_frame.word_wrap = True
649
-
650
- # 소제목 추가
651
- subtitle_para = text_frame.paragraphs[0]
652
- subtitle_para.text = slide_data.get('subtitle', '')
653
- subtitle_para.font.size = Pt(20)
654
- subtitle_para.font.bold = True
655
- subtitle_para.font.color.rgb = RGBColor(52, 73, 94)
656
- subtitle_para.space_after = Pt(20)
657
-
658
- # 불릿 포인트 추가
659
- bullet_points = slide_data.get('bullet_points', [])
660
- for point in bullet_points:
661
- p = text_frame.add_paragraph()
662
- p.text = point.replace('•', '').strip()
663
- p.font.size = Pt(16)
664
- p.font.color.rgb = RGBColor(52, 73, 94)
665
- p.level = 0
666
- p.space_after = Pt(12)
667
- p.line_spacing = 1.5
668
-
669
- # 우측 이미지 추가
670
- if slide_data.get('image'):
671
- try:
672
- # PIL 이미지를 BytesIO로 변환
673
- img_buffer = BytesIO()
674
- slide_data['image'].save(img_buffer, format='PNG')
675
- img_buffer.seek(0)
676
-
677
- # 이미지 추가 (우측)
678
- pic = slide.shapes.add_picture(
679
- img_buffer,
680
- Inches(8.5), Inches(1.5),
681
- width=Inches(7), height=Inches(6)
682
- )
683
-
684
- # 이미지 테두리 스타일
685
- pic.line.color.rgb = RGBColor(189, 195, 199)
686
- pic.line.width = Pt(1)
687
-
688
- except Exception as e:
689
- print(f"[PPTX] 이미지 추가 실패: {str(e)}")
690
-
691
- # 페이지 번호 추가
692
- page_num = slide.shapes.add_textbox(
693
- Inches(15), Inches(8.5),
694
- Inches(1), Inches(0.5)
695
- )
696
- page_frame = page_num.text_frame
697
- page_frame.text = str(i + 2) # 표지가 1페이지
698
- page_para = page_frame.paragraphs[0]
699
- page_para.font.size = Pt(12)
700
- page_para.font.color.rgb = RGBColor(127, 140, 141)
701
- page_para.alignment = PP_ALIGN.RIGHT
702
-
703
- # 파일 저장
704
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
705
- filename = f"presentation_{timestamp}.pptx"
706
- filepath = os.path.join("/tmp", filename)
707
- prs.save(filepath)
708
-
709
- print(f"[PPTX] 파일 생성 완료: {filename}")
710
- return filepath
711
-
712
- def generate_ppt_with_content(topic: str, template_name: str, custom_slides: List[Dict], seed: int, progress=gr.Progress()):
713
- """PPT 이미지와 텍스트 내용을 함께 생성"""
714
- results = []
715
- preview_html = ""
716
-
717
- # 템플릿 선택
718
- if template_name == "사용자 정의" and custom_slides:
719
- slides = custom_slides
720
- else:
721
- slides = PPT_TEMPLATES[template_name]["slides"]
722
-
723
- if not slides:
724
- yield "", "슬라이드가 정의되지 않았습니다.", None
725
- return
726
-
727
- total_slides = len(slides)
728
- print(f"\n[PPT 생성] 시작 - 총 {total_slides}개 슬라이드")
729
- print(f"[PPT 생성] 주제: {topic}")
730
- print(f"[PPT 생성] 템플릿: {template_name}")
731
-
732
- # CSS 스타일 ���가
733
- preview_html = """
734
- <style>
735
- .slides-container {
736
- width: 100%;
737
- max-width: 1400px;
738
- margin: 0 auto;
739
- }
740
- </style>
741
- <div class="slides-container">
742
- """
743
-
744
- # 각 슬라이드 순차 처리
745
- for i, slide in enumerate(slides):
746
- progress((i + 1) / (total_slides + 1), f"슬라이드 {i+1}/{total_slides} 처리 중...")
747
-
748
- slide_info = f"슬라이드 {i+1}: {slide['title']}"
749
-
750
- # 텍스트 내용 생성
751
- slide_context = f"{slide['title']} - {slide.get('prompt_hint', '')}"
752
- content = generate_slide_content(topic, slide['title'], slide_context)
753
-
754
- # 프롬프트 생성 및 이미지 생성
755
- style_key = slide["style"]
756
- if style_key in STYLE_TEMPLATES:
757
- style_info = STYLE_TEMPLATES[style_key]
758
- prompt = generate_prompt_with_llm(topic, style_info["example"], slide_context)
759
-
760
- # 이미지 생성
761
- slide_seed = seed + i
762
- img, used_prompt = generate_image(prompt, slide_seed, slide_info)
763
-
764
- # 슬라이드 데이터 구성
765
- slide_data = {
766
- "slide_number": i + 1,
767
- "title": slide["title"],
768
- "subtitle": content["subtitle"],
769
- "bullet_points": content["bullet_points"],
770
- "image": img,
771
- "style": style_info["name"]
772
- }
773
-
774
- # 프리뷰 HTML 생성
775
- preview_html += create_slide_preview_html(slide_data)
776
-
777
- # 현재까지의 상태 업데이트
778
- yield preview_html + "</div>", f"### 🔄 {slide_info} 생성 중...", None
779
-
780
- results.append({
781
- "slide_data": slide_data,
782
- "success": img is not None
783
- })
784
-
785
- # PPTX 파일 생성
786
- progress(0.95, "PPTX 파일 생성 중...")
787
- pptx_path = None
788
- try:
789
- pptx_path = create_pptx_file(results, topic, template_name)
790
- except Exception as e:
791
- print(f"[PPTX] 파일 생성 오류: {str(e)}")
792
-
793
- # 최종 결과
794
- preview_html += "</div>"
795
- progress(1.0, "완료!")
796
- successful = sum(1 for r in results if r["success"])
797
- final_status = f"### 🎉 생성 완료! 총 {total_slides}개 슬라이드 중 {successful}개 성공"
798
-
799
- if pptx_path:
800
- final_status += f"\n\n### 📥 PPTX 파일이 준비되었습니다! 아래에서 다운로드하세요."
801
-
802
- yield preview_html, final_status, pptx_path
803
-
804
- def create_custom_slides_ui():
805
- """사용자 정의 슬라이드 구성 UI"""
806
- slides = []
807
- for i in range(10):
808
- with gr.Row():
809
- with gr.Column(scale=2):
810
- title = gr.Textbox(
811
- label=f"슬라이드 {i+1} 제목",
812
- placeholder="예: 표지, 목차, 현황 분석...",
813
- visible=(i < 3)
814
- )
815
- with gr.Column(scale=3):
816
- style = gr.Dropdown(
817
- choices=list(STYLE_TEMPLATES.keys()),
818
- label=f"스타일 선택",
819
- visible=(i < 3)
820
- )
821
- with gr.Column(scale=3):
822
- hint = gr.Textbox(
823
- label=f"프롬프트 힌트",
824
- placeholder="이 슬라이드에서 표현하고 싶은 내용",
825
- visible=(i < 3)
826
- )
827
- slides.append({"title": title, "style": style, "hint": hint})
828
- return slides
829
-
830
- # Gradio 인터페이스 생성
831
- with gr.Blocks(title="PPT 이미지 생성기", theme=gr.themes.Soft(), css="""
832
- .preview-container { max-width: 1400px; margin: 0 auto; }
833
- """) as demo:
834
- gr.Markdown("""
835
- # 🎯 AI 기반 PPT 통합 생성기
836
-
837
- ### 텍스트와 이미지가 완벽하게 조화된 프레젠테이션을 자동으로 생성하고 다운로드하세요!
838
- """)
839
-
840
- # API 토큰 상태 확인
841
- if not REPLICATE_API_TOKEN:
842
- gr.Markdown("⚠️ **경고**: RAPI_TOKEN 환경 변수가 설정되지 않았습니다.")
843
- if not FRIENDLI_TOKEN:
844
- gr.Markdown("⚠️ **경고**: FRIENDLI_TOKEN 환경 변수가 설정되지 않았습니다.")
845
-
846
- with gr.Row():
847
- with gr.Column(scale=1):
848
- # 기본 입력
849
- topic_input = gr.Textbox(
850
- label="프레젠테이션 주제",
851
- placeholder="예: AI 스타트업 투자 유치, 신제품 런칭, 디지털 전환 전략",
852
- lines=2
853
- )
854
-
855
- # PPT 템플릿 선택
856
- template_select = gr.Dropdown(
857
- choices=list(PPT_TEMPLATES.keys()),
858
- label="PPT 템플릿 선택",
859
- value="비즈니스 제안서",
860
- info="목적에 맞는 템플릿을 선택하세요"
861
- )
862
-
863
- # 템플릿 설명
864
- template_info = gr.Markdown()
865
-
866
- # 시드 값
867
- seed_input = gr.Slider(
868
- minimum=1,
869
- maximum=100,
870
- value=10,
871
- step=1,
872
- label="시드 값"
873
- )
874
-
875
- generate_btn = gr.Button("🚀 PPT 전체 생성 (텍스트 + 이미지)", variant="primary", size="lg")
876
-
877
- # PPTX 다운로드 영역
878
- with gr.Row():
879
- download_file = gr.File(
880
- label="📥 생성된 PPTX 파일 다운로드",
881
- visible=True,
882
- elem_id="download-file"
883
- )
884
-
885
- # 사용자 정의 섹션
886
- with gr.Accordion("📝 사용자 정의 슬라이드 구성", open=False) as custom_accordion:
887
- gr.Markdown("템플릿을 사용하지 않고 직접 슬라이드를 구성하세요.")
888
- custom_slides_components = create_custom_slides_ui()
889
-
890
- # 상태 표시
891
- status_output = gr.Markdown(
892
- value="### 👆 템플릿을 선택하고 생성 버튼을 클릭하세요!"
893
- )
894
-
895
- # 프리뷰 영역
896
- preview_output = gr.HTML(
897
- label="PPT 프리뷰 (16:9)",
898
- elem_classes="preview-container"
899
- )
900
-
901
- # 활용 팁
902
- gr.Markdown("""
903
- ---
904
- ### 💡 새로운 기능:
905
-
906
- 1. **자동 텍스트 생성**: 각 슬라이드마다 적절한 제목과 5개의 핵심 포인트 자동 생성
907
- 2. **16:9 프리뷰**: 실제 PPT와 동일한 비율로 미리보기
908
- 3. **PPTX 다운로드**: 생성된 내용을 실제 PowerPoint 파일로 다운로드
909
- 4. **좌우 레이아웃**: 좌측 텍스트, 우측 이미지로 깔끔한 구성
910
-
911
- ### 📌 필요한 추가 패키지:
912
- ```bash
913
- pip install python-pptx
914
- ```
915
-
916
- ### 📊 활용 팁:
917
- - 다운로드한 PPTX 파일은 PowerPoint에서 바로 편집 가능
918
- - 폰트, 색상, 레이아웃을 추가로 커스터마이징 가능
919
- - 생성된 이미지는 고해상도로 PPT에 포함됨
920
- """)
921
-
922
- # 이벤트 핸들러
923
- def update_template_info(template_name):
924
- if template_name in PPT_TEMPLATES:
925
- template = PPT_TEMPLATES[template_name]
926
- info = f"**{template['description']}**\n\n포함된 슬라이드:\n"
927
- for i, slide in enumerate(template['slides']):
928
- info += f"{i+1}. {slide['title']} - {STYLE_TEMPLATES[slide['style']]['use_case']}\n"
929
- return info
930
- return ""
931
-
932
- def generate_ppt_handler(topic, template_name, seed, progress=gr.Progress(), *custom_inputs):
933
- if not topic.strip():
934
- yield "", "❌ 주제를 입력해주세요.", None
935
- return
936
-
937
- # 사용자 정의 슬라이드 처리
938
- custom_slides = []
939
- if template_name == "사용자 정의":
940
- for i in range(0, len(custom_inputs), 3):
941
- title = custom_inputs[i]
942
- style = custom_inputs[i+1] if i+1 < len(custom_inputs) else None
943
- hint = custom_inputs[i+2] if i+2 < len(custom_inputs) else ""
944
-
945
- if title and style:
946
- custom_slides.append({
947
- "title": title,
948
- "style": style,
949
- "prompt_hint": hint
950
- })
951
-
952
- # PPT 생성
953
- for preview, status, pptx_file in generate_ppt_with_content(topic, template_name, custom_slides, seed, progress):
954
- yield preview, status, pptx_file
955
-
956
- # 이벤트 연결
957
- template_select.change(
958
- fn=update_template_info,
959
- inputs=[template_select],
960
- outputs=[template_info]
961
- )
962
-
963
- # 사용자 정의 입력 수집
964
- all_custom_inputs = []
965
- for slide_components in custom_slides_components:
966
- all_custom_inputs.extend([
967
- slide_components["title"],
968
- slide_components["style"],
969
- slide_components["hint"]
970
- ])
971
-
972
- generate_btn.click(
973
- fn=generate_ppt_handler,
974
- inputs=[topic_input, template_select, seed_input] + all_custom_inputs,
975
- outputs=[preview_output, status_output, download_file]
976
- )
977
-
978
- # 초기 템플릿 정보 표시
979
- demo.load(
980
- fn=update_template_info,
981
- inputs=[template_select],
982
- outputs=[template_info]
983
- )
984
-
985
- # 앱 실행
986
- if __name__ == "__main__":
987
- print("\n" + "="*50)
988
- print("🚀 PPT 통합 생성기 시작!")
989
- print("="*50)
990
-
991
- # 환경 변수 확인
992
- if not REPLICATE_API_TOKEN:
993
- print("⚠️ 경고: RAPI_TOKEN 환경 변수가 설정되지 않았습니다.")
994
- else:
995
- print("✅ RAPI_TOKEN 확인됨")
996
-
997
- if not FRIENDLI_TOKEN:
998
- print("⚠️ 경고: FRIENDLI_TOKEN 환경 변수가 설정되지 않았습니다.")
999
- else:
1000
- print("✅ FRIENDLI_TOKEN 확인됨")
1001
-
1002
- print("="*50 + "\n")
1003
-
1004
- demo.launch()