dianacasti's picture
Add HF Space app with adapters (no venv)
fb55abc
|
Raw
History Blame Contribute Delete
8.38 kB
metadata
title: Multi-Personality Chatbot
emoji: 🎭
colorFrom: purple
colorTo: pink
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
license: mit

🎭 Multi-Personality Chatbot

An interactive web application featuring multiple AI personalities powered by LoRA adapters fine-tuned on Qwen2-0.5B-Instruct.

🌟 Features

  • 4 Unique Personalities: Brainrot, Pirate, Yoda, and Nerd
  • Interactive Web Interface: Clean Gradio-based chat UI
  • Real-time Personality Switching: Change personalities on the fly
  • Customizable Generation: Adjust temperature and response length
  • Free Public Deployment: Deployable to Hugging Face Spaces

πŸš€ Quick Start (Local Testing)

1. Install Dependencies

pip install -r requirements.txt

2. Run the App

python app.py

The app will launch at http://localhost:7860

3. Test Models (Optional)

Test individual models with the test script:

python test_model.py

🌐 Deploy to Hugging Face Spaces (Free & Public)

See README_DEPLOYMENT.md for complete deployment instructions.

Quick version:

  1. Create a Space at https://huggingface.co/spaces
  2. Push your code:
    git init
    git lfs install
    git lfs track "*.safetensors"
    git add .
    git commit -m "Initial commit"
    git remote add space https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
    git push space main
    
  3. Your app will be live at: https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space

πŸ“ Project Structure

llm-project/
β”œβ”€β”€ app.py                              # Main Gradio web application
β”œβ”€β”€ test_model.py                       # CLI testing script
β”œβ”€β”€ requirements.txt                    # Python dependencies
β”œβ”€β”€ README.md                           # This file
β”œβ”€β”€ README_DEPLOYMENT.md                # Deployment guide
β”œβ”€β”€ llm_personality_brainrot.py        # Training script (brainrot)
β”œβ”€β”€ llm_personality_pirate.py          # Training script (pirate)
β”œβ”€β”€ llm_personality_yoda.py            # Training script (yoda)
β”œβ”€β”€ llm_personality_nerd.py            # Training script (nerd)
β”œβ”€β”€ qwen-brainrot-lora-stage1-final/   # LoRA adapter (brainrot)
β”œβ”€β”€ pirate-lora-adapter/               # LoRA adapter (pirate)
β”œβ”€β”€ yoda-lora-adapter/                 # LoRA adapter (yoda)
└── nerd-lora-adapter/                 # LoRA adapter (nerd)

🎭 Personality Descriptions

  • 🧠 Brainrot: Speaks in internet slang and Gen-Z language
  • πŸ΄β€β˜ οΈ Pirate: Talks like a swashbuckling pirate, arr matey!
  • πŸ§™ Yoda: Wise Jedi master who speaks in reverse order
  • πŸ€“ Nerd: Intellectual who loves facts, science, and technical details

πŸ› οΈ Technical Details

  • Base Model: Qwen/Qwen2-0.5B-Instruct (500M parameters)
  • Fine-tuning Method: LoRA (Low-Rank Adaptation)
  • Framework: Hugging Face Transformers + PEFT
  • UI: Gradio 4.x
  • Training: Each personality trained on 5-250 examples

LoRA Configuration

  • Rank (r): 16
  • Alpha: 32
  • Dropout: 0.05-0.1
  • Target modules: q_proj, k_proj, v_proj, o_proj

πŸ“ Usage Examples

Local Testing

# Test a specific personality
python test_model.py
# Select personality when prompted
# Chat interactively

Programmatic Use

from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel

# Load model
tokenizer = AutoTokenizer.from_pretrained("pirate-lora-adapter")
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
model = PeftModel.from_pretrained(base_model, "pirate-lora-adapter")

# Generate
prompt = "<|im_start|>user\nHello!<|im_end|>\n<|im_start|>assistant\n"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_length=100)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)

πŸ”§ Configuration

Edit app.py to customize:

  • Temperature: Controls randomness (0.1 = focused, 2.0 = creative)
  • Max Tokens: Response length limit
  • Base Model: Change BASE_MODEL constant
  • Adapters: Add/remove from ADAPTERS dictionary

πŸ“Š Performance

  • CPU: ~2-5 seconds per response (usable but slow)
  • GPU (T4): ~0.5-1 seconds per response (recommended)
  • Memory: ~2GB RAM (CPU) or ~4GB VRAM (GPU)

πŸ› Troubleshooting

"CUDA out of memory"

  • Switch to CPU: Edit app.py, change device to "cpu"
  • Or use smaller batch size

"Module not found"

pip install -r requirements.txt

"Adapter files not found"

  • Ensure adapter folders are in the same directory as app.py
  • Check folder names match ADAPTERS dict in app.py

🀝 Team Collaboration

πŸ“š For detailed collaboration instructions, see COLLABORATION.md

For Team Members: Updating Adapters After Deployment

Once the app is deployed to Hugging Face Spaces, team members can collaborate in several ways:

Method 1: Direct Upload on Hugging Face (Easiest)

  1. Get added as a collaborator on the Space (ask the Space owner)
  2. Go to the Space: https://huggingface.co/spaces/dianacasti/personality-chatbot
  3. Click "Files" tab
  4. To replace an adapter:
    • Delete the old adapter folder (e.g., nerd-lora-adapter)
    • Click "Add file" β†’ "Upload files"
    • Upload your new adapter folder with all files:
      • adapter_model.safetensors
      • adapter_config.json
      • All tokenizer files
    • Commit changes
  5. Space automatically rebuilds (2-5 minutes)
  6. Test the updated personality!

Method 2: Using Git

# Clone the Space
git clone https://huggingface.co/spaces/dianacasti/personality-chatbot
cd personality-chatbot

# Replace adapter folders with your newly trained versions
# Copy your improved: nerd-lora-adapter, yoda-lora-adapter, etc.

# Commit and push
git add .
git commit -m "Updated adapters with improved training (50+ examples)"
git push

Method 3: Upload Adapters to Hugging Face Model Hub

  1. Upload your adapter as a model repo:

    from huggingface_hub import HfApi
    api = HfApi()
    api.upload_folder(
        folder_path="nerd-lora-adapter",
        repo_id="YOUR_USERNAME/nerd-lora-improved",
        repo_type="model"
    )
    
  2. Update app.py to use the new model:

    ADAPTERS = {
        "πŸ€“ Nerd": "YOUR_USERNAME/nerd-lora-improved",  # Changed
        # ... other adapters
    }
    
  3. Commit and push the app.py change

Training New/Better Adapters

To retrain a personality with more data:

  1. Edit the corresponding training script (e.g., llm_personality_nerd.py)
  2. Increase training examples (50+ recommended instead of 5)
  3. Run the training script: python llm_personality_nerd.py
  4. Upload the new adapter folder using Method 1, 2, or 3 above

Adding New Personalities

  1. Create training data (20-50+ examples recommended)
  2. Use a personality script as template (copy llm_personality_pirate.py)
  3. Train the new adapter
  4. Add to ADAPTERS dict in app.py:
    ADAPTERS = {
        "🧠 Brainrot": "qwen-brainrot-lora-stage1-final",
        "πŸ΄β€β˜ οΈ Pirate": "pirate-lora-adapter",
        "πŸ§™ Yoda": "yoda-lora-adapter",
        "πŸ€“ Nerd": "nerd-lora-adapter",
        "πŸ€– YourNew": "yournew-lora-adapter",  # Add this
    }
    
  5. Update personality descriptions in the UI markdown
  6. Commit and push changes

🀝 Contributing

To add a new personality:

  1. Create training data (20-50+ examples recommended)
  2. Train using a personality script as template
  3. Add adapter to ADAPTERS in app.py
  4. Update personality descriptions in the Gradio UI
  5. Upload adapter folder and push changes

πŸ“œ License

MIT License - Feel free to use and modify!

πŸ™ Acknowledgments

  • Base Model: Qwen2-0.5B-Instruct by Alibaba Cloud
  • Framework: Hugging Face Transformers & PEFT
  • UI: Gradio by Gradio Team
  • Training Dataset (Brainrot): GoofyLM/Brainrot-xK-large

πŸ“ž Support

For deployment help, see README_DEPLOYMENT.md

For issues, check:

  • Adapter folder structure is correct
  • All dependencies installed
  • Model files present in adapter folders

Built with ❀️ using Qwen2, LoRA, and Gradio