openfree commited on
Commit
8ae8099
ยท
verified ยท
1 Parent(s): 8866e62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -24
app.py CHANGED
@@ -485,6 +485,11 @@ class ScreenplayGenerationSystem:
485
  "Authorization": f"Bearer {self.api_key}"
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:
@@ -743,23 +748,29 @@ class ScreenplayGenerationSystem:
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,
758
- current_stage=0,
759
- total_stages=len(stages),
760
- current_expert="",
761
- status="in_progress"
762
- )
763
 
764
  act_content = ""
765
 
@@ -853,44 +864,81 @@ class ScreenplayGenerationSystem:
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
  """์‹œ๋‚˜๋ฆฌ์˜ค ๊ฒ€์ฆ ๋ฐ ์ •๋ฆฌ"""
@@ -1107,6 +1155,23 @@ class ScreenplayGenerationSystem:
1107
  summary += f"[{act}]\n{content[:300]}...\n"
1108
  return summary[:1000]
1109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1110
  # UI ํ•จ์ˆ˜๋“ค
1111
  def create_act_progress_display(act_progress: ActProgress) -> str:
1112
  """๋ง‰๋ณ„ ์ง„ํ–‰ ์ƒํ™ฉ ํ‘œ์‹œ"""
 
485
  "Authorization": f"Bearer {self.api_key}"
486
  }
487
 
488
+ def get_session_data(self, session_id: str) -> Optional[Dict]:
489
+ """์„ธ์…˜ ๋ฐ์ดํ„ฐ ์กฐํšŒ - ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ"""
490
+ return ScreenplayDatabase.get_session_data(session_id)
491
+
492
+
493
  def call_llm_streaming(self, messages: List[Dict[str, str]], max_tokens: int = 8000) -> Generator[str, None, None]:
494
  """LLM ํ˜ธ์ถœ - ๊ฐœ์„ ๋œ ๋ฒ„์ „"""
495
  try:
 
748
  previous_acts: Dict) -> Generator[Tuple[ActProgress, str], None, None]:
749
  """๋ง‰๋ณ„ ์‹œ๋‚˜๋ฆฌ์˜ค ์ƒ์„ฑ - ๊ฐœ์„ ๋œ ๋ฒ„์ „"""
750
 
751
+ # act_progress๋ฅผ ๋จผ์ € ์ดˆ๊ธฐํ™”
752
+ act_progress = ActProgress(
753
+ act_name=act_name,
754
+ current_stage=0,
755
+ total_stages=0,
756
+ current_expert="",
757
+ status="initializing"
758
+ )
759
+
760
  try:
761
  self.current_session_id = session_id
762
  self.planning_data = planning_data
763
 
764
  # ์„ธ์…˜ ์ •๋ณด ๋ณต์›
765
+ session_data = self.get_session_data(session_id) # ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ˆ˜์ •
766
  if session_data:
767
  self.original_query = session_data.get('user_query', '')
768
 
769
  stages = ACT_WRITING_STAGES.get(act_name, [])
770
+
771
+ # act_progress ์—…๋ฐ์ดํŠธ
772
+ act_progress.total_stages = len(stages)
773
+ act_progress.status = "in_progress"
 
 
 
774
 
775
  act_content = ""
776
 
 
864
  logger.error(f"Act generation error: {e}\n{traceback.format_exc()}")
865
  act_progress.status = "error"
866
  yield act_progress, f"โŒ ์˜ค๋ฅ˜: {str(e)}"
867
+
868
  def _clean_output(self, text: str) -> str:
869
  """์ถœ๋ ฅ ํ…์ŠคํŠธ ์ •๋ฆฌ"""
870
+ # ์‹œ๋‚˜๋ฆฌ์˜ค ํฌ๋งท ์šฉ์–ด ํ™•์žฅ
871
+ format_terms = [
872
+ "INT.", "EXT.", "CUT TO:", "FADE IN:", "FADE OUT:",
873
+ "DISSOLVE TO:", "CONT'D", "O.S.", "V.O.", "POV",
874
+ "MONTAGE", "FLASHBACK", "DREAM SEQUENCE", "CLOSE UP",
875
+ "WIDE SHOT", "ESTABLISHING SHOT"
876
+ ]
877
+
878
+ # ํ•œ๊ตญ์–ด ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์•ฝ์–ด๋“ค
879
+ korean_screenplay_abbreviations = [
880
+ "CG", "SFX", "BGM", "OST", "NG", "OK", "VIP", "CEO", "AI",
881
+ "DJ", "MC", "PD", "VJ", "IT", "PC", "TV", "DVD", "USB"
882
+ ]
883
 
884
  lines = text.split('\n')
885
  cleaned_lines = []
886
 
887
  for line in lines:
888
  # ํฌ๋งท ์šฉ์–ด๋Š” ์œ ์ง€
889
+ if any(term in line.upper() for term in format_terms):
890
  cleaned_lines.append(line)
891
  else:
892
+ # ์•ฝ์–ด๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ ํ—ˆ์šฉ
893
+ contains_abbr = any(abbr in line for abbr in korean_screenplay_abbreviations)
894
+
895
+ # ์˜์–ด ๊ฐ์ง€ (์•ฝ์–ด ์ œ์™ธ)
896
+ if contains_abbr or not self._contains_english(line):
897
  # ๋ถˆ์™„์ „ํ•œ ๋ถ€๋ถ„ ์ œ๊ฑฐ
898
+ line = line.replace("......", "").replace(".....", "").replace("...", ".")
899
  cleaned_lines.append(line)
900
  else:
901
+ # ์‹ค์ œ ์˜์–ด๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ๋งŒ ๊ฒฝ๊ณ 
902
+ if len(line.strip()) > 10: # ์งง์€ ์ค„์€ ๋ฌด์‹œ
903
+ logger.debug(f"Skipping potential English line: {line[:50]}")
904
 
905
  return '\n'.join(cleaned_lines)
906
+
907
+
908
+
909
  def _contains_english(self, text: str) -> bool:
910
+ """์˜์–ด ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ (ํฌ๋งท ์šฉ์–ด ๋ฐ ํ—ˆ์šฉ ๋‹จ์–ด ์ œ์™ธ)"""
911
+ # ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ ํ—ˆ์šฉ๋˜๋Š” ์šฉ์–ด๋“ค
912
+ allowed_terms = [
913
+ "INT", "EXT", "CUT", "FADE", "DISSOLVE", "CONT'D",
914
+ "O.S.", "V.O.", "POV", "CG", "SFX", "BGM", "OST",
915
+ "vs", "ex", "D", "Main", "Conflict", "wang", "PO", "V"
916
+ ]
917
 
918
  # ํฌ๋งท ์šฉ์–ด ์ œ๊ฑฐ ํ›„ ํ™•์ธ
919
  test_text = text
920
+ for term in allowed_terms:
921
  test_text = test_text.replace(term, "")
922
 
923
+ # ์•ŒํŒŒ๋ฒณ ์—ฐ์† 4๊ฐœ ์ด์ƒ์ด๋ฉด ์˜์–ด๋กœ ํŒ๋‹จ (3๊ฐœ์—์„œ 4๊ฐœ๋กœ ๋ณ€๊ฒฝ)
924
  import re
925
+ english_pattern = re.compile(r'[a-zA-Z]{4,}')
926
+ matches = english_pattern.findall(test_text)
927
+
928
+ # ๋ฐœ๊ฒฌ๋œ ์˜์–ด ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉด ๋กœ๊น…ํ•˜๊ณ  ํ™•์ธ
929
+ if matches:
930
+ # ์ถ”๊ฐ€ ํ•„ํ„ฐ๋ง: ํ•œ๊ตญ์–ด ์†์˜ ์•ฝ์–ด๋‚˜ ์ด๋‹ˆ์…œ์€ ํ—ˆ์šฉ
931
+ real_english = []
932
+ for match in matches:
933
+ if len(match) > 5: # 5์ž ์ด์ƒ์˜ ์˜์–ด ๋‹จ์–ด๋งŒ ์‹ค์ œ ์˜์–ด๋กœ ํŒ๋‹จ
934
+ real_english.append(match)
935
+
936
+ if real_english:
937
+ logger.debug(f"Real English words found: {real_english}")
938
+ return True
939
+
940
+ return False
941
+
942
 
943
  def _validate_and_clean_screenplay(self, content: str) -> str:
944
  """์‹œ๋‚˜๋ฆฌ์˜ค ๊ฒ€์ฆ ๋ฐ ์ •๋ฆฌ"""
 
1155
  summary += f"[{act}]\n{content[:300]}...\n"
1156
  return summary[:1000]
1157
 
1158
+
1159
+
1160
+ @staticmethod
1161
+ def get_session_data(session_id: str) -> Optional[Dict]:
1162
+ """์„ธ์…˜ ๋ฐ์ดํ„ฐ ์กฐํšŒ"""
1163
+ with ScreenplayDatabase.get_db() as conn:
1164
+ cursor = conn.cursor()
1165
+ row = cursor.execute(
1166
+ 'SELECT * FROM screenplay_sessions WHERE session_id = ?',
1167
+ (session_id,)
1168
+ ).fetchone()
1169
+ if row:
1170
+ # Row ๊ฐ์ฒด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ณ€ํ™˜
1171
+ columns = [description[0] for description in cursor.description]
1172
+ return dict(zip(columns, row))
1173
+ return None
1174
+
1175
  # UI ํ•จ์ˆ˜๋“ค
1176
  def create_act_progress_display(act_progress: ActProgress) -> str:
1177
  """๋ง‰๋ณ„ ์ง„ํ–‰ ์ƒํ™ฉ ํ‘œ์‹œ"""