stanlys96 commited on
Commit
0281523
·
verified ·
1 Parent(s): 682fd81

Upload 6 files

Browse files
Files changed (2) hide show
  1. eda.py +17 -17
  2. prediction.py +17 -15
eda.py CHANGED
@@ -2,8 +2,8 @@ import streamlit as st
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
- # from nltk.corpus import stopwords
6
- # from wordcloud import WordCloud
7
 
8
  def app():
9
  df_original = pd.read_csv("data.csv", delimiter=";")
@@ -15,15 +15,15 @@ def app():
15
  st.header('Exploratory Data Analysis', divider='rainbow')
16
  eda_list = ["Text Length Distribution", "Sentiment Distribution", "Word Clouds", "Stopwords Boxplot Distributions"]
17
  val = st.sidebar.radio("Choose plot to show", eda_list)
18
- # stop_words = set(stopwords.words('english'))
19
- # def plot_wordcloud(sentiment):
20
- # text = ' '.join(df[df['feeling'] == sentiment]['text'])
21
- # wordcloud = WordCloud(stopwords=stop_words, background_color='white').generate(text)
22
- # plt.figure(figsize=(10, 6))
23
- # plt.imshow(wordcloud, interpolation='bilinear')
24
- # plt.axis('off')
25
- # plt.title(f"Word Cloud for {sentiment} Sentiment")
26
- # st.pyplot(plt)
27
  if val == "Text Length Distribution":
28
  # Plot distribution
29
  st.header('Text Length Distribution')
@@ -53,12 +53,12 @@ def app():
53
  st.pyplot(plt)
54
  st.write("Insight: surprise sentiment has the lowest value of around 900 data")
55
  elif val == "Word Clouds":
56
- # plot_wordcloud('joy')
57
- # plot_wordcloud('sadness')
58
- # plot_wordcloud('anger')
59
- # plot_wordcloud('love')
60
- # plot_wordcloud('surprise')
61
- # plot_wordcloud('fear')
62
  pass
63
  elif val == "Stopwords Boxplot Distributions":
64
  temp_b = df.copy()
 
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
+ from nltk.corpus import stopwords
6
+ from wordcloud import WordCloud
7
 
8
  def app():
9
  df_original = pd.read_csv("data.csv", delimiter=";")
 
15
  st.header('Exploratory Data Analysis', divider='rainbow')
16
  eda_list = ["Text Length Distribution", "Sentiment Distribution", "Word Clouds", "Stopwords Boxplot Distributions"]
17
  val = st.sidebar.radio("Choose plot to show", eda_list)
18
+ stop_words = set(stopwords.words('english'))
19
+ def plot_wordcloud(sentiment):
20
+ text = ' '.join(df[df['feeling'] == sentiment]['text'])
21
+ wordcloud = WordCloud(stopwords=stop_words, background_color='white').generate(text)
22
+ plt.figure(figsize=(10, 6))
23
+ plt.imshow(wordcloud, interpolation='bilinear')
24
+ plt.axis('off')
25
+ plt.title(f"Word Cloud for {sentiment} Sentiment")
26
+ st.pyplot(plt)
27
  if val == "Text Length Distribution":
28
  # Plot distribution
29
  st.header('Text Length Distribution')
 
53
  st.pyplot(plt)
54
  st.write("Insight: surprise sentiment has the lowest value of around 900 data")
55
  elif val == "Word Clouds":
56
+ plot_wordcloud('joy')
57
+ plot_wordcloud('sadness')
58
+ plot_wordcloud('anger')
59
+ plot_wordcloud('love')
60
+ plot_wordcloud('surprise')
61
+ plot_wordcloud('fear')
62
  pass
63
  elif val == "Stopwords Boxplot Distributions":
64
  temp_b = df.copy()
prediction.py CHANGED
@@ -14,7 +14,6 @@ number_to_feeling = {
14
  }
15
 
16
  def get_feeling(number):
17
- # Convert the number to string to match the keys in the dictionary
18
  feeling = number_to_feeling.get(str(number), "Unknown feeling")
19
  return feeling
20
 
@@ -22,18 +21,21 @@ def app():
22
  st.header('Prediction', divider='rainbow')
23
 
24
  user_input = st.text_input("Enter your text here:")
25
- the_model = tf.keras.models.load_model('model.keras', custom_objects={'KerasLayer': tf_hub.KerasLayer})
26
- if st.button('Predict', type="secondary"):
27
- data = {
28
- "text_processed": [
29
- user_input
30
- ]
31
- }
32
- df = pd.DataFrame(data)
33
- predictions = the_model.predict(df)
34
- predicted_class = np.argmax(predictions, axis=1)
35
- the_sentiment = predicted_class[0]
 
 
 
36
 
37
- st.write(f"We have predicted that the sentiment of this text is {get_feeling(the_sentiment)}")
38
- else:
39
- st.write("Click the button to predict!")
 
14
  }
15
 
16
  def get_feeling(number):
 
17
  feeling = number_to_feeling.get(str(number), "Unknown feeling")
18
  return feeling
19
 
 
21
  st.header('Prediction', divider='rainbow')
22
 
23
  user_input = st.text_input("Enter your text here:")
24
+ with st.spinner("Loading the model, please wait..."):
25
+ the_model = tf.keras.models.load_model('model.keras', custom_objects={'KerasLayer': tf_hub.KerasLayer})
26
+ if st.button('Predict', type="secondary"):
27
+ data = {
28
+ "text_processed": [
29
+ user_input
30
+ ]
31
+ }
32
+ df = pd.DataFrame(data)
33
+ with st.spinner("Making prediction..."):
34
+ # Replace with your preprocessing and prediction code
35
+ predictions = the_model.predict(df)
36
+ predicted_class = np.argmax(predictions, axis=1)
37
+ the_sentiment = predicted_class[0]
38
 
39
+ st.write(f"We have predicted that the sentiment of this text is {get_feeling(the_sentiment)}")
40
+ else:
41
+ st.write("Click the button to predict!")