openfree commited on
Commit
ae87a48
ยท
verified ยท
1 Parent(s): 230b487

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -87
app.py CHANGED
@@ -1868,107 +1868,86 @@ You provide feedback that's critical yet encouraging."""
1868
 
1869
  return "\n\n---\n\n".join(previous) if previous else ""
1870
 
1871
- def _extract_field(self, content: str, field_pattern: str) -> Optional[str]:
1872
- """Extract field value from content with improved parsing"""
1873
- # More flexible pattern that handles various formats
1874
  pattern = rf'{field_pattern}[:\s]*([^\n]+?)(?=\n[A-Z๊ฐ€-ํžฃ]|$)'
1875
  match = re.search(pattern, content, re.IGNORECASE | re.DOTALL)
1876
- if match and match.group(1): # Check if group(1) exists and is not None
1877
  value = match.group(1).strip()
1878
- # Remove markdown formatting if present
1879
- value = re.sub(r'\*\*', '', value)
1880
- value = re.sub(r'^\s*[-โ€ข]\s*', '', value)
1881
- # Remove trailing punctuation
1882
- value = re.sub(r'[,.:;]
1883
 
1884
  def _parse_character_profile(self, content: str, role: str) -> CharacterProfile:
1885
  """Parse character profile from content"""
1886
- # Debug logging
1887
  logger.debug(f"Parsing character profile for role: {role}")
1888
  logger.debug(f"Content preview: {content[:200]}...")
1889
-
1890
- # Extract name first - handle various formats
1891
- name = f"Character_{role}" # default
1892
  name_patterns = [
1893
- r'(?:์ด๋ฆ„|Name)[:\s]*([^,\n]+?)(?:\s*\([^)]+\))?\s*',
1894
- r'^\s*[-*โ€ข]\s*([^,\n]+?)(?:\s*\([^)]+\))?\s*',
1895
- r'^([^,\n]+?)(?:\s*\([^)]+\))?\s*'
1896
  ]
1897
-
1898
- for pattern in name_patterns:
1899
- name_match = re.search(pattern, content, re.IGNORECASE | re.MULTILINE)
1900
- if name_match and name_match.group(1):
1901
- extracted_name = name_match.group(1).strip()
1902
- # Remove markdown and extra characters
1903
- extracted_name = re.sub(r'[*:\s]+, '', extracted_name)
1904
- extracted_name = re.sub(r'^[*:\s]+', '', extracted_name)
1905
- if extracted_name and len(extracted_name) > 1:
1906
- name = extracted_name
1907
- break
1908
-
1909
- # Helper function to extract clean fields
1910
- def extract_clean_field(patterns):
1911
- if isinstance(patterns, str):
1912
- patterns = [patterns]
1913
-
1914
- for pattern in patterns:
1915
- # Improved pattern with better capturing groups
1916
- match = re.search(rf'{pattern}[:\s]*([^\n*]+?)(?=\n|$)', content, re.IGNORECASE | re.DOTALL)
1917
- if match and match.group(1):
1918
- value = match.group(1).strip()
1919
- # Clean up the value
1920
- value = re.sub(r'^[-*โ€ข:\s]+', '', value)
1921
- value = re.sub(r'[*]+', '', value)
1922
- value = re.sub(r'\s+', ' ', value)
1923
- if value:
1924
- return value
1925
  return ""
1926
-
1927
- # Extract all fields with safer extraction
1928
- profile = CharacterProfile(
 
 
 
 
 
 
 
 
 
 
 
 
1929
  name=name,
1930
  role=role,
1931
- archetype=extract_clean_field([
1932
- r"์บ๋ฆญํ„ฐ ์•„ํฌํƒ€์ž…",
1933
- r"Character Archetype",
1934
- r"Archetype",
1935
- r"์•„ํฌํƒ€์ž…"
1936
- ]),
1937
- want=extract_clean_field([
1938
- r"WANT\s*\(์™ธ์  ๋ชฉํ‘œ\)",
1939
- r"WANT",
1940
- r"์™ธ์  ๋ชฉํ‘œ",
1941
- r"External Goal"
1942
- ]),
1943
- need=extract_clean_field([
1944
- r"NEED\s*\(๋‚ด์  ํ•„์š”\)",
1945
- r"NEED",
1946
- r"๋‚ด์  ํ•„์š”",
1947
- r"Internal Need"
1948
- ]),
1949
- backstory=extract_clean_field([
1950
- r"๋ฐฑ์Šคํ† ๋ฆฌ",
1951
- r"Backstory",
1952
- r"ํ•ต์‹ฌ ์ƒ์ฒ˜",
1953
- r"Core Wound"
1954
- ]),
1955
- personality=self._extract_personality_traits(content),
1956
- speech_pattern=extract_clean_field([
1957
- r"๋งํˆฌ.*?ํŒจํ„ด",
1958
- r"Speech Pattern",
1959
- r"์–ธ์–ด ํŒจํ„ด",
1960
- r"๋งํˆฌ"
1961
- ]),
1962
- character_arc=extract_clean_field([
1963
- r"์บ๋ฆญํ„ฐ ์•„ํฌ",
1964
- r"Character Arc",
1965
- r"Arc",
1966
- r"๋ณ€ํ™”"
1967
- ])
1968
  )
1969
-
1970
- logger.debug(f"Parsed character: {profile.name}")
1971
- return profile
1972
 
1973
  def _extract_personality_traits(self, content: str) -> List[str]:
1974
  """Extract personality traits from content"""
 
1868
 
1869
  return "\n\n---\n\n".join(previous) if previous else ""
1870
 
1871
+ def _extract_field(content: str, field_pattern: str) -> Optional[str]:
 
 
1872
  pattern = rf'{field_pattern}[:\s]*([^\n]+?)(?=\n[A-Z๊ฐ€-ํžฃ]|$)'
1873
  match = re.search(pattern, content, re.IGNORECASE | re.DOTALL)
1874
+ if match and match.group(1):
1875
  value = match.group(1).strip()
1876
+ value = re.sub(r'\*\*', '', value) # **bold** ์ œ๊ฑฐ
1877
+ value = re.sub(r'^\s*[-โ€ข]\s*', '', value) # ๊ธ€๋จธ๋ฆฌํ‘œ ์ œ๊ฑฐ
1878
+ value = re.sub(r'[,.:;]+$', '', value) # ํ–‰ ๋ ๊ตฌ๋‘์  ์ œ๊ฑฐ
1879
+ return value.strip() or None
1880
+ return None
1881
 
1882
  def _parse_character_profile(self, content: str, role: str) -> CharacterProfile:
1883
  """Parse character profile from content"""
 
1884
  logger.debug(f"Parsing character profile for role: {role}")
1885
  logger.debug(f"Content preview: {content[:200]}...")
1886
+
1887
+ # 1) ์ด๋ฆ„ ์ถ”์ถœ โ”€ ํŒจํ„ด 3์ข…
1888
+ name = f"Character_{role}" # fallback
1889
  name_patterns = [
1890
+ r'(?:์ด๋ฆ„|Name)[:\s]*([^\n,(]+)', # "์ด๋ฆ„: ํ™๊ธธ๋™"
1891
+ r'^\s*[-*โ€ข]\s*([^\n,(]+)', # "- ํ™๊ธธ๋™"
1892
+ r'^([^\n,(]+)' # ๋ฌธ๋‹จ ์ฒซ ๋‹จ์–ด
1893
  ]
1894
+ for pat in name_patterns:
1895
+ m = re.search(pat, content, re.IGNORECASE | re.MULTILINE)
1896
+ if m and m.group(1).strip():
1897
+ name = re.sub(r'[\*:\s]+', '', m.group(1).strip()) # ๋ถˆํ•„์š” ๊ธฐํ˜ธ ์ œ๊ฑฐ
1898
+ break
1899
+
1900
+ # 2) ํ•„๋“œ ์ถ”์ถœ์šฉ ํ—ฌํผ
1901
+ def extract_clean_field(pats):
1902
+ pats = [pats] if isinstance(pats, str) else pats
1903
+ for p in pats:
1904
+ m = re.search(rf'{p}[:\s]*([^\n*]+?)(?=\n|$)', content,
1905
+ re.IGNORECASE | re.DOTALL)
1906
+ if m and m.group(1).strip():
1907
+ v = m.group(1).strip()
1908
+ v = re.sub(r'^[-*โ€ข:\s]+', '', v) # ๋ฆฌ์ŠคํŠธยท๊ธฐํ˜ธ ์ œ๊ฑฐ
1909
+ v = v.replace('*', '').strip()
1910
+ return v
 
 
 
 
 
 
 
 
 
 
 
1911
  return ""
1912
+
1913
+ # 3) Personality(์—ฌ๋Ÿฌ ์ค„) ๋”ฐ๋กœ ํŒŒ์‹ฑ
1914
+ def extract_traits():
1915
+ section = re.search(r'(?:Personality|์„ฑ๊ฒฉ[^\n]*)\n((?:[-*โ€ข].+\n?)+)',
1916
+ content, re.IGNORECASE)
1917
+ if not section:
1918
+ return []
1919
+ traits = [
1920
+ re.sub(r'^[-*โ€ข]\s*', '', line.strip())
1921
+ for line in section.group(1).splitlines() if line.strip()
1922
+ ]
1923
+ return traits[:5]
1924
+
1925
+ # 4) CharacterProfile ์ƒ์„ฑ
1926
+ return CharacterProfile(
1927
  name=name,
1928
  role=role,
1929
+ archetype=extract_clean_field(
1930
+ [r"์บ๋ฆญํ„ฐ ์•„ํฌํƒ€์ž…", r"Character Archetype", r"Archetype", r"์•„ํฌํƒ€์ž…"]
1931
+ ),
1932
+ want=extract_clean_field(
1933
+ [r"WANT\s*\(์™ธ์  ๋ชฉํ‘œ\)", r"WANT", r"์™ธ์  ๋ชฉํ‘œ", r"External Goal"]
1934
+ ),
1935
+ need=extract_clean_field(
1936
+ [r"NEED\s*\(๋‚ด์  ํ•„์š”\)", r"NEED", r"๋‚ด์  ํ•„์š”", r"Internal Need"]
1937
+ ),
1938
+ backstory=extract_clean_field(
1939
+ [r"๋ฐฑ์Šคํ† ๋ฆฌ", r"Backstory", r"ํ•ต์‹ฌ ์ƒ์ฒ˜", r"Core Wound"]
1940
+ ),
1941
+ personality=extract_traits(),
1942
+ speech_pattern=extract_clean_field(
1943
+ [r"๋งํˆฌ.*?ํŒจํ„ด", r"Speech Pattern", r"์–ธ์–ด ํŒจํ„ด", r"๋งํˆฌ"]
1944
+ ),
1945
+ character_arc=extract_clean_field(
1946
+ [r"์บ๋ฆญํ„ฐ ์•„ํฌ", r"Character Arc", r"Arc", r"๋ณ€ํ™”"]
1947
+ ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1948
  )
1949
+
1950
+
 
1951
 
1952
  def _extract_personality_traits(self, content: str) -> List[str]:
1953
  """Extract personality traits from content"""