Spaces:
Sleeping
Sleeping
Katherine
commited on
Commit
·
c487b8d
1
Parent(s):
6bf06c5
initial commit
Browse files- LICENSE +21 -0
- README.md +151 -7
- __pycache__/config.cpython-311.pyc +0 -0
- app.py +85 -0
- chatbot_conversation_example.txt +71 -0
- chatbot_development.ipynb +161 -0
- config.py +16 -0
- requirements.txt +10 -0
- src/__pycache__/chat.cpython-311.pyc +0 -0
- src/chat.py +73 -0
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2025 Sarah Bentley
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,13 +1,157 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
|
|
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: 6.S041 Staff Chatbot
|
3 |
+
emoji: 🚀
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.23.3
|
8 |
+
python_version: "3.10"
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
+
secrets:
|
12 |
+
- HF_TOKEN
|
13 |
---
|
14 |
|
15 |
+
# Boston Public School Selection Chatbot
|
16 |
+
|
17 |
+
This is a skeleton repo you can use to design your school choice 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.
|
18 |
+
|
19 |
+
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](https://huggingface.co/spaces/sbentley/Boston-School-Choice) of a chatbot made by the course staff. Yours should be much better!
|
20 |
+
|
21 |
+
Note: We encourage you to use AI tools (like Cursor or LLMs) to help you on this assignment. Learn how to leverage these tools.
|
22 |
+
|
23 |
+
## Setup
|
24 |
+
|
25 |
+
1. Make a virtual environment and install the required dependencies:
|
26 |
+
```bash
|
27 |
+
python -m venv venv
|
28 |
+
source venv/bin/activate
|
29 |
+
pip install -r requirements.txt
|
30 |
+
```
|
31 |
+
|
32 |
+
2. Make a HuggingFace account and make an access token:
|
33 |
+
- Visit [Hugging Face](https://huggingface.co)
|
34 |
+
- Make an account if you don't already have one
|
35 |
+
- Click on your profile, then "Access Tokens" and make a new token
|
36 |
+
- Make a .env file with `HF_TOKEN=<insert your token here>`
|
37 |
+
- Now, log in to Hugging Face in the terminal as well:
|
38 |
+
```bash
|
39 |
+
huggingface-cli login
|
40 |
+
```
|
41 |
+
|
42 |
+
3. Choose a base model:
|
43 |
+
- In config.py, set the BASE_MODEL variable to your base model of choice from HuggingFace.
|
44 |
+
- Keep in mind it's better to have a small, lightweight model if you plan on finetuning.
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
## Repository Organization
|
49 |
+
|
50 |
+
```
|
51 |
+
6.s041-chatbot/
|
52 |
+
├── app.py # Gradio web interface - implement the chat function
|
53 |
+
├── requirements.txt # Python dependencies
|
54 |
+
├── chatbot_development.ipynb # Notebook for developing and testing your chatbot
|
55 |
+
├── .env # Add this file yourself for storing your HF_TOKEN
|
56 |
+
├── config.py # Holds variables for the models from HuggingFace you will use
|
57 |
+
├── chatbot_conversation_example.txt # Example conversation we might want to have with this chatbot
|
58 |
+
└── src/
|
59 |
+
└── chat.py # SchoolChatbot class (implement this)
|
60 |
+
```
|
61 |
+
|
62 |
+
### Key Files:
|
63 |
+
|
64 |
+
- **app.py**: Creates the web interface using Gradio. You only need to implement the `chat` function that generates responses.
|
65 |
+
|
66 |
+
- **chat.py**: Contains the `SchoolChatbot` class where you'll implement:
|
67 |
+
- `format_prompt`: Format user input into proper prompts
|
68 |
+
- `get_response`: Generate responses using the model
|
69 |
+
|
70 |
+
- **config.py**: Contains the `BASE_MODEL` and `MY_MODEL` variables, which are names of models on HuggingFace. Update the `MY_MODEL` variable if you create a new model and upload it to the HuggingFace Hub.
|
71 |
+
|
72 |
+
- **chatbot_development.ipynb**: Jupyter notebook for:
|
73 |
+
- Experimenting with the chatbot
|
74 |
+
- Trying different approaches
|
75 |
+
- Testing responses before deployment
|
76 |
+
|
77 |
+
### What You Need to Implement:
|
78 |
+
|
79 |
+
1. In `chat.py`:
|
80 |
+
- Complete the `SchoolChatbot` class methods
|
81 |
+
- Design how the chatbot formats prompts
|
82 |
+
- Implement response generation
|
83 |
+
|
84 |
+
2. In `app.py`:
|
85 |
+
- Implement the `chat` function to work with Gradio
|
86 |
+
- The rest of the file is already set up
|
87 |
+
|
88 |
+
3. Use `chatbot_development.ipynb` to:
|
89 |
+
- Develop and test your implementation
|
90 |
+
- Try different approaches
|
91 |
+
- Verify everything works before deployment
|
92 |
+
|
93 |
+
4. After you update the code, you can run the chatbot locally:
|
94 |
+
```bash
|
95 |
+
python app.py
|
96 |
+
```
|
97 |
+
|
98 |
+
|
99 |
+
## Deploying to Hugging Face
|
100 |
+
|
101 |
+
To deploy your chatbot as a free web interface using Hugging Face Spaces:
|
102 |
+
|
103 |
+
1. Create a Hugging Face Space:
|
104 |
+
- Go to [Hugging Face Spaces](https://huggingface.co/spaces)
|
105 |
+
- Click "New Space"
|
106 |
+
- Choose a name for your space (e.g., "boston-school-chatbot")
|
107 |
+
- Select "Gradio" as the SDK
|
108 |
+
- Choose "CPU" as the hardware (free tier)
|
109 |
+
- Make it "Public" so others can use your chatbot
|
110 |
+
|
111 |
+
2. Prepare your files:
|
112 |
+
Your repository should already have all needed files:
|
113 |
+
```
|
114 |
+
6.so41-midterm/
|
115 |
+
├── README.md # Description of your chatbot
|
116 |
+
├── app.py # Your Gradio interface
|
117 |
+
├── requirements.txt # Already set up with needed dependencies
|
118 |
+
└── src/ # Your implementation files
|
119 |
+
```
|
120 |
+
|
121 |
+
3. Push your code to the Space:
|
122 |
+
```bash
|
123 |
+
git init
|
124 |
+
git add .
|
125 |
+
git commit -m "Initial commit"
|
126 |
+
git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
127 |
+
git push -u origin main
|
128 |
+
```
|
129 |
+
|
130 |
+
4. Add your HF_TOKEN to the space as a secret.
|
131 |
+
- Go to Files.
|
132 |
+
- Go to Settings.
|
133 |
+
- Under secrets, add HF_TOKEN.
|
134 |
+
|
135 |
+
|
136 |
+
5. Important Free Tier Considerations:
|
137 |
+
- Use free tier model (already configured in model.py)
|
138 |
+
- Free CPU spaces have 2GB RAM limit
|
139 |
+
- The interface might queue requests when multiple users access it. Sometimes there will be 503 errors.
|
140 |
+
|
141 |
+
6. After Deployment:
|
142 |
+
- Your chatbot will be available at: `https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME`
|
143 |
+
- Anyone can use it through their web browser
|
144 |
+
- You can update the deployment anytime by pushing changes:
|
145 |
+
```bash
|
146 |
+
git add .
|
147 |
+
git commit -m "Update chatbot"
|
148 |
+
git push
|
149 |
+
```
|
150 |
+
|
151 |
+
7. Troubleshooting:
|
152 |
+
- Check the Space's logs if the chatbot isn't working
|
153 |
+
- Verify the chatbot works locally before deploying
|
154 |
+
- 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.
|
155 |
+
|
156 |
+
Your chatbot should now be accessible to anyone through their web browser!
|
157 |
+
|
__pycache__/config.cpython-311.pyc
ADDED
Binary file (412 Bytes). View file
|
|
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Gradio Web Interface for Boston School Chatbot
|
3 |
+
|
4 |
+
This script creates a web interface for your chatbot using Gradio.
|
5 |
+
You only need to implement the chat function.
|
6 |
+
|
7 |
+
Key Features:
|
8 |
+
- Creates a web UI for your chatbot
|
9 |
+
- Handles conversation history
|
10 |
+
- Provides example questions
|
11 |
+
- Can be deployed to Hugging Face Spaces
|
12 |
+
|
13 |
+
Example Usage:
|
14 |
+
# Run locally:
|
15 |
+
python app.py
|
16 |
+
|
17 |
+
# Access in browser:
|
18 |
+
# http://localhost:7860
|
19 |
+
"""
|
20 |
+
|
21 |
+
import gradio as gr
|
22 |
+
from src.chat import SchoolChatbot
|
23 |
+
|
24 |
+
def create_chatbot():
|
25 |
+
"""
|
26 |
+
Creates and configures the chatbot interface.
|
27 |
+
"""
|
28 |
+
chatbot = SchoolChatbot()
|
29 |
+
|
30 |
+
def chat(message, history):
|
31 |
+
"""
|
32 |
+
TODO:Generate a response for the current message in a Gradio chat interface.
|
33 |
+
|
34 |
+
This function is called by Gradio's ChatInterface every time a user sends a message.
|
35 |
+
You only need to generate and return the assistant's response - Gradio handles the
|
36 |
+
chat display and history management automatically.
|
37 |
+
|
38 |
+
Args:
|
39 |
+
message (str): The current message from the user
|
40 |
+
history (list): List of previous message pairs, where each pair is
|
41 |
+
[user_message, assistant_message]
|
42 |
+
Example:
|
43 |
+
[
|
44 |
+
["What schools offer Spanish?", "The Hernandez School..."],
|
45 |
+
["Where is it located?", "The Hernandez School is in Roxbury..."]
|
46 |
+
]
|
47 |
+
|
48 |
+
Returns:
|
49 |
+
str: The assistant's response to the current message.
|
50 |
+
|
51 |
+
|
52 |
+
Note:
|
53 |
+
- Gradio automatically:
|
54 |
+
- Displays the user's message
|
55 |
+
- Displays your returned response
|
56 |
+
- Updates the chat history
|
57 |
+
- Maintains the chat interface
|
58 |
+
- You only need to:
|
59 |
+
- Generate an appropriate response to the current message
|
60 |
+
- Return that response as a string
|
61 |
+
"""
|
62 |
+
# TODO: Generate and return response
|
63 |
+
try:
|
64 |
+
response = chatbot.get_response(message)
|
65 |
+
return response
|
66 |
+
except Exception as e:
|
67 |
+
return f"Sorry, something went wrong: {str(e)}"
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
# Create Gradio interface. Customize the interface however you'd like!
|
72 |
+
demo = gr.ChatInterface(
|
73 |
+
chat,
|
74 |
+
title="Boston Public School Selection Assistant",
|
75 |
+
description="Ask me anything about Boston public schools! Since I am a free tier chatbot, I may give a 503 error when I'm busy. If that happens, please try again a few seconds later.",
|
76 |
+
examples=[
|
77 |
+
"I live in Jamaica Plain and want to send my child to kindergarten. What schools are available?"
|
78 |
+
]
|
79 |
+
)
|
80 |
+
|
81 |
+
return demo
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
demo = create_chatbot()
|
85 |
+
demo.launch()
|
chatbot_conversation_example.txt
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
User: Hi, I'm looking for a public school in Boston for my child who will be starting kindergarten next year.
|
2 |
+
|
3 |
+
Bot: Hello! I'd be happy to help you find the right public school in Boston. To better assist you, could you tell me:
|
4 |
+
1. Which neighborhood do you live in?
|
5 |
+
2. Do you have any specific preferences (like language programs, special education services, or after-school programs)?
|
6 |
+
3. Would you prefer a traditional public school or a charter school?
|
7 |
+
|
8 |
+
User: We live in Jamaica Plain, and I'm interested in schools with strong language programs. My child is already showing interest in learning Spanish.
|
9 |
+
|
10 |
+
Bot: Great! I can help you with that. There are several excellent options in Jamaica Plain and nearby areas. Let me suggest a few schools that might be a good fit:
|
11 |
+
|
12 |
+
1. The Hernandez K-8 School in Roxbury (about 1.5 miles from JP) offers a Spanish-English dual language program starting from kindergarten. They have a strong academic reputation and diverse student body.
|
13 |
+
|
14 |
+
2. The Mozart Elementary School in JP has a Spanish language immersion program and is known for its strong arts integration.
|
15 |
+
|
16 |
+
3. The Mission Hill K-8 School in Roxbury offers Spanish language classes and has a progressive education model.
|
17 |
+
|
18 |
+
Would you like to know more about any of these schools? I can provide details about:
|
19 |
+
- Class sizes
|
20 |
+
- Test scores
|
21 |
+
- Special programs
|
22 |
+
- Parent reviews
|
23 |
+
- Transportation options
|
24 |
+
|
25 |
+
User: Could you tell me more about the Hernandez School? I've heard good things about it.
|
26 |
+
|
27 |
+
Bot: Certainly! The Hernandez K-8 School has several notable features:
|
28 |
+
|
29 |
+
- Their dual language program is one of the strongest in the district, with 50% of instruction in Spanish and 50% in English
|
30 |
+
- Average class size is 22 students
|
31 |
+
- They offer free breakfast and lunch programs
|
32 |
+
- The school has a strong parent-teacher organization
|
33 |
+
- They provide after-school programs until 6 PM
|
34 |
+
- The school is accessible by public transportation (Orange Line and several bus routes)
|
35 |
+
|
36 |
+
Would you like to know about:
|
37 |
+
1. Their enrollment process?
|
38 |
+
2. How to schedule a school tour?
|
39 |
+
3. Their academic performance metrics?
|
40 |
+
4. Specific programs for kindergarten students?
|
41 |
+
|
42 |
+
User: Yes, how do I schedule a tour?
|
43 |
+
|
44 |
+
Bot: You can schedule a tour of the Hernandez School in two ways:
|
45 |
+
|
46 |
+
1. Call the main office at (617) 635-XXXX during school hours (8:00 AM - 4:00 PM)
|
47 |
+
2. Visit the Boston Public Schools website and use their online tour scheduling system
|
48 |
+
|
49 |
+
Tours are typically offered on Tuesday and Thursday mornings at 9:00 AM. They last about 45 minutes and include:
|
50 |
+
- A classroom visit
|
51 |
+
- Meeting with the principal
|
52 |
+
- Q&A session
|
53 |
+
- Overview of the dual language program
|
54 |
+
|
55 |
+
Would you like me to provide the exact contact information for scheduling a tour?
|
56 |
+
|
57 |
+
User: Yes, please.
|
58 |
+
|
59 |
+
Bot: Here's the contact information for the Hernandez School:
|
60 |
+
|
61 |
+
Main Office: (617) 635-XXXX
|
62 |
+
Email: [email protected]
|
63 |
+
Address: 61 School Street, Roxbury, MA 02119
|
64 |
+
|
65 |
+
The school's website is: www.bostonpublicschools.org/hernandez
|
66 |
+
|
67 |
+
Is there anything else you'd like to know about the school or other options in the area?
|
68 |
+
|
69 |
+
User: No, that's very helpful! Thank you.
|
70 |
+
|
71 |
+
Bot: You're welcome! Good luck with your school search. If you have any more questions about Boston public schools or need help with the enrollment process, feel free to ask. Have a great day!
|
chatbot_development.ipynb
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"# Chatbot Development\n",
|
8 |
+
"\n",
|
9 |
+
"Use this notebook to load the model and then initialize, update, and test the chatbot."
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "markdown",
|
14 |
+
"metadata": {},
|
15 |
+
"source": [
|
16 |
+
"### Setup and Imports"
|
17 |
+
]
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"cell_type": "code",
|
21 |
+
"execution_count": 1,
|
22 |
+
"metadata": {},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"import torch\n",
|
26 |
+
"from huggingface_hub import login\n",
|
27 |
+
"\n",
|
28 |
+
"\n",
|
29 |
+
"from src.chat import SchoolChatbot\n",
|
30 |
+
"from config import BASE_MODEL, MY_MODEL"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"cell_type": "code",
|
35 |
+
"execution_count": 6,
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [
|
38 |
+
{
|
39 |
+
"data": {
|
40 |
+
"application/vnd.jupyter.widget-view+json": {
|
41 |
+
"model_id": "50617a1a9bdc434d9b897a5e9b529b52",
|
42 |
+
"version_major": 2,
|
43 |
+
"version_minor": 0
|
44 |
+
},
|
45 |
+
"text/plain": [
|
46 |
+
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
|
47 |
+
]
|
48 |
+
},
|
49 |
+
"metadata": {},
|
50 |
+
"output_type": "display_data"
|
51 |
+
}
|
52 |
+
],
|
53 |
+
"source": [
|
54 |
+
"\"\"\"\n",
|
55 |
+
"TODO: Add your Hugging Face token\n",
|
56 |
+
"Options:\n",
|
57 |
+
"1. Use login() and enter token when prompted. It won't ask for your token if you already logged in using the command: huggingface-cli login in the terminal.\n",
|
58 |
+
"2. Set environment variable HUGGINGFACE_TOKEN\n",
|
59 |
+
"3. Pass token directly (not recommended for shared notebooks)\n",
|
60 |
+
"\"\"\"\n",
|
61 |
+
"\n",
|
62 |
+
"login()\n",
|
63 |
+
"\n"
|
64 |
+
]
|
65 |
+
},
|
66 |
+
{
|
67 |
+
"cell_type": "markdown",
|
68 |
+
"metadata": {},
|
69 |
+
"source": [
|
70 |
+
"### Initialize and test chatbot"
|
71 |
+
]
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"cell_type": "code",
|
75 |
+
"execution_count": 7,
|
76 |
+
"metadata": {},
|
77 |
+
"outputs": [],
|
78 |
+
"source": [
|
79 |
+
"\"\"\"\n",
|
80 |
+
"Create chatbot instance using chat.py\n",
|
81 |
+
"\"\"\"\n",
|
82 |
+
"chatbot = SchoolChatbot()"
|
83 |
+
]
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"cell_type": "code",
|
87 |
+
"execution_count": 8,
|
88 |
+
"metadata": {},
|
89 |
+
"outputs": [
|
90 |
+
{
|
91 |
+
"name": "stdout",
|
92 |
+
"output_type": "stream",
|
93 |
+
"text": [
|
94 |
+
"\n",
|
95 |
+
"Question: I live in Jamaica Plain and want to send my child to a school that offers Spanish classes. What schools are available?\n",
|
96 |
+
"Response: There are several Boston Public Schools in and around Jamaica Plain that offer Spanish language instruction. Here are some options:\n",
|
97 |
+
"\n",
|
98 |
+
"1. Joseph P. Kelly Elementary School: This school offers Spanish bilingual education for students in grades Pre-K through 5. It is located at 1200 Columbus Ave, Boston, MA 02130.\n",
|
99 |
+
"\n",
|
100 |
+
"2. Edwin M. Bacon Elementary School: This school offers Spanish language instruction in grades Pre-K through 6. It is located at 30 Fayette St, Boston, MA 02130.\n",
|
101 |
+
"\n",
|
102 |
+
"3. Samuel J. & Dorothy Donnelly Elementary School: This school offers a Spanish Dual Language Learning Program in grades Pre-K through 5. It is located at 524 Cummins Highway, Roxbury, MA 02119.\n",
|
103 |
+
"\n",
|
104 |
+
"4. James Otis Elementary School: This school offers a Spanish Bilingual Program in grades Pre-K through 6. It is located at 1234 Columbus Ave, Boston, MA 02120.\n",
|
105 |
+
"\n",
|
106 |
+
"5. The John D. O'Bryant School of Mathematics and Science: This middle school (grades 6-8) offers Spanish language instruction as part of its academic program. It is located at 1345 Tremont St, Roxbury, MA 02120.\n",
|
107 |
+
"\n",
|
108 |
+
"It's recommended that you contact these schools directly to learn more about their programs and enrollment processes. Good luck!\n"
|
109 |
+
]
|
110 |
+
}
|
111 |
+
],
|
112 |
+
"source": [
|
113 |
+
"\"\"\"\n",
|
114 |
+
"Test out generating some responses from the chatbot.\n",
|
115 |
+
"Inference time\n",
|
116 |
+
"\"\"\"\n",
|
117 |
+
"test_question = \"I live in Jamaica Plain and want to send my child to a school that offers Spanish classes. What schools are available?\"\n",
|
118 |
+
"\n",
|
119 |
+
"print(f\"\\nQuestion: {test_question}\")\n",
|
120 |
+
"response = chatbot.get_response(test_question)\n",
|
121 |
+
"print(f\"Response: {response}\")\n"
|
122 |
+
]
|
123 |
+
},
|
124 |
+
{
|
125 |
+
"cell_type": "markdown",
|
126 |
+
"metadata": {},
|
127 |
+
"source": [
|
128 |
+
"# TODO: Update pre-trained Llama to be a school choice chatbot\n",
|
129 |
+
"\n",
|
130 |
+
"This part is up to you! You might want to finetune the model, simply make a really good system prompt, use RAG, provide the model boston school choice data in-context, etc. Be creative!\n",
|
131 |
+
"\n",
|
132 |
+
"You can also feel free to do this in another script and then evaluate the model here.\n",
|
133 |
+
"\n",
|
134 |
+
"Tips:\n",
|
135 |
+
"- HuggingFace has built-in methods to finetune models, if you choose that route. Take advantage of those methods! You can then save your new, finetuned model in the HuggingFace Hub. Change MY_MODEL in config.py to the name of the model in the hub to make your chatbot use it.\n",
|
136 |
+
"- You may also want to consider LoRA if you choose finetuning."
|
137 |
+
]
|
138 |
+
}
|
139 |
+
],
|
140 |
+
"metadata": {
|
141 |
+
"kernelspec": {
|
142 |
+
"display_name": ".venv",
|
143 |
+
"language": "python",
|
144 |
+
"name": "python3"
|
145 |
+
},
|
146 |
+
"language_info": {
|
147 |
+
"codemirror_mode": {
|
148 |
+
"name": "ipython",
|
149 |
+
"version": 3
|
150 |
+
},
|
151 |
+
"file_extension": ".py",
|
152 |
+
"mimetype": "text/x-python",
|
153 |
+
"name": "python",
|
154 |
+
"nbconvert_exporter": "python",
|
155 |
+
"pygments_lexer": "ipython3",
|
156 |
+
"version": "3.12.2"
|
157 |
+
}
|
158 |
+
},
|
159 |
+
"nbformat": 4,
|
160 |
+
"nbformat_minor": 2
|
161 |
+
}
|
config.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
|
4 |
+
# Load from .env file. Store your HF token in the .env file.
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
BASE_MODEL = "HuggingFaceH4/zephyr-7b-beta"
|
8 |
+
# BASE_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
9 |
+
# Other options:
|
10 |
+
# MODEL = "meta-llama/Llama-2-7b-chat-hf"
|
11 |
+
# MODEL = "openlm-research/open_llama_3b"
|
12 |
+
|
13 |
+
# If you finetune the model or change it in any way, save it to huggingface hub, then set MY_MODEL to your model ID. The model ID is in the format "your-username/your-model-name".
|
14 |
+
MY_MODEL = None
|
15 |
+
|
16 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=2.1.0
|
2 |
+
transformers>=4.34.0
|
3 |
+
datasets>=2.14.0
|
4 |
+
accelerate>=0.24.0
|
5 |
+
sentencepiece>=0.1.99
|
6 |
+
gradio>=3.50.0
|
7 |
+
huggingface-hub>=0.19.0
|
8 |
+
numpy<2.0.0
|
9 |
+
ipywidgets>=8.0.0
|
10 |
+
python-dotenv>=1.1.0
|
src/__pycache__/chat.cpython-311.pyc
ADDED
Binary file (3.31 kB). View file
|
|
src/chat.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
from config import BASE_MODEL, MY_MODEL, HF_TOKEN
|
3 |
+
|
4 |
+
class SchoolChatbot:
|
5 |
+
"""
|
6 |
+
This class is extra scaffolding around a model. Modify this class to specify how the model recieves prompts and generates responses.
|
7 |
+
|
8 |
+
Example usage:
|
9 |
+
chatbot = SchoolChatbot()
|
10 |
+
response = chatbot.get_response("What schools offer Spanish programs?")
|
11 |
+
"""
|
12 |
+
|
13 |
+
def __init__(self):
|
14 |
+
"""
|
15 |
+
Initialize the chatbot with a HF model ID
|
16 |
+
"""
|
17 |
+
model_id = MY_MODEL if MY_MODEL else BASE_MODEL # define MY_MODEL in config.py if you create a new model in the HuggingFace Hub
|
18 |
+
self.client = InferenceClient(model=model_id, token=HF_TOKEN)
|
19 |
+
|
20 |
+
def format_prompt(self, user_input):
|
21 |
+
"""
|
22 |
+
TODO: Implement this method to format the user's input into a proper prompt.
|
23 |
+
|
24 |
+
This method should:
|
25 |
+
1. Add any necessary system context or instructions
|
26 |
+
2. Format the user's input appropriately
|
27 |
+
3. Add any special tokens or formatting the model expects
|
28 |
+
|
29 |
+
Args:
|
30 |
+
user_input (str): The user's question about Boston schools
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
str: A formatted prompt ready for the model
|
34 |
+
|
35 |
+
Example prompt format:
|
36 |
+
"You are a helpful assistant that specializes in Boston schools...
|
37 |
+
User: {user_input}
|
38 |
+
Assistant:"
|
39 |
+
"""
|
40 |
+
return (
|
41 |
+
"<|system|>You are a helpful assistant that specializes in Boston public school enrollment.<|end|>\n"
|
42 |
+
f"<|user|>{user_input}<|end|>\n"
|
43 |
+
"<|assistant|>"
|
44 |
+
)
|
45 |
+
|
46 |
+
def get_response(self, user_input):
|
47 |
+
"""
|
48 |
+
TODO: Implement this method to generate responses to user questions.
|
49 |
+
|
50 |
+
This method should:
|
51 |
+
1. Use format_prompt() to prepare the input
|
52 |
+
2. Generate a response using the model
|
53 |
+
3. Clean up and return the response
|
54 |
+
|
55 |
+
Args:
|
56 |
+
user_input (str): The user's question about Boston schools
|
57 |
+
|
58 |
+
Returns:
|
59 |
+
str: The chatbot's response
|
60 |
+
|
61 |
+
Implementation tips:
|
62 |
+
- Use self.format_prompt() to format the user's input
|
63 |
+
- Use self.client to generate responses
|
64 |
+
"""
|
65 |
+
prompt = self.format_prompt(user_input)
|
66 |
+
response = self.client.text_generation(
|
67 |
+
prompt,
|
68 |
+
max_new_tokens=512,
|
69 |
+
temperature=0.7,
|
70 |
+
top_p=0.9,
|
71 |
+
stop_sequences=["<|end|>"]
|
72 |
+
)
|
73 |
+
return response.strip()
|