| import gradio as gr |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification |
| import torch |
|
|
| MODEL_NAME = "Ak47-model-ml/Bert-Sentiment" |
|
|
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) |
|
|
| def predict_sentiment(text): |
| inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) |
| |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| |
| probs = torch.softmax(outputs.logits, dim=1)[0].tolist() |
| labels = ["Negative", "Positive"] |
| |
| return {labels[i]: probs[i] for i in range(len(labels))} |
|
|
| gr.Interface( |
| fn=predict_sentiment, |
| inputs=gr.Textbox(lines=4, placeholder="Enter text here..."), |
| outputs=gr.Label(num_top_classes=2), |
| title="BERT Sentiment Analyzer", |
| description="Real-time sentiment prediction using fine-tuned BERT model" |
| ).launch() |