File size: 1,841 Bytes
9a8e83a
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: calculator"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('examples')\n", "!wget -q -O examples/log.csv https://github.com/gradio-app/gradio/raw/main/demo/calculator/examples/log.csv"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "def calculator(num1, operation, num2):\n", "    if operation == \"add\":\n", "        return num1 + num2\n", "    elif operation == \"subtract\":\n", "        return num1 - num2\n", "    elif operation == \"multiply\":\n", "        return num1 * num2\n", "    elif operation == \"divide\":\n", "        if num2 == 0:\n", "            raise gr.Error(\"Cannot divide by zero!\")\n", "        return num1 / num2\n", "\n", "demo = gr.Interface(\n", "    calculator,\n", "    [\n", "        \"number\",\n", "        gr.Radio([\"add\", \"subtract\", \"multiply\", \"divide\"]),\n", "        \"number\"\n", "    ],\n", "    \"number\",\n", "    examples=[\n", "        [45, \"add\", 3],\n", "        [3.14, \"divide\", 2],\n", "        [144, \"multiply\", 2.5],\n", "        [0, \"subtract\", 1.2],\n", "    ],\n", "    title=\"Toy Calculator\",\n", "    description=\"Here's a sample toy calculator.\",\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}