|
import gradio as gr |
|
import torch |
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer |
|
|
|
|
|
model_name = "distilbert-base-uncased-finetuned-sst-2-english" |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
def analyze_sentiment(text): |
|
inputs = tokenizer(text, return_tensors='pt') |
|
outputs = model(**inputs) |
|
prediction = torch.argmax(outputs.logits) |
|
|
|
sentiment = "Positive" if prediction.item() == 1 else "Negative" |
|
return sentiment |
|
|
|
|
|
demo = gr.Interface( |
|
fn=analyze_sentiment, |
|
inputs="text", |
|
outputs="text", |
|
title="Sentiment Analysis with DistilBERT", |
|
description="Enter text to analyze its sentiment (Positive/Negative)", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |