Spaces:
Sleeping
A newer version of the Gradio SDK is available: 6.12.0
title: 6.C395 Chatbot
emoji: π
colorFrom: blue
colorTo: red
sdk: gradio
sdk_version: 5.23.3
python_version: '3.10'
app_file: app.py
pinned: false
secrets:
- HF_TOKEN
6.C395 Chatbot
This is a skeleton repo you can use to design your chatbot. Feel free to change it however you'd like! This repo is compatible with CPU (using your own computer) because it uses models on HuggingFace. You can also load models locally if you'd like, but we recommend using smaller ones.
The end goal: make the chatbot and upload it to a Huggingface Space. We have included instructions for using HuggingFace below. Here's an example of a chatbot made by the course staff. Yours should be better!
Note: We encourage you to use AI tools (like Cursor or LLMs) to help you on this assignment. Learn how to leverage these tools.
Setup
- Make a virtual environment and install the required dependencies:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Make a HuggingFace account and make an access token:
- Visit Hugging Face
- Make an account if you don't already have one
- Click on your profile, then "Access Tokens" and make a new token
- Make a .env file with
HF_TOKEN=<insert your token here> - Now, log in to Hugging Face in the terminal as well:
hf auth loginChoose a base model:
- In config.py, set the BASE_MODEL variable to your base model of choice from HuggingFace.
- Keep in mind it's better to have a small, lightweight model if you plan on finetuning.
Use all FindTreatment.gov data (optional): The app loads facilities from
data/facilities.csvlocally. To use the full dataset (same as FindTreatment.gov), run:python scripts/download_findtreatment_data.pyThis downloads the official SAMHSA National Directory and builds
data/facilities.csv. Requiresopenpyxl(inrequirements.txt).
For Hugging Face Spaces: The full CSV is too large to push. Use a Hugging Face Dataset and set theFACILITIES_DATASETvariable (e.g.your-username/samhsa-facilities). See data/README.md for step-by-step instructions.
Repository Organization
6.c395-chatbot/
βββ app.py # Gradio web interface - implement the chat function
βββ requirements.txt # Python dependencies
βββ chatbot_development.ipynb # Notebook for developing and testing your chatbot
βββ .env # Add this file yourself for storing your HF_TOKEN
βββ config.py # Holds variables for the models from HuggingFace you will use
βββ bps_chatbot_conversation_example.txt # Example conversation we might want to have with the Option 1 chatbot
βββ mit_chatbot_conversation_example.txt # Example conversation we might want to have with the Option 2 chatbot
βββ samhsa_chatbot_conversation_example.txt # Example conversation we might want to have with the Option 3 chatbot
βββ src/
βββ chat.py # Chatbot class (implement this)
Key Files:
app.py: Creates the web interface using Gradio. You only need to implement the
chatfunction that generates responses.chat.py: Contains the
Chatbotclass where you'll implement:format_prompt: Format user input into proper promptsget_response: Generate responses using the model
config.py: Contains the
BASE_MODELandMY_MODELvariables, which are names of models on HuggingFace. Update theMY_MODELvariable if you create a new model and upload it to the HuggingFace Hub.chatbot_development.ipynb: Jupyter notebook for:
- Experimenting with the chatbot
- Trying different approaches
- Testing responses before deployment
What You Need to Implement:
In
chat.py:- Complete the
Chatbotclass methods - Design how the chatbot formats prompts
- Implement response generation
- Complete the
In
app.py:- Implement the
chatfunction to work with Gradio - The rest of the file is already set up
- Implement the
Use
chatbot_development.ipynbto:- Develop and test your implementation
- Try different approaches
- Verify everything works before deployment
After you update the code, you can run the chatbot locally:
python app.py
Deploying to Hugging Face
To deploy your chatbot as a free web interface using Hugging Face Spaces:
Create a Hugging Face Space:
- Go to Hugging Face Spaces
- Click "New Space"
- Choose a name for your space (e.g., "6.C395-chatbot")
- Select "Gradio" as the SDK
- Choose "CPU" as the hardware (free tier)
- Make it "Public" so others can use your chatbot
Prepare your files: Your repository should already have all needed files:
6.c395-chatbot/ βββ README.md # Description of your chatbot βββ app.py # Your Gradio interface βββ requirements.txt # Already set up with needed dependencies βββ src/ # Your implementation filesPush your code to the Space:
git init git add . git commit -m "Initial commit" git remote set-url huggingface https://phanny:YOUR_TOKEN_HERE@huggingface.co/spaces/phanny/6.C395-chatbot git push huggingface student-version:main --force
4. Add your HF_TOKEN to the space as a secret.
- Go to Files.
- Go to Settings.
- Under secrets, add HF_TOKEN.
5. Important Free Tier Considerations:
- The default model (meta-llama/Llama-3.1-8B-Instruct) runs via HuggingFace's Inference Providers, not on your Space's CPU. Your Space just hosts the Gradio UI.
- Free HuggingFace accounts have a limited monthly credit quota for Inference Providers. You may hit a 402 "Payment Required" error if you exceed it. To conserve credits, test locally when possible (`python app.py`) and avoid unnecessary requests.
- The interface might queue requests when multiple users access it. Sometimes there will be 503 errors. Just try again a few seconds later.
6. After Deployment:
- Your chatbot will be available at: `https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME`
- Anyone can use it through their web browser
- You can update the deployment anytime by pushing changes:
```bash
git add .
git commit -m "Update chatbot"
git push
```
7. Troubleshooting:
- Check the Space's logs if the chatbot isn't working
- Verify the chatbot works locally before deploying
- Remember free tier has limited resources. Sometimes if you get a 503 error it means the server is overloaded. Just try again a few seconds later.
Your chatbot should now be accessible to anyone through their web browser!