Nicolettem's picture
Add Gradio app.py file
4c5c12c verified
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
import numpy as np
# Load model and tokenizer from Hugging Face Hub
model_name = "Nicolettem/bert-sentiment-nic"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Label mapping (match your label encoder)
label_map = {0: "negative", 1: "neutral", 2: "positive"}
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_id = int(torch.argmax(logits, dim=-1))
return label_map.get(predicted_class_id, "Unknown")
# Gradio interface
demo = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(lines=3, placeholder="Enter airline feedback here..."),
outputs="text",
title="Airline Sentiment Classifier",
description="Enter a sentence about an airline, and this model predicts the sentiment: positive, neutral, or negative."
)
demo.launch()