File size: 1,044 Bytes
cb7b755 6a48938 a6abf3f cb7b755 6a48938 e05594a a6abf3f dc97d53 4928a02 dc97d53 a6abf3f 4928a02 e05594a e3177ba 6a48938 a6abf3f 4928a02 6a48938 4928a02 6a48938 dc97d53 6a48938 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import gradio as gr
from transformers import BertForSequenceClassification, BertTokenizer
import torch
model_name = "ArunNyp7/sentimentclassifier-finetuned-bert"
model = BertForSequenceClassification.from_pretrained(model_name)
tokenizer = BertTokenizer.from_pretrained(model_name)
def classify_sentiment(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
prediction = torch.argmax(logits, dim=-1).item()
labels = {0: "Positive", 1: "Neutral", 2: "Negative"}
return labels[prediction]
with gr.Blocks() as demo:
gr.Markdown("### Sentiment Classification using Fine-Tuned BERT Model")
text_input = gr.Textbox(label="Enter Text", placeholder="Type here...", lines=2)
sentiment_output = gr.Label()
submit_btn = gr.Button("Classify Sentiment")
submit_btn.click(fn=classify_sentiment, inputs=text_input, outputs=sentiment_output)
demo.launch(share=False) |