davideuler commited on
Commit
6990e21
·
1 Parent(s): d3f93de

fix: KeyError(_missing_key_error_message(key)) KeyError: 'st.session_state has no key 75180WIDGET_ID-735786182b942ec4a4aca0d5638237df-None. Did you forget to initialize it?

Browse files
Files changed (1) hide show
  1. pdf_translator_web.py +20 -17
pdf_translator_web.py CHANGED
@@ -224,21 +224,37 @@ def translate_all_pages(
224
 
225
  return output_doc
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  def main():
228
  st.set_page_config(layout="wide", page_title="PDF Translator for Human: with Local-LLM/GPT")
229
  st.title("PDF Translator for Human: with Local-LLM/GPT")
230
 
 
 
 
231
  # Sidebar configuration
232
  with st.sidebar:
233
  st.header("Settings")
234
 
235
- # Store previous file name to detect changes
236
- previous_file = st.session_state.get('previous_file', None)
237
  uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
238
 
239
  # Reset session state when a new file is uploaded
240
- if uploaded_file is not None and (previous_file is None or uploaded_file.name != previous_file):
241
- # Reset all relevant session state variables
242
  st.session_state.current_page = 0
243
  st.session_state.translation_started = True
244
  st.session_state.all_translated = False
@@ -306,8 +322,6 @@ def main():
306
  )
307
 
308
  # Store API settings in session state
309
- if 'api_settings' not in st.session_state:
310
- st.session_state.api_settings = {}
311
  st.session_state.api_settings.update({
312
  'api_key': api_key,
313
  'api_base': api_base,
@@ -332,17 +346,6 @@ def main():
332
  # Create two columns for side-by-side display
333
  col1, col2 = st.columns(2)
334
 
335
- # Initialize session state
336
- if 'current_page' not in st.session_state:
337
- st.session_state.current_page = 0
338
- st.session_state.translation_started = True # 自动开始翻译
339
-
340
- # Initialize translation status
341
- if 'all_translated' not in st.session_state:
342
- st.session_state.all_translated = False
343
- if 'translated_doc' not in st.session_state:
344
- st.session_state.translated_doc = None
345
-
346
  # Display original pages immediately
347
  with col1:
348
  st.header("Original")
 
224
 
225
  return output_doc
226
 
227
+ def init_session_state():
228
+ """Initialize session state variables"""
229
+ if 'current_page' not in st.session_state:
230
+ st.session_state.current_page = 0
231
+ if 'translation_started' not in st.session_state:
232
+ st.session_state.translation_started = True
233
+ if 'all_translated' not in st.session_state:
234
+ st.session_state.all_translated = False
235
+ if 'translated_doc' not in st.session_state:
236
+ st.session_state.translated_doc = None
237
+ if 'previous_file' not in st.session_state:
238
+ st.session_state.previous_file = None
239
+ if 'api_settings' not in st.session_state:
240
+ st.session_state.api_settings = {}
241
+
242
  def main():
243
  st.set_page_config(layout="wide", page_title="PDF Translator for Human: with Local-LLM/GPT")
244
  st.title("PDF Translator for Human: with Local-LLM/GPT")
245
 
246
+ # Initialize session state
247
+ init_session_state()
248
+
249
  # Sidebar configuration
250
  with st.sidebar:
251
  st.header("Settings")
252
 
 
 
253
  uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
254
 
255
  # Reset session state when a new file is uploaded
256
+ if uploaded_file is not None and (st.session_state.previous_file is None or
257
+ uploaded_file.name != st.session_state.previous_file):
258
  st.session_state.current_page = 0
259
  st.session_state.translation_started = True
260
  st.session_state.all_translated = False
 
322
  )
323
 
324
  # Store API settings in session state
 
 
325
  st.session_state.api_settings.update({
326
  'api_key': api_key,
327
  'api_base': api_base,
 
346
  # Create two columns for side-by-side display
347
  col1, col2 = st.columns(2)
348
 
 
 
 
 
 
 
 
 
 
 
 
349
  # Display original pages immediately
350
  with col1:
351
  st.header("Original")