Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,10 @@
|
|
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 |
-
#
|
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=
|
22 |
|
23 |
# Make prediction using the trained model
|
24 |
prediction = model.predict(padded_sequence)
|
@@ -32,6 +18,9 @@ def classify_sentiment(text):
|
|
32 |
|
33 |
return sentiment
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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])
|
7 |
+
padded_sequence = pad_sequences(text_sequence, maxlen=max_seq_length)
|
8 |
|
9 |
# Make prediction using the trained model
|
10 |
prediction = model.predict(padded_sequence)
|
|
|
18 |
|
19 |
return sentiment
|
20 |
|
21 |
+
# Define Gradio interface
|
22 |
+
input_text = gr.inputs.Textbox(lines=5, label="Enter your text")
|
23 |
+
output_sentiment = gr.outputs.Textbox(label="Sentiment")
|
24 |
+
|
25 |
+
# Launch Gradio app
|
26 |
+
gr.Interface(fn=classify_sentiment, inputs=input_text, outputs=output_sentiment, title="Sentiment Analysis", description="Enter a text to classify its sentiment.").launch()
|