Spaces:
Build error
Build error
File size: 2,830 Bytes
c487b8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
"""
Gradio Web Interface for Boston School Chatbot
This script creates a web interface for your chatbot using Gradio.
You only need to implement the chat function.
Key Features:
- Creates a web UI for your chatbot
- Handles conversation history
- Provides example questions
- Can be deployed to Hugging Face Spaces
Example Usage:
# Run locally:
python app.py
# Access in browser:
# http://localhost:7860
"""
import gradio as gr
from src.chat import SchoolChatbot
def create_chatbot():
"""
Creates and configures the chatbot interface.
"""
chatbot = SchoolChatbot()
def chat(message, history):
"""
TODO:Generate a response for the current message in a Gradio chat interface.
This function is called by Gradio's ChatInterface every time a user sends a message.
You only need to generate and return the assistant's response - Gradio handles the
chat display and history management automatically.
Args:
message (str): The current message from the user
history (list): List of previous message pairs, where each pair is
[user_message, assistant_message]
Example:
[
["What schools offer Spanish?", "The Hernandez School..."],
["Where is it located?", "The Hernandez School is in Roxbury..."]
]
Returns:
str: The assistant's response to the current message.
Note:
- Gradio automatically:
- Displays the user's message
- Displays your returned response
- Updates the chat history
- Maintains the chat interface
- You only need to:
- Generate an appropriate response to the current message
- Return that response as a string
"""
# TODO: Generate and return response
try:
response = chatbot.get_response(message)
return response
except Exception as e:
return f"Sorry, something went wrong: {str(e)}"
# Create Gradio interface. Customize the interface however you'd like!
demo = gr.ChatInterface(
chat,
title="Boston Public School Selection Assistant",
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.",
examples=[
"I live in Jamaica Plain and want to send my child to kindergarten. What schools are available?"
]
)
return demo
if __name__ == "__main__":
demo = create_chatbot()
demo.launch() |