freddyaboulton HF Staff commited on
Commit
5504b27
·
verified ·
1 Parent(s): 8a07a0c

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. run.ipynb +1 -1
  3. run.py +12 -18
README.md CHANGED
@@ -5,7 +5,7 @@ emoji: 🔥
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
- sdk_version: 5.28.0
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
 
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 5.29.0
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
run.ipynb CHANGED
@@ -1 +1 @@
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):\n", " \"\"\"\n", " Compute the prime factorization of a positive integer.\n", "\n", " Args:\n", " n (int): The integer to factorize. Must be greater than 1.\n", "\n", " Returns:\n", " List[int]: A list of prime factors in ascending order.\n", "\n", " Raises:\n", " ValueError: If n is not greater than 1.\n", " \"\"\"\n", " n = int(n)\n", " if n <= 1:\n", " raise ValueError(\"Input must be an integer greater than 1.\")\n", "\n", " factors = []\n", " while n % 2 == 0:\n", " factors.append(2)\n", " n //= 2\n", "\n", " divisor = 3\n", " while divisor * divisor <= n:\n", " while n % divisor == 0:\n", " factors.append(divisor)\n", " n //= divisor\n", " divisor += 2\n", "\n", " if n > 1:\n", " factors.append(n)\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 (str): 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}
 
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}
run.py CHANGED
@@ -4,37 +4,31 @@ from pathlib import Path
4
  import os
5
  from PIL import Image
6
 
7
- def prime_factors(n):
8
  """
9
  Compute the prime factorization of a positive integer.
10
 
11
  Args:
12
- n (int): The integer to factorize. Must be greater than 1.
13
-
14
- Returns:
15
- List[int]: A list of prime factors in ascending order.
16
-
17
- Raises:
18
- ValueError: If n is not greater than 1.
19
  """
20
- n = int(n)
21
- if n <= 1:
22
  raise ValueError("Input must be an integer greater than 1.")
23
 
24
  factors = []
25
- while n % 2 == 0:
26
  factors.append(2)
27
- n //= 2
28
 
29
  divisor = 3
30
- while divisor * divisor <= n:
31
- while n % divisor == 0:
32
  factors.append(divisor)
33
- n //= divisor
34
  divisor += 2
35
 
36
- if n > 1:
37
- factors.append(n)
38
 
39
  return factors
40
 
@@ -67,7 +61,7 @@ def sepia(input_img):
67
  Apply a sepia filter to the input image.
68
 
69
  Args:
70
- input_img (str): The input image to apply the sepia filter to.
71
 
72
  Returns:
73
  The sepia filtered image.
 
4
  import os
5
  from PIL import Image
6
 
7
+ def prime_factors(n: str):
8
  """
9
  Compute the prime factorization of a positive integer.
10
 
11
  Args:
12
+ n (str): The integer to factorize. Must be greater than 1.
 
 
 
 
 
 
13
  """
14
+ n_int = int(n)
15
+ if n_int <= 1:
16
  raise ValueError("Input must be an integer greater than 1.")
17
 
18
  factors = []
19
+ while n_int % 2 == 0:
20
  factors.append(2)
21
+ n_int //= 2
22
 
23
  divisor = 3
24
+ while divisor * divisor <= n_int:
25
+ while n_int % divisor == 0:
26
  factors.append(divisor)
27
+ n_int //= divisor
28
  divisor += 2
29
 
30
+ if n_int > 1:
31
+ factors.append(n_int)
32
 
33
  return factors
34
 
 
61
  Apply a sepia filter to the input image.
62
 
63
  Args:
64
+ input_img (np.array): The input image to apply the sepia filter to.
65
 
66
  Returns:
67
  The sepia filtered image.