File size: 1,315 Bytes
682fd81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91171b
 
537cf6b
21a9c98
682fd81
 
 
 
6803680
 
 
537cf6b
 
 
 
 
 
 
 
 
 
 
 
682fd81
537cf6b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import streamlit as st
import pandas as pd
import numpy as np
import tensorflow as tf
import tensorflow_hub as tf_hub

number_to_feeling = {
  '0': 'sadness',
  '1': 'anger',
  '2': 'love',
  '3': 'surprise',
  '4': 'fear',
  '5': 'joy'
}

def get_feeling(number):
  feeling = number_to_feeling.get(str(number), "Unknown feeling")
  return feeling

# Load the model function
def load_model():
  return tf.keras.models.load_model('model.keras', custom_objects={'KerasLayer': tf_hub.KerasLayer})

def app():
  st.header('Prediction', divider='rainbow')

  user_input = st.text_input("Enter your text here:")
  if 'the_model' not in st.session_state:
    with st.spinner("Loading the model, please wait..."):
      st.session_state.the_model = load_model()
  if st.button('Predict', type="secondary"):
    data = {
      "text_processed": [
          user_input
      ]
    }
    df = pd.DataFrame(data)
    with st.spinner("Making prediction..."):
      # Replace with your preprocessing and prediction code
      predictions = st.session_state.the_model.predict(df)
      predicted_class = np.argmax(predictions, axis=1)
      the_sentiment = predicted_class[0]

      st.write(f"We have predicted that the sentiment of this text is {get_feeling(the_sentiment)}")
  else:
    st.write("Click the button to predict!")