Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,22 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
# Define a function to classify sentiment
|
4 |
def classify_sentiment(text):
|
5 |
# Preprocess the text (tokenization, padding, etc.)
|
6 |
text_sequence = tokenizer.texts_to_sequences([text])
|
@@ -13,14 +29,18 @@ def classify_sentiment(text):
|
|
13 |
predicted_label = np.argmax(prediction)
|
14 |
|
15 |
# Map class label to sentiment
|
16 |
-
sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
|
17 |
sentiment = sentiment_mapping[predicted_label]
|
18 |
|
19 |
return sentiment
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
5 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
model = load_model('sentiment_model.h5')
|
9 |
+
|
10 |
+
# Load the tokenizer
|
11 |
+
with open('tokenizer.pickle', 'rb') as handle:
|
12 |
+
tokenizer = pickle.load(handle)
|
13 |
+
|
14 |
+
# Define the max sequence length (as used during training)
|
15 |
+
max_seq_length = 100 # Adjust this based on your training setup
|
16 |
+
|
17 |
+
# Sentiment mapping
|
18 |
+
sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
|
19 |
|
|
|
20 |
def classify_sentiment(text):
|
21 |
# Preprocess the text (tokenization, padding, etc.)
|
22 |
text_sequence = tokenizer.texts_to_sequences([text])
|
|
|
29 |
predicted_label = np.argmax(prediction)
|
30 |
|
31 |
# Map class label to sentiment
|
|
|
32 |
sentiment = sentiment_mapping[predicted_label]
|
33 |
|
34 |
return sentiment
|
35 |
|
36 |
+
# Gradio interface
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=classify_sentiment,
|
39 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter a sentence..."),
|
40 |
+
outputs="text",
|
41 |
+
title="Sentiment Analysis",
|
42 |
+
description="Enter a sentence to classify its sentiment."
|
43 |
+
)
|
44 |
|
45 |
+
if __name__ == "__main__":
|
46 |
+
interface.launch()
|