patrickbdevaney commited on
Commit
35a837e
1 Parent(s): 981a502

upload of demo to hf spaces

Browse files
Files changed (5) hide show
  1. app.py +42 -0
  2. index.html +35 -0
  3. requirements.txt +5 -0
  4. sentiment_analysis_model.h5 +3 -0
  5. tokenizer.pickle +3 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import joblib
3
+ import numpy as np
4
+ from tensorflow.keras.models import load_model
5
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
6
+ import pickle
7
+
8
+ # Load the model and tokenizer
9
+ try:
10
+ model = load_model('sentiment_analysis_model.h5')
11
+ except Exception as e:
12
+ st.error(f"Error loading model: {e}")
13
+
14
+ try:
15
+ with open('tokenizer.pickle', 'rb') as handle:
16
+ tokenizer = pickle.load(handle)
17
+ except Exception as e:
18
+ st.error(f"Error loading tokenizer: {e}")
19
+
20
+ # Define a function to predict the sentiment of input text
21
+ def predict_sentiment(text):
22
+ # Tokenize and pad the input text
23
+ text_sequence = tokenizer.texts_to_sequences([text])
24
+ text_sequence = pad_sequences(text_sequence, maxlen=100)
25
+
26
+ # Make a prediction using the trained model
27
+ predicted_rating = model.predict(text_sequence)[0]
28
+ if np.argmax(predicted_rating) == 0:
29
+ return 'Negative'
30
+ elif np.argmax(predicted_rating) == 1:
31
+ return 'Neutral'
32
+ else:
33
+ return 'Positive'
34
+
35
+ st.title('Sentiment Analysis')
36
+ comment = st.text_area('Enter your comment:')
37
+ if st.button('Analyze Sentiment'):
38
+ if 'model' in globals() and 'tokenizer' in globals():
39
+ sentiment = predict_sentiment(comment)
40
+ st.write(f'Sentiment: {sentiment}')
41
+ else:
42
+ st.error("Model or tokenizer not loaded properly.")
index.html ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Sentiment Analysis</title>
7
+ </head>
8
+ <body>
9
+ <h1>Sentiment Analysis</h1>
10
+ <form id="commentForm">
11
+ <label for="comment">Enter your comment:</label><br>
12
+ <textarea id="comment" name="comment" rows="4" cols="50"></textarea><br><br>
13
+ <input type="submit" value="Analyze Sentiment">
14
+ </form>
15
+ <p id="result"></p>
16
+
17
+ <script>
18
+ document.getElementById('commentForm').addEventListener('submit', function(event) {
19
+ event.preventDefault();
20
+ const comment = document.getElementById('comment').value;
21
+ fetch('/predict', {
22
+ method: 'POST',
23
+ headers: {
24
+ 'Content-Type': 'application/json',
25
+ },
26
+ body: JSON.stringify({ comment: comment }),
27
+ })
28
+ .then(response => response.json())
29
+ .then(data => {
30
+ document.getElementById('result').innerText = 'Sentiment: ' + data.sentiment;
31
+ });
32
+ });
33
+ </script>
34
+ </body>
35
+ </html>
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ tensorflow
3
+ joblib
4
+ numpy
5
+ flask
sentiment_analysis_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f9ddfd21899724f70867f400fc4769f91072b25f095f8556712e24c0cf229f3e
3
+ size 6448856
tokenizer.pickle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:522f4e3a9e107b2f6e045ae4f90fadbb8055d21045ce42cbaa960b060a7b8b15
3
+ size 2111402