Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -486,16 +486,29 @@ class ScreenplayGenerationSystem:
|
|
| 486 |
}
|
| 487 |
|
| 488 |
def call_llm_streaming(self, messages: List[Dict[str, str]], max_tokens: int = 8000) -> Generator[str, None, None]:
|
|
|
|
| 489 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
payload = {
|
| 491 |
"model": self.model_id,
|
| 492 |
"messages": messages,
|
| 493 |
"max_tokens": max_tokens,
|
| 494 |
-
"temperature": 0.
|
| 495 |
-
"top_p": 0.
|
| 496 |
-
"top_k":
|
| 497 |
-
"presence_penalty": 0.
|
| 498 |
-
"frequency_penalty": 0.
|
| 499 |
"stream": True
|
| 500 |
}
|
| 501 |
|
|
@@ -514,6 +527,8 @@ class ScreenplayGenerationSystem:
|
|
| 514 |
return
|
| 515 |
|
| 516 |
buffer = ""
|
|
|
|
|
|
|
| 517 |
for line in response.iter_lines():
|
| 518 |
if not line:
|
| 519 |
continue
|
|
@@ -525,20 +540,41 @@ class ScreenplayGenerationSystem:
|
|
| 525 |
|
| 526 |
data_str = line_str[6:]
|
| 527 |
if data_str == "[DONE]":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
break
|
| 529 |
|
| 530 |
data = json.loads(data_str)
|
| 531 |
if "choices" in data and len(data["choices"]) > 0:
|
| 532 |
content = data["choices"][0].get("delta", {}).get("content", "")
|
| 533 |
if content:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
buffer += content
|
| 535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
yield buffer
|
| 537 |
buffer = ""
|
| 538 |
-
|
|
|
|
|
|
|
| 539 |
continue
|
| 540 |
|
|
|
|
| 541 |
if buffer:
|
|
|
|
| 542 |
yield buffer
|
| 543 |
|
| 544 |
except Exception as e:
|
|
@@ -705,11 +741,17 @@ class ScreenplayGenerationSystem:
|
|
| 705 |
|
| 706 |
def generate_act(self, session_id: str, act_name: str, planning_data: Dict,
|
| 707 |
previous_acts: Dict) -> Generator[Tuple[ActProgress, str], None, None]:
|
| 708 |
-
"""๋ง๋ณ ์๋๋ฆฌ์ค ์์ฑ"""
|
|
|
|
| 709 |
try:
|
| 710 |
self.current_session_id = session_id
|
| 711 |
self.planning_data = planning_data
|
| 712 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
stages = ACT_WRITING_STAGES.get(act_name, [])
|
| 714 |
act_progress = ActProgress(
|
| 715 |
act_name=act_name,
|
|
@@ -732,29 +774,49 @@ class ScreenplayGenerationSystem:
|
|
| 732 |
# ์ญํ ๋ณ ํ๋กฌํํธ ์์ฑ
|
| 733 |
prompt = self._create_act_prompt(role, act_name, act_content, planning_data, previous_acts, stage_type)
|
| 734 |
|
| 735 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
messages = [
|
| 737 |
-
{"role": "system", "content":
|
| 738 |
{"role": "user", "content": prompt}
|
| 739 |
]
|
| 740 |
|
| 741 |
expert_output = ""
|
| 742 |
-
|
|
|
|
|
|
|
| 743 |
if chunk and not chunk.startswith("โ"):
|
| 744 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
# ์ค์๊ฐ ์
๋ฐ์ดํธ
|
| 746 |
-
yield act_progress, f"โ๏ธ {role}: ์์
์ค..."
|
| 747 |
|
| 748 |
# ์น ๊ฒ์ ๊ณ ์ฆ
|
| 749 |
if role == "๊ณ ์ฆ์ ๋ฌธ๊ฐ" and self.web_searcher.enabled:
|
| 750 |
verification = self.web_searcher.verify_facts(act_content, act_name)
|
| 751 |
expert_output += f"\n\nใ์น ๊ฒ์ฆ ๊ฒฐ๊ณผใ\n{json.dumps(verification, ensure_ascii=False, indent=2)}"
|
| 752 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
# ํผ๋๋ฐฑ ์ ์ฅ
|
| 754 |
feedback = ExpertFeedback(
|
| 755 |
role=role,
|
| 756 |
stage=f"{act_name}_{stage_type}",
|
| 757 |
-
feedback=
|
| 758 |
suggestions=self._extract_suggestions(expert_output),
|
| 759 |
score=85.0 + random.uniform(-5, 10)
|
| 760 |
)
|
|
@@ -767,7 +829,7 @@ class ScreenplayGenerationSystem:
|
|
| 767 |
elif stage_type == "์ด๊ณ ":
|
| 768 |
act_content = expert_output
|
| 769 |
|
| 770 |
-
yield act_progress, f"โ
{role} ์์
์๋ฃ"
|
| 771 |
|
| 772 |
time.sleep(0.5)
|
| 773 |
|
|
@@ -776,50 +838,258 @@ class ScreenplayGenerationSystem:
|
|
| 776 |
break
|
| 777 |
except Exception as e:
|
| 778 |
logger.error(f"Error in act stage {idx}: {e}")
|
|
|
|
| 779 |
continue
|
| 780 |
|
| 781 |
# ๋ง ์์ฑ
|
| 782 |
act_progress.status = "complete"
|
| 783 |
ScreenplayDatabase.save_act_content(session_id, act_name, act_content)
|
| 784 |
|
| 785 |
-
|
|
|
|
|
|
|
| 786 |
|
| 787 |
except Exception as e:
|
| 788 |
logger.error(f"Act generation error: {e}\n{traceback.format_exc()}")
|
| 789 |
act_progress.status = "error"
|
| 790 |
yield act_progress, f"โ ์ค๋ฅ: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 791 |
|
| 792 |
def _create_act_prompt(self, role: str, act_name: str, current_content: str,
|
| 793 |
planning_data: Dict, previous_acts: Dict, stage_type: str) -> str:
|
| 794 |
-
"""๋ง ์์ฑ ํ๋กฌํํธ ์์ฑ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
|
| 796 |
if role == "์คํ ๋ฆฌ์๊ฐ":
|
| 797 |
if stage_type == "์ด๊ณ ":
|
| 798 |
return f"""ใ{act_name} ์ด๊ณ ์์ฑใ
|
| 799 |
-
|
| 800 |
|
| 801 |
-
๊ธฐํ์
|
| 802 |
-
|
|
|
|
| 803 |
|
| 804 |
-
|
|
|
|
|
|
|
|
|
|
| 805 |
{self._summarize_previous_acts(previous_acts)}
|
| 806 |
|
| 807 |
-
|
| 808 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 809 |
else: # ์์ฑ
|
| 810 |
return f"""ใ{act_name} ์ต์ข
์์ฑใ
|
|
|
|
|
|
|
| 811 |
์ด์ ์ ๋ฌธ๊ฐ๋ค์ ํผ๋๋ฐฑ์ ๋ฐ์ํ์ฌ ์ต์ข
๋ณธ์ ์์ฑํ์ธ์.
|
| 812 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
elif role == "๊ณ ์ฆ์ ๋ฌธ๊ฐ":
|
| 815 |
return f"""ใ{act_name} ์ฌ์ค ํ์ธใ
|
| 816 |
-
|
|
|
|
|
|
|
| 817 |
{current_content[:2000]}...
|
| 818 |
|
| 819 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
|
| 821 |
-
|
| 822 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 823 |
|
| 824 |
def _summarize_planning(self, planning_data: Dict) -> str:
|
| 825 |
"""๊ธฐํ์ ์์ฝ"""
|
|
|
|
| 486 |
}
|
| 487 |
|
| 488 |
def call_llm_streaming(self, messages: List[Dict[str, str]], max_tokens: int = 8000) -> Generator[str, None, None]:
|
| 489 |
+
"""LLM ํธ์ถ - ๊ฐ์ ๋ ๋ฒ์ """
|
| 490 |
try:
|
| 491 |
+
# ํ๊ตญ์ด ๊ฐ์ ๋ฐ ์์ ์ฑ ๋ณด์ฅ์ ์ํ ์์คํ
๋ฉ์์ง ๊ฐํ
|
| 492 |
+
if messages and messages[0].get("role") == "system":
|
| 493 |
+
messages[0]["content"] = messages[0]["content"] + """
|
| 494 |
+
|
| 495 |
+
ใ์ ๋ ์ค์ ์ฌํญใ
|
| 496 |
+
1. ๋ชจ๋ ์๋ต์ ์์ ํ ํ๊ตญ์ด๋ก ์์ฑํ์ธ์.
|
| 497 |
+
2. ์์ด ๋จ์ด๋ ๋ฌธ์ฅ์ ์ ๋ ์ฌ์ฉํ์ง ๋ง์ธ์.
|
| 498 |
+
3. ... ์ด๋ ๋ฏธ์์ฑ ๋ฌธ์ฅ์ ์์ฑํ์ง ๋ง์ธ์.
|
| 499 |
+
4. ๋ชจ๋ ๋ฌธ์ฅ์ ์์ ํ๊ฒ ๋ง๋ฌด๋ฆฌํ์ธ์.
|
| 500 |
+
5. ์๋๋ฆฌ์ค๋ ์ํ ํฌ๋งท์ด๋ฉฐ, ์ฐ๊ทน์ด ์๋๋๋ค.
|
| 501 |
+
"""
|
| 502 |
+
|
| 503 |
payload = {
|
| 504 |
"model": self.model_id,
|
| 505 |
"messages": messages,
|
| 506 |
"max_tokens": max_tokens,
|
| 507 |
+
"temperature": 0.6, # ๋ ์ผ๊ด์ฑ ์๊ฒ ๋ฎ์ถค
|
| 508 |
+
"top_p": 0.85, # ๋ ๋ณด์์ ์ผ๋ก
|
| 509 |
+
"top_k": 30, # ๋ ์ ํ์ ์ผ๋ก
|
| 510 |
+
"presence_penalty": 0.4, # ๋ฐ๋ณต ๋ฐฉ์ง ๊ฐํ
|
| 511 |
+
"frequency_penalty": 0.4,
|
| 512 |
"stream": True
|
| 513 |
}
|
| 514 |
|
|
|
|
| 527 |
return
|
| 528 |
|
| 529 |
buffer = ""
|
| 530 |
+
incomplete_check = 0
|
| 531 |
+
|
| 532 |
for line in response.iter_lines():
|
| 533 |
if not line:
|
| 534 |
continue
|
|
|
|
| 540 |
|
| 541 |
data_str = line_str[6:]
|
| 542 |
if data_str == "[DONE]":
|
| 543 |
+
# ๋ง์ง๋ง์ ๋ถ์์ ํ ๋ด์ฉ ์ฒดํฌ
|
| 544 |
+
if buffer and "..." in buffer[-100:]:
|
| 545 |
+
buffer = buffer.replace("...", "")
|
| 546 |
+
if buffer:
|
| 547 |
+
yield buffer
|
| 548 |
break
|
| 549 |
|
| 550 |
data = json.loads(data_str)
|
| 551 |
if "choices" in data and len(data["choices"]) > 0:
|
| 552 |
content = data["choices"][0].get("delta", {}).get("content", "")
|
| 553 |
if content:
|
| 554 |
+
# ์์ด ๊ฐ์ง ๋ฐ ํํฐ๋ง
|
| 555 |
+
if any(ord(c) < 128 and c.isalpha() for c in content):
|
| 556 |
+
# ์์ด๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ์คํตํ๊ฑฐ๋ ๊ฒฝ๊ณ
|
| 557 |
+
logger.warning(f"English detected in content: {content[:50]}")
|
| 558 |
+
# INT. EXT. ๊ฐ์ ์๋๋ฆฌ์ค ํฌ๋งท์ ํ์ฉ
|
| 559 |
+
if not any(term in content for term in ["INT.", "EXT.", "CUT TO", "FADE"]):
|
| 560 |
+
continue
|
| 561 |
+
|
| 562 |
buffer += content
|
| 563 |
+
|
| 564 |
+
# ์ถฉ๋ถํ ๋ฒํผ๊ฐ ์์ด๋ฉด yield
|
| 565 |
+
if len(buffer) >= 200 or '\n\n' in buffer:
|
| 566 |
+
# ๋ถ์์ ํ ... ์ ๊ฑฐ
|
| 567 |
+
buffer = buffer.replace("......", "").replace(".....", "")
|
| 568 |
yield buffer
|
| 569 |
buffer = ""
|
| 570 |
+
|
| 571 |
+
except Exception as e:
|
| 572 |
+
logger.error(f"Line processing error: {e}")
|
| 573 |
continue
|
| 574 |
|
| 575 |
+
# ๋จ์ ๋ฒํผ ์ฒ๋ฆฌ
|
| 576 |
if buffer:
|
| 577 |
+
buffer = buffer.replace("......", "").replace(".....", "")
|
| 578 |
yield buffer
|
| 579 |
|
| 580 |
except Exception as e:
|
|
|
|
| 741 |
|
| 742 |
def generate_act(self, session_id: str, act_name: str, planning_data: Dict,
|
| 743 |
previous_acts: Dict) -> Generator[Tuple[ActProgress, str], None, None]:
|
| 744 |
+
"""๋ง๋ณ ์๋๋ฆฌ์ค ์์ฑ - ๊ฐ์ ๋ ๋ฒ์ """
|
| 745 |
+
|
| 746 |
try:
|
| 747 |
self.current_session_id = session_id
|
| 748 |
self.planning_data = planning_data
|
| 749 |
|
| 750 |
+
# ์ธ์
์ ๋ณด ๋ณต์
|
| 751 |
+
session_data = ScreenplayDatabase.get_session_data(session_id)
|
| 752 |
+
if session_data:
|
| 753 |
+
self.original_query = session_data.get('user_query', '')
|
| 754 |
+
|
| 755 |
stages = ACT_WRITING_STAGES.get(act_name, [])
|
| 756 |
act_progress = ActProgress(
|
| 757 |
act_name=act_name,
|
|
|
|
| 774 |
# ์ญํ ๋ณ ํ๋กฌํํธ ์์ฑ
|
| 775 |
prompt = self._create_act_prompt(role, act_name, act_content, planning_data, previous_acts, stage_type)
|
| 776 |
|
| 777 |
+
# ํ๊ตญ์ด ๋ฐ ์ํ ํฌ๋งท ๊ฐ์กฐ
|
| 778 |
+
system_message = f"""๋น์ ์ {role}์
๋๋ค.
|
| 779 |
+
{EXPERT_ROLES[role]['description']}
|
| 780 |
+
|
| 781 |
+
ใ์ ๋ ๊ท์นใ
|
| 782 |
+
1. ๋ชจ๋ ๋ด์ฉ์ ํ๊ตญ์ด๋ก ์์ฑํ์ธ์.
|
| 783 |
+
2. ์ํ ์๋๋ฆฌ์ค ํฌ๋งท์ ์ฌ์ฉํ์ธ์ (์ฐ๊ทน ์๋).
|
| 784 |
+
3. ๊ธฐํ์ ๋ด์ฉ์ 100% ๋ฐ์ํ์ธ์.
|
| 785 |
+
4. ์์ด ์ฌ์ฉ ๊ธ์ง (INT. EXT. ๋ฑ ํฌ๋งท ์ฉ์ด ์ ์ธ).
|
| 786 |
+
5. ์์ ํ ๋ฌธ์ฅ์ผ๋ก ์์ฑ (... ์ฌ์ฉ ๊ธ์ง)."""
|
| 787 |
+
|
| 788 |
messages = [
|
| 789 |
+
{"role": "system", "content": system_message},
|
| 790 |
{"role": "user", "content": prompt}
|
| 791 |
]
|
| 792 |
|
| 793 |
expert_output = ""
|
| 794 |
+
line_count = 0
|
| 795 |
+
|
| 796 |
+
for chunk in self.call_llm_streaming(messages, max_tokens=12000):
|
| 797 |
if chunk and not chunk.startswith("โ"):
|
| 798 |
+
# ์์ด ํํฐ๋ง (ํฌ๋งท ์ฉ์ด ์ ์ธ)
|
| 799 |
+
cleaned_chunk = self._clean_output(chunk)
|
| 800 |
+
expert_output += cleaned_chunk
|
| 801 |
+
line_count = len(expert_output.split('\n'))
|
| 802 |
+
|
| 803 |
# ์ค์๊ฐ ์
๋ฐ์ดํธ
|
| 804 |
+
yield act_progress, f"โ๏ธ {role}: ์์
์ค... ({line_count}์ค ์์ฑ)"
|
| 805 |
|
| 806 |
# ์น ๊ฒ์ ๊ณ ์ฆ
|
| 807 |
if role == "๊ณ ์ฆ์ ๋ฌธ๊ฐ" and self.web_searcher.enabled:
|
| 808 |
verification = self.web_searcher.verify_facts(act_content, act_name)
|
| 809 |
expert_output += f"\n\nใ์น ๊ฒ์ฆ ๊ฒฐ๊ณผใ\n{json.dumps(verification, ensure_ascii=False, indent=2)}"
|
| 810 |
|
| 811 |
+
# ์ถ๋ ฅ ๊ฒ์ฆ ๋ฐ ์ ๋ฆฌ
|
| 812 |
+
if stage_type in ["์ด๊ณ ", "์์ฑ"]:
|
| 813 |
+
expert_output = self._validate_and_clean_screenplay(expert_output)
|
| 814 |
+
|
| 815 |
# ํผ๋๋ฐฑ ์ ์ฅ
|
| 816 |
feedback = ExpertFeedback(
|
| 817 |
role=role,
|
| 818 |
stage=f"{act_name}_{stage_type}",
|
| 819 |
+
feedback=f"{role} ์์
์๋ฃ. {line_count}์ค ์์ฑ.",
|
| 820 |
suggestions=self._extract_suggestions(expert_output),
|
| 821 |
score=85.0 + random.uniform(-5, 10)
|
| 822 |
)
|
|
|
|
| 829 |
elif stage_type == "์ด๊ณ ":
|
| 830 |
act_content = expert_output
|
| 831 |
|
| 832 |
+
yield act_progress, f"โ
{role} ์์
์๋ฃ ({line_count}์ค)"
|
| 833 |
|
| 834 |
time.sleep(0.5)
|
| 835 |
|
|
|
|
| 838 |
break
|
| 839 |
except Exception as e:
|
| 840 |
logger.error(f"Error in act stage {idx}: {e}")
|
| 841 |
+
yield act_progress, f"โ ๏ธ {role} ์์
์ค ์ค๋ฅ ๋ฐ์"
|
| 842 |
continue
|
| 843 |
|
| 844 |
# ๋ง ์์ฑ
|
| 845 |
act_progress.status = "complete"
|
| 846 |
ScreenplayDatabase.save_act_content(session_id, act_name, act_content)
|
| 847 |
|
| 848 |
+
# ์ต์ข
ํ์ด์ง ์ ๊ณ์ฐ
|
| 849 |
+
page_count = len(act_content.split('\n')) / 58
|
| 850 |
+
yield act_progress, f"โ
{act_name} ์์ฑ! (์ฝ {page_count:.1f}ํ์ด์ง)"
|
| 851 |
|
| 852 |
except Exception as e:
|
| 853 |
logger.error(f"Act generation error: {e}\n{traceback.format_exc()}")
|
| 854 |
act_progress.status = "error"
|
| 855 |
yield act_progress, f"โ ์ค๋ฅ: {str(e)}"
|
| 856 |
+
|
| 857 |
+
def _clean_output(self, text: str) -> str:
|
| 858 |
+
"""์ถ๋ ฅ ํ
์คํธ ์ ๋ฆฌ"""
|
| 859 |
+
# ์์ด ๋จ์ด ์ ๊ฑฐ (์๋๋ฆฌ์ค ํฌ๋งท ์ ์ธ)
|
| 860 |
+
format_terms = ["INT.", "EXT.", "CUT TO:", "FADE IN:", "FADE OUT:", "DISSOLVE TO:"]
|
| 861 |
+
|
| 862 |
+
lines = text.split('\n')
|
| 863 |
+
cleaned_lines = []
|
| 864 |
+
|
| 865 |
+
for line in lines:
|
| 866 |
+
# ํฌ๋งท ์ฉ์ด๋ ์ ์ง
|
| 867 |
+
if any(term in line for term in format_terms):
|
| 868 |
+
cleaned_lines.append(line)
|
| 869 |
+
else:
|
| 870 |
+
# ์์ด ๊ฐ์ง ๋ฐ ์ ๊ฑฐ
|
| 871 |
+
if not self._contains_english(line):
|
| 872 |
+
# ๋ถ์์ ํ ๋ถ๋ถ ์ ๊ฑฐ
|
| 873 |
+
line = line.replace("......", "").replace(".....", "").replace("...", "")
|
| 874 |
+
cleaned_lines.append(line)
|
| 875 |
+
else:
|
| 876 |
+
# ์์ด๊ฐ ํฌํจ๋ ์ค์ ์คํตํ๊ฑฐ๋ ๊ฒฝ๊ณ
|
| 877 |
+
logger.warning(f"Skipping English line: {line[:50]}")
|
| 878 |
+
|
| 879 |
+
return '\n'.join(cleaned_lines)
|
| 880 |
+
|
| 881 |
+
def _contains_english(self, text: str) -> bool:
|
| 882 |
+
"""์์ด ํฌํจ ์ฌ๋ถ ํ์ธ (ํฌ๋งท ์ฉ์ด ์ ์ธ)"""
|
| 883 |
+
format_terms = ["INT", "EXT", "CUT", "FADE", "DISSOLVE"]
|
| 884 |
+
|
| 885 |
+
# ํฌ๋งท ์ฉ์ด ์ ๊ฑฐ ํ ํ์ธ
|
| 886 |
+
test_text = text
|
| 887 |
+
for term in format_terms:
|
| 888 |
+
test_text = test_text.replace(term, "")
|
| 889 |
+
|
| 890 |
+
# ์ํ๋ฒณ ์ฐ์ 3๊ฐ ์ด์์ด๋ฉด ์์ด๋ก ํ๋จ
|
| 891 |
+
import re
|
| 892 |
+
english_pattern = re.compile(r'[a-zA-Z]{3,}')
|
| 893 |
+
return bool(english_pattern.search(test_text))
|
| 894 |
+
|
| 895 |
+
def _validate_and_clean_screenplay(self, content: str) -> str:
|
| 896 |
+
"""์๋๋ฆฌ์ค ๊ฒ์ฆ ๋ฐ ์ ๋ฆฌ"""
|
| 897 |
+
if not content:
|
| 898 |
+
return ""
|
| 899 |
+
|
| 900 |
+
lines = content.split('\n')
|
| 901 |
+
cleaned = []
|
| 902 |
+
|
| 903 |
+
for line in lines:
|
| 904 |
+
# ๋น ์ค์ ์ ์ง
|
| 905 |
+
if not line.strip():
|
| 906 |
+
cleaned.append(line)
|
| 907 |
+
continue
|
| 908 |
+
|
| 909 |
+
# ๋ถ์์ ํ ๋ถ๋ถ ์ ๊ฑฐ
|
| 910 |
+
if "..." in line and not line.strip().endswith('.'):
|
| 911 |
+
continue
|
| 912 |
+
|
| 913 |
+
# ์์ด ์ฒดํฌ (ํฌ๋งท ์ ์ธ)
|
| 914 |
+
if self._contains_english(line):
|
| 915 |
+
logger.warning(f"Removing English line: {line[:50]}")
|
| 916 |
+
continue
|
| 917 |
+
|
| 918 |
+
cleaned.append(line)
|
| 919 |
+
|
| 920 |
+
return '\n'.join(cleaned)
|
| 921 |
+
|
| 922 |
+
@staticmethod
|
| 923 |
+
def get_session_data(session_id: str) -> Optional[Dict]:
|
| 924 |
+
"""์ธ์
๋ฐ์ดํฐ ์กฐํ"""
|
| 925 |
+
with ScreenplayDatabase.get_db() as conn:
|
| 926 |
+
row = conn.cursor().execute(
|
| 927 |
+
'SELECT * FROM screenplay_sessions WHERE session_id = ?',
|
| 928 |
+
(session_id,)
|
| 929 |
+
).fetchone()
|
| 930 |
+
if row:
|
| 931 |
+
return dict(row)
|
| 932 |
+
return None
|
| 933 |
|
| 934 |
def _create_act_prompt(self, role: str, act_name: str, current_content: str,
|
| 935 |
planning_data: Dict, previous_acts: Dict, stage_type: str) -> str:
|
| 936 |
+
"""๋ง ์์ฑ ํ๋กฌํํธ ์์ฑ - ๊ฐ์ ๋ ๋ฒ์ """
|
| 937 |
+
|
| 938 |
+
# ๊ธฐํ์์์ ํต์ฌ ์ ๋ณด ์ถ์ถ
|
| 939 |
+
title = ""
|
| 940 |
+
genre = ""
|
| 941 |
+
screenplay_type = ""
|
| 942 |
+
logline = ""
|
| 943 |
+
synopsis = ""
|
| 944 |
+
characters = ""
|
| 945 |
+
|
| 946 |
+
for key, value in planning_data.items():
|
| 947 |
+
if "ํ๋ก๋์" in key and value:
|
| 948 |
+
# ์ ๋ชฉ๊ณผ ๋ก๊ทธ๋ผ์ธ ์ถ์ถ
|
| 949 |
+
if "์ ๋ชฉ" in value:
|
| 950 |
+
title_match = re.search(r'์ ๋ชฉ[:\s]*([^\n]+)', value)
|
| 951 |
+
if title_match:
|
| 952 |
+
title = title_match.group(1).strip()
|
| 953 |
+
if "๋ก๊ทธ๋ผ์ธ" in value:
|
| 954 |
+
logline_match = re.search(r'๋ก๊ทธ๋ผ์ธ[:\s]*([^\n]+)', value)
|
| 955 |
+
if logline_match:
|
| 956 |
+
logline = logline_match.group(1).strip()
|
| 957 |
+
elif "์คํ ๋ฆฌ" in key and value:
|
| 958 |
+
synopsis = value[:1000]
|
| 959 |
+
elif "์บ๋ฆญํฐ" in key and value:
|
| 960 |
+
characters = value[:1000]
|
| 961 |
+
|
| 962 |
+
# ์๋ณธ ์์ฒญ ์ ๋ณด ๊ฐ์กฐ
|
| 963 |
+
core_info = f"""
|
| 964 |
+
ใํต์ฌ ์ ๋ณด - ์ ๋ ์ค์ใ
|
| 965 |
+
์๋ณธ ์์ฒญ: {self.original_query}
|
| 966 |
+
์ ๋ชฉ: {title}
|
| 967 |
+
๋ก๊ทธ๋ผ์ธ: {logline}
|
| 968 |
+
์ฅ๋ฅด: {genre if genre else '๋๋ผ๋ง'}
|
| 969 |
+
ํ์: {screenplay_type if screenplay_type else '์ํ'}
|
| 970 |
+
|
| 971 |
+
โ ๏ธ ์ค์: ์ ์ ๋ณด๋ฅผ ์ ๋์ ์ผ๋ก ์ค์ํ์ธ์. ๋ค๋ฅธ ์ด์ผ๊ธฐ๋ก ๋ฐ๊พธ์ง ๋ง์ธ์.
|
| 972 |
+
โ ๏ธ ์ค์: ๋ชจ๋ ๋ด์ฉ์ ํ๊ตญ์ด๋ก๋ง ์์ฑํ์ธ์. ์์ด ์ฌ์ฉ ๊ธ์ง.
|
| 973 |
+
โ ๏ธ ์ค์: ํ์ค ์๋๋ฆฌ์ค ํฌ๋งท์ ์ ํํ ์ค์ํ์ธ์.
|
| 974 |
+
"""
|
| 975 |
|
| 976 |
if role == "์คํ ๋ฆฌ์๊ฐ":
|
| 977 |
if stage_type == "์ด๊ณ ":
|
| 978 |
return f"""ใ{act_name} ์ด๊ณ ์์ฑใ
|
| 979 |
+
{core_info}
|
| 980 |
|
| 981 |
+
ใ๊ธฐํ์ ๋ด์ฉใ
|
| 982 |
+
์๋์์ค:
|
| 983 |
+
{synopsis}
|
| 984 |
|
| 985 |
+
์บ๋ฆญํฐ:
|
| 986 |
+
{characters}
|
| 987 |
+
|
| 988 |
+
ใ์ด์ ๋ง ๋ด์ฉใ
|
| 989 |
{self._summarize_previous_acts(previous_acts)}
|
| 990 |
|
| 991 |
+
ใ์์ฑ ์๊ตฌ์ฌํญใ
|
| 992 |
+
1. ๋ฐ๋์ ํ๊ตญ์ด๋ก๋ง ์์ฑ
|
| 993 |
+
2. ํ์ค ์๋๋ฆฌ์ค ํฌ๋งท:
|
| 994 |
+
INT. ์ฅ์ - ์๊ฐ (๋๋ EXT. ์ฅ์ - ์๊ฐ)
|
| 995 |
+
|
| 996 |
+
์ฅ๋ฉด ์ค๋ช
์ ํ์ฌํ์ผ๋ก ์์ฑ.
|
| 997 |
+
|
| 998 |
+
์บ๋ฆญํฐ๋ช
(๋๋ฌธ์)
|
| 999 |
+
๋์ฌ๋ ์์ฐ์ค๋ฌ์ด ํ๊ตญ์ด๋ก.
|
| 1000 |
+
|
| 1001 |
+
3. ๋ถ๋: ์ต์ 1000์ค ์ด์
|
| 1002 |
+
4. ๊ฐ ์ฌ: 5-7ํ์ด์ง ๋ถ๋
|
| 1003 |
+
5. ๊ธฐํ์ ๋ด์ฉ 100% ๋ฐ์
|
| 1004 |
+
|
| 1005 |
+
์ ๋ ์์ด๋ฅผ ์ฌ์ฉํ์ง ๋ง์ธ์.
|
| 1006 |
+
์ ๋ ๋ค๋ฅธ ์ด์ผ๊ธฐ๋ก ๋ฐ๊พธ์ง ๋ง์ธ์.
|
| 1007 |
+
์ ๋ ์ฐ๊ทน์ด๋ ๋ค๋ฅธ ํ์์ผ๋ก ์์ฑํ์ง ๋ง์ธ์.
|
| 1008 |
+
|
| 1009 |
+
{act_name}์ ์์ ํ ์๋๋ฆฌ์ค ํ์์ผ๋ก ์์ฑํ์ธ์."""
|
| 1010 |
+
|
| 1011 |
else: # ์์ฑ
|
| 1012 |
return f"""ใ{act_name} ์ต์ข
์์ฑใ
|
| 1013 |
+
{core_info}
|
| 1014 |
+
|
| 1015 |
์ด์ ์ ๋ฌธ๊ฐ๋ค์ ํผ๋๋ฐฑ์ ๋ฐ์ํ์ฌ ์ต์ข
๋ณธ์ ์์ฑํ์ธ์.
|
| 1016 |
+
|
| 1017 |
+
ใํ์ ์ฌํญใ
|
| 1018 |
+
1. ํ๊ตญ์ด๋ก๋ง ์์ฑ
|
| 1019 |
+
2. ์ํ ์๋๋ฆฌ์ค ํฌ๋งท ์ค์
|
| 1020 |
+
3. ๊ธฐํ์ ์คํ ๋ฆฌ ์ ์ง
|
| 1021 |
+
4. 1200์ค ์ด์
|
| 1022 |
+
|
| 1023 |
+
๊นจ์ง ๋ถ๋ถ์ด๋ ... ๊ฐ์ ๋ฏธ์์ฑ ๋ถ๋ถ ์์ด ์์ ํ๊ฒ ์์ฑํ์ธ์."""
|
| 1024 |
|
| 1025 |
elif role == "๊ณ ์ฆ์ ๋ฌธ๊ฐ":
|
| 1026 |
return f"""ใ{act_name} ์ฌ์ค ํ์ธใ
|
| 1027 |
+
{core_info}
|
| 1028 |
+
|
| 1029 |
+
๋ค์ ์๋๋ฆฌ์ค๋ฅผ ๊ฒํ ํ์ธ์:
|
| 1030 |
{current_content[:2000]}...
|
| 1031 |
|
| 1032 |
+
ใ๊ฒํ ์ฌํญใ
|
| 1033 |
+
1. ์ฌ์ค ์ค๋ฅ ํ์ธ
|
| 1034 |
+
2. ์๋ ๊ณ ์ฆ
|
| 1035 |
+
3. ์์ด ์ฌ์ฉ ๋ถ๋ถ์ ํ๊ตญ์ด๋ก ์์
|
| 1036 |
+
4. ๊นจ์ง ํ
์คํธ๋ ... ๋ถ๋ถ ์์
|
| 1037 |
+
|
| 1038 |
+
๋ชจ๋ ์์ ์ฌํญ์ ํ๊ตญ์ด๋ก ์ ์ํ์ธ์."""
|
| 1039 |
+
|
| 1040 |
+
elif role == "ํธ์ง์":
|
| 1041 |
+
return f"""ใ{act_name} ํธ์งใ
|
| 1042 |
+
{core_info}
|
| 1043 |
+
|
| 1044 |
+
ใํธ์ง ์ง์นจใ
|
| 1045 |
+
1. ์์ด๊ฐ ์๋ค๋ฉด ๋ชจ๋ ํ๊ตญ์ด๋ก ๋ณ๊ฒฝ
|
| 1046 |
+
2. ๊นจ์ง ๋ถ๋ถ(...) ์์ฑ
|
| 1047 |
+
3. ๊ธฐํ์๊ณผ ์ผ์นํ์ง ์๋ ๋ด์ฉ ์์
|
| 1048 |
+
4. ์๋๋ฆฌ์ค ํฌ๋งท ๊ต์
|
| 1049 |
+
|
| 1050 |
+
ํธ์ง๋ ๋ฒ์ ์ ์์ ํ ํ๊ตญ์ด๋ก ์ ์ํ์ธ์."""
|
| 1051 |
+
|
| 1052 |
+
elif role == "๊ฐ๋
":
|
| 1053 |
+
return f"""ใ{act_name} ์ฐ์ถ ๋
ธํธใ
|
| 1054 |
+
{core_info}
|
| 1055 |
+
|
| 1056 |
+
ใ์ฐ์ถ ๊ฐํใ
|
| 1057 |
+
1. ์๊ฐ์ ๋ํ
์ผ ์ถ๊ฐ
|
| 1058 |
+
2. ์นด๋ฉ๋ผ ์ต๊ธ ์ ์
|
| 1059 |
+
3. ๋ถ์๊ธฐ ๋ฌ์ฌ ๊ฐํ
|
| 1060 |
+
|
| 1061 |
+
๋ชจ๋ ์ฐ์ถ ๋
ธํธ๋ฅผ ํ๊ตญ์ด๋ก ์์ฑํ์ธ์.
|
| 1062 |
+
์ํ ์๋๋ฆฌ์ค์์ ๋ช
ํํ ํ์ธ์."""
|
| 1063 |
+
|
| 1064 |
+
elif role == "๋ํ์ ๋ฌธ๊ฐ":
|
| 1065 |
+
return f"""ใ{act_name} ๋์ฌ ๊ฐ์ ใ
|
| 1066 |
+
{core_info}
|
| 1067 |
+
|
| 1068 |
+
ใ๋์ฌ ์์ ใ
|
| 1069 |
+
1. ๋ชจ๋ ๋์ฌ๋ฅผ ์์ฐ์ค๋ฌ์ด ํ๊ตญ์ด๋ก
|
| 1070 |
+
2. ์์ด ๋์ฌ๊ฐ ์๋ค๋ฉด ํ๊ตญ์ด๋ก ๋ฒ์ญ
|
| 1071 |
+
3. ์บ๋ฆญํฐ๋ณ ๋งํฌ ์ฐจ๋ณํ
|
| 1072 |
+
4. ์ด์ํ ๋ฒ์ญํฌ ์ ๊ฑฐ
|
| 1073 |
+
|
| 1074 |
+
์์ ํ ํ๊ตญ์ด ๋์ฌ๋ก ์์ ํ์ธ์."""
|
| 1075 |
|
| 1076 |
+
elif role == "๋นํ๊ฐ":
|
| 1077 |
+
return f"""ใ{act_name} ๊ฒํ ใ
|
| 1078 |
+
{core_info}
|
| 1079 |
+
|
| 1080 |
+
ใํ๊ฐ ํญ๋ชฉใ
|
| 1081 |
+
1. ๊ธฐํ์๊ณผ์ ์ผ์น๋ (์ต์ฐ์ )
|
| 1082 |
+
2. ํ๊ตญ์ด ์ฌ์ฉ (์์ด ํผ์ฉ ๊ธ์ง)
|
| 1083 |
+
3. ์๋๋ฆฌ์ค ํฌ๋งท ์ค์
|
| 1084 |
+
4. ์คํ ๋ฆฌ ์์ฑ๋
|
| 1085 |
+
|
| 1086 |
+
๋ฌธ์ ์ ๊ณผ ๊ฐ์ ์ฌํญ์ ๊ตฌ์ฒด์ ์ผ๋ก ์ ์ํ์ธ์."""
|
| 1087 |
+
|
| 1088 |
+
return f"""ใ{role} ์์
ใ
|
| 1089 |
+
{core_info}
|
| 1090 |
+
|
| 1091 |
+
{act_name}์(๋ฅผ) ๊ฒํ ํ๊ณ ๊ฐ์ ํ์ธ์.
|
| 1092 |
+
๋ฐ๋์ ํ๊ตญ์ด๋ก๋ง ์์ฑํ๊ณ , ๊ธฐํ์ ๋ด์ฉ์ ์ถฉ์คํ ๋ฐ์ํ์ธ์."""
|
| 1093 |
|
| 1094 |
def _summarize_planning(self, planning_data: Dict) -> str:
|
| 1095 |
"""๊ธฐํ์ ์์ฝ"""
|