{ "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 }