""" Run PALADIM directly from Hugging Face Hub No need to clone the repository - everything loads from the cloud! """ import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification from peft import PeftModel def demo_from_huggingface(): """ Load and use PALADIM model directly from Hugging Face Perfect for: Notebooks, Colab, or quick testing """ print("="*70) print("PALADIM Demo - Running from Hugging Face Hub") print("="*70) print("\nšŸ”— Model: https://huggingface.co/nickagge/paladim-sentiment") print("šŸ“¦ No installation needed - everything loads from the cloud!\n") # Step 1: Load base model and tokenizer from Hugging Face print("Step 1: Loading base model from Hugging Face...") base_model = "distilbert-base-uncased" model = AutoModelForSequenceClassification.from_pretrained( base_model, num_labels=2 ) tokenizer = AutoTokenizer.from_pretrained(base_model) print(" āœ… Base model loaded") # Step 2: Load PALADIM LoRA adapters from Hugging Face print("\nStep 2: Loading PALADIM adapters from your Hugging Face repo...") model = PeftModel.from_pretrained(model, "nickagge/paladim-sentiment") print(" āœ… PALADIM model ready!") # Step 3: Run inference print("\n" + "="*70) print("Testing PALADIM on various inputs:") print("="*70) test_cases = [ # Positive examples "This product exceeded my expectations! Absolutely love it!", "Best purchase I've made this year. Highly recommend!", "Amazing quality and fast shipping. Five stars!", # Negative examples "Terrible quality. Complete waste of money.", "Very disappointed. Do not buy this product.", "Worst experience ever. Will never buy again.", # Neutral/Mixed "It's okay, nothing special.", "Good product but overpriced.", ] model.eval() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) print(f"\nšŸ’» Running on: {device}") if device.type == "cuda": print(f" GPU: {torch.cuda.get_device_name(0)}\n") print("-"*70) with torch.no_grad(): for i, text in enumerate(test_cases, 1): # Tokenize inputs = tokenizer( text, return_tensors="pt", truncation=True, max_length=128, padding=True ) inputs = {k: v.to(device) for k, v in inputs.items()} # Predict outputs = model(**inputs) logits = outputs.logits probs = torch.softmax(logits, dim=-1)[0] prediction = torch.argmax(logits, dim=-1).item() confidence = probs[prediction].item() # Format output sentiment = "šŸ‘ Positive" if prediction == 1 else "šŸ‘Ž Negative" bar_length = int(confidence * 20) bar = "ā–ˆ" * bar_length + "ā–‘" * (20 - bar_length) print(f"\n{i}. \"{text}\"") print(f" → {sentiment}") print(f" Confidence: [{bar}] {confidence:.1%}") print("\n" + "="*70) print("✨ Demo complete!") print("\nšŸ“š How to use this in your code:") print("-"*70) print(""" from transformers import AutoModelForSequenceClassification from peft import PeftModel import torch # Load model model = AutoModelForSequenceClassification.from_pretrained( "distilbert-base-uncased", num_labels=2 ) model = PeftModel.from_pretrained(model, "nickagge/paladim-sentiment") # Tokenizer tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased") # Predict text = "Your text here" inputs = tokenizer(text, return_tensors="pt", truncation=True) outputs = model(**inputs) prediction = torch.argmax(outputs.logits, dim=-1).item() sentiment = "Positive" if prediction == 1 else "Negative" print(f"Sentiment: {sentiment}") """) print("="*70) print("\nšŸ’” Tips:") print(" • Works in Google Colab, Jupyter, or any Python environment") print(" • No need to clone repository") print(" • Model automatically cached locally after first run") print(" • Use GPU for faster inference (CUDA required)") print("\nšŸš€ Ready to use PALADIM in your projects!") if __name__ == "__main__": try: demo_from_huggingface() except Exception as e: print(f"\nāŒ Error: {e}") print("\nTroubleshooting:") print("1. Install required packages:") print(" pip install transformers peft torch") print("\n2. Check internet connection (model loads from cloud)") print("\n3. Verify model exists: https://huggingface.co/nickagge/paladim-sentiment") import traceback traceback.print_exc()