File size: 3,254 Bytes
9e118e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
"""
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 Chatbot

def create_chatbot():
    """
    Creates and configures the chatbot interface.
    """
    chatbot = Chatbot()
    
    def chat(message, history, mode):
        """
        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
        """
        return chatbot.get_response(user_input=message, history=history, mode=mode)

    
    # Define the radio buttons for the UI
    mode_selector = gr.Radio(
        choices=["Find School", "Registration Guide", "Contact Info"],
        value="Find School", # Default value
        label="What do you need help with today?"
    )
    # Create Gradio interface
    demo = gr.ChatInterface(
        chat,
        title="🏫 Boston School Finder 🔎",
        additional_inputs=[mode_selector],
        additional_inputs_accordion=gr.Accordion(label="Chatbot Mode", open=True),
        description=(
            "Hi! I can help you navigate Boston school registration, find the "
            "right school for your child, or connect you with the right people! "
            "Select one of the modes below! (If you see a 503 error, please try again in a "
            "few seconds.)"
        ),
        examples=[
            ["How can I find the right school for my child?", "Find School"],
            ["How do I register my child for school in Boston?", "Registration Guide"],
            ["How can I find more information about who to contact?", "Contact Info"],
        ],
    )
 
    return demo

if __name__ == "__main__":
    demo = create_chatbot()
    demo.launch()