shukdevdatta123 commited on
Commit
982521b
·
verified ·
1 Parent(s): 02c751c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -20,16 +20,39 @@ def predict_with_loaded_model(in_sentences):
20
  # Streamlit interface
21
  st.title("Stress Prediction with DistilBERT")
22
 
 
 
 
 
 
 
23
  # Add a text input box for the user to enter a sentence
24
- user_input = st.text_area("Enter a sentence or text:", "")
25
-
26
- # When the user clicks "Predict", run the prediction function
27
- if st.button("Predict"):
28
- if user_input:
29
- # Make the prediction using the model
30
- prediction = predict_with_loaded_model([user_input])[0]
31
- st.write(f"Text: {prediction['text']}")
32
- st.write(f"Prediction: {prediction['label']}")
33
- # st.write(f"Confidence: {prediction['confidence']}")
34
- else:
35
- st.write("Please enter a sentence to predict.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  # Streamlit interface
21
  st.title("Stress Prediction with DistilBERT")
22
 
23
+ # Initialize session state variables for user input and prediction output
24
+ if 'user_input' not in st.session_state:
25
+ st.session_state.user_input = ""
26
+ if 'prediction' not in st.session_state:
27
+ st.session_state.prediction = None
28
+
29
  # Add a text input box for the user to enter a sentence
30
+ user_input = st.text_area("Enter a sentence or text:", st.session_state.user_input)
31
+
32
+ # Define buttons with a sidebar layout for easy spacing
33
+ col1, col2 = st.columns([1, 1])
34
+ with col1:
35
+ # When the user clicks "Predict", run the prediction function
36
+ if st.button("Predict"):
37
+ if user_input:
38
+ # Update session state with the user input
39
+ st.session_state.user_input = user_input
40
+ # Make the prediction using the model
41
+ st.session_state.prediction = predict_with_loaded_model([user_input])[0]
42
+ else:
43
+ st.write("Please enter a sentence to predict.")
44
+
45
+ with col2:
46
+ # Refresh button to reset the user input and output
47
+ if st.button("Refresh"):
48
+ st.session_state.user_input = ""
49
+ st.session_state.prediction = None
50
+ st.experimental_rerun()
51
+
52
+ # Display the prediction result
53
+ if st.session_state.prediction:
54
+ prediction = st.session_state.prediction
55
+ st.write(f"Text: {prediction['text']}")
56
+ st.write(f"Prediction: {prediction['label']}")
57
+ # st.write(f"Confidence: {prediction['confidence']}")
58
+