Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,238 +17,197 @@ from dataclasses import dataclass, field, asdict
|
|
| 17 |
from collections import defaultdict
|
| 18 |
import random
|
| 19 |
|
| 20 |
-
# ---
|
| 21 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
-
# ---
|
| 25 |
FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY", "")
|
| 26 |
BRAVE_SEARCH_API_KEY = os.getenv("BRAVE_SEARCH_API_KEY", "")
|
| 27 |
API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 28 |
MODEL_ID = "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507"
|
| 29 |
-
DB_PATH = "
|
| 30 |
|
| 31 |
-
#
|
| 32 |
SCREENPLAY_LENGTHS = {
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
}
|
| 38 |
|
| 39 |
-
#
|
| 40 |
if not FIREWORKS_API_KEY:
|
| 41 |
-
logger.error("FIREWORKS_API_KEY
|
| 42 |
FIREWORKS_API_KEY = "dummy_token_for_testing"
|
| 43 |
|
| 44 |
-
|
| 45 |
-
logger.warning("BRAVE_SEARCH_API_KEY not set. Web search features will be disabled.")
|
| 46 |
-
|
| 47 |
-
# --- Global variables ---
|
| 48 |
db_lock = threading.Lock()
|
| 49 |
|
| 50 |
-
#
|
| 51 |
GENRE_TEMPLATES = {
|
| 52 |
-
"
|
| 53 |
-
"pacing": "
|
| 54 |
-
"scene_length": "
|
| 55 |
"dialogue_ratio": 0.3,
|
| 56 |
-
"key_elements": ["
|
| 57 |
-
"structure_beats": ["
|
| 58 |
"scene_density": 1.2,
|
| 59 |
"action_description_ratio": 0.6
|
| 60 |
},
|
| 61 |
-
"
|
| 62 |
-
"pacing": "
|
| 63 |
-
"scene_length": "
|
| 64 |
"dialogue_ratio": 0.35,
|
| 65 |
-
"key_elements": ["
|
| 66 |
-
"structure_beats": ["
|
| 67 |
"scene_density": 1.1,
|
| 68 |
"action_description_ratio": 0.5
|
| 69 |
},
|
| 70 |
-
"
|
| 71 |
-
"pacing": "
|
| 72 |
-
"scene_length": "
|
| 73 |
"dialogue_ratio": 0.55,
|
| 74 |
-
"key_elements": ["
|
| 75 |
-
"structure_beats": ["
|
| 76 |
"scene_density": 0.9,
|
| 77 |
"action_description_ratio": 0.3
|
| 78 |
},
|
| 79 |
-
"
|
| 80 |
-
"pacing": "
|
| 81 |
-
"scene_length": "
|
| 82 |
"dialogue_ratio": 0.65,
|
| 83 |
-
"key_elements": ["
|
| 84 |
-
"structure_beats": ["
|
| 85 |
"scene_density": 1.0,
|
| 86 |
"action_description_ratio": 0.35
|
| 87 |
},
|
| 88 |
-
"
|
| 89 |
-
"pacing": "
|
| 90 |
-
"scene_length": "
|
| 91 |
"dialogue_ratio": 0.3,
|
| 92 |
-
"key_elements": ["
|
| 93 |
-
"structure_beats": ["
|
| 94 |
"scene_density": 1.0,
|
| 95 |
"action_description_ratio": 0.55
|
| 96 |
},
|
| 97 |
-
"
|
| 98 |
-
"pacing": "
|
| 99 |
-
"scene_length": "
|
| 100 |
"dialogue_ratio": 0.45,
|
| 101 |
-
"key_elements": ["
|
| 102 |
-
"structure_beats": ["
|
| 103 |
"scene_density": 0.95,
|
| 104 |
"action_description_ratio": 0.45
|
| 105 |
},
|
| 106 |
-
"
|
| 107 |
-
"pacing": "
|
| 108 |
-
"scene_length": "
|
| 109 |
"dialogue_ratio": 0.6,
|
| 110 |
-
"key_elements": ["
|
| 111 |
-
"structure_beats": ["
|
| 112 |
"scene_density": 0.85,
|
| 113 |
"action_description_ratio": 0.25
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
("
|
| 120 |
-
("
|
| 121 |
-
("
|
| 122 |
-
("
|
| 123 |
-
("
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
("
|
| 128 |
-
("
|
| 129 |
-
("
|
| 130 |
-
("
|
| 131 |
|
| 132 |
-
|
| 133 |
-
("
|
| 134 |
-
("
|
| 135 |
-
("
|
| 136 |
-
("screenwriter", "โ๏ธ Act 2A - Final Extended Version"),
|
| 137 |
|
| 138 |
-
|
| 139 |
-
("
|
| 140 |
-
("
|
| 141 |
-
("
|
| 142 |
-
("screenwriter", "โ๏ธ Act 2B - Final Extended Version"),
|
| 143 |
|
| 144 |
-
|
| 145 |
-
("
|
| 146 |
-
("
|
| 147 |
-
("
|
| 148 |
-
("screenwriter", "โ๏ธ Act 3 - Final Extended Version"),
|
| 149 |
|
| 150 |
-
("
|
| 151 |
-
("
|
| 152 |
]
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
SAVE_THE_CAT_BEATS = {
|
| 156 |
-
1: {"name": "Opening Image", "percentage": "0-1%"},
|
| 157 |
-
2: {"name": "Setup", "percentage": "1-10%"},
|
| 158 |
-
3: {"name": "Theme Stated", "percentage": "5%"},
|
| 159 |
-
4: {"name": "Catalyst", "percentage": "10%"},
|
| 160 |
-
5: {"name": "Debate", "percentage": "10-20%"},
|
| 161 |
-
6: {"name": "Break into Two", "percentage": "20%"},
|
| 162 |
-
7: {"name": "B Story", "percentage": "22%"},
|
| 163 |
-
8: {"name": "Fun and Games", "percentage": "20-50%"},
|
| 164 |
-
9: {"name": "Midpoint", "percentage": "50%"},
|
| 165 |
-
10: {"name": "Bad Guys Close In", "percentage": "50-75%"},
|
| 166 |
-
11: {"name": "All Is Lost", "percentage": "75%"},
|
| 167 |
-
12: {"name": "Dark Night of the Soul", "percentage": "75-80%"},
|
| 168 |
-
13: {"name": "Break into Three", "percentage": "80%"},
|
| 169 |
-
14: {"name": "Finale", "percentage": "80-99%"},
|
| 170 |
-
15: {"name": "Final Image", "percentage": "99-100%"}
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
# --- Data classes ---
|
| 174 |
-
@dataclass
|
| 175 |
-
class ScreenplayBible:
|
| 176 |
-
"""Enhanced screenplay bible"""
|
| 177 |
-
title: str = ""
|
| 178 |
-
logline: str = ""
|
| 179 |
-
genre: str = ""
|
| 180 |
-
subgenre: str = ""
|
| 181 |
-
tone: str = ""
|
| 182 |
-
themes: List[str] = field(default_factory=list)
|
| 183 |
-
protagonist: Dict[str, Any] = field(default_factory=dict)
|
| 184 |
-
antagonist: Dict[str, Any] = field(default_factory=dict)
|
| 185 |
-
supporting_cast: Dict[str, Dict[str, Any]] = field(default_factory=dict)
|
| 186 |
-
three_act_structure: Dict[str, str] = field(default_factory=dict)
|
| 187 |
-
save_the_cat_beats: Dict[int, str] = field(default_factory=dict)
|
| 188 |
-
time_period: str = ""
|
| 189 |
-
primary_locations: List[Dict[str, str]] = field(default_factory=list)
|
| 190 |
-
world_rules: List[str] = field(default_factory=list)
|
| 191 |
-
visual_style: str = ""
|
| 192 |
-
key_imagery: List[str] = field(default_factory=list)
|
| 193 |
-
|
| 194 |
@dataclass
|
| 195 |
-
class
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
@dataclass
|
| 209 |
-
class
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
def __init__(self):
|
| 227 |
-
self.screenplay_bible = ScreenplayBible()
|
| 228 |
-
self.scenes: List[SceneBreakdown] = []
|
| 229 |
-
self.characters: Dict[str, CharacterProfile] = {}
|
| 230 |
-
self.page_count = 0
|
| 231 |
-
self.act_pages = {"1": 0, "2A": 0, "2B": 0, "3": 0}
|
| 232 |
-
|
| 233 |
-
def add_scene(self, scene: SceneBreakdown):
|
| 234 |
-
self.scenes.append(scene)
|
| 235 |
-
self.page_count += scene.page_count
|
| 236 |
-
|
| 237 |
-
def add_character(self, character: CharacterProfile):
|
| 238 |
-
self.characters[character.name] = character
|
| 239 |
-
if character.role == "protagonist":
|
| 240 |
-
self.screenplay_bible.protagonist = asdict(character)
|
| 241 |
-
elif character.role == "antagonist":
|
| 242 |
-
self.screenplay_bible.antagonist = asdict(character)
|
| 243 |
-
elif character.role == "supporting":
|
| 244 |
-
self.screenplay_bible.supporting_cast[character.name] = asdict(character)
|
| 245 |
-
|
| 246 |
-
def update_bible(self, key: str, value: Any):
|
| 247 |
-
if hasattr(self.screenplay_bible, key):
|
| 248 |
-
setattr(self.screenplay_bible, key, value)
|
| 249 |
-
|
| 250 |
class ScreenplayDatabase:
|
| 251 |
-
"""Database management"""
|
| 252 |
@staticmethod
|
| 253 |
def init_db():
|
| 254 |
with sqlite3.connect(DB_PATH) as conn:
|
|
@@ -262,18 +221,13 @@ class ScreenplayDatabase:
|
|
| 262 |
screenplay_type TEXT NOT NULL,
|
| 263 |
genre TEXT NOT NULL,
|
| 264 |
target_pages INTEGER,
|
| 265 |
-
language TEXT NOT NULL,
|
| 266 |
title TEXT,
|
| 267 |
logline TEXT,
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
character_profiles TEXT,
|
| 271 |
-
scene_breakdown TEXT,
|
| 272 |
-
screenplay_bible TEXT,
|
| 273 |
-
final_screenplay TEXT,
|
| 274 |
created_at TEXT DEFAULT (datetime('now')),
|
| 275 |
updated_at TEXT DEFAULT (datetime('now')),
|
| 276 |
-
status TEXT DEFAULT '
|
| 277 |
current_stage INTEGER DEFAULT 0,
|
| 278 |
total_pages REAL DEFAULT 0
|
| 279 |
)
|
|
@@ -283,28 +237,15 @@ class ScreenplayDatabase:
|
|
| 283 |
CREATE TABLE IF NOT EXISTS screenplay_stages (
|
| 284 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 285 |
session_id TEXT NOT NULL,
|
|
|
|
| 286 |
stage_number INTEGER NOT NULL,
|
| 287 |
stage_name TEXT NOT NULL,
|
| 288 |
-
role TEXT NOT NULL,
|
| 289 |
content TEXT,
|
| 290 |
page_count REAL DEFAULT 0,
|
| 291 |
status TEXT DEFAULT 'pending',
|
| 292 |
created_at TEXT DEFAULT (datetime('now')),
|
| 293 |
FOREIGN KEY (session_id) REFERENCES screenplay_sessions(session_id),
|
| 294 |
-
UNIQUE(session_id, stage_number)
|
| 295 |
-
)
|
| 296 |
-
''')
|
| 297 |
-
|
| 298 |
-
cursor.execute('''
|
| 299 |
-
CREATE TABLE IF NOT EXISTS screenplay_themes_library (
|
| 300 |
-
theme_id TEXT PRIMARY KEY,
|
| 301 |
-
theme_text TEXT NOT NULL,
|
| 302 |
-
screenplay_type TEXT NOT NULL,
|
| 303 |
-
genre TEXT NOT NULL,
|
| 304 |
-
language TEXT NOT NULL,
|
| 305 |
-
title TEXT,
|
| 306 |
-
logline TEXT,
|
| 307 |
-
generated_at TEXT DEFAULT (datetime('now'))
|
| 308 |
)
|
| 309 |
''')
|
| 310 |
|
|
@@ -322,101 +263,68 @@ class ScreenplayDatabase:
|
|
| 322 |
conn.close()
|
| 323 |
|
| 324 |
@staticmethod
|
| 325 |
-
def create_session(user_query: str, screenplay_type: str, genre: str
|
| 326 |
session_id = hashlib.md5(f"{user_query}{screenplay_type}{datetime.now()}".encode()).hexdigest()
|
| 327 |
target_pages = SCREENPLAY_LENGTHS[screenplay_type]["pages"]
|
| 328 |
|
| 329 |
with ScreenplayDatabase.get_db() as conn:
|
| 330 |
conn.cursor().execute(
|
| 331 |
'''INSERT INTO screenplay_sessions
|
| 332 |
-
(session_id, user_query, screenplay_type, genre, target_pages
|
| 333 |
-
VALUES (?, ?, ?, ?,
|
| 334 |
-
(session_id, user_query, screenplay_type, genre, target_pages
|
| 335 |
)
|
| 336 |
conn.commit()
|
| 337 |
return session_id
|
| 338 |
|
| 339 |
@staticmethod
|
| 340 |
-
def
|
| 341 |
-
role: str, content: str, status: str = 'complete'):
|
| 342 |
-
page_count = 0
|
| 343 |
-
if role == "screenwriter" and content:
|
| 344 |
-
page_count = ScreenplayGenerationSystem.calculate_screenplay_pages(content)
|
| 345 |
-
|
| 346 |
with ScreenplayDatabase.get_db() as conn:
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
DO UPDATE SET content=?, page_count=?, status=?, created_at=datetime('now')
|
| 354 |
-
''', (session_id, stage_number, stage_name, role, content, page_count, status,
|
| 355 |
-
content, page_count, status))
|
| 356 |
conn.commit()
|
| 357 |
|
| 358 |
@staticmethod
|
| 359 |
-
def
|
| 360 |
-
with ScreenplayDatabase.get_db() as conn:
|
| 361 |
-
rows = conn.cursor().execute('''
|
| 362 |
-
SELECT content FROM screenplay_stages
|
| 363 |
-
WHERE session_id = ?
|
| 364 |
-
AND role = 'screenwriter'
|
| 365 |
-
AND stage_name LIKE '%Final%'
|
| 366 |
-
ORDER BY stage_number
|
| 367 |
-
''', (session_id,)).fetchall()
|
| 368 |
-
|
| 369 |
-
if rows:
|
| 370 |
-
return '\n\n'.join(row['content'] for row in rows if row['content'])
|
| 371 |
-
else:
|
| 372 |
-
rows = conn.cursor().execute('''
|
| 373 |
-
SELECT content FROM screenplay_stages
|
| 374 |
-
WHERE session_id = ? AND role = 'screenwriter'
|
| 375 |
-
ORDER BY stage_number
|
| 376 |
-
''', (session_id,)).fetchall()
|
| 377 |
-
return '\n\n'.join(row['content'] for row in rows if row['content'])
|
| 378 |
-
return ""
|
| 379 |
-
|
| 380 |
-
@staticmethod
|
| 381 |
-
def get_active_sessions() -> List[Dict]:
|
| 382 |
with ScreenplayDatabase.get_db() as conn:
|
| 383 |
-
|
| 384 |
-
'
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
).fetchall()
|
| 391 |
-
return [dict(row) for row in rows]
|
| 392 |
|
| 393 |
@staticmethod
|
| 394 |
-
def
|
| 395 |
-
theme_id = hashlib.md5(f"{theme_text}{datetime.now()}".encode()).hexdigest()[:12]
|
| 396 |
-
|
| 397 |
with ScreenplayDatabase.get_db() as conn:
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
| 403 |
conn.commit()
|
| 404 |
-
|
| 405 |
-
return theme_id
|
| 406 |
|
|
|
|
| 407 |
class ScreenplayGenerationSystem:
|
| 408 |
-
"""Professional screenplay generation system"""
|
| 409 |
def __init__(self):
|
| 410 |
self.api_key = FIREWORKS_API_KEY
|
| 411 |
self.api_url = API_URL
|
| 412 |
self.model_id = MODEL_ID
|
| 413 |
-
self.screenplay_tracker = ScreenplayTracker()
|
| 414 |
self.current_session_id = None
|
|
|
|
| 415 |
ScreenplayDatabase.init_db()
|
| 416 |
|
| 417 |
def create_headers(self):
|
| 418 |
if not self.api_key or self.api_key == "dummy_token_for_testing":
|
| 419 |
-
raise ValueError("
|
| 420 |
|
| 421 |
return {
|
| 422 |
"Accept": "application/json",
|
|
@@ -424,109 +332,13 @@ class ScreenplayGenerationSystem:
|
|
| 424 |
"Authorization": f"Bearer {self.api_key}"
|
| 425 |
}
|
| 426 |
|
| 427 |
-
def
|
| 428 |
-
genre: str, language: str) -> str:
|
| 429 |
-
lang_prompts = {
|
| 430 |
-
"Korean": f"""๋น์ ์ ํ ๋ฆฌ์ฐ๋ ํ๋ก๋์์
๋๋ค. {screenplay_type} ์ปจ์
์ ๊ฐ๋ฐํ์ธ์.
|
| 431 |
-
|
| 432 |
-
**์์ฒญ์ฌํญ:** {user_query}
|
| 433 |
-
**ํ์
:** {SCREENPLAY_LENGTHS[screenplay_type]['description']}
|
| 434 |
-
**์ฅ๋ฅด:** {genre}
|
| 435 |
-
|
| 436 |
-
**ํ์ ์ ๊ณต ํญ๋ชฉ:**
|
| 437 |
-
1. **์ ๋ชฉ (TITLE)** - ๊ธฐ์ตํ๊ธฐ ์ฝ๊ณ ๋ง์ผํ
๊ฐ๋ฅํ ์ ๋ชฉ
|
| 438 |
-
2. **๋ก๊ทธ๋ผ์ธ (LOGLINE)** - 25๋จ์ด ์ด๋ด ํ ๋ฌธ์ฅ
|
| 439 |
-
3. **์ฅ๋ฅด ๋ถ๏ฟฝ๏ฟฝ๏ฟฝ** - ์ฃผ ์ฅ๋ฅด์ ์๋ธ ์ฅ๋ฅด
|
| 440 |
-
4. **ํ๊ฒ ๊ด๊ฐ** - ์ฐ๋ น๋์ ๊ด์ฌ์ฌ
|
| 441 |
-
5. **๋น๊ต ์ํ (COMPS)** - ์ฑ๊ณตํ ์ ์ฌ ํ๋ก์ ํธ 3๊ฐ
|
| 442 |
-
6. **๊ณ ์ ํ๋งค ํฌ์ธํธ (USP)** - ๋
ํนํ ์
|
| 443 |
-
7. **๋น์ฃผ์ผ ์ปจ์
** - ํต์ฌ ์ด๋ฏธ์ง 3๊ฐ""",
|
| 444 |
-
|
| 445 |
-
"English": f"""You are a Hollywood producer. Develop a {screenplay_type} concept.
|
| 446 |
-
|
| 447 |
-
**Request:** {user_query}
|
| 448 |
-
**Type:** {SCREENPLAY_LENGTHS[screenplay_type]['description']}
|
| 449 |
-
**Genre:** {genre}
|
| 450 |
-
|
| 451 |
-
**Required Elements:**
|
| 452 |
-
1. **TITLE** - Memorable and marketable
|
| 453 |
-
2. **LOGLINE** - One sentence, 25 words max
|
| 454 |
-
3. **GENRE ANALYSIS** - Primary and sub-genre
|
| 455 |
-
4. **TARGET AUDIENCE** - Age range and interests
|
| 456 |
-
5. **COMPARABLE FILMS (COMPS)** - 3 successful similar projects
|
| 457 |
-
6. **UNIQUE SELLING POINT (USP)** - What makes it unique
|
| 458 |
-
7. **VISUAL CONCEPT** - 3 key visual images"""
|
| 459 |
-
}
|
| 460 |
-
return lang_prompts.get(language, lang_prompts["English"])
|
| 461 |
-
|
| 462 |
-
def create_screenwriter_prompt(self, act: str, scene_breakdown: str,
|
| 463 |
-
characters: str, previous_acts: str,
|
| 464 |
-
screenplay_type: str, genre: str, language: str,
|
| 465 |
-
is_polish: bool = False, is_final: bool = False) -> str:
|
| 466 |
-
|
| 467 |
-
act_pages = int(SCREENPLAY_LENGTHS[screenplay_type]['pages'] * 0.25)
|
| 468 |
-
min_lines = int(act_pages * 65) # Enhanced for longer content
|
| 469 |
-
|
| 470 |
-
if is_final:
|
| 471 |
-
min_lines = int(min_lines * 1.4)
|
| 472 |
-
instruction_focus = "final polished extended version"
|
| 473 |
-
elif is_polish:
|
| 474 |
-
min_lines = int(min_lines * 1.2)
|
| 475 |
-
instruction_focus = "enhanced expanded version"
|
| 476 |
-
else:
|
| 477 |
-
instruction_focus = "complete detailed draft"
|
| 478 |
-
|
| 479 |
-
lang_prompts = {
|
| 480 |
-
"Korean": f"""ํ๋ก ์๋๋ฆฌ์ค ์๊ฐ๋ก์ {act}์ {instruction_focus}์ ์์ฑํ์ธ์.
|
| 481 |
-
|
| 482 |
-
**๋ชฉํ:** {act_pages}ํ์ด์ง (์ต์ {min_lines}์ค ํ์)
|
| 483 |
-
|
| 484 |
-
**์ฌ ์ ๋ณด:** {scene_breakdown}
|
| 485 |
-
**์บ๋ฆญํฐ:** {characters}
|
| 486 |
-
**์ด์ ๋ด์ฉ:** {previous_acts if previous_acts else "์ฒซ ๋ง์
๋๋ค."}
|
| 487 |
-
|
| 488 |
-
๊ฐ ์ฌ๋ง๋ค:
|
| 489 |
-
- ์ต์ 5-7ํ์ด์ง ๋ถ๋
|
| 490 |
-
- ์ถฉ๋ถํ ๋ํ์ ์ก์
|
| 491 |
-
- ๋ํ
์ผํ ๋ฌ์ฌ
|
| 492 |
-
- ๊ฐ์ ์ ๊น์ด
|
| 493 |
-
|
| 494 |
-
๋ฐ๋์ {min_lines}์ค ์ด์ ์์ฑํ์ธ์.""",
|
| 495 |
-
|
| 496 |
-
"English": f"""As a professional screenwriter, write the {instruction_focus} of {act}.
|
| 497 |
-
|
| 498 |
-
**Target:** {act_pages} pages (minimum {min_lines} lines required)
|
| 499 |
-
|
| 500 |
-
**Scene Info:** {scene_breakdown}
|
| 501 |
-
**Characters:** {characters}
|
| 502 |
-
**Previous:** {previous_acts if previous_acts else "This is Act 1."}
|
| 503 |
-
|
| 504 |
-
For each scene:
|
| 505 |
-
- Minimum 5-7 pages
|
| 506 |
-
- Sufficient dialogue and action
|
| 507 |
-
- Detailed descriptions
|
| 508 |
-
- Emotional depth
|
| 509 |
-
|
| 510 |
-
MUST write {min_lines}+ lines."""
|
| 511 |
-
}
|
| 512 |
-
|
| 513 |
-
return lang_prompts.get(language, lang_prompts["English"])
|
| 514 |
-
|
| 515 |
-
def call_llm_streaming(self, messages: List[Dict[str, str]], role: str,
|
| 516 |
-
language: str) -> Generator[str, None, None]:
|
| 517 |
try:
|
| 518 |
-
system_content = f"You are a professional {role} creating high-quality screenplay content."
|
| 519 |
-
|
| 520 |
-
full_messages = [
|
| 521 |
-
{"role": "system", "content": system_content},
|
| 522 |
-
*messages
|
| 523 |
-
]
|
| 524 |
-
|
| 525 |
payload = {
|
| 526 |
"model": self.model_id,
|
| 527 |
-
"messages":
|
| 528 |
-
"max_tokens":
|
| 529 |
-
"temperature": 0.8
|
| 530 |
"top_p": 1,
|
| 531 |
"top_k": 40,
|
| 532 |
"presence_penalty": 0.2,
|
|
@@ -545,7 +357,7 @@ MUST write {min_lines}+ lines."""
|
|
| 545 |
)
|
| 546 |
|
| 547 |
if response.status_code != 200:
|
| 548 |
-
yield f"โ API
|
| 549 |
return
|
| 550 |
|
| 551 |
buffer = ""
|
|
@@ -577,108 +389,290 @@ MUST write {min_lines}+ lines."""
|
|
| 577 |
yield buffer
|
| 578 |
|
| 579 |
except Exception as e:
|
| 580 |
-
yield f"โ
|
| 581 |
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
return 0.0
|
| 586 |
-
lines = content.split('\n')
|
| 587 |
-
total_lines = len(lines)
|
| 588 |
-
return total_lines / 58.0
|
| 589 |
-
|
| 590 |
-
def process_screenplay_stream(self, query: str, screenplay_type: str, genre: str,
|
| 591 |
-
language: str) -> Generator[Tuple[str, List[Dict], str], None, None]:
|
| 592 |
try:
|
| 593 |
-
self.current_session_id = ScreenplayDatabase.create_session(
|
| 594 |
-
query, screenplay_type, genre, language
|
| 595 |
-
)
|
| 596 |
|
| 597 |
-
|
|
|
|
| 598 |
|
| 599 |
-
for
|
| 600 |
-
|
| 601 |
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
|
| 612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
else:
|
| 626 |
-
|
| 627 |
|
| 628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
stages[stage_idx]["status"] = "complete"
|
| 641 |
-
ScreenplayDatabase.save_stage(
|
| 642 |
-
self.current_session_id, stage_idx, stage_name, role,
|
| 643 |
-
stage_content, "complete"
|
| 644 |
-
)
|
| 645 |
|
| 646 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
|
| 648 |
-
|
| 649 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
|
| 667 |
-
|
| 668 |
-
|
|
|
|
|
|
|
| 669 |
|
| 670 |
-
|
| 671 |
-
return f"**์ปจ์
:** {selected}\n๊ฐ๋ฑ๊ณผ ์บ๋ฆญํฐ ์ฑ์ฅ์ด ์๋ ์ด์ผ๊ธฐ"
|
| 672 |
-
else:
|
| 673 |
-
return f"**Concept:** {selected}\nA story with conflict and character growth"
|
| 674 |
|
| 675 |
def format_screenplay_display(screenplay_text: str) -> str:
|
|
|
|
| 676 |
if not screenplay_text:
|
| 677 |
-
return "
|
| 678 |
|
| 679 |
-
formatted = "# ๐ฌ
|
| 680 |
|
| 681 |
-
#
|
| 682 |
formatted_text = re.sub(
|
| 683 |
r'^(INT\.|EXT\.).*$',
|
| 684 |
r'**\g<0>**',
|
|
@@ -686,79 +680,69 @@ def format_screenplay_display(screenplay_text: str) -> str:
|
|
| 686 |
flags=re.MULTILINE
|
| 687 |
)
|
| 688 |
|
| 689 |
-
#
|
| 690 |
formatted_text = re.sub(
|
| 691 |
-
r'^([A-Z][A-Z\s]+)$',
|
| 692 |
r'**\g<0>**',
|
| 693 |
formatted_text,
|
| 694 |
flags=re.MULTILINE
|
| 695 |
)
|
| 696 |
|
| 697 |
-
#
|
| 698 |
page_count = len(screenplay_text.splitlines()) / 58
|
| 699 |
-
formatted = f"
|
| 700 |
|
| 701 |
return formatted
|
| 702 |
|
| 703 |
-
def
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
|
| 731 |
-
|
|
|
|
| 732 |
|
| 733 |
-
|
| 734 |
-
if not query.strip():
|
| 735 |
-
yield "", "", "โ Please enter a screenplay concept.", ""
|
| 736 |
-
return
|
| 737 |
-
|
| 738 |
-
system = ScreenplayGenerationSystem()
|
| 739 |
-
stages_markdown = ""
|
| 740 |
-
screenplay_display = ""
|
| 741 |
-
|
| 742 |
-
for status, stages, session_id in system.process_screenplay_stream(
|
| 743 |
-
query, screenplay_type, genre, language
|
| 744 |
-
):
|
| 745 |
-
stages_markdown = format_stages_display(stages)
|
| 746 |
-
|
| 747 |
-
if stages and all(s.get("status") == "complete" for s in stages[-4:]):
|
| 748 |
-
screenplay_text = ScreenplayDatabase.get_screenplay_content(session_id)
|
| 749 |
-
screenplay_display = format_screenplay_display(screenplay_text)
|
| 750 |
-
|
| 751 |
-
yield stages_markdown, screenplay_display, status, session_id
|
| 752 |
-
|
| 753 |
-
def get_active_sessions_list() -> List[str]:
|
| 754 |
-
sessions = ScreenplayDatabase.get_active_sessions()
|
| 755 |
-
return [
|
| 756 |
-
f"{s['session_id'][:8]}... - {s.get('title', s['user_query'][:30])}... "
|
| 757 |
-
f"({s['screenplay_type']}/{s['genre']}) [{s['total_pages']:.1f} pages]"
|
| 758 |
-
for s in sessions
|
| 759 |
-
]
|
| 760 |
-
|
| 761 |
-
# Create Gradio interface
|
| 762 |
def create_interface():
|
| 763 |
css = """
|
| 764 |
.main-header {
|
|
@@ -772,13 +756,22 @@ def create_interface():
|
|
| 772 |
}
|
| 773 |
|
| 774 |
.header-title {
|
| 775 |
-
font-size:
|
| 776 |
margin-bottom: 1rem;
|
| 777 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 778 |
}
|
| 779 |
|
| 780 |
-
|
| 781 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 782 |
white-space: pre-wrap;
|
| 783 |
background: white;
|
| 784 |
padding: 2rem;
|
|
@@ -787,180 +780,227 @@ def create_interface():
|
|
| 787 |
max-height: 800px;
|
| 788 |
overflow-y: auto;
|
| 789 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 790 |
"""
|
| 791 |
|
| 792 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=css, title="AI
|
| 793 |
gr.HTML("""
|
| 794 |
<div class="main-header">
|
| 795 |
-
<h1 class="header-title">๐ฌ AI
|
| 796 |
<p style="font-size: 1.2rem; opacity: 0.95;">
|
| 797 |
-
|
| 798 |
-
|
| 799 |
</p>
|
| 800 |
</div>
|
| 801 |
""")
|
| 802 |
|
| 803 |
current_session_id = gr.State(None)
|
|
|
|
| 804 |
|
| 805 |
with gr.Tabs():
|
| 806 |
-
with gr.Tab("
|
| 807 |
with gr.Row():
|
| 808 |
-
with gr.Column(scale=
|
| 809 |
query_input = gr.Textbox(
|
| 810 |
-
label="
|
| 811 |
-
placeholder="""
|
| 812 |
-
-
|
| 813 |
-
-
|
| 814 |
-
-
|
| 815 |
-
-
|
| 816 |
-
lines=
|
| 817 |
)
|
| 818 |
|
| 819 |
with gr.Column(scale=1):
|
| 820 |
screenplay_type = gr.Radio(
|
| 821 |
choices=list(SCREENPLAY_LENGTHS.keys()),
|
| 822 |
-
value="
|
| 823 |
-
label="๐ฝ๏ธ
|
| 824 |
-
info="Choose your format"
|
| 825 |
)
|
| 826 |
|
| 827 |
genre_select = gr.Dropdown(
|
| 828 |
choices=list(GENRE_TEMPLATES.keys()),
|
| 829 |
-
value="
|
| 830 |
-
label="๐ญ
|
| 831 |
-
info="Select main genre"
|
| 832 |
-
)
|
| 833 |
-
|
| 834 |
-
language_select = gr.Radio(
|
| 835 |
-
choices=["English", "Korean"],
|
| 836 |
-
value="English",
|
| 837 |
-
label="๐ Language"
|
| 838 |
)
|
| 839 |
|
| 840 |
with gr.Row():
|
| 841 |
-
random_btn = gr.Button("
|
| 842 |
-
clear_btn = gr.Button("๐๏ธ
|
| 843 |
-
|
| 844 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 845 |
status_text = gr.Textbox(
|
| 846 |
-
label="๐
|
| 847 |
interactive=False,
|
| 848 |
-
value="
|
| 849 |
)
|
| 850 |
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 870 |
|
|
|
|
| 871 |
gr.Examples(
|
| 872 |
examples=[
|
| 873 |
-
["
|
| 874 |
-
["
|
| 875 |
-
["
|
| 876 |
-
["
|
| 877 |
],
|
| 878 |
inputs=query_input,
|
| 879 |
-
label="๐ก
|
| 880 |
)
|
| 881 |
|
| 882 |
-
with gr.Tab("๐
|
| 883 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
with gr.Row():
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
interactive=True,
|
| 889 |
-
scale=3
|
| 890 |
-
)
|
| 891 |
-
refresh_btn = gr.Button("๐ Refresh", scale=1)
|
| 892 |
-
load_btn = gr.Button("๐ Load", scale=1)
|
| 893 |
|
| 894 |
-
#
|
| 895 |
-
def
|
| 896 |
if not query:
|
| 897 |
-
yield "", "
|
| 898 |
return
|
| 899 |
|
| 900 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 901 |
|
| 902 |
-
def
|
| 903 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 904 |
|
| 905 |
def handle_clear():
|
| 906 |
-
|
|
|
|
| 907 |
|
| 908 |
-
def handle_download(screenplay_text
|
| 909 |
-
if not screenplay_text:
|
| 910 |
return None
|
| 911 |
|
| 912 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 913 |
-
filename = f"
|
| 914 |
|
| 915 |
with open(filename, 'w', encoding='utf-8') as f:
|
| 916 |
-
|
|
|
|
|
|
|
|
|
|
| 917 |
|
| 918 |
return filename
|
| 919 |
|
| 920 |
-
#
|
| 921 |
-
|
| 922 |
-
fn=
|
| 923 |
-
inputs=[query_input, screenplay_type, genre_select
|
| 924 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 925 |
)
|
| 926 |
|
| 927 |
random_btn.click(
|
| 928 |
fn=handle_random,
|
| 929 |
-
inputs=[screenplay_type, genre_select
|
| 930 |
outputs=[query_input]
|
| 931 |
)
|
| 932 |
|
| 933 |
clear_btn.click(
|
| 934 |
fn=handle_clear,
|
| 935 |
-
outputs=[
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
refresh_btn.click(
|
| 939 |
-
fn=get_active_sessions_list,
|
| 940 |
-
outputs=[session_dropdown]
|
| 941 |
)
|
| 942 |
|
| 943 |
download_btn.click(
|
| 944 |
fn=handle_download,
|
| 945 |
-
inputs=[screenplay_output
|
| 946 |
-
outputs=[gr.File(label="
|
| 947 |
-
)
|
| 948 |
-
|
| 949 |
-
interface.load(
|
| 950 |
-
fn=get_active_sessions_list,
|
| 951 |
-
outputs=[session_dropdown]
|
| 952 |
)
|
| 953 |
|
| 954 |
return interface
|
| 955 |
|
| 956 |
-
#
|
| 957 |
if __name__ == "__main__":
|
| 958 |
logger.info("=" * 60)
|
| 959 |
-
logger.info("AI
|
| 960 |
-
logger.info(
|
| 961 |
-
logger.info(f"Max tokens: 15000")
|
| 962 |
logger.info("=" * 60)
|
| 963 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 964 |
ScreenplayDatabase.init_db()
|
| 965 |
|
| 966 |
interface = create_interface()
|
|
|
|
| 17 |
from collections import defaultdict
|
| 18 |
import random
|
| 19 |
|
| 20 |
+
# --- ๋ก๊น
์ค์ ---
|
| 21 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
+
# --- ํ๊ฒฝ ๋ณ์ ๋ฐ ์์ ---
|
| 25 |
FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY", "")
|
| 26 |
BRAVE_SEARCH_API_KEY = os.getenv("BRAVE_SEARCH_API_KEY", "")
|
| 27 |
API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 28 |
MODEL_ID = "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507"
|
| 29 |
+
DB_PATH = "screenplay_sessions_korean.db"
|
| 30 |
|
| 31 |
+
# ์๋๋ฆฌ์ค ๊ธธ์ด ์ค์
|
| 32 |
SCREENPLAY_LENGTHS = {
|
| 33 |
+
"์ํ": {"pages": 120, "description": "์ฅํธ ์ํ (110-130ํ์ด์ง)", "min_pages": 110},
|
| 34 |
+
"๋๋ผ๋ง": {"pages": 60, "description": "TV ๋๋ผ๋ง (55-65ํ์ด์ง)", "min_pages": 55},
|
| 35 |
+
"์น๋๋ผ๋ง": {"pages": 50, "description": "์น/OTT ์๋ฆฌ์ฆ (45-55ํ์ด์ง)", "min_pages": 45},
|
| 36 |
+
"๋จํธ": {"pages": 20, "description": "๋จํธ ์ํ (15-25ํ์ด์ง)", "min_pages": 15}
|
| 37 |
}
|
| 38 |
|
| 39 |
+
# ํ๊ฒฝ ๊ฒ์ฆ
|
| 40 |
if not FIREWORKS_API_KEY:
|
| 41 |
+
logger.error("FIREWORKS_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 42 |
FIREWORKS_API_KEY = "dummy_token_for_testing"
|
| 43 |
|
| 44 |
+
# ๊ธ๋ก๋ฒ ๋ณ์
|
|
|
|
|
|
|
|
|
|
| 45 |
db_lock = threading.Lock()
|
| 46 |
|
| 47 |
+
# ์ฅ๋ฅด ํ
ํ๋ฆฟ
|
| 48 |
GENRE_TEMPLATES = {
|
| 49 |
+
"์ก์
": {
|
| 50 |
+
"pacing": "๋น ๋ฆ",
|
| 51 |
+
"scene_length": "์งง์",
|
| 52 |
"dialogue_ratio": 0.3,
|
| 53 |
+
"key_elements": ["์ก์
์ํ์ค", "๋ฌผ๋ฆฌ์ ๊ฐ๋ฑ", "๊ธด๋ฐ๊ฐ", "์๊ธฐ ๊ณ ์กฐ", "์์
์ ์๊ฐ"],
|
| 54 |
+
"structure_beats": ["ํญ๋ฐ์ ์คํ๋", "์ถ๊ฒฉ", "๋๊ฒฐ", "ํด๋ผ์ด๋งฅ์ค ์ ํฌ", "์์
์ ์น๋ฆฌ"],
|
| 55 |
"scene_density": 1.2,
|
| 56 |
"action_description_ratio": 0.6
|
| 57 |
},
|
| 58 |
+
"์ค๋ฆด๋ฌ": {
|
| 59 |
+
"pacing": "๋น ๋ฆ",
|
| 60 |
+
"scene_length": "์งง์",
|
| 61 |
"dialogue_ratio": 0.35,
|
| 62 |
+
"key_elements": ["์์คํ์ค", "๋ฐ์ ", "ํธ์ง์ฆ", "์๊ฐ ์๋ฐ", "์ง์ค ํญ๋ก"],
|
| 63 |
+
"structure_beats": ["ํ
", "๋ฏธ์คํฐ๋ฆฌ ์ฌํ", "๊ฑฐ์ง ์น๋ฆฌ", "์ง์ค ํญ๋ก", "์ต์ข
๋๊ฒฐ"],
|
| 64 |
"scene_density": 1.1,
|
| 65 |
"action_description_ratio": 0.5
|
| 66 |
},
|
| 67 |
+
"๋๋ผ๋ง": {
|
| 68 |
+
"pacing": "๋ณดํต",
|
| 69 |
+
"scene_length": "์ค๊ฐ",
|
| 70 |
"dialogue_ratio": 0.55,
|
| 71 |
+
"key_elements": ["์บ๋ฆญํฐ ๊น์ด", "๊ฐ์ ์ ์ง์ค", "๊ด๊ณ", "๋ด์ ๊ฐ๋ฑ", "๋ณํ"],
|
| 72 |
+
"structure_beats": ["์ผ์", "์ด๋งค", "๊ณ ๋ฏผ", "๊ฒฐ์ฌ", "๋ณต์กํ", "์๊ธฐ", "ํด๊ฒฐ"],
|
| 73 |
"scene_density": 0.9,
|
| 74 |
"action_description_ratio": 0.3
|
| 75 |
},
|
| 76 |
+
"์ฝ๋ฏธ๋": {
|
| 77 |
+
"pacing": "๋น ๋ฆ",
|
| 78 |
+
"scene_length": "์งง์",
|
| 79 |
"dialogue_ratio": 0.65,
|
| 80 |
+
"key_elements": ["์
์
๊ณผ ํ์ด์คํ", "ํ์ด๋ฐ", "์บ๋ฆญํฐ ์ฝ๋ฏธ๋", "์ํฉ ๊ณ ์กฐ", "๋ฌ๋ ๊ฐ๊ทธ"],
|
| 81 |
+
"structure_beats": ["์๊ธด ์คํ๋", "๋ณต์กํ", "์คํด ์ฆํญ", "ํผ๋์ ์ ์ ", "ํด๊ฒฐ๊ณผ ์ฝ๋ฐฑ"],
|
| 82 |
"scene_density": 1.0,
|
| 83 |
"action_description_ratio": 0.35
|
| 84 |
},
|
| 85 |
+
"๊ณตํฌ": {
|
| 86 |
+
"pacing": "๊ฐ๋ณ์ ",
|
| 87 |
+
"scene_length": "ํผํฉ",
|
| 88 |
"dialogue_ratio": 0.3,
|
| 89 |
+
"key_elements": ["๋ถ์๊ธฐ", "๊ณตํฌ๊ฐ", "์ ํ ์ค์ผ์ด", "์ฌ๋ฆฌ์ ๊ณตํฌ", "๊ณ ๋ฆฝ"],
|
| 90 |
+
"structure_beats": ["ํ๋ฒํ ์ผ์", "์ฒซ ์ง์กฐ", "์กฐ์ฌ", "์ฒซ ๊ณต๊ฒฉ", "์์กด", "์ต์ข
๋๊ฒฐ"],
|
| 91 |
"scene_density": 1.0,
|
| 92 |
"action_description_ratio": 0.55
|
| 93 |
},
|
| 94 |
+
"SF": {
|
| 95 |
+
"pacing": "๋ณดํต",
|
| 96 |
+
"scene_length": "์ค๊ฐ",
|
| 97 |
"dialogue_ratio": 0.45,
|
| 98 |
+
"key_elements": ["์ธ๊ณ๊ด ๊ตฌ์ถ", "๊ธฐ์ ", "๊ฐ๋
", "์๊ฐ์ ์คํํฐํด", "์ฒ ํ์ ์ง๋ฌธ"],
|
| 99 |
+
"structure_beats": ["์ผ์ ์ธ๊ณ", "๋ฐ๊ฒฌ", "์๋ก์ด ์ธ๊ณ", "๋ณต์กํ", "์ดํด", "์ ํ", "์๋ก์ด ์ผ์"],
|
| 100 |
"scene_density": 0.95,
|
| 101 |
"action_description_ratio": 0.45
|
| 102 |
},
|
| 103 |
+
"๋ก๋งจ์ค": {
|
| 104 |
+
"pacing": "๋ณดํต",
|
| 105 |
+
"scene_length": "์ค๊ฐ",
|
| 106 |
"dialogue_ratio": 0.6,
|
| 107 |
+
"key_elements": ["์ผ๋ฏธ์คํธ๋ฆฌ", "์ฅ์ ๋ฌผ", "๊ฐ์ ์ ์๊ฐ", "์น๋ฐ๊ฐ", "์ทจ์ฝ์ฑ"],
|
| 108 |
+
"structure_beats": ["๋ง๋จ", "๋๋ฆผ", "์ฒซ ๊ฐ๋ฑ", "๊น์ด์ง", "์๊ธฐ/์ด๋ณ", "ํํด", "๊ฒฐํฉ"],
|
| 109 |
"scene_density": 0.85,
|
| 110 |
"action_description_ratio": 0.25
|
| 111 |
+
},
|
| 112 |
+
"ํํ์ง": {
|
| 113 |
+
"pacing": "๋ณดํต",
|
| 114 |
+
"scene_length": "์ค๊ฐ",
|
| 115 |
+
"dialogue_ratio": 0.4,
|
| 116 |
+
"key_elements": ["๋ง๋ฒ ์ฒด๊ณ", "์์
์ ์ฌ์ ", "์ ํ์ ์์", "์ธ๊ณ๊ด", "์ฑ์ฅ"],
|
| 117 |
+
"structure_beats": ["ํ๋ฒํ ์ธ๊ณ", "์๋ช
", "๊ฑฐ๋ถ", "๋ฉํ ", "์ฒซ ์ํ", "์๋ จ", "๋ณด์", "๊ทํ"],
|
| 118 |
+
"scene_density": 1.0,
|
| 119 |
+
"action_description_ratio": 0.5
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
+
# ์๋๋ฆฌ์ค ๋จ๊ณ (๊ธฐํ ๋จ๊ณ์ ์์ฑ ๋จ๊ณ ๋ถ๋ฆฌ)
|
| 124 |
+
PLANNING_STAGES = [
|
| 125 |
+
("์ปจ์
๊ฐ๋ฐ", "๐ฌ ํ๋ก๋์: ํต์ฌ ์ปจ์
๋ฐ ์์ฅ์ฑ ๋ถ์"),
|
| 126 |
+
("์คํ ๋ฆฌ ๊ตฌ์ฑ", "๐ ์คํ ๋ฆฌ ๊ฐ๋ฐ: ์๋์์ค ๋ฐ 3๋ง ๊ตฌ์กฐ"),
|
| 127 |
+
("์บ๋ฆญํฐ ์ค๊ณ", "๐ฅ ์บ๋ฆญํฐ ๋์์ด๋: ์ธ๋ฌผ ํ๋กํ ๋ฐ ๊ด๊ณ๋"),
|
| 128 |
+
("์ธ๊ณ๊ด ๊ตฌ์ถ", "๐ ์ธ๊ณ๊ด: ๋ฐฐ๊ฒฝ, ๋ถ์๊ธฐ, ๋น์ฃผ์ผ ์ค์ "),
|
| 129 |
+
("์ฌ ๊ตฌ์ฑ", "๐ฏ ์ฌ ํ๋๋: ์์ธ ์ฌ ๋ธ๋ ์ดํฌ๋ค์ด"),
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
WRITING_STAGES = [
|
| 133 |
+
("1๋ง ์ด๊ณ ", "โ๏ธ 1๋ง - ์ค์ (25%) - ์ด๊ณ ์์ฑ"),
|
| 134 |
+
("1๋ง ์์ ", "๐ 1๋ง - ํ์ฅ ๋ฐ ๋ค๋ฌ๊ธฐ"),
|
| 135 |
+
("1๋ง ๊ฒํ ", "๐ง 1๋ง - ์ ๋ฌธ๊ฐ ๊ฒํ "),
|
| 136 |
+
("1๋ง ์์ฑ", "โ
1๋ง - ์ต์ข
์์ฑ๋ณธ"),
|
| 137 |
|
| 138 |
+
("2๋งA ์ด๊ณ ", "โ๏ธ 2๋งA - ์์น (25%) - ์ด๊ณ ์์ฑ"),
|
| 139 |
+
("2๋งA ์์ ", "๐ 2๋งA - ํ์ฅ ๋ฐ ๋ค๋ฌ๊ธฐ"),
|
| 140 |
+
("2๋งA ๊ฒํ ", "๐ง 2๋งA - ์ ๋ฌธ๊ฐ ๊ฒํ "),
|
| 141 |
+
("2๋งA ์์ฑ", "โ
2๋งA - ์ต์ข
์์ฑ๋ณธ"),
|
|
|
|
| 142 |
|
| 143 |
+
("2๋งB ์ด๊ณ ", "โ๏ธ 2๋งB - ๋ณต์กํ (25%) - ์ด๊ณ ์์ฑ"),
|
| 144 |
+
("2๋งB ์์ ", "๐ 2๋งB - ํ์ฅ ๋ฐ ๋ค๋ฌ๊ธฐ"),
|
| 145 |
+
("2๋งB ๊ฒํ ", "๐ง 2๋งB - ์ ๋ฌธ๊ฐ ๊ฒํ "),
|
| 146 |
+
("2๋งB ์์ฑ", "โ
2๋งB - ์ต์ข
์์ฑ๋ณธ"),
|
|
|
|
| 147 |
|
| 148 |
+
("3๋ง ์ด๊ณ ", "โ๏ธ 3๋ง - ํด๊ฒฐ (25%) - ์ด๊ณ ์์ฑ"),
|
| 149 |
+
("3๋ง ์์ ", "๐ 3๋ง - ํ์ฅ ๋ฐ ๋ค๋ฌ๊ธฐ"),
|
| 150 |
+
("3๋ง ๊ฒํ ", "๐ง 3๋ง - ์ ๋ฌธ๊ฐ ๊ฒํ "),
|
| 151 |
+
("3๋ง ์์ฑ", "โ
3๋ง - ์ต์ข
์์ฑ๋ณธ"),
|
|
|
|
| 152 |
|
| 153 |
+
("๋์ฌ ๊ฐํ", "๐ฌ ๋์ฌ ์ ๋ฌธ๊ฐ: ๋์ฌ ๊ฐ์ ๋ฐ ์๋ธํ
์คํธ"),
|
| 154 |
+
("์ต์ข
๊ฒํ ", "๐ญ ์ต์ข
๋ฆฌ๋ทฐ: ์ ์ฒด ์๋๋ฆฌ์ค ๋ถ์ ๋ฐ ์์ฑ"),
|
| 155 |
]
|
| 156 |
|
| 157 |
+
# ๋ฐ์ดํฐ ํด๋์ค
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
@dataclass
|
| 159 |
+
class ScreenplayPlan:
|
| 160 |
+
"""์๋๋ฆฌ์ค ๊ธฐํ์"""
|
| 161 |
+
title: str = ""
|
| 162 |
+
logline: str = ""
|
| 163 |
+
genre: str = ""
|
| 164 |
+
subgenre: str = ""
|
| 165 |
+
themes: List[str] = field(default_factory=list)
|
| 166 |
+
|
| 167 |
+
# ์๋์์ค
|
| 168 |
+
synopsis: str = ""
|
| 169 |
+
extended_synopsis: str = ""
|
| 170 |
+
|
| 171 |
+
# 3๋ง ๊ตฌ์กฐ
|
| 172 |
+
act1_summary: str = ""
|
| 173 |
+
act2a_summary: str = ""
|
| 174 |
+
act2b_summary: str = ""
|
| 175 |
+
act3_summary: str = ""
|
| 176 |
+
|
| 177 |
+
# ์ฃผ์ ์บ๋ฆญํฐ
|
| 178 |
+
protagonist: Dict[str, str] = field(default_factory=dict)
|
| 179 |
+
antagonist: Dict[str, str] = field(default_factory=dict)
|
| 180 |
+
supporting_characters: List[Dict[str, str]] = field(default_factory=list)
|
| 181 |
+
|
| 182 |
+
# ์ธ๊ณ๊ด
|
| 183 |
+
time_period: str = ""
|
| 184 |
+
locations: List[str] = field(default_factory=list)
|
| 185 |
+
atmosphere: str = ""
|
| 186 |
+
visual_style: str = ""
|
| 187 |
+
|
| 188 |
+
# ์ฌ ๊ตฌ์ฑ
|
| 189 |
+
total_scenes: int = 0
|
| 190 |
+
scene_breakdown: List[Dict] = field(default_factory=list)
|
| 191 |
|
| 192 |
@dataclass
|
| 193 |
+
class ScreenplayBible:
|
| 194 |
+
"""์๋๋ฆฌ์ค ๋ฐ์ด๋ธ"""
|
| 195 |
+
title: str = ""
|
| 196 |
+
logline: str = ""
|
| 197 |
+
genre: str = ""
|
| 198 |
+
subgenre: str = ""
|
| 199 |
+
tone: str = ""
|
| 200 |
+
themes: List[str] = field(default_factory=list)
|
| 201 |
+
protagonist: Dict[str, Any] = field(default_factory=dict)
|
| 202 |
+
antagonist: Dict[str, Any] = field(default_factory=dict)
|
| 203 |
+
supporting_cast: Dict[str, Dict[str, Any]] = field(default_factory=dict)
|
| 204 |
+
three_act_structure: Dict[str, str] = field(default_factory=dict)
|
| 205 |
+
time_period: str = ""
|
| 206 |
+
primary_locations: List[Dict[str, str]] = field(default_factory=list)
|
| 207 |
+
visual_style: str = ""
|
| 208 |
+
|
| 209 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ํด๋์ค
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
class ScreenplayDatabase:
|
|
|
|
| 211 |
@staticmethod
|
| 212 |
def init_db():
|
| 213 |
with sqlite3.connect(DB_PATH) as conn:
|
|
|
|
| 221 |
screenplay_type TEXT NOT NULL,
|
| 222 |
genre TEXT NOT NULL,
|
| 223 |
target_pages INTEGER,
|
|
|
|
| 224 |
title TEXT,
|
| 225 |
logline TEXT,
|
| 226 |
+
planning_data TEXT,
|
| 227 |
+
screenplay_content TEXT,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
created_at TEXT DEFAULT (datetime('now')),
|
| 229 |
updated_at TEXT DEFAULT (datetime('now')),
|
| 230 |
+
status TEXT DEFAULT 'planning',
|
| 231 |
current_stage INTEGER DEFAULT 0,
|
| 232 |
total_pages REAL DEFAULT 0
|
| 233 |
)
|
|
|
|
| 237 |
CREATE TABLE IF NOT EXISTS screenplay_stages (
|
| 238 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 239 |
session_id TEXT NOT NULL,
|
| 240 |
+
stage_type TEXT NOT NULL,
|
| 241 |
stage_number INTEGER NOT NULL,
|
| 242 |
stage_name TEXT NOT NULL,
|
|
|
|
| 243 |
content TEXT,
|
| 244 |
page_count REAL DEFAULT 0,
|
| 245 |
status TEXT DEFAULT 'pending',
|
| 246 |
created_at TEXT DEFAULT (datetime('now')),
|
| 247 |
FOREIGN KEY (session_id) REFERENCES screenplay_sessions(session_id),
|
| 248 |
+
UNIQUE(session_id, stage_type, stage_number)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
)
|
| 250 |
''')
|
| 251 |
|
|
|
|
| 263 |
conn.close()
|
| 264 |
|
| 265 |
@staticmethod
|
| 266 |
+
def create_session(user_query: str, screenplay_type: str, genre: str) -> str:
|
| 267 |
session_id = hashlib.md5(f"{user_query}{screenplay_type}{datetime.now()}".encode()).hexdigest()
|
| 268 |
target_pages = SCREENPLAY_LENGTHS[screenplay_type]["pages"]
|
| 269 |
|
| 270 |
with ScreenplayDatabase.get_db() as conn:
|
| 271 |
conn.cursor().execute(
|
| 272 |
'''INSERT INTO screenplay_sessions
|
| 273 |
+
(session_id, user_query, screenplay_type, genre, target_pages)
|
| 274 |
+
VALUES (?, ?, ?, ?, ?)''',
|
| 275 |
+
(session_id, user_query, screenplay_type, genre, target_pages)
|
| 276 |
)
|
| 277 |
conn.commit()
|
| 278 |
return session_id
|
| 279 |
|
| 280 |
@staticmethod
|
| 281 |
+
def save_planning_data(session_id: str, planning_data: Dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
with ScreenplayDatabase.get_db() as conn:
|
| 283 |
+
conn.cursor().execute(
|
| 284 |
+
'''UPDATE screenplay_sessions
|
| 285 |
+
SET planning_data = ?, status = 'planned', updated_at = datetime('now')
|
| 286 |
+
WHERE session_id = ?''',
|
| 287 |
+
(json.dumps(planning_data, ensure_ascii=False), session_id)
|
| 288 |
+
)
|
|
|
|
|
|
|
|
|
|
| 289 |
conn.commit()
|
| 290 |
|
| 291 |
@staticmethod
|
| 292 |
+
def get_planning_data(session_id: str) -> Optional[Dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
with ScreenplayDatabase.get_db() as conn:
|
| 294 |
+
row = conn.cursor().execute(
|
| 295 |
+
'SELECT planning_data FROM screenplay_sessions WHERE session_id = ?',
|
| 296 |
+
(session_id,)
|
| 297 |
+
).fetchone()
|
| 298 |
+
if row and row['planning_data']:
|
| 299 |
+
return json.loads(row['planning_data'])
|
| 300 |
+
return None
|
|
|
|
|
|
|
| 301 |
|
| 302 |
@staticmethod
|
| 303 |
+
def save_screenplay_content(session_id: str, content: str, title: str, logline: str):
|
|
|
|
|
|
|
| 304 |
with ScreenplayDatabase.get_db() as conn:
|
| 305 |
+
total_pages = len(content.split('\n')) / 58
|
| 306 |
+
conn.cursor().execute(
|
| 307 |
+
'''UPDATE screenplay_sessions
|
| 308 |
+
SET screenplay_content = ?, title = ?, logline = ?,
|
| 309 |
+
total_pages = ?, status = 'complete', updated_at = datetime('now')
|
| 310 |
+
WHERE session_id = ?''',
|
| 311 |
+
(content, title, logline, total_pages, session_id)
|
| 312 |
+
)
|
| 313 |
conn.commit()
|
|
|
|
|
|
|
| 314 |
|
| 315 |
+
# ์๋๋ฆฌ์ค ์์ฑ ์์คํ
|
| 316 |
class ScreenplayGenerationSystem:
|
|
|
|
| 317 |
def __init__(self):
|
| 318 |
self.api_key = FIREWORKS_API_KEY
|
| 319 |
self.api_url = API_URL
|
| 320 |
self.model_id = MODEL_ID
|
|
|
|
| 321 |
self.current_session_id = None
|
| 322 |
+
self.current_plan = ScreenplayPlan()
|
| 323 |
ScreenplayDatabase.init_db()
|
| 324 |
|
| 325 |
def create_headers(self):
|
| 326 |
if not self.api_key or self.api_key == "dummy_token_for_testing":
|
| 327 |
+
raise ValueError("์ ํจํ FIREWORKS_API_KEY๊ฐ ํ์ํฉ๋๋ค")
|
| 328 |
|
| 329 |
return {
|
| 330 |
"Accept": "application/json",
|
|
|
|
| 332 |
"Authorization": f"Bearer {self.api_key}"
|
| 333 |
}
|
| 334 |
|
| 335 |
+
def call_llm_streaming(self, messages: List[Dict[str, str]], max_tokens: int = 8000) -> Generator[str, None, None]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
payload = {
|
| 338 |
"model": self.model_id,
|
| 339 |
+
"messages": messages,
|
| 340 |
+
"max_tokens": max_tokens,
|
| 341 |
+
"temperature": 0.8,
|
| 342 |
"top_p": 1,
|
| 343 |
"top_k": 40,
|
| 344 |
"presence_penalty": 0.2,
|
|
|
|
| 357 |
)
|
| 358 |
|
| 359 |
if response.status_code != 200:
|
| 360 |
+
yield f"โ API ์ค๋ฅ: {response.status_code}"
|
| 361 |
return
|
| 362 |
|
| 363 |
buffer = ""
|
|
|
|
| 389 |
yield buffer
|
| 390 |
|
| 391 |
except Exception as e:
|
| 392 |
+
yield f"โ ์ค๋ฅ: {str(e)}"
|
| 393 |
|
| 394 |
+
def generate_planning(self, query: str, screenplay_type: str, genre: str,
|
| 395 |
+
progress_callback=None) -> Generator[Tuple[str, float, Dict], None, None]:
|
| 396 |
+
"""๊ธฐํ์ ์์ฑ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
try:
|
| 398 |
+
self.current_session_id = ScreenplayDatabase.create_session(query, screenplay_type, genre)
|
|
|
|
|
|
|
| 399 |
|
| 400 |
+
planning_content = {}
|
| 401 |
+
total_stages = len(PLANNING_STAGES)
|
| 402 |
|
| 403 |
+
for idx, (stage_name, stage_desc) in enumerate(PLANNING_STAGES):
|
| 404 |
+
progress = (idx / total_stages) * 100
|
| 405 |
|
| 406 |
+
yield f"๐ {stage_desc} ์งํ ์ค...", progress, planning_content
|
| 407 |
+
|
| 408 |
+
# ๊ฐ ๋จ๊ณ๋ณ ํ๋กฌํํธ ์์ฑ
|
| 409 |
+
if stage_name == "์ปจ์
๊ฐ๋ฐ":
|
| 410 |
+
prompt = f"""๋น์ ์ ํ ๋ฆฌ์ฐ๋ ํ๋ก๋์์
๋๋ค. ๋ค์ ์์ฒญ์ผ๋ก {screenplay_type} ์๋๋ฆฌ์ค ์ปจ์
์ ๊ฐ๋ฐํ์ธ์.
|
| 411 |
+
|
| 412 |
+
์์ฒญ: {query}
|
| 413 |
+
์ฅ๋ฅด: {genre}
|
| 414 |
+
|
| 415 |
+
๋ค์์ ํฌํจํ์ฌ ์์ฑํ์ธ์:
|
| 416 |
+
1. ์ ๋ชฉ
|
| 417 |
+
2. ๋ก๊ทธ๋ผ์ธ (ํ ๋ฌธ์ฅ์ผ๋ก ํต์ฌ ๊ฐ๋ฑ๊ณผ ์ฃผ์ธ๊ณต ์ค๋ช
)
|
| 418 |
+
3. ํต์ฌ ํ
๋ง
|
| 419 |
+
4. ํ๊ฒ ๊ด๊ฐ
|
| 420 |
+
5. ์ ์ฌ ์ํ 3๊ฐ์ ์ฐจ๋ณ์
|
| 421 |
+
6. ์์
์ ๋งค๋ ฅ ํฌ์ธํธ"""
|
| 422 |
+
|
| 423 |
+
elif stage_name == "์คํ ๋ฆฌ ๊ตฌ์ฑ":
|
| 424 |
+
prompt = f"""์ด์ ์ปจ์
์ ๋ฐํ์ผ๋ก ์์ธํ ์คํ ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋ง๋์ธ์.
|
| 425 |
+
|
| 426 |
+
1. ์๋์์ค (500์)
|
| 427 |
+
2. ํ์ฅ ์๋์์ค (1000์)
|
| 428 |
+
3. 3๋ง ๊ตฌ์กฐ:
|
| 429 |
+
- 1๋ง (์ค์ ): ์ฃผ์ธ๊ณต ์๊ฐ, ์ผ์, ์ฌ๊ฑด ๋ฐ์
|
| 430 |
+
- 2๋งA (์์น): ์๋ก์ด ์ธ๊ณ, ์๋ จ ์์
|
| 431 |
+
- 2๋งB (๋ณต์กํ): ๊ฐ๋ฑ ์ฌํ, ์๊ธฐ ๊ณ ์กฐ
|
| 432 |
+
- 3๋ง (ํด๊ฒฐ): ํด๋ผ์ด๋งฅ์ค, ๊ฒฐ๋ง"""
|
| 433 |
+
|
| 434 |
+
elif stage_name == "์บ๋ฆญํฐ ์ค๊ณ":
|
| 435 |
+
prompt = f"""์ฃผ์ ์บ๋ฆญํฐ๋ค์ ์์ธํ ์ค๊ณํ์ธ์.
|
| 436 |
+
|
| 437 |
+
์ฃผ์ธ๊ณต:
|
| 438 |
+
- ์ด๋ฆ๊ณผ ๋์ด
|
| 439 |
+
- ์ธ๋ชจ์ ์ฑ๊ฒฉ
|
| 440 |
+
- ๋ชฉํ (WANT)
|
| 441 |
+
- ํ์ (NEED)
|
| 442 |
+
- ์ฑ์ฅ ์ํฌ
|
| 443 |
+
|
| 444 |
+
์ ๋์:
|
| 445 |
+
- ์ด๋ฆ๊ณผ ์ญํ
|
| 446 |
+
- ๋๊ธฐ์ ๋ชฉํ
|
| 447 |
+
- ์ฃผ์ธ๊ณต๊ณผ์ ๊ด๊ณ
|
| 448 |
+
|
| 449 |
+
์กฐ์ฐ 3๋ช
:
|
| 450 |
+
- ๊ฐ์์ ์ญํ ๊ณผ ํน์ง"""
|
| 451 |
+
|
| 452 |
+
elif stage_name == "์ธ๊ณ๊ด ๊ตฌ์ถ":
|
| 453 |
+
prompt = f"""์๋๋ฆฌ์ค์ ์ธ๊ณ๊ด๊ณผ ๋ถ์๊ธฐ๋ฅผ ๊ตฌ์ถํ์ธ์.
|
| 454 |
+
|
| 455 |
+
1. ์๋ ๋ฐฐ๊ฒฝ
|
| 456 |
+
2. ์ฃผ์ ์ฅ์ 5๊ณณ
|
| 457 |
+
3. ์ ์ฒด์ ๋ถ์๊ธฐ์ ํค
|
| 458 |
+
4. ์๊ฐ์ ์คํ์ผ
|
| 459 |
+
5. ์ํฅ/์์
์คํ์ผ"""
|
| 460 |
+
|
| 461 |
+
elif stage_name == "์ฌ ๊ตฌ์ฑ":
|
| 462 |
+
prompt = f"""์ ์ฒด ์ฌ ๊ตฌ์ฑ์ ๊ณํํ์ธ์.
|
| 463 |
+
|
| 464 |
+
{SCREENPLAY_LENGTHS[screenplay_type]['pages']}ํ์ด์ง ๋ถ๋์ผ๋ก:
|
| 465 |
+
- 1๋ง: ์ฝ {int(SCREENPLAY_LENGTHS[screenplay_type]['pages']*0.25)}ํ์ด์ง (10-12๊ฐ ์ฌ)
|
| 466 |
+
- 2๋งA: ์ฝ {int(SCREENPLAY_LENGTHS[screenplay_type]['pages']*0.25)}ํ์ด์ง (12-15๊ฐ ์ฌ)
|
| 467 |
+
- 2๋งB: ์ฝ {int(SCREENPLAY_LENGTHS[screenplay_type]['pages']*0.25)}ํ์ด์ง (12-15๊ฐ ์ฌ)
|
| 468 |
+
- 3๋ง: ์ฝ {int(SCREENPLAY_LENGTHS[screenplay_type]['pages']*0.25)}ํ์ด์ง (8-10๊ฐ ์ฌ)
|
| 469 |
|
| 470 |
+
๊ฐ ์ฌ๋ง๋ค:
|
| 471 |
+
- ์ฅ์์ ์๊ฐ
|
| 472 |
+
- ๋ฑ์ฅ์ธ๋ฌผ
|
| 473 |
+
- ํต์ฌ ์ฌ๊ฑด
|
| 474 |
+
- ํ์ด์ง ์"""
|
| 475 |
+
|
| 476 |
+
# LLM ํธ์ถ
|
| 477 |
+
messages = [
|
| 478 |
+
{"role": "system", "content": "๋น์ ์ ์ ๋ฌธ ์๋๋ฆฌ์ค ์๊ฐ์
๋๋ค. ์์ธํ๊ณ ์ฐฝ์์ ์ธ ๋ด์ฉ์ ์์ฑํ์ธ์."},
|
| 479 |
+
{"role": "user", "content": prompt}
|
| 480 |
+
]
|
| 481 |
|
| 482 |
+
content = ""
|
| 483 |
+
for chunk in self.call_llm_streaming(messages):
|
| 484 |
+
content += chunk
|
| 485 |
+
planning_content[stage_name] = content
|
| 486 |
+
yield f"โ
{stage_desc} ์๋ฃ", progress, planning_content
|
| 487 |
+
|
| 488 |
+
time.sleep(0.5) # API ์ ํ ๊ณ ๋ ค
|
| 489 |
+
|
| 490 |
+
# ์ต์ข
๊ธฐํ์ ์ ์ฅ
|
| 491 |
+
ScreenplayDatabase.save_planning_data(self.current_session_id, planning_content)
|
| 492 |
+
yield "โ
๊ธฐํ์ ์์ฑ!", 100, planning_content
|
| 493 |
+
|
| 494 |
+
except Exception as e:
|
| 495 |
+
yield f"โ ์ค๋ฅ ๋ฐ์: {str(e)}", 0, {}
|
| 496 |
+
|
| 497 |
+
def generate_screenplay(self, session_id: str, planning_data: Dict,
|
| 498 |
+
progress_callback=None) -> Generator[Tuple[str, float, str], None, None]:
|
| 499 |
+
"""์๋๋ฆฌ์ค ์์ฑ"""
|
| 500 |
+
try:
|
| 501 |
+
total_stages = len(WRITING_STAGES)
|
| 502 |
+
screenplay_content = ""
|
| 503 |
+
act_contents = {"1๋ง": "", "2๋งA": "", "2๋งB": "", "3๋ง": ""}
|
| 504 |
+
|
| 505 |
+
for idx, (stage_name, stage_desc) in enumerate(WRITING_STAGES):
|
| 506 |
+
progress = (idx / total_stages) * 100
|
| 507 |
+
|
| 508 |
+
yield f"๐ {stage_desc} ์งํ ์ค...", progress, screenplay_content
|
| 509 |
+
|
| 510 |
+
# ๋ง ๊ฒฐ์
|
| 511 |
+
current_act = ""
|
| 512 |
+
if "1๋ง" in stage_name:
|
| 513 |
+
current_act = "1๋ง"
|
| 514 |
+
elif "2๋งA" in stage_name:
|
| 515 |
+
current_act = "2๋งA"
|
| 516 |
+
elif "2๋งB" in stage_name:
|
| 517 |
+
current_act = "2๋งB"
|
| 518 |
+
elif "3๋ง" in stage_name:
|
| 519 |
+
current_act = "3๋ง"
|
| 520 |
+
|
| 521 |
+
# ์์ฑ ๋จ๊ณ๋ณ ํ๋กฌํํธ
|
| 522 |
+
if "์ด๊ณ " in stage_name:
|
| 523 |
+
prompt = self._create_writing_prompt(current_act, planning_data, act_contents, "์ด๊ณ ")
|
| 524 |
+
elif "์์ " in stage_name:
|
| 525 |
+
prompt = self._create_writing_prompt(current_act, planning_data, act_contents, "์์ ")
|
| 526 |
+
elif "๊ฒํ " in stage_name:
|
| 527 |
+
prompt = self._create_review_prompt(current_act, act_contents[current_act])
|
| 528 |
+
elif "์์ฑ" in stage_name:
|
| 529 |
+
prompt = self._create_writing_prompt(current_act, planning_data, act_contents, "์์ฑ")
|
| 530 |
+
elif "๋์ฌ ๊ฐํ" in stage_name:
|
| 531 |
+
prompt = self._create_dialogue_prompt(screenplay_content)
|
| 532 |
+
elif "์ต์ข
๊ฒํ " in stage_name:
|
| 533 |
+
prompt = self._create_final_review_prompt(screenplay_content)
|
| 534 |
else:
|
| 535 |
+
continue
|
| 536 |
|
| 537 |
+
# LLM ํธ์ถ
|
| 538 |
+
messages = [
|
| 539 |
+
{"role": "system", "content": "๋น์ ์ ์ ๋ฌธ ์๋๋ฆฌ์ค ์๊ฐ์
๋๋ค. ํ์ค ์๋๋ฆฌ์ค ํฌ๋งท์ผ๋ก ์์ฑํ์ธ์."},
|
| 540 |
+
{"role": "user", "content": prompt}
|
| 541 |
+
]
|
| 542 |
|
| 543 |
+
content = ""
|
| 544 |
+
for chunk in self.call_llm_streaming(messages, max_tokens=15000):
|
| 545 |
+
content += chunk
|
| 546 |
+
|
| 547 |
+
# ๋ง ๋ด์ฉ ์
๋ฐ์ดํธ
|
| 548 |
+
if current_act and "์์ฑ" in stage_name:
|
| 549 |
+
act_contents[current_act] = content
|
| 550 |
+
screenplay_content = "\n\n".join([act_contents[act] for act in ["1๋ง", "2๋งA", "2๋งB", "3๋ง"] if act_contents[act]])
|
| 551 |
+
|
| 552 |
+
yield f"โ๏ธ {stage_desc} ์์ฑ ์ค...", progress, screenplay_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
|
| 554 |
+
time.sleep(0.5)
|
| 555 |
+
|
| 556 |
+
# ์ต์ข
์ ์ฅ
|
| 557 |
+
title = planning_data.get("์ปจ์
๊ฐ๋ฐ", "").split("์ ๋ชฉ")[1].split("\n")[0].strip() if "์ปจ์
๊ฐ๋ฐ" in planning_data else "๋ฌด์ "
|
| 558 |
+
logline = planning_data.get("์ปจ์
๊ฐ๋ฐ", "").split("๋ก๊ทธ๋ผ์ธ")[1].split("\n")[0].strip() if "์ปจ์
๊ฐ๋ฐ" in planning_data else ""
|
| 559 |
+
|
| 560 |
+
ScreenplayDatabase.save_screenplay_content(session_id, screenplay_content, title, logline)
|
| 561 |
+
yield "โ
์๋๋ฆฌ์ค ์์ฑ!", 100, screenplay_content
|
| 562 |
+
|
| 563 |
+
except Exception as e:
|
| 564 |
+
yield f"โ ์ค๋ฅ ๋ฐ์: {str(e)}", 0, ""
|
| 565 |
|
| 566 |
+
def _create_writing_prompt(self, act: str, planning_data: Dict, previous_acts: Dict, phase: str) -> str:
|
| 567 |
+
"""์์ฑ ํ๋กฌํํธ ์์ฑ"""
|
| 568 |
+
|
| 569 |
+
story_structure = planning_data.get("์คํ ๋ฆฌ ๊ตฌ์ฑ", "")
|
| 570 |
+
characters = planning_data.get("์บ๋ฆญํฐ ์ค๊ณ", "")
|
| 571 |
+
scenes = planning_data.get("์ฌ ๊ตฌ์ฑ", "")
|
| 572 |
+
|
| 573 |
+
if phase == "์ด๊ณ ":
|
| 574 |
+
min_lines = 800
|
| 575 |
+
instruction = "์ด๊ณ ๋ฅผ ์์ฑํ์ธ์"
|
| 576 |
+
elif phase == "์์ ":
|
| 577 |
+
min_lines = 1000
|
| 578 |
+
instruction = "๋ด์ฉ์ ํ์ฅํ๊ณ ๋ค๋ฌ์ผ์ธ์"
|
| 579 |
+
else: # ์์ฑ
|
| 580 |
+
min_lines = 1200
|
| 581 |
+
instruction = "์ต์ข
์์ฑ๋ณธ์ ์์ฑํ์ธ์"
|
| 582 |
+
|
| 583 |
+
prompt = f"""{act} {instruction}.
|
| 584 |
|
| 585 |
+
๋ชฉํ ๋ถ๋: ์ต์ {min_lines}์ค
|
| 586 |
+
|
| 587 |
+
์คํ ๋ฆฌ ๊ตฌ์กฐ:
|
| 588 |
+
{story_structure}
|
| 589 |
+
|
| 590 |
+
์บ๋ฆญํฐ:
|
| 591 |
+
{characters}
|
| 592 |
+
|
| 593 |
+
์ฌ ๊ตฌ์ฑ:
|
| 594 |
+
{scenes}
|
| 595 |
+
|
| 596 |
+
์ด์ ๋ง ๋ด์ฉ:
|
| 597 |
+
{previous_acts if previous_acts else "์ฒซ ๋ง์
๋๋ค"}
|
| 598 |
+
|
| 599 |
+
์๊ตฌ์ฌํญ:
|
| 600 |
+
1. ํ์ค ์๋๋ฆฌ์ค ํฌ๋งท ์ฌ์ฉ
|
| 601 |
+
2. INT./EXT. ์ฅ์ - ์๊ฐ
|
| 602 |
+
3. ์บ๋ฆญํฐ๋ช
์ ๋๋ฌธ์
|
| 603 |
+
4. ๋์ฌ๋ ์์ฐ์ค๋ฝ๊ฒ
|
| 604 |
+
5. ์ก์
์ ํ์ฌํ์ผ๋ก ์๊ฐ์ ์ผ๋ก
|
| 605 |
+
6. ๊ฐ ์ฌ๋ง๋ค ์ถฉ๋ถํ ๋ถ๋ (5-7ํ์ด์ง)
|
| 606 |
+
|
| 607 |
+
๋ฐ๋์ {min_lines}์ค ์ด์ ์์ฑํ์ธ์."""
|
| 608 |
+
|
| 609 |
+
return prompt
|
| 610 |
+
|
| 611 |
+
def _create_review_prompt(self, act: str, content: str) -> str:
|
| 612 |
+
"""๊ฒํ ํ๋กฌํํธ"""
|
| 613 |
+
return f"""{act}๋ฅผ ๊ฒํ ํ๊ณ ๊ฐ์ ์ ์ ์ ์ํ์ธ์.
|
| 614 |
+
|
| 615 |
+
๊ฒํ ๋ด์ฉ:
|
| 616 |
+
{content[:2000]}...
|
| 617 |
+
|
| 618 |
+
๊ฒํ ํญ๋ชฉ:
|
| 619 |
+
1. ์คํ ๋ฆฌ ๋
ผ๋ฆฌ์ฑ
|
| 620 |
+
2. ์บ๋ฆญํฐ ์ผ๊ด์ฑ
|
| 621 |
+
3. ๋์ฌ ์์ฐ์ค๋ฌ์
|
| 622 |
+
4. ํ์ด์ฑ
|
| 623 |
+
5. ์๊ฐ์ ๋ช
ํ์ฑ
|
| 624 |
+
|
| 625 |
+
๊ตฌ์ฒด์ ์ธ ๊ฐ์ ์ ์์ ํ์ธ์."""
|
| 626 |
+
|
| 627 |
+
def _create_dialogue_prompt(self, screenplay: str) -> str:
|
| 628 |
+
"""๋์ฌ ๊ฐํ ํ๋กฌํํธ"""
|
| 629 |
+
return f"""์ ์ฒด ์๋๋ฆฌ์ค์ ๋์ฌ๋ฅผ ๊ฒํ ํ๊ณ ๊ฐ์ ํ์ธ์.
|
| 630 |
+
|
| 631 |
+
๊ฐ์ ํฌ์ธํธ:
|
| 632 |
+
1. ์บ๋ฆญํฐ๋ณ ๊ณ ์ ํ ๋งํฌ
|
| 633 |
+
2. ์๋ธํ
์คํธ ์ถ๊ฐ
|
| 634 |
+
3. ๋ถํ์ํ ์ค๋ช
์ ๊ฑฐ
|
| 635 |
+
4. ๊ฐ์ ์ ์ง์ ์ฑ
|
| 636 |
+
5. ์ฅ๋ฅด์ ๋ง๋ ํค
|
| 637 |
+
|
| 638 |
+
์ฃผ์ ๊ฐ์ ์ด ํ์ํ ๋์ฌ๋ค์ ์ฐพ์ ์์ ์์ ์ ์ํ์ธ์."""
|
| 639 |
+
|
| 640 |
+
def _create_final_review_prompt(self, screenplay: str) -> str:
|
| 641 |
+
"""์ต์ข
๊ฒํ ํ๋กฌํํธ"""
|
| 642 |
+
return f"""์์ฑ๋ ์๋๋ฆฌ์ค๋ฅผ ์ต์ข
๊ฒํ ํ์ธ์.
|
| 643 |
+
|
| 644 |
+
ํ๊ฐ ํญ๋ชฉ:
|
| 645 |
+
1. ์ ์ฒด ๊ตฌ์กฐ์ ํ์ด์ฑ
|
| 646 |
+
2. ์บ๋ฆญํฐ ์ํฌ
|
| 647 |
+
3. ํ
๋ง ์ ๋ฌ
|
| 648 |
+
4. ์์
์ฑ
|
| 649 |
+
5. ์ ์ ๊ฐ๋ฅ์ฑ
|
| 650 |
+
|
| 651 |
+
์ดํ๊ณผ ํจ๊ป ์ต์ข
๊ฐ์ ์ฌํญ์ ์ ์ํ์ธ์."""
|
| 652 |
+
|
| 653 |
+
# ์ ํธ๋ฆฌํฐ ํจ์
|
| 654 |
+
def format_planning_display(planning_data: Dict) -> str:
|
| 655 |
+
"""๊ธฐํ์ ํ์ ํฌ๋งท"""
|
| 656 |
+
if not planning_data:
|
| 657 |
+
return "๊ธฐํ์์ด ์์ง ์์ฑ๋์ง ์์์ต๋๋ค."
|
| 658 |
+
|
| 659 |
+
formatted = "# ๐ ์๋๋ฆฌ์ค ๊ธฐํ์\n\n"
|
| 660 |
|
| 661 |
+
for stage_name, content in planning_data.items():
|
| 662 |
+
formatted += f"## {stage_name}\n\n"
|
| 663 |
+
formatted += content + "\n\n"
|
| 664 |
+
formatted += "---\n\n"
|
| 665 |
|
| 666 |
+
return formatted
|
|
|
|
|
|
|
|
|
|
| 667 |
|
| 668 |
def format_screenplay_display(screenplay_text: str) -> str:
|
| 669 |
+
"""์๋๋ฆฌ์ค ํ์ ํฌ๋งท"""
|
| 670 |
if not screenplay_text:
|
| 671 |
+
return "์๋๋ฆฌ์ค๊ฐ ์์ง ์์ฑ๋์ง ์์์ต๋๋ค."
|
| 672 |
|
| 673 |
+
formatted = "# ๐ฌ ์๋๋ฆฌ์ค\n\n"
|
| 674 |
|
| 675 |
+
# ์ฌ ํค๋ฉ ๊ฐ์กฐ
|
| 676 |
formatted_text = re.sub(
|
| 677 |
r'^(INT\.|EXT\.).*$',
|
| 678 |
r'**\g<0>**',
|
|
|
|
| 680 |
flags=re.MULTILINE
|
| 681 |
)
|
| 682 |
|
| 683 |
+
# ์บ๋ฆญํฐ๋ช
๊ฐ์กฐ
|
| 684 |
formatted_text = re.sub(
|
| 685 |
+
r'^([๊ฐ-ํฃA-Z][๊ฐ-ํฃA-Z\s]+)$',
|
| 686 |
r'**\g<0>**',
|
| 687 |
formatted_text,
|
| 688 |
flags=re.MULTILINE
|
| 689 |
)
|
| 690 |
|
| 691 |
+
# ํ์ด์ง ์ ๊ณ์ฐ
|
| 692 |
page_count = len(screenplay_text.splitlines()) / 58
|
| 693 |
+
formatted = f"**์ด ํ์ด์ง: {page_count:.1f}**\n\n" + formatted_text
|
| 694 |
|
| 695 |
return formatted
|
| 696 |
|
| 697 |
+
def generate_random_concept(screenplay_type: str, genre: str) -> str:
|
| 698 |
+
"""๋๋ค ์ปจ์
์์ฑ"""
|
| 699 |
+
concepts = {
|
| 700 |
+
'์ก์
': [
|
| 701 |
+
"์ํดํ ํน์์์์ด ๋ฉ์น๋ ๋ธ์ ๊ตฌํ๊ธฐ ์ํด ๋ค์ ํ์ฅ์ผ๋ก ๋ณต๊ทํ๋ค",
|
| 702 |
+
"ํ๋ฒํ ํ์๊ธฐ์ฌ๊ฐ ์ฐ์ฐํ ๊ตญ์ ํ
๋ฌ ์กฐ์ง์ ์๋ชจ์ ํ๋ง๋ฆฐ๋ค",
|
| 703 |
+
"๋ถํจ ๊ฒฝ์ฐฐ๊ณผ ๋ง์ ์ธ์ฐ๋ ์ ์๋ก์ด ํ์ฌ์ ๊ณ ๋
ํ ์ธ์"
|
| 704 |
+
],
|
| 705 |
+
'์ค๋ฆด๋ฌ': [
|
| 706 |
+
"๊ธฐ์ต์ ์์ ๋จ์๊ฐ ์์ ์ด ์ฐ์์ด์ธ๋ฒ์ด๋ผ๋ ์ฆ๊ฑฐ๋ฅผ ๋ฐ๊ฒฌํ๋ค",
|
| 707 |
+
"์ค์ข
๋ ์์ด๋ฅผ ์ฐพ๋ ๊ณผ์ ์์ ๋ง์์ ๋์ฐํ ๋น๋ฐ์ด ๋๋ฌ๋๋ค",
|
| 708 |
+
"์๋ฒฝํ ์๋ฆฌ๋ฐ์ด๋ฅผ ๊ฐ์ง ์ฉ์์๋ฅผ ์ซ๋ ๊ฒ์ฌ์ ์ง์ํ ์ถ์ "
|
| 709 |
+
],
|
| 710 |
+
'๋๋ผ๋ง': [
|
| 711 |
+
"๋ง๊ธฐ ์ ํ์๊ฐ ๋ง์ง๋ง ์์์ผ๋ก ๊ฐ์กฑ๊ณผ์ ํํด๋ฅผ ์๋ํ๋ค",
|
| 712 |
+
"์ฌ๊ฐ๋ฐ๋ก ์ฌ๋ผ์ง ๋๋ค๋ฅผ ์งํค๋ ค๋ ์ฃผ๋ฏผ๋ค์ ๋ง์ง๋ง ์ธ์",
|
| 713 |
+
"์
์์๊ฐ ์๋ชจ๋ฅผ ์ฐพ์๊ฐ๋ ๊ฐ์ด ์ํ ์ฌ์ "
|
| 714 |
+
],
|
| 715 |
+
'์ฝ๋ฏธ๋': [
|
| 716 |
+
"๊ฒฐํผ์์ฅ์ ์ด์ํ๋ ์ปคํ์ด ์์ ๋ค์ ์ดํผ์ ์จ๊ธฐ๋ฉฐ ์ผํ๋ค",
|
| 717 |
+
"๋ณต๊ถ์ ๋น์ฒจ๋ ๊ฐ์กฑ์ด ์๋ก ๋์ ์ฐจ์งํ๋ ค ์๋ชจ๋ฅผ ๊พธ๋ฏผ๋ค",
|
| 718 |
+
"์ค์๋ก ๋ํต๋ น ๊ฒฝํธ์์ด ๋ ํ๋ฒํ ํ์ฌ์์ ์ข์ถฉ์ฐ๋ ์ด์ผ๊ธฐ"
|
| 719 |
+
],
|
| 720 |
+
'๊ณตํฌ': [
|
| 721 |
+
"ํ๋ณ์์์ ๋ฐค์ ์์กดํด์ผ ํ๋ ์๋์๋ค์ ๊ณตํฌ ์ฒดํ",
|
| 722 |
+
"๊ฑฐ์ธ ์์์ ๋ํ๋๋ ๋ ๋ค๋ฅธ ์์ ๊ณผ ๋ง์ฃผํ ์ฌ์",
|
| 723 |
+
"์ ์ฃผ๋ฐ์ ๊ณจ๋ํ์ ํ๋งคํ ํ ๋ฒ์ด์ง๋ ๊ธฐ์ดํ ์ฌ๊ฑด๋ค"
|
| 724 |
+
],
|
| 725 |
+
'SF': [
|
| 726 |
+
"์๊ฐ ์ฌํ์ผ๋ก ๊ณผ๊ฑฐ๋ฅผ ๋ฐ๊พธ๋ ค๋ค ๋ ํฐ ์ฌ์์ ๋ง๋ ๊ณผํ์",
|
| 727 |
+
"์ธ๊ณต์ง๋ฅ์ด ์ธ๊ฐ์ ๊ฐ์ ์ ํ์ตํ๋ฉฐ ๋ฒ์ด์ง๋ ์์์น ๋ชปํ ์ฌ๊ฑด",
|
| 728 |
+
"์ธ๊ณ ์ ํธ๋ฅผ ํด๋
ํ ์ธ์ดํ์๊ฐ ์ธ๋ฅ์ ์ด๋ช
์ ๊ฒฐ์ ํด์ผ ํ๋ค"
|
| 729 |
+
],
|
| 730 |
+
'๋ก๋งจ์ค': [
|
| 731 |
+
"์ฒซ์ฌ๋๊ณผ 20๋
๋ง์ ์ฌํํ ๋ ์ฌ๋์ ๋ ๋ฒ์งธ ๊ธฐํ",
|
| 732 |
+
"๊ณ์ฝ๊ฒฐํผ์ผ๋ก ์์ํ๋ค๊ฐ ์ง์ง ์ฌ๋์ ๋น ์ง ์ปคํ",
|
| 733 |
+
"์๋ก ๋ค๋ฅธ ์๋๋ฅผ ์ด์๊ฐ๋ ๋ ์ฌ๋์ ํธ์ง๋ฅผ ํตํ ์ฌ๋"
|
| 734 |
+
],
|
| 735 |
+
'ํํ์ง': [
|
| 736 |
+
"ํ๋ฒํ ๊ณ ๋ฑํ์์ด ํํ์ง ์ธ๊ณ์ ์ ํ๋ฐ์ ์ฉ์ฌ๊ฐ ๋๋ค",
|
| 737 |
+
"๋์์ ๋ํ๋ ์ฉ๊ณผ ์ํตํ ์ ์๋ ์ ์ผํ ์ฌ๋",
|
| 738 |
+
"๋ง๋ฒ์ด ์ฌ๋ผ์ง ์ธ๊ณ์์ ๋ง์ง๋ง ๋ง๋ฒ์ฌ๊ฐ ๋์ด๋ฒ๋ฆฐ ์ฒญ๋
"
|
| 739 |
+
]
|
| 740 |
+
}
|
| 741 |
|
| 742 |
+
genre_concepts = concepts.get(genre, concepts['๋๋ผ๋ง'])
|
| 743 |
+
return random.choice(genre_concepts)
|
| 744 |
|
| 745 |
+
# Gradio ์ธํฐํ์ด์ค
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 746 |
def create_interface():
|
| 747 |
css = """
|
| 748 |
.main-header {
|
|
|
|
| 756 |
}
|
| 757 |
|
| 758 |
.header-title {
|
| 759 |
+
font-size: 3rem;
|
| 760 |
margin-bottom: 1rem;
|
| 761 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 762 |
}
|
| 763 |
|
| 764 |
+
.planning-section {
|
| 765 |
+
background: #f8f9fa;
|
| 766 |
+
padding: 1.5rem;
|
| 767 |
+
border-radius: 10px;
|
| 768 |
+
margin: 1rem 0;
|
| 769 |
+
max-height: 600px;
|
| 770 |
+
overflow-y: auto;
|
| 771 |
+
}
|
| 772 |
+
|
| 773 |
+
.screenplay-output {
|
| 774 |
+
font-family: 'Nanum Gothic Coding', 'Courier New', monospace;
|
| 775 |
white-space: pre-wrap;
|
| 776 |
background: white;
|
| 777 |
padding: 2rem;
|
|
|
|
| 780 |
max-height: 800px;
|
| 781 |
overflow-y: auto;
|
| 782 |
}
|
| 783 |
+
|
| 784 |
+
.progress-bar {
|
| 785 |
+
background: #e0e0e0;
|
| 786 |
+
border-radius: 10px;
|
| 787 |
+
height: 30px;
|
| 788 |
+
position: relative;
|
| 789 |
+
margin: 1rem 0;
|
| 790 |
+
}
|
| 791 |
+
|
| 792 |
+
.progress-fill {
|
| 793 |
+
background: linear-gradient(90deg, #667eea, #764ba2);
|
| 794 |
+
height: 100%;
|
| 795 |
+
border-radius: 10px;
|
| 796 |
+
transition: width 0.3s ease;
|
| 797 |
+
}
|
| 798 |
"""
|
| 799 |
|
| 800 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css, title="AI ์๋๋ฆฌ์ค ์๊ฐ") as interface:
|
| 801 |
gr.HTML("""
|
| 802 |
<div class="main-header">
|
| 803 |
+
<h1 class="header-title">๐ฌ AI ์๋๋ฆฌ์ค ์๊ฐ</h1>
|
| 804 |
<p style="font-size: 1.2rem; opacity: 0.95;">
|
| 805 |
+
์ ๋ฌธ๊ฐ ์์ค์ ํ๊ตญ์ด ์๋๋ฆฌ์ค๋ฅผ AI๊ฐ ์์ฑํฉ๋๋ค<br>
|
| 806 |
+
์ํ, ๋๋ผ๋ง, ์น๋๋ผ๋ง, ๋จํธ ์๋๋ฆฌ์ค๋ฅผ ์๋ฒฝํ๊ฒ ๊ตฌ์ฑํฉ๋๋ค
|
| 807 |
</p>
|
| 808 |
</div>
|
| 809 |
""")
|
| 810 |
|
| 811 |
current_session_id = gr.State(None)
|
| 812 |
+
current_planning_data = gr.State({})
|
| 813 |
|
| 814 |
with gr.Tabs():
|
| 815 |
+
with gr.Tab("๐ ์ ์๋๋ฆฌ์ค"):
|
| 816 |
with gr.Row():
|
| 817 |
+
with gr.Column(scale=2):
|
| 818 |
query_input = gr.Textbox(
|
| 819 |
+
label="๐ก ์๋๋ฆฌ์ค ์์ด๋์ด",
|
| 820 |
+
placeholder="""์ํ๋ ์๋๋ฆฌ์ค์ ํต์ฌ ์์ด๋์ด๋ฅผ ์
๋ ฅํ์ธ์. ์์:
|
| 821 |
+
- ๊ธฐ์ต์ ์์ ํ์ฌ๊ฐ ์์ ์ด ์ซ๋ ์ฐ์์ด์ธ๋ฒ์์ ์๊ฒ ๋๋ค
|
| 822 |
+
- ์๊ฐ์ ๋๋๋ฆด ์ ์๋ ๋ฅ๋ ฅ์ ์ป์ ์์ฌ๊ฐ ํ์๋ฅผ ๊ตฌํ๋ ค ํ๋ค
|
| 823 |
+
- ๊ฐ์ํ์ค ๊ฒ์์ ๊ฐํ ํ๋ก๊ฒ์ด๋จธ๊ฐ ํ์ค๋ก ๋์๊ฐ๋ ค ํ๋ค
|
| 824 |
+
- ์กฐ์ ์๋๋ก ํ์์ฌ๋ฆฝํ ํ๋ ๊ฒ์ฌ๊ฐ ๋ถ์ ๋ถํจ์ ์ธ์ด๋ค""",
|
| 825 |
+
lines=5
|
| 826 |
)
|
| 827 |
|
| 828 |
with gr.Column(scale=1):
|
| 829 |
screenplay_type = gr.Radio(
|
| 830 |
choices=list(SCREENPLAY_LENGTHS.keys()),
|
| 831 |
+
value="์ํ",
|
| 832 |
+
label="๐ฝ๏ธ ์๋๋ฆฌ์ค ์ ํ"
|
|
|
|
| 833 |
)
|
| 834 |
|
| 835 |
genre_select = gr.Dropdown(
|
| 836 |
choices=list(GENRE_TEMPLATES.keys()),
|
| 837 |
+
value="๋๋ผ๋ง",
|
| 838 |
+
label="๐ญ ์ฅ๋ฅด"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
)
|
| 840 |
|
| 841 |
with gr.Row():
|
| 842 |
+
random_btn = gr.Button("๏ฟฝ๏ฟฝ ๋๋ค ์์ด๋์ด", scale=1)
|
| 843 |
+
clear_btn = gr.Button("๐๏ธ ์ด๊ธฐํ", scale=1)
|
| 844 |
+
planning_btn = gr.Button("๐ ๊ธฐํ์ ์์ฑ", variant="primary", scale=2)
|
| 845 |
|
| 846 |
+
# ์งํ ์ํ
|
| 847 |
+
progress_bar = gr.HTML(
|
| 848 |
+
value='<div class="progress-bar"><div class="progress-fill" style="width: 0%"></div></div>'
|
| 849 |
+
)
|
| 850 |
status_text = gr.Textbox(
|
| 851 |
+
label="๐ ์งํ ์ํ",
|
| 852 |
interactive=False,
|
| 853 |
+
value="์์ด๋์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ธฐํ์ ์์ฑ์ ์์ํ์ธ์"
|
| 854 |
)
|
| 855 |
|
| 856 |
+
# ๊ธฐํ์ ์น์
|
| 857 |
+
with gr.Group():
|
| 858 |
+
gr.Markdown("### ๐ ์๋๋ฆฌ์ค ๊ธฐํ์")
|
| 859 |
+
planning_display = gr.Markdown(
|
| 860 |
+
value="*๊ธฐํ์์ด ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค...*",
|
| 861 |
+
elem_classes=["planning-section"]
|
| 862 |
+
)
|
| 863 |
+
|
| 864 |
+
with gr.Row():
|
| 865 |
+
edit_planning_btn = gr.Button("โ๏ธ ๊ธฐํ์ ์์ ", scale=1)
|
| 866 |
+
save_planning_btn = gr.Button("๐พ ๊ธฐํ์ ์ ์ฅ", scale=1)
|
| 867 |
+
generate_screenplay_btn = gr.Button("๐ฌ ์๋๋ฆฌ์ค ์์ฑ ์์", variant="primary", scale=2)
|
| 868 |
+
|
| 869 |
+
# ์๋๋ฆฌ์ค ์ถ๋ ฅ
|
| 870 |
+
with gr.Group():
|
| 871 |
+
gr.Markdown("### ๐ ์์ฑ๋ ์๋๋ฆฌ์ค")
|
| 872 |
+
screenplay_output = gr.Markdown(
|
| 873 |
+
value="*์๋๋ฆฌ์ค๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค...*",
|
| 874 |
+
elem_classes=["screenplay-output"]
|
| 875 |
+
)
|
| 876 |
+
|
| 877 |
+
with gr.Row():
|
| 878 |
+
download_btn = gr.Button("๐พ ๋ค์ด๋ก๋ (TXT)", scale=1)
|
| 879 |
+
pdf_btn = gr.Button("๐ PDF ๋ณํ", scale=1)
|
| 880 |
|
| 881 |
+
# ์์
|
| 882 |
gr.Examples(
|
| 883 |
examples=[
|
| 884 |
+
["๊ธฐ์ต์ ์์ ํ์ฌ๊ฐ ์์ ์ด ์ซ๋ ์ฐ์์ด์ธ๋ฒ์์ ์๊ฒ ๋๋ค"],
|
| 885 |
+
["์๊ฐ์ ๋๋๋ฆด ์ ์๋ ๋ฅ๋ ฅ์ ์ป์ ์์ฌ๊ฐ ํ์๋ฅผ ๊ตฌํ๋ ค ํ๋ค"],
|
| 886 |
+
["๊ฐ์ํ์ค ๊ฒ์์ ๊ฐํ ํ๋ก๊ฒ์ด๋จธ๊ฐ ํ์ค๋ก ๋์๊ฐ๋ ค ํ๋ค"],
|
| 887 |
+
["์กฐ์ ์๋๋ก ํ์์ฌ๋ฆฝํ ํ๋ ๊ฒ์ฌ๊ฐ ๋ถ์ ๋ถํจ์ ์ธ์ด๋ค"]
|
| 888 |
],
|
| 889 |
inputs=query_input,
|
| 890 |
+
label="๐ก ์์ ์์ด๋์ด"
|
| 891 |
)
|
| 892 |
|
| 893 |
+
with gr.Tab("๐ ๋ด ์ํ"):
|
| 894 |
+
gr.Markdown("""
|
| 895 |
+
### ๐ ์ ์ฅ๋ ์๋๋ฆฌ์ค
|
| 896 |
+
|
| 897 |
+
์์
์ค์ด๊ฑฐ๋ ์์ฑ๋ ์๋๋ฆฌ์ค๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.
|
| 898 |
+
""")
|
| 899 |
+
|
| 900 |
+
saved_list = gr.Dataframe(
|
| 901 |
+
headers=["์ ๋ชฉ", "์ฅ๋ฅด", "์ ํ", "ํ์ด์ง", "์ํ", "์์ ์ผ"],
|
| 902 |
+
value=[],
|
| 903 |
+
label="์ ์ฅ๋ ์ํ ๋ชฉ๋ก"
|
| 904 |
+
)
|
| 905 |
+
|
| 906 |
with gr.Row():
|
| 907 |
+
refresh_btn = gr.Button("๐ ์๋ก๊ณ ์นจ")
|
| 908 |
+
load_btn = gr.Button("๐ ๋ถ๋ฌ์ค๊ธฐ")
|
| 909 |
+
delete_btn = gr.Button("๐๏ธ ์ญ์ ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 910 |
|
| 911 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 912 |
+
def handle_planning(query, s_type, genre):
|
| 913 |
if not query:
|
| 914 |
+
yield "", "โ ์์ด๋์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์", "", {}
|
| 915 |
return
|
| 916 |
|
| 917 |
+
system = ScreenplayGenerationSystem()
|
| 918 |
+
|
| 919 |
+
for status, progress, planning_data in system.generate_planning(query, s_type, genre):
|
| 920 |
+
progress_html = f'<div class="progress-bar"><div class="progress-fill" style="width: {progress}%"></div></div>'
|
| 921 |
+
planning_display = format_planning_display(planning_data)
|
| 922 |
+
|
| 923 |
+
yield progress_html, status, planning_display, planning_data
|
| 924 |
|
| 925 |
+
def handle_screenplay_generation(session_id, planning_data):
|
| 926 |
+
if not planning_data:
|
| 927 |
+
yield "", "โ ๋จผ์ ๊ธฐํ์์ ์์ฑํด์ฃผ์ธ์", ""
|
| 928 |
+
return
|
| 929 |
+
|
| 930 |
+
system = ScreenplayGenerationSystem()
|
| 931 |
+
|
| 932 |
+
for status, progress, screenplay in system.generate_screenplay(session_id, planning_data):
|
| 933 |
+
progress_html = f'<div class="progress-bar"><div class="progress-fill" style="width: {progress}%"></div></div>'
|
| 934 |
+
screenplay_display = format_screenplay_display(screenplay)
|
| 935 |
+
|
| 936 |
+
yield progress_html, status, screenplay_display
|
| 937 |
+
|
| 938 |
+
def handle_random(s_type, genre):
|
| 939 |
+
return generate_random_concept(s_type, genre)
|
| 940 |
|
| 941 |
def handle_clear():
|
| 942 |
+
empty_progress = '<div class="progress-bar"><div class="progress-fill" style="width: 0%"></div></div>'
|
| 943 |
+
return "", empty_progress, "์ค๋น๋จ", "", "", None, {}
|
| 944 |
|
| 945 |
+
def handle_download(screenplay_text):
|
| 946 |
+
if not screenplay_text or screenplay_text == "*์๋๋ฆฌ์ค๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค...*":
|
| 947 |
return None
|
| 948 |
|
| 949 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 950 |
+
filename = f"์๋๋ฆฌ์ค_{timestamp}.txt"
|
| 951 |
|
| 952 |
with open(filename, 'w', encoding='utf-8') as f:
|
| 953 |
+
# ๋งํฌ๋ค์ด ์ ๊ฑฐ
|
| 954 |
+
clean_text = re.sub(r'\*\*([^*]+)\*\*', r'\1', screenplay_text)
|
| 955 |
+
clean_text = re.sub(r'^#+ ', '', clean_text, flags=re.MULTILINE)
|
| 956 |
+
f.write(clean_text)
|
| 957 |
|
| 958 |
return filename
|
| 959 |
|
| 960 |
+
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 961 |
+
planning_btn.click(
|
| 962 |
+
fn=handle_planning,
|
| 963 |
+
inputs=[query_input, screenplay_type, genre_select],
|
| 964 |
+
outputs=[progress_bar, status_text, planning_display, current_planning_data]
|
| 965 |
+
)
|
| 966 |
+
|
| 967 |
+
generate_screenplay_btn.click(
|
| 968 |
+
fn=handle_screenplay_generation,
|
| 969 |
+
inputs=[current_session_id, current_planning_data],
|
| 970 |
+
outputs=[progress_bar, status_text, screenplay_output]
|
| 971 |
)
|
| 972 |
|
| 973 |
random_btn.click(
|
| 974 |
fn=handle_random,
|
| 975 |
+
inputs=[screenplay_type, genre_select],
|
| 976 |
outputs=[query_input]
|
| 977 |
)
|
| 978 |
|
| 979 |
clear_btn.click(
|
| 980 |
fn=handle_clear,
|
| 981 |
+
outputs=[query_input, progress_bar, status_text, planning_display,
|
| 982 |
+
screenplay_output, current_session_id, current_planning_data]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 983 |
)
|
| 984 |
|
| 985 |
download_btn.click(
|
| 986 |
fn=handle_download,
|
| 987 |
+
inputs=[screenplay_output],
|
| 988 |
+
outputs=[gr.File(label="๋ค์ด๋ก๋")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 989 |
)
|
| 990 |
|
| 991 |
return interface
|
| 992 |
|
| 993 |
+
# ๋ฉ์ธ ์คํ
|
| 994 |
if __name__ == "__main__":
|
| 995 |
logger.info("=" * 60)
|
| 996 |
+
logger.info("AI ์๋๋ฆฌ์ค ์๊ฐ ์์...")
|
| 997 |
+
logger.info("Fireworks AI ๋ชจ๋ธ ์ฌ์ฉ")
|
|
|
|
| 998 |
logger.info("=" * 60)
|
| 999 |
|
| 1000 |
+
if not FIREWORKS_API_KEY or FIREWORKS_API_KEY == "dummy_token_for_testing":
|
| 1001 |
+
logger.warning("โ ๏ธ FIREWORKS_API_KEY๋ฅผ ์ค์ ํด์ฃผ์ธ์!")
|
| 1002 |
+
logger.warning("export FIREWORKS_API_KEY='your-api-key'")
|
| 1003 |
+
|
| 1004 |
ScreenplayDatabase.init_db()
|
| 1005 |
|
| 1006 |
interface = create_interface()
|