joko333 commited on
Commit
1e4dfde
·
1 Parent(s): 04dc908

Add error handling for model loading and improve analysis feedback

Browse files
Files changed (2) hide show
  1. app.py +5 -1
  2. pages/Analysis.py +40 -31
app.py CHANGED
@@ -8,8 +8,12 @@ x = st.slider('Select a value')
8
  st.write(x, 'squared is', x * x)
9
  # load the model and cache it
10
  model, label_encoder, tokenizer = load_model_for_prediction()
 
 
 
 
11
  st.session_state['model'] = model
12
  st.session_state['label_encoder'] = label_encoder
13
  st.session_state['tokenizer'] = tokenizer
14
- st.write('Model loaded')
15
 
 
8
  st.write(x, 'squared is', x * x)
9
  # load the model and cache it
10
  model, label_encoder, tokenizer = load_model_for_prediction()
11
+ if model is None or label_encoder is None or tokenizer is None:
12
+ st.error("Failed to load model components")
13
+ st.stop()
14
+
15
  st.session_state['model'] = model
16
  st.session_state['label_encoder'] = label_encoder
17
  st.session_state['tokenizer'] = tokenizer
18
+ st.success('Model loaded successfully')
19
 
pages/Analysis.py CHANGED
@@ -42,43 +42,52 @@ def split_sentences_with_abbrev(text):
42
 
43
  def show_analysis():
44
  st.title("Text Analysis")
45
- st.write("Use this section to analyze the logical structure of your text.")
46
 
47
- try:
48
- if 'model' not in st.session_state:
49
- st.error("Please initialize the model from the home page first.")
50
- return
51
-
52
- model = st.session_state.model
53
- label_encoder = st.session_state.label_encoder
54
- tokenizer = st.session_state.tokenizer
55
 
56
- # Text input section
57
- st.header("Analyze Your Text")
58
- user_text = st.text_area("Enter your text here (multiple sentences allowed):", height=150)
59
 
60
- if st.button("Analyze"):
61
- if user_text:
62
- # Split and analyze sentences
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  sentences = split_sentences_regex(user_text)
 
64
 
65
- st.subheader("Analysis Results:")
66
- for i, sentence in enumerate(sentences, 1):
67
- with st.container():
68
- label, confidence = predict_sentence(
69
- model, sentence, tokenizer, label_encoder
70
- )
71
- if label not in ("Unknown", "Error"):
72
- st.write("---")
73
- st.write(f"**Sentence:** {sentence}")
74
- st.write(f"**Predicted:** {label}")
75
- st.progress(confidence)
76
- else:
77
- st.warning("Please enter some text to analyze.")
78
-
79
 
80
- except Exception as e:
81
- st.error(f"Error: {str(e)}")
82
 
83
  if __name__ == "__main__":
84
  show_analysis()
 
42
 
43
  def show_analysis():
44
  st.title("Text Analysis")
 
45
 
46
+ # Check model loading state
47
+ if not all(key in st.session_state for key in ['model', 'label_encoder', 'tokenizer']):
48
+ st.warning("Model components not found in session state")
49
+ st.info("Please go to the Home page first to load the model")
50
+ return
 
 
 
51
 
52
+ if any(st.session_state[key] is None for key in ['model', 'label_encoder', 'tokenizer']):
53
+ st.error("One or more model components failed to load")
54
+ return
55
 
56
+ # Get model components
57
+ model = st.session_state.model
58
+ label_encoder = st.session_state.label_encoder
59
+ tokenizer = st.session_state.tokenizer
60
+
61
+ # Text input section
62
+ st.header("Analyze Your Text")
63
+ user_text = st.text_area("Enter your text here (multiple sentences allowed):", height=150)
64
+
65
+ if st.button("Analyze"):
66
+ if not user_text:
67
+ st.warning("Please enter some text to analyze")
68
+ return
69
+
70
+ with st.spinner("Analyzing text..."):
71
+ try:
72
+ # Split text into sentences
73
  sentences = split_sentences_regex(user_text)
74
+ results = []
75
 
76
+ # Process each sentence
77
+ for sentence in sentences:
78
+ label, confidence = predict_sentence(model, sentence, tokenizer, label_encoder)
79
+ results.append({
80
+ "Sentence": sentence,
81
+ "Label": label,
82
+ "Confidence": f"{confidence:.2%}"
83
+ })
84
+
85
+ # Display results
86
+ df = pd.DataFrame(results)
87
+ st.dataframe(df)
 
 
88
 
89
+ except Exception as e:
90
+ st.error(f"Analysis failed: {str(e)}")
91
 
92
  if __name__ == "__main__":
93
  show_analysis()