Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,37 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
import tensorflow as tf
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
# Example preprocessing
|
23 |
-
text = text.lower()
|
24 |
-
|
25 |
-
# Make a prediction
|
26 |
-
prediction = model.predict([text])
|
27 |
-
return prediction
|
28 |
|
29 |
-
|
30 |
-
iface = gr.Interface(fn=predict, inputs="text", outputs="label")
|
31 |
|
32 |
-
#
|
|
|
33 |
iface.launch()
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
import tensorflow as tf
|
4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
5 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
6 |
+
import gradio as gr
|
7 |
|
8 |
+
# Load the trained model
|
9 |
+
model_path = "/content/drive/MyDrive/Twitter_data/sentiment_analysis_model.h5"
|
10 |
+
model = tf.keras.models.load_model(model_path)
|
11 |
|
12 |
+
# Load the tokenizer
|
13 |
+
tokenizer_path = "/content/drive/MyDrive/Twitter_data/tokenizer.pickle"
|
14 |
+
with open(tokenizer_path, 'rb') as handle:
|
15 |
+
tokenizer = pickle.load(handle)
|
16 |
|
17 |
+
# Define a function for sentiment classification
|
18 |
+
def classify_sentiment(text):
|
19 |
+
# Preprocess the text
|
20 |
+
text_sequence = tokenizer.texts_to_sequences([text])
|
21 |
+
padded_sequence = pad_sequences(text_sequence, maxlen=100)
|
22 |
|
23 |
+
# Make prediction using the trained model
|
24 |
+
prediction = model.predict(padded_sequence)
|
25 |
+
|
26 |
+
# Convert prediction to class label
|
27 |
+
predicted_label = np.argmax(prediction)
|
28 |
|
29 |
+
# Map class label to sentiment
|
30 |
+
sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
|
31 |
+
sentiment = sentiment_mapping[predicted_label]
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
return sentiment
|
|
|
34 |
|
35 |
+
# Create Gradio interface
|
36 |
+
iface = gr.Interface(fn=classify_sentiment, inputs="text", outputs="text", title="Sentiment Analysis", description="Enter a sentence to classify its sentiment.")
|
37 |
iface.launch()
|