File size: 5,899 Bytes
50a9403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "26e23461",
   "metadata": {},
   "source": [
    "### 🧩 The Function:\n",
    "\n",
    "```python\n",
    "def update_input_type(choice):\n",
    "```\n",
    "\n",
    "This function is called when the user selects a different **input type** from the radio buttons (`Text`, `Image`, `Voice`, `PDF`, or `PDF(QUIZ)`) in the Gradio interface.\n",
    "\n",
    "Its job:\n",
    "👉 Dynamically show or hide the relevant input components in the interface depending on the user's choice.\n",
    "\n",
    "---\n",
    "\n",
    "### 🧠 What `gr.update()` Does\n",
    "\n",
    "`gr.update()` is a **Gradio helper function** that allows you to update properties of components *without recreating them*.\n",
    "\n",
    "For example:\n",
    "\n",
    "```python\n",
    "gr.update(visible=True)\n",
    "```\n",
    "\n",
    "means “make this component visible.”\n",
    "\n",
    "You can also update other properties like `value`, `label`, `interactive`, etc.\n",
    "\n",
    "---\n",
    "\n",
    "### 📦 What the Function Returns\n",
    "\n",
    "The function returns **a tuple of 6 updates**, one for each component that depends on the selected mode:\n",
    "\n",
    "1. **input_text**\n",
    "2. **image_input**\n",
    "3. **audio_input**\n",
    "4. **pdf_input**\n",
    "5. **quiz_questions_slider**\n",
    "6. **quiz_mode**\n",
    "\n",
    "Each of these is either:\n",
    "\n",
    "* `gr.update(visible=True)` → show this component\n",
    "* `gr.update(visible=False)` → hide this component\n",
    "* `gr.update(value=False)` → reset a checkbox or control value\n",
    "\n",
    "---\n",
    "\n",
    "### 🧭 Mode-by-Mode Explanation\n",
    "\n",
    "#### 1️⃣ If user chooses **\"Text\"**\n",
    "\n",
    "```python\n",
    "return gr.update(visible=True),  # Show text box\n",
    "       gr.update(visible=False), # Hide image input\n",
    "       gr.update(visible=False), # Hide audio input\n",
    "       gr.update(visible=False), # Hide PDF upload\n",
    "       gr.update(visible=False), # Hide quiz slider\n",
    "       gr.update(value=False)    # Turn off quiz mode\n",
    "```\n",
    "\n",
    "✅ Only the **text box** is shown.\n",
    "\n",
    "---\n",
    "\n",
    "#### 2️⃣ If user chooses **\"Image\"**\n",
    "\n",
    "```python\n",
    "return gr.update(visible=True),  # Show text (e.g. question)\n",
    "       gr.update(visible=True),  # Show image input\n",
    "       gr.update(visible=False), # Hide audio\n",
    "       gr.update(visible=False), # Hide PDF\n",
    "       gr.update(visible=False), # Hide quiz slider\n",
    "       gr.update(value=False)    # Quiz mode off\n",
    "```\n",
    "\n",
    "✅ Shows both **text** and **image upload** fields.\n",
    "\n",
    "---\n",
    "\n",
    "#### 3️⃣ If user chooses **\"Voice\"**\n",
    "\n",
    "```python\n",
    "return gr.update(visible=False), # Hide text\n",
    "       gr.update(visible=False), # Hide image\n",
    "       gr.update(visible=True),  # Show audio input\n",
    "       gr.update(visible=False), # Hide PDF\n",
    "       gr.update(visible=False), # Hide quiz slider\n",
    "       gr.update(value=False)    # Quiz mode off\n",
    "```\n",
    "\n",
    "✅ Only the **audio recorder/upload** is visible.\n",
    "\n",
    "---\n",
    "\n",
    "#### 4️⃣ If user chooses **\"PDF\"**\n",
    "\n",
    "```python\n",
    "return gr.update(visible=True),  # Show text (for asking questions about PDF)\n",
    "       gr.update(visible=False), # Hide image\n",
    "       gr.update(visible=False), # Hide audio\n",
    "       gr.update(visible=True),  # Show PDF upload\n",
    "       gr.update(visible=False), # Hide quiz slider\n",
    "       gr.update(value=False)    # Quiz mode off\n",
    "```\n",
    "\n",
    "✅ Shows **text box + PDF upload** (for asking questions about a document).\n",
    "\n",
    "---\n",
    "\n",
    "#### 5️⃣ If user chooses **\"PDF(QUIZ)\"**\n",
    "\n",
    "```python\n",
    "return gr.update(visible=False), # Hide text\n",
    "       gr.update(visible=False), # Hide image\n",
    "       gr.update(visible=False), # Hide audio\n",
    "       gr.update(visible=True),  # Show PDF upload\n",
    "       gr.update(visible=True),  # Show quiz question slider\n",
    "       gr.update(value=True)     # Turn on quiz mode\n",
    "```\n",
    "\n",
    "✅ Shows **PDF upload + quiz settings**, hides everything else.\n",
    "\n",
    "---\n",
    "\n",
    "### ⚙️ How It’s Used\n",
    "\n",
    "This function is connected to the radio button like this:\n",
    "\n",
    "```python\n",
    "input_type.change(\n",
    "    fn=update_input_type,\n",
    "    inputs=[input_type],\n",
    "    outputs=[input_text, image_input, audio_input, pdf_input, quiz_questions_slider, quiz_mode]\n",
    ")\n",
    "```\n",
    "\n",
    "So whenever the user changes the radio selection, Gradio calls `update_input_type(choice)` and updates the visibility of the components accordingly.\n",
    "\n",
    "---\n",
    "\n",
    "### 🧩 In Short\n",
    "\n",
    "| Mode          | Visible Components                        |\n",
    "| ------------- | ----------------------------------------- |\n",
    "| **Text**      | Textbox                                   |\n",
    "| **Image**     | Textbox + Image Upload                    |\n",
    "| **Voice**     | Audio Upload                              |\n",
    "| **PDF**       | Textbox + PDF Upload                      |\n",
    "| **PDF(QUIZ)** | PDF Upload + Quiz Slider (Quiz Mode = ON) |\n",
    "\n",
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18eade10",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}