SA_test / app.py
Hamza3107's picture
Update app.py
2f9bd26 verified
print("first test for hugging face")
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("Remicm/sentiment-analysis-model-for-socialmedia")
model = AutoModelForSequenceClassification.from_pretrained("Remicm/sentiment-analysis-model-for-socialmedia")
# Function to predict sentiment
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class = torch.argmax(logits, dim=1).item()
# Define sentiment labels (adjust based on your model's output)
sentiments = ["Negative", "Neutral", "Positive"]
return sentiments[predicted_class]
# Create the Gradio interface
interface = gr.Interface(fn=predict_sentiment,
inputs="text",
outputs="label",
title="Sentiment Analysis of Instagram Comments",
description="Enter a comment to determine its sentiment (Positive, Neutral, Negative).")
# Launch the interface
interface.launch()