sanitiment / app.py
usamaferoz12's picture
Update app.py
89e0900
raw
history blame
No virus
1.16 kB
import streamlit as st
from transformers import BertTokenizer, BertForSequenceClassification
import torch
# Load the fine-tuned model and tokenizer
model_path = 'path/to/your/fine-tuned/model'
tokenizer = BertTokenizer.from_pretrained(model_path)
model = BertForSequenceClassification.from_pretrained(model_path)
# Set the model to evaluation mode
model.eval()
# Function to perform sentiment analysis
def analyze_sentiment(text):
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class = logits.argmax().item()
return predicted_class
# Create a Streamlit app
st.title("Sentiment Analysis App")
# Get the user input
text = st.text_area("Enter text for sentiment analysis")
if st.button("Analyze Sentiment"):
if text:
sentiment_class = analyze_sentiment(text)
sentiment_labels = ['Negative', 'Neutral', 'Positive'] # Adjust as needed
sentiment = sentiment_labels[sentiment_class]
st.write(f"Sentiment: {sentiment}")
else:
st.warning("Please enter text for analysis.")