openfree commited on
Commit
aa05286
Β·
verified Β·
1 Parent(s): 61ae973

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -54
app.py CHANGED
@@ -1918,9 +1918,13 @@ def get_active_sessions(language: str) -> List[Tuple[str, str]]:
1918
  logger.error(f"Error getting active sessions: {str(e)}", exc_info=True)
1919
  return []
1920
 
1921
- def resume_session(session_id: str, language: str) -> Generator[Tuple[str, str, str, str], None, None]:
1922
  """Resume an existing session"""
1923
  if not session_id:
 
 
 
 
1924
  return
1925
 
1926
  # Process with existing session ID
@@ -2094,7 +2098,7 @@ def download_novel(novel_text: str, format: str, language: str, session_id: str
2094
  logger.info(f"TXT saved: {filepath}")
2095
  return filepath
2096
 
2097
- # Custom CSS with enhanced recovery status
2098
  custom_css = """
2099
  .gradio-container {
2100
  background: linear-gradient(135deg, #1e3c72, #2a5298);
@@ -2128,15 +2132,6 @@ custom_css = """
2128
  color: white;
2129
  }
2130
 
2131
- .recovery-status {
2132
- background-color: rgba(76, 175, 80, 0.2);
2133
- border: 1px solid #4CAF50;
2134
- padding: 10px;
2135
- border-radius: 5px;
2136
- margin: 10px 0;
2137
- color: #4CAF50;
2138
- }
2139
-
2140
  #stages-display {
2141
  background-color: rgba(255, 255, 255, 0.95);
2142
  padding: 20px;
@@ -2197,13 +2192,6 @@ def create_interface():
2197
  # State management
2198
  current_session_id = gr.State(None)
2199
 
2200
- # Recovery status display
2201
- recovery_status = gr.Markdown(
2202
- value="",
2203
- elem_classes=["recovery-status"],
2204
- visible=False
2205
- )
2206
-
2207
  with gr.Row():
2208
  with gr.Column(scale=1):
2209
  with gr.Group(elem_classes=["input-section"]):
@@ -2294,9 +2282,6 @@ def create_interface():
2294
  )
2295
 
2296
  # Event handlers
2297
- def update_novel_state(stages, novel, status, recovery_msg):
2298
- return stages, novel, status, novel, recovery_msg
2299
-
2300
  def refresh_sessions():
2301
  try:
2302
  sessions = get_active_sessions("English")
@@ -2307,23 +2292,19 @@ def create_interface():
2307
 
2308
  def handle_auto_recover(language):
2309
  session_id, message = auto_recover_session(language)
2310
- if session_id:
2311
- return session_id, gr.update(value=message, visible=True)
2312
- else:
2313
- if language == "Korean":
2314
- message = "❌ 볡ꡬ할 μ„Έμ…˜μ΄ μ—†μŠ΅λ‹ˆλ‹€."
2315
- else:
2316
- message = "❌ No session to recover."
2317
- return None, gr.update(value=message, visible=True)
2318
 
2319
  submit_btn.click(
2320
  fn=process_query,
2321
  inputs=[query_input, language_select, current_session_id],
2322
- outputs=[stages_display, novel_output, status_text, recovery_status.value]
2323
- ).then(
2324
- fn=update_novel_state,
2325
- inputs=[stages_display, novel_output, status_text, recovery_status.value],
2326
- outputs=[stages_display, novel_output, status_text, novel_text_state, recovery_status]
 
 
 
2327
  )
2328
 
2329
  resume_btn.click(
@@ -2333,17 +2314,17 @@ def create_interface():
2333
  ).then(
2334
  fn=resume_session,
2335
  inputs=[current_session_id, language_select],
2336
- outputs=[stages_display, novel_output, status_text, recovery_status.value]
2337
  )
2338
 
2339
  auto_recover_btn.click(
2340
  fn=handle_auto_recover,
2341
  inputs=[language_select],
2342
- outputs=[current_session_id, recovery_status]
2343
  ).then(
2344
  fn=resume_session,
2345
  inputs=[current_session_id, language_select],
2346
- outputs=[stages_display, novel_output, status_text, recovery_status.value]
2347
  )
2348
 
2349
  refresh_btn.click(
@@ -2352,11 +2333,11 @@ def create_interface():
2352
  )
2353
 
2354
  clear_btn.click(
2355
- fn=lambda: ("", "", "πŸ”„ Ready", "", None, gr.update(visible=False)),
2356
- outputs=[stages_display, novel_output, status_text, novel_text_state, current_session_id, recovery_status]
2357
  )
2358
 
2359
- def handle_download(novel_text, format_type, language, session_id):
2360
  if not novel_text and not session_id:
2361
  return gr.update(visible=False)
2362
 
@@ -2368,24 +2349,14 @@ def create_interface():
2368
 
2369
  download_btn.click(
2370
  fn=handle_download,
2371
- inputs=[novel_text_state, format_select, language_select, current_session_id],
2372
  outputs=[download_file]
2373
  )
2374
 
2375
- # Load sessions and check for auto-recovery on startup
2376
- def on_load():
2377
- sessions = get_active_sessions("English")
2378
- # Check for latest session
2379
- latest_session = NovelDatabase.get_latest_active_session()
2380
- if latest_session:
2381
- recovery_msg = f"πŸ’‘ Found active session: '{latest_session['user_query'][:30]}...' - Click 'Auto Recover Latest' to continue"
2382
- return gr.update(choices=sessions), gr.update(value=recovery_msg, visible=True)
2383
- else:
2384
- return gr.update(choices=sessions), gr.update(visible=False)
2385
-
2386
  interface.load(
2387
- fn=on_load,
2388
- outputs=[session_dropdown, recovery_status]
2389
  )
2390
 
2391
  return interface
 
1918
  logger.error(f"Error getting active sessions: {str(e)}", exc_info=True)
1919
  return []
1920
 
1921
+ def resume_session(session_id: str, language: str) -> Generator[Tuple[str, str, str], None, None]:
1922
  """Resume an existing session"""
1923
  if not session_id:
1924
+ if language == "Korean":
1925
+ yield "", "", "❌ μ„Έμ…˜μ„ μ„ νƒν•΄μ£Όμ„Έμš”."
1926
+ else:
1927
+ yield "", "", "❌ Please select a session."
1928
  return
1929
 
1930
  # Process with existing session ID
 
2098
  logger.info(f"TXT saved: {filepath}")
2099
  return filepath
2100
 
2101
+ # Custom CSS
2102
  custom_css = """
2103
  .gradio-container {
2104
  background: linear-gradient(135deg, #1e3c72, #2a5298);
 
2132
  color: white;
2133
  }
2134
 
 
 
 
 
 
 
 
 
 
2135
  #stages-display {
2136
  background-color: rgba(255, 255, 255, 0.95);
2137
  padding: 20px;
 
2192
  # State management
2193
  current_session_id = gr.State(None)
2194
 
 
 
 
 
 
 
 
2195
  with gr.Row():
2196
  with gr.Column(scale=1):
2197
  with gr.Group(elem_classes=["input-section"]):
 
2282
  )
2283
 
2284
  # Event handlers
 
 
 
2285
  def refresh_sessions():
2286
  try:
2287
  sessions = get_active_sessions("English")
 
2292
 
2293
  def handle_auto_recover(language):
2294
  session_id, message = auto_recover_session(language)
2295
+ return session_id
 
 
 
 
 
 
 
2296
 
2297
  submit_btn.click(
2298
  fn=process_query,
2299
  inputs=[query_input, language_select, current_session_id],
2300
+ outputs=[stages_display, novel_output, status_text]
2301
+ )
2302
+
2303
+ # Update novel text state when novel output changes
2304
+ novel_output.change(
2305
+ fn=lambda x: x,
2306
+ inputs=[novel_output],
2307
+ outputs=[novel_text_state]
2308
  )
2309
 
2310
  resume_btn.click(
 
2314
  ).then(
2315
  fn=resume_session,
2316
  inputs=[current_session_id, language_select],
2317
+ outputs=[stages_display, novel_output, status_text]
2318
  )
2319
 
2320
  auto_recover_btn.click(
2321
  fn=handle_auto_recover,
2322
  inputs=[language_select],
2323
+ outputs=[current_session_id]
2324
  ).then(
2325
  fn=resume_session,
2326
  inputs=[current_session_id, language_select],
2327
+ outputs=[stages_display, novel_output, status_text]
2328
  )
2329
 
2330
  refresh_btn.click(
 
2333
  )
2334
 
2335
  clear_btn.click(
2336
+ fn=lambda: ("", "", "πŸ”„ Ready", "", None),
2337
+ outputs=[stages_display, novel_output, status_text, novel_text_state, current_session_id]
2338
  )
2339
 
2340
+ def handle_download(format_type, language, session_id, novel_text):
2341
  if not novel_text and not session_id:
2342
  return gr.update(visible=False)
2343
 
 
2349
 
2350
  download_btn.click(
2351
  fn=handle_download,
2352
+ inputs=[format_select, language_select, current_session_id, novel_text_state],
2353
  outputs=[download_file]
2354
  )
2355
 
2356
+ # Load sessions on startup
 
 
 
 
 
 
 
 
 
 
2357
  interface.load(
2358
+ fn=refresh_sessions,
2359
+ outputs=[session_dropdown]
2360
  )
2361
 
2362
  return interface