Ahtisham1583 commited on
Commit
1e0534b
·
verified ·
1 Parent(s): b5390ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -1,33 +1,37 @@
1
- import os
2
- import gradio as gr
3
  import tensorflow as tf
 
 
 
4
 
5
- # Mount Google Drive (if necessary)
6
- # from google.colab import drive
7
- # drive.mount('/content/drive')
8
 
9
- # Path to the saved model
10
- model_path = '/content/sentiment_analysis_model.h5'
 
 
11
 
12
- # Check if the model file exists
13
- if not os.path.exists(model_path):
14
- raise FileNotFoundError(f"No file or directory found at {model_path}")
 
 
15
 
16
- # Load the model
17
- model = tf.keras.models.load_model(model_path)
 
 
 
18
 
19
- # Define the prediction function
20
- def predict(text):
21
- # Preprocess the text (implement your own preprocessing if needed)
22
- # Example preprocessing
23
- text = text.lower()
24
-
25
- # Make a prediction
26
- prediction = model.predict([text])
27
- return prediction
28
 
29
- # Create the Gradio interface
30
- iface = gr.Interface(fn=predict, inputs="text", outputs="label")
31
 
32
- # Launch the interface
 
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()