import torch import gradio as gr from transformers import AutoModelForSequenceClassification, AutoTokenizer # Load model and tokenizer from Hugging Face Hub model_name = "shukdevdatta123/twitter-distilbert-base-uncased-sentiment-analysis-lora-text-classification" model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) # Define the label mapping (binary sentiment: 0 = Negative, 1 = Positive) id2label = { 0: "Negative", 1: "Positive" } # Set device device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) # Define the prediction function def predict_sentiment(text): inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device) with torch.no_grad(): logits = model(**inputs).logits predicted_class = torch.argmax(logits, dim=1).item() label = id2label[predicted_class] # Optional: add confidence score probs = torch.nn.functional.softmax(logits, dim=1) confidence = probs[0][predicted_class].item() return f"{label} (Confidence: {confidence:.2f})" # Create Gradio Interface interface = gr.Interface( fn=predict_sentiment, inputs=gr.Textbox(lines=2, placeholder="Enter a sentence to analyze sentiment..."), outputs="text", title="Twitter Sentiment Classifier", description="This app uses a fine-tuned DistilBERT model with LoRA adapters to predict whether a tweet or sentence is Positive or Negative." ) # Launch the app interface.launch()