import gradio as gr import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification import pandas as pd from collections import Counter class ClimateChangeAnalyzer: def __init__(self): self.model = None self.tokenizer = None self.sentiment_labels = { -1: "Anti-Climate", 0: "Neutral", 1: "Pro-Climate", 2: "News" } self.load_model() def load_model(self): try: print("Loading tokenizer and model...") self.tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") self.model = AutoModelForSequenceClassification.from_pretrained("keanteng/bert-large-raw-climate-sentiment-wqf7007") device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.model.to(device) self.model.eval() print(f"✅ Model loaded successfully on {device}!") except Exception as e: print(f"❌ Model loading failed: {e}") def predict_sentiment(self, text): if not self.model or not self.tokenizer: return "❌ Model not loaded" if not text.strip(): return "⚠️ Please enter some text" try: device = next(self.model.parameters()).device inputs = self.tokenizer( text, return_tensors="pt", truncation=True, max_length=128, padding=True ) inputs = {k: v.to(device) for k, v in inputs.items()} with torch.no_grad(): outputs = self.model(**inputs) probabilities = torch.softmax(outputs.logits, dim=1) prediction = torch.argmax(outputs.logits, dim=1) predicted_label = prediction.item() if predicted_label == 0: sentiment_key = -1 elif predicted_label == 1: sentiment_key = 0 elif predicted_label == 2: sentiment_key = 1 else: sentiment_key = 2 confidence = probabilities[0][predicted_label].item() sentiment = self.sentiment_labels[sentiment_key] result = f""" **🎯 Prediction:** {sentiment} **📊 Confidence:** {confidence:.2%} **🤖 Model:** keanteng/bert-large-raw-climate-sentiment-wqf7007 **⚡ Device:** {device} ✅ *Real prediction from your team's BERT model* """ return result except Exception as e: return f"❌ Prediction failed: {str(e)}" def predict_batch(self, texts): if not texts: return "⚠️ No texts to analyze", None text_list = [t.strip() for t in texts.split('\n') if t.strip()] if not text_list: return "⚠️ No valid texts found", None results = [] for text in text_list[:20]: pred_result = self.predict_sentiment(text) if "❌" not in pred_result and "⚠️" not in pred_result: lines = pred_result.split('\n') sentiment = lines[1].split('**🎯 Prediction:** ')[1] confidence = lines[2].split('**📊 Confidence:** ')[1] results.append({ "Text": text[:80] + "..." if len(text) > 80 else text, "Sentiment": sentiment, "Confidence": confidence }) if not results: return "❌ No successful predictions", None sentiments = [r["Sentiment"] for r in results] sentiment_counts = Counter(sentiments) summary = f"**Analysis Results:**\n\nTotal texts: {len(results)}\n\n**Distribution:**\n" for sentiment, count in sentiment_counts.items(): percentage = (count / len(results)) * 100 summary += f"\n- **{sentiment}**: {count} ({percentage:.1f}%)" df = pd.DataFrame(results) return summary, df print("🌍 Initializing Climate Change Sentiment Analyzer...") analyzer = ClimateChangeAnalyzer() with gr.Blocks(title="Climate Change Sentiment Analysis") as demo: gr.HTML("""
Group 4
Model: keanteng/bert-large-raw-climate-sentiment-wqf7007