Ahtisham1583 commited on
Commit
b4e81b1
·
verified ·
1 Parent(s): 98e2cff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -1,29 +1,28 @@
1
  import streamlit as st
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
 
3
 
4
- # Replace with your actual model ID or access token from Hugging Face Hub
5
- model_id = "your-model-id"
6
 
7
- # Load the pre-trained sentiment analysis model and tokenizer from Hugging Face Hub
8
- model = AutoModelForSequenceClassification.from_pretrained(model_id)
9
- tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
 
 
 
10
 
11
  def classify_sentiment(text):
12
  """
13
  Function to preprocess text, make predictions using the loaded model,
14
  and return the predicted sentiment.
15
  """
16
- # Preprocess text (tokenization)
17
- encoded_text = tokenizer(text, return_tensors="pt")
18
-
19
- # Make prediction using the loaded model
20
- output = model(**encoded_text)
21
- predictions = output.logits.argmax(-1)
22
-
23
- # Map predicted class label to sentiment category
24
  sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
25
- sentiment = sentiment_mapping[predictions.item()]
26
-
27
  return sentiment
28
 
29
  def main():
 
1
  import streamlit as st
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing.text import Tokenizer # Assuming you used Tokenizer during training
4
 
5
+ # Load the pre-trained sentiment analysis model
6
+ model = load_model("sentiment_analysis_model.h5")
7
 
8
+ # Define a tokenizer (replace with your actual tokenization logic from training)
9
+ tokenizer = Tokenizer(num_words=5000) # Adjust vocabulary size based on your training
10
+
11
+ def preprocess_text(text):
12
+ # Preprocess text based on your training process (e.g., tokenization)
13
+ tokens = tokenizer.texts_to_sequences([text])
14
+ padded_sequence = pad_sequences(tokens, maxlen=200) # Adjust max length based on your training
15
+ return padded_sequence
16
 
17
  def classify_sentiment(text):
18
  """
19
  Function to preprocess text, make predictions using the loaded model,
20
  and return the predicted sentiment.
21
  """
22
+ preprocessed_text = preprocess_text(text)
23
+ prediction = model.predict(preprocessed_text)
 
 
 
 
 
 
24
  sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
25
+ sentiment = sentiment_mapping[prediction.argmax()]
 
26
  return sentiment
27
 
28
  def main():