Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -370,7 +370,7 @@ class NovelDatabase:
|
|
370 |
|
371 |
@staticmethod
|
372 |
def get_all_writer_content(session_id: str, test_mode: bool = False) -> str:
|
373 |
-
"""๋ชจ๋ ์๊ฐ์ ์์ ๋ณธ ๋ด์ฉ์ ๊ฐ์ ธ์์ ํฉ์น๊ธฐ
|
374 |
with NovelDatabase.get_db() as conn:
|
375 |
cursor = conn.cursor()
|
376 |
|
@@ -380,12 +380,14 @@ class NovelDatabase:
|
|
380 |
|
381 |
if test_mode:
|
382 |
# ํ
์คํธ ๋ชจ๋: writer1 revision + writer10 content
|
383 |
-
# Writer 1 ์์ ๋ณธ
|
384 |
cursor.execute('''
|
385 |
SELECT content, stage_name, word_count FROM stages
|
386 |
-
WHERE session_id = ? AND role = 'writer1'
|
|
|
387 |
''', (session_id,))
|
388 |
|
|
|
389 |
row = cursor.fetchone()
|
390 |
if row and row['content']:
|
391 |
clean_content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', row['content'])
|
@@ -1986,20 +1988,27 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
1986 |
logger.error(f"Session not found: {session_id}")
|
1987 |
return None
|
1988 |
|
|
|
|
|
|
|
|
|
1989 |
# DB์์ ๋ชจ๋ ์คํ
์ด์ง ๊ฐ์ ธ์ค๊ธฐ
|
1990 |
stages = NovelDatabase.get_stages(session_id)
|
1991 |
logger.info(f"Found {len(stages)} stages in database")
|
1992 |
|
1993 |
# ๋๋ฒ๊น
: ๋ชจ๋ stage ์ ๋ณด ์ถ๋ ฅ
|
1994 |
-
for stage in stages:
|
1995 |
-
|
1996 |
-
|
|
|
|
|
|
|
1997 |
|
1998 |
# ํ
์คํธ ๋ชจ๋ ๊ฐ์ง - writer10์ด ์์ผ๋ฉด ํ
์คํธ ๋ชจ๋
|
1999 |
is_test_mode = False
|
2000 |
-
has_writer10 = any(stage['role'] == 'writer10' for stage in stages)
|
2001 |
-
has_writer1 = any(stage['role'] == 'writer1' for stage in stages)
|
2002 |
-
has_writer3 = any(stage['role'] == 'writer3' for stage in stages)
|
2003 |
|
2004 |
if has_writer10 and has_writer1 and not has_writer3:
|
2005 |
is_test_mode = True
|
@@ -2045,17 +2054,20 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2045 |
if is_test_mode:
|
2046 |
# ํ
์คํธ ๋ชจ๋: writer1 revision + writer10 ๋ด์ฉ ์ฒ๋ฆฌ
|
2047 |
for stage in stages:
|
2048 |
-
|
|
|
|
|
|
|
|
|
2049 |
|
2050 |
# Writer 1 ์์ ๋ณธ
|
2051 |
-
if
|
2052 |
-
content = stage['content'] or ""
|
2053 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2054 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2055 |
content = content.strip()
|
2056 |
|
2057 |
if content:
|
2058 |
-
word_count = stage['word_count']
|
2059 |
total_words += word_count
|
2060 |
writer_contents.append({
|
2061 |
'writer_num': 1,
|
@@ -2066,8 +2078,7 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2066 |
logger.info(f"Added writer 1 (Chapter 1): {word_count} words")
|
2067 |
|
2068 |
# Writer 10 (ํ
์คํธ ๋ชจ๋์์ ๋๋จธ์ง ์ฑํฐ๋ค)
|
2069 |
-
elif
|
2070 |
-
content = stage['content'] or ""
|
2071 |
logger.info(f"Processing writer10 content: {len(content)} chars")
|
2072 |
|
2073 |
# [Chapter X] ํจํด์ผ๋ก ์ฑํฐ ๋ถ๋ฆฌ
|
@@ -2094,34 +2105,37 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2094 |
logger.info("[NORMAL MODE] Processing all writer revisions...")
|
2095 |
|
2096 |
for stage in stages:
|
2097 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2098 |
|
2099 |
# ์ธ์ด์ ์๊ด์์ด ์๊ฐ ์์ ๋ณธ ์ฐพ๊ธฐ
|
2100 |
-
|
2101 |
-
|
2102 |
-
('Revision' in stage['stage_name'] or '์์ ๋ณธ' in stage['stage_name']))
|
2103 |
|
2104 |
-
if is_revision:
|
2105 |
# ์๊ฐ ๋ฒํธ ์ถ์ถ
|
2106 |
try:
|
2107 |
-
writer_num = int(
|
2108 |
-
logger.info(f"Found writer {writer_num} revision - stage_name: {
|
2109 |
except:
|
2110 |
-
logger.warning(f"Could not extract writer number from role: {
|
2111 |
continue
|
2112 |
|
2113 |
-
content = stage['content'] or ""
|
2114 |
-
|
2115 |
# ํ์ด์ง ๋งํฌ ์ ๊ฑฐ
|
2116 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2117 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2118 |
content = content.strip()
|
2119 |
|
2120 |
if content:
|
2121 |
-
word_count = stage['word_count']
|
2122 |
total_words += word_count
|
2123 |
writer_contents.append({
|
2124 |
-
'writer_num': writer_num,
|
2125 |
'content': content,
|
2126 |
'word_count': word_count
|
2127 |
})
|
@@ -2139,7 +2153,7 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2139 |
doc.add_paragraph(f'Total Chapters: {writer_count}')
|
2140 |
doc.add_paragraph(f'Total Words: {total_words:,}')
|
2141 |
doc.add_paragraph(f'Estimated Pages: ~{total_words/500:.0f}')
|
2142 |
-
doc.add_paragraph(f'Language: {
|
2143 |
doc.add_page_break()
|
2144 |
|
2145 |
# ๋ชฉ์ฐจ
|
@@ -2210,7 +2224,7 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2210 |
f.write(f"AI COLLABORATIVE NOVEL - {'TEST MODE' if is_test_mode else 'COMPLETE VERSION'}\n")
|
2211 |
f.write("="*60 + "\n")
|
2212 |
f.write(f"Theme: {session['user_query']}\n")
|
2213 |
-
f.write(f"Language: {
|
2214 |
f.write(f"Created: {datetime.now()}\n")
|
2215 |
f.write(f"Mode: {'Test Mode (Actual chapters)' if is_test_mode else 'Full Mode (10 chapters)'}\n")
|
2216 |
f.write("="*60 + "\n\n")
|
@@ -2221,15 +2235,18 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2221 |
if is_test_mode:
|
2222 |
# ํ
์คํธ ๋ชจ๋: writer1 + writer10 ์ฒ๋ฆฌ
|
2223 |
for stage in stages:
|
|
|
|
|
|
|
|
|
2224 |
# Writer 1 ์์ ๋ณธ
|
2225 |
-
if
|
2226 |
-
content = stage['content'] or ""
|
2227 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2228 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2229 |
content = content.strip()
|
2230 |
|
2231 |
if content:
|
2232 |
-
word_count = stage['word_count']
|
2233 |
total_words += word_count
|
2234 |
writer_count = 1
|
2235 |
|
@@ -2241,9 +2258,7 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2241 |
f.write("\n\n")
|
2242 |
|
2243 |
# Writer 10
|
2244 |
-
elif
|
2245 |
-
content = stage['content'] or ""
|
2246 |
-
|
2247 |
# [Chapter X] ํจํด์ผ๋ก ์ฑํฐ ๋ถ๋ฆฌ
|
2248 |
chapters = re.split(r'\[Chapter\s+(\d+)\]', content)
|
2249 |
|
@@ -2266,26 +2281,28 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
|
|
2266 |
else:
|
2267 |
# ์ผ๋ฐ ๋ชจ๋ - ์์ ๋ ๋ก์ง
|
2268 |
for stage in stages:
|
|
|
|
|
|
|
|
|
2269 |
# ์ธ์ด์ ์๊ด์์ด ์๊ฐ ์์ ๋ณธ ์ฐพ๊ธฐ
|
2270 |
-
|
2271 |
-
|
2272 |
-
('Revision' in stage['stage_name'] or '์์ ๋ณธ' in stage['stage_name']))
|
2273 |
|
2274 |
-
if is_revision:
|
2275 |
# ์๊ฐ ๋ฒํธ ์ถ์ถ
|
2276 |
try:
|
2277 |
-
writer_num = int(
|
2278 |
except:
|
2279 |
continue
|
2280 |
|
2281 |
-
content = stage['content'] or ""
|
2282 |
# ํ์ด์ง ๋งํฌ ์ ๊ฑฐ
|
2283 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2284 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2285 |
content = content.strip()
|
2286 |
|
2287 |
if content:
|
2288 |
-
word_count = stage['word_count']
|
2289 |
total_words += word_count
|
2290 |
writer_count += 1
|
2291 |
|
|
|
370 |
|
371 |
@staticmethod
|
372 |
def get_all_writer_content(session_id: str, test_mode: bool = False) -> str:
|
373 |
+
"""๋ชจ๋ ์๊ฐ์ ์์ ๋ณธ ๋ด์ฉ์ ๊ฐ์ ธ์์ ํฉ์น๊ธฐ"""
|
374 |
with NovelDatabase.get_db() as conn:
|
375 |
cursor = conn.cursor()
|
376 |
|
|
|
380 |
|
381 |
if test_mode:
|
382 |
# ํ
์คํธ ๋ชจ๋: writer1 revision + writer10 content
|
383 |
+
# Writer 1 ์์ ๋ณธ - ์ธ์ด ๋ฌด๊ด
|
384 |
cursor.execute('''
|
385 |
SELECT content, stage_name, word_count FROM stages
|
386 |
+
WHERE session_id = ? AND role = 'writer1'
|
387 |
+
AND (stage_name LIKE '%Revision%' OR stage_name LIKE '%์์ ๋ณธ%')
|
388 |
''', (session_id,))
|
389 |
|
390 |
+
|
391 |
row = cursor.fetchone()
|
392 |
if row and row['content']:
|
393 |
clean_content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', row['content'])
|
|
|
1988 |
logger.error(f"Session not found: {session_id}")
|
1989 |
return None
|
1990 |
|
1991 |
+
# ์ธ์
์ ์ค์ ์ธ์ด ์ฌ์ฉ (ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ language ๋์ )
|
1992 |
+
actual_language = session['language']
|
1993 |
+
logger.info(f"Using session language: {actual_language} (parameter was: {language})")
|
1994 |
+
|
1995 |
# DB์์ ๋ชจ๋ ์คํ
์ด์ง ๊ฐ์ ธ์ค๊ธฐ
|
1996 |
stages = NovelDatabase.get_stages(session_id)
|
1997 |
logger.info(f"Found {len(stages)} stages in database")
|
1998 |
|
1999 |
# ๋๋ฒ๊น
: ๋ชจ๋ stage ์ ๋ณด ์ถ๋ ฅ
|
2000 |
+
for i, stage in enumerate(stages):
|
2001 |
+
role = stage['role'] if 'role' in stage.keys() else 'None'
|
2002 |
+
stage_name = stage['stage_name'] if 'stage_name' in stage.keys() else 'None'
|
2003 |
+
content_len = len(stage['content']) if 'content' in stage.keys() and stage['content'] else 0
|
2004 |
+
status = stage['status'] if 'status' in stage.keys() else 'None'
|
2005 |
+
logger.debug(f"Stage {i}: role={role}, stage_name={stage_name}, content_length={content_len}, status={status}")
|
2006 |
|
2007 |
# ํ
์คํธ ๋ชจ๋ ๊ฐ์ง - writer10์ด ์์ผ๋ฉด ํ
์คํธ ๋ชจ๋
|
2008 |
is_test_mode = False
|
2009 |
+
has_writer10 = any(stage['role'] == 'writer10' for stage in stages if 'role' in stage.keys())
|
2010 |
+
has_writer1 = any(stage['role'] == 'writer1' for stage in stages if 'role' in stage.keys())
|
2011 |
+
has_writer3 = any(stage['role'] == 'writer3' for stage in stages if 'role' in stage.keys())
|
2012 |
|
2013 |
if has_writer10 and has_writer1 and not has_writer3:
|
2014 |
is_test_mode = True
|
|
|
2054 |
if is_test_mode:
|
2055 |
# ํ
์คํธ ๋ชจ๋: writer1 revision + writer10 ๋ด์ฉ ์ฒ๋ฆฌ
|
2056 |
for stage in stages:
|
2057 |
+
role = stage['role'] if 'role' in stage.keys() else None
|
2058 |
+
stage_name = stage['stage_name'] if 'stage_name' in stage.keys() else ''
|
2059 |
+
content = stage['content'] if 'content' in stage.keys() else ''
|
2060 |
+
|
2061 |
+
logger.info(f"[TEST MODE] Checking stage: role={role}, name={stage_name}, status={stage['status'] if 'status' in stage.keys() else 'None'}")
|
2062 |
|
2063 |
# Writer 1 ์์ ๋ณธ
|
2064 |
+
if role == 'writer1' and stage_name and ('Revision' in stage_name or '์์ ๋ณธ' in stage_name):
|
|
|
2065 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2066 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2067 |
content = content.strip()
|
2068 |
|
2069 |
if content:
|
2070 |
+
word_count = stage['word_count'] if 'word_count' in stage.keys() else len(content.split())
|
2071 |
total_words += word_count
|
2072 |
writer_contents.append({
|
2073 |
'writer_num': 1,
|
|
|
2078 |
logger.info(f"Added writer 1 (Chapter 1): {word_count} words")
|
2079 |
|
2080 |
# Writer 10 (ํ
์คํธ ๋ชจ๋์์ ๋๋จธ์ง ์ฑํฐ๋ค)
|
2081 |
+
elif role == 'writer10':
|
|
|
2082 |
logger.info(f"Processing writer10 content: {len(content)} chars")
|
2083 |
|
2084 |
# [Chapter X] ํจํด์ผ๋ก ์ฑํฐ ๋ถ๋ฆฌ
|
|
|
2105 |
logger.info("[NORMAL MODE] Processing all writer revisions...")
|
2106 |
|
2107 |
for stage in stages:
|
2108 |
+
role = stage['role'] if 'role' in stage.keys() else None
|
2109 |
+
stage_name = stage['stage_name'] if 'stage_name' in stage.keys() else ''
|
2110 |
+
content = stage['content'] if 'content' in stage.keys() else ''
|
2111 |
+
status = stage['status'] if 'status' in stage.keys() else ''
|
2112 |
+
|
2113 |
+
# ๋๋ฒ๊น
์ ๋ณด
|
2114 |
+
logger.debug(f"[NORMAL MODE] Checking: role={role}, name={stage_name}, status={status}, content_len={len(content)}")
|
2115 |
|
2116 |
# ์ธ์ด์ ์๊ด์์ด ์๊ฐ ์์ ๋ณธ ์ฐพ๊ธฐ
|
2117 |
+
is_writer = role and role.startswith('writer')
|
2118 |
+
is_revision = stage_name and ('Revision' in stage_name or '์์ ๋ณธ' in stage_name)
|
|
|
2119 |
|
2120 |
+
if is_writer and is_revision:
|
2121 |
# ์๊ฐ ๋ฒํธ ์ถ์ถ
|
2122 |
try:
|
2123 |
+
writer_num = int(role.replace('writer', ''))
|
2124 |
+
logger.info(f"Found writer {writer_num} revision - stage_name: {stage_name}")
|
2125 |
except:
|
2126 |
+
logger.warning(f"Could not extract writer number from role: {role}")
|
2127 |
continue
|
2128 |
|
|
|
|
|
2129 |
# ํ์ด์ง ๋งํฌ ์ ๊ฑฐ
|
2130 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2131 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2132 |
content = content.strip()
|
2133 |
|
2134 |
if content:
|
2135 |
+
word_count = stage['word_count'] if 'word_count' in stage.keys() else len(content.split())
|
2136 |
total_words += word_count
|
2137 |
writer_contents.append({
|
2138 |
+
'writer_num': writer_num,
|
2139 |
'content': content,
|
2140 |
'word_count': word_count
|
2141 |
})
|
|
|
2153 |
doc.add_paragraph(f'Total Chapters: {writer_count}')
|
2154 |
doc.add_paragraph(f'Total Words: {total_words:,}')
|
2155 |
doc.add_paragraph(f'Estimated Pages: ~{total_words/500:.0f}')
|
2156 |
+
doc.add_paragraph(f'Language: {actual_language}')
|
2157 |
doc.add_page_break()
|
2158 |
|
2159 |
# ๋ชฉ์ฐจ
|
|
|
2224 |
f.write(f"AI COLLABORATIVE NOVEL - {'TEST MODE' if is_test_mode else 'COMPLETE VERSION'}\n")
|
2225 |
f.write("="*60 + "\n")
|
2226 |
f.write(f"Theme: {session['user_query']}\n")
|
2227 |
+
f.write(f"Language: {actual_language}\n")
|
2228 |
f.write(f"Created: {datetime.now()}\n")
|
2229 |
f.write(f"Mode: {'Test Mode (Actual chapters)' if is_test_mode else 'Full Mode (10 chapters)'}\n")
|
2230 |
f.write("="*60 + "\n\n")
|
|
|
2235 |
if is_test_mode:
|
2236 |
# ํ
์คํธ ๋ชจ๋: writer1 + writer10 ์ฒ๋ฆฌ
|
2237 |
for stage in stages:
|
2238 |
+
role = stage['role'] if 'role' in stage.keys() else None
|
2239 |
+
stage_name = stage['stage_name'] if 'stage_name' in stage.keys() else ''
|
2240 |
+
content = stage['content'] if 'content' in stage.keys() else ''
|
2241 |
+
|
2242 |
# Writer 1 ์์ ๋ณธ
|
2243 |
+
if role == 'writer1' and stage_name and ('Revision' in stage_name or '์์ ๋ณธ' in stage_name):
|
|
|
2244 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2245 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2246 |
content = content.strip()
|
2247 |
|
2248 |
if content:
|
2249 |
+
word_count = stage['word_count'] if 'word_count' in stage.keys() else len(content.split())
|
2250 |
total_words += word_count
|
2251 |
writer_count = 1
|
2252 |
|
|
|
2258 |
f.write("\n\n")
|
2259 |
|
2260 |
# Writer 10
|
2261 |
+
elif role == 'writer10':
|
|
|
|
|
2262 |
# [Chapter X] ํจํด์ผ๋ก ์ฑํฐ ๋ถ๋ฆฌ
|
2263 |
chapters = re.split(r'\[Chapter\s+(\d+)\]', content)
|
2264 |
|
|
|
2281 |
else:
|
2282 |
# ์ผ๋ฐ ๋ชจ๋ - ์์ ๋ ๋ก์ง
|
2283 |
for stage in stages:
|
2284 |
+
role = stage['role'] if 'role' in stage.keys() else None
|
2285 |
+
stage_name = stage['stage_name'] if 'stage_name' in stage.keys() else ''
|
2286 |
+
content = stage['content'] if 'content' in stage.keys() else ''
|
2287 |
+
|
2288 |
# ์ธ์ด์ ์๊ด์์ด ์๊ฐ ์์ ๋ณธ ์ฐพ๊ธฐ
|
2289 |
+
is_writer = role and role.startswith('writer')
|
2290 |
+
is_revision = stage_name and ('Revision' in stage_name or '์์ ๋ณธ' in stage_name)
|
|
|
2291 |
|
2292 |
+
if is_writer and is_revision:
|
2293 |
# ์๊ฐ ๋ฒํธ ์ถ์ถ
|
2294 |
try:
|
2295 |
+
writer_num = int(role.replace('writer', ''))
|
2296 |
except:
|
2297 |
continue
|
2298 |
|
|
|
2299 |
# ํ์ด์ง ๋งํฌ ์ ๊ฑฐ
|
2300 |
content = re.sub(r'\[(?:ํ์ด์ง|Page|page)\s*\d+\]', '', content)
|
2301 |
content = re.sub(r'(?:ํ์ด์ง|Page)\s*\d+:', '', content)
|
2302 |
content = content.strip()
|
2303 |
|
2304 |
if content:
|
2305 |
+
word_count = stage['word_count'] if 'word_count' in stage.keys() else len(content.split())
|
2306 |
total_words += word_count
|
2307 |
writer_count += 1
|
2308 |
|