File size: 9,434 Bytes
6b509f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#
# SPDX-FileCopyrightText: Hadad <[email protected]>
# SPDX-License-Identifier: Apache-2.0
#

import gradio as gr  # Import the Gradio library to build interactive web interfaces for machine learning applications
from src.core.parameter import parameters  # Import the 'parameters' function from the core parameter module, which returns model parameter settings based on reasoning mode
from src.client.chat_handler import respond  # Import the 'respond' function from the chat handler module, responsible for generating AI assistant responses
from config import model, meta_tags  # Import 'model' dictionary containing available model precision options and their details, and 'meta_tags' containing HTML meta tag data

# Gradio
def ui():
    """
    Constructs the Gradio user interface for the J.A.R.V.I.S. AI assistant application.

    This function sets up a web app with a sidebar for configuring model parameters and a main chat interface
    for user interaction. It returns the Gradio Blocks object representing the entire app.
    """
    # Create a Gradio Blocks container that fills the entire available height and width of the browser window
    with gr.Blocks(fill_height=True, fill_width=True, head=meta_tags) as app:
        # Create a sidebar panel on the left side, initially closed, to hold model configuration controls
        with gr.Sidebar(open=False):
            # Dropdown menu for selecting the model precision from the keys of the 'model' dictionary
            model_precision = gr.Dropdown(
                choices=list(model.keys()),  # List of available model precision options, e.g., "F16", "F32"
                label="Model Precision",  # Label displayed above the dropdown menu
                info=(
                    # Tooltip explaining the tradeoff between speed and accuracy based on precision choice
                    "The smaller the value, the faster the response but less accurate. "
                    "Conversely, the larger the value, the response is slower but more accurate."
                ),
                value="F16"  # Default selected precision value
            )

            # Checkbox to enable or disable reasoning mode, which toggles the AI's "thinking" capability
            reasoning = gr.Checkbox(
                label="Reasoning",  # Label shown next to the checkbox
                info="Switching between thinking and non-thinking mode.",  # Tooltip describing the feature
                value=True  # Default state is enabled (checked)
            )

            # Slider controlling the 'Temperature' parameter, affecting randomness in AI responses, initially non-interactive
            temperature = gr.Slider(
                minimum=0.0,  # Minimum slider value
                maximum=2.0,  # Maximum slider value
                step=0.01,  # Increment step size
                label="Temperature",  # Label for the slider
                interactive=False  # User cannot directly adjust this slider, updated dynamically
            )

            # Slider controlling the 'Top K' parameter, which limits the number of highest probability tokens considered, non-interactive initially
            top_k = gr.Slider(
                minimum=0,
                maximum=100,
                step=1,
                label="Top K",
                interactive=False
            )

            # Slider for 'Min P' parameter, representing minimum cumulative probability threshold, non-interactive initially
            min_p = gr.Slider(
                minimum=0.0,
                maximum=1.0,
                step=0.01,
                label="Min P",
                interactive=False
            )

            # Slider for 'Top P' parameter, controlling nucleus sampling probability, non-interactive initially
            top_p = gr.Slider(
                minimum=0.0,
                maximum=1.0,
                step=0.01,
                label="Top P",
                interactive=False
            )

            # Slider for 'Repetition Penalty' parameter to reduce repetitive text generation, non-interactive initially
            repetition_penalty = gr.Slider(
                minimum=0.1,
                maximum=2.0,
                step=0.01,
                label="Repetition Penalty",
                interactive=False
            )

            # Define a function to update the model parameter sliders based on the reasoning checkbox state
            def update_parameters(switching):
                """
                Retrieve updated model parameter values based on reasoning mode.

                Args:
                    switching (bool): Current state of the reasoning checkbox.

                Returns:
                    tuple: Updated values for temperature, top_k, min_p, top_p, and repetition_penalty sliders.
                """
                # Call the 'parameters' function passing the reasoning state to get new parameter values
                return parameters(switching)

            # Set up an event listener to update parameter sliders when the reasoning checkbox state changes
            reasoning.change(
                fn=update_parameters,  # Function to call on checkbox state change
                inputs=[reasoning],  # Input is the reasoning checkbox's current value
                outputs=[temperature, top_k, min_p, top_p, repetition_penalty]  # Update these sliders with new values
            )

            # Initialize the parameter sliders with values corresponding to the default reasoning checkbox state
            values = parameters(reasoning.value)
            temperature.value, top_k.value, min_p.value, top_p.value, repetition_penalty.value = values

            # Checkbox to enable or disable the image generation feature in the chat interface
            image_generation = gr.Checkbox(
                label="Image Generation",  # Label displayed next to the checkbox
                info=(
                    # Tooltip explaining how to trigger image generation via chat commands
                    "Type <i><b>/image</b></i> followed by the instructions to start generating an image."
                ),
                value=True  # Enabled by default
            )

            # Checkbox to enable or disable the audio generation feature in the chat interface
            audio_generation = gr.Checkbox(
                label="Audio Generation",
                info=(
                    "Type <i><b>/audio</b></i> followed by the instructions to start generating audio."
                ),
                value=True
            )

            # Checkbox to enable or disable the deep web search feature in the chat interface
            search_generation = gr.Checkbox(
                label="Deep Search",
                info=(
                    "Type <i><b>/dp</b></i> followed by the instructions to search the web."
                ),
                value=True
            )

        # Create the main chat interface where users interact with the AI assistant
        gr.ChatInterface(
            fn=respond,  # Function called to generate responses to user inputs
            additional_inputs=[
                # Pass the current states of all configuration controls as additional inputs to the respond function
                model_precision,
                temperature,
                top_k,
                min_p,
                top_p,
                repetition_penalty,
                reasoning,
                image_generation,
                audio_generation,
                search_generation
            ],
            type='tuples', #  The format of the messages
            chatbot=gr.Chatbot(
                label="J.A.R.V.I.S.",   # Title label displayed above the chat window
                show_copy_button=True,  # Show a button allowing users to copy chat messages
                scale=1,  # Scale factor for the chatbot UI size
                type='tuples' # Duplicate form Chat Interface to Chatbot
            ),
            examples=[
                # Predefined example inputs to help users quickly test the assistant's features
                ["Please introduce yourself."],
                ["/audio Could you explain what Artificial Intelligence (AI) is?"],
                ["/audio What is Hugging Face?"],
                ["/dp Please search for the J.A.R.V.I.S. AI model on Hugging Face."],
                ["/dp What is the capital city of Indonesia?"],
                ["/image Create an image of a futuristic city."],
                ["/image Create a cartoon-style image of a man."],
                ["What day is it today, what's the date, and what time is it?"],
                ['/audio Say "I am J.A.R.V.I.S.".'],
                ["Please generate a highly complex code snippet on any topic."],
                ["Explain about quantum computers."]
            ],
            cache_examples=False,  # Disable caching of example outputs to always generate fresh responses
            multimodal=False,      # Disable support for multimodal inputs such as images or audio files
            fill_height=True,      # Duplicate from Blocks to Chat Interface
            fill_width=True,       # Duplicate from Blocks to Chat Interface
            head=meta_tags         # Duplicate from Blocks to Chat Interface
        )
    # Return the complete Gradio app object for launching or embedding
    return app