Spaces:
Sleeping
Sleeping
Last commit not found
import streamlit as st | |
from transformers import AutoModelForSequenceClassification, AutoTokenizer | |
# Replace with your actual model ID or access token from Hugging Face Hub | |
model_id = "your-model-id" | |
# Load the pre-trained sentiment analysis model and tokenizer from Hugging Face Hub | |
model = AutoModelForSequenceClassification.from_pretrained(model_id) | |
tokenizer = AutoTokenizer.from_pretrained(model_id) | |
def classify_sentiment(text): | |
""" | |
Function to preprocess text, make predictions using the loaded model, | |
and return the predicted sentiment. | |
""" | |
# Preprocess text (tokenization) | |
encoded_text = tokenizer(text, return_tensors="pt") | |
# Make prediction using the loaded model | |
output = model(**encoded_text) | |
predictions = output.logits.argmax(-1) | |
# Map predicted class label to sentiment category | |
sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"} | |
sentiment = sentiment_mapping[predictions.item()] | |
return sentiment | |
def main(): | |
""" | |
Streamlit app for sentiment analysis | |
""" | |
st.title("Sentiment Analysis App") | |
text_input = st.text_input("Enter text to analyze:") | |
if text_input: | |
sentiment = classify_sentiment(text_input) | |
st.write(f"Predicted Sentiment: {sentiment}") | |
if __name__ == "__main__": | |
main() | |