File size: 3,614 Bytes
5504b27
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: mcp_tools"]}, {"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", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/mcp_tools/cheetah.jpg"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import numpy as np\n", "import gradio as gr\n", "from pathlib import Path\n", "import os\n", "from PIL import Image\n", "\n", "def prime_factors(n: str):\n", "    \"\"\"\n", "    Compute the prime factorization of a positive integer.\n", "\n", "    Args:\n", "        n (str): The integer to factorize. Must be greater than 1.\n", "    \"\"\"\n", "    n_int = int(n)\n", "    if n_int <= 1:\n", "        raise ValueError(\"Input must be an integer greater than 1.\")\n", "\n", "    factors = []\n", "    while n_int % 2 == 0:\n", "        factors.append(2)\n", "        n_int //= 2\n", "\n", "    divisor = 3\n", "    while divisor * divisor <= n_int:\n", "        while n_int % divisor == 0:\n", "            factors.append(divisor)\n", "            n_int //= divisor\n", "        divisor += 2\n", "\n", "    if n_int > 1:\n", "        factors.append(n_int)\n", "\n", "    return factors\n", "\n", "\n", "def generate_cheetah_image():\n", "    \"\"\"\n", "    Generate a cheetah image.\n", "\n", "    Returns:\n", "        The generated cheetah image.\n", "    \"\"\"\n", "    return Path(os.path.abspath('')) / \"cheetah.jpg\"\n", "\n", "\n", "def image_orientation(image: Image.Image) -> str:\n", "    \"\"\"\n", "    Returns whether image is portrait or landscape.\n", "\n", "    Args:\n", "        image (Image.Image): The image to check.\n", "\n", "    Returns:\n", "        str: \"Portrait\" if image is portrait, \"Landscape\" if image is landscape.\n", "    \"\"\"\n", "    return \"Portrait\" if image.height > image.width else \"Landscape\"\n", "\n", "\n", "def sepia(input_img):\n", "    \"\"\"\n", "    Apply a sepia filter to the input image.\n", "\n", "    Args:\n", "        input_img (np.array): The input image to apply the sepia filter to.\n", "\n", "    Returns:\n", "        The sepia filtered image.\n", "    \"\"\"\n", "    sepia_filter = np.array([\n", "        [0.393, 0.769, 0.189],\n", "        [0.349, 0.686, 0.168],\n", "        [0.272, 0.534, 0.131]\n", "    ])\n", "    sepia_img = input_img.dot(sepia_filter.T)\n", "    sepia_img /= sepia_img.max()\n", "    return sepia_img\n", "\n", "\n", "\n", "demo = gr.TabbedInterface(\n", "    [\n", "        gr.Interface(prime_factors, gr.Textbox(), gr.Textbox(), api_name=\"prime_factors\"),\n", "        gr.Interface(generate_cheetah_image, None, gr.Image(), api_name=\"generate_cheetah_image\"),\n", "        gr.Interface(image_orientation, gr.Image(type=\"pil\"), gr.Textbox(), api_name=\"image_orientation\"),\n", "        gr.Interface(sepia, gr.Image(), gr.Image(), api_name=\"sepia\"),\n", "    ],\n", "    [\n", "        \"Prime Factors\",\n", "        \"Cheetah Image\",\n", "        \"Image Orientation Checker\",\n", "        \"Sepia Filter\",\n", "    ]\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch(mcp_server=True)\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}