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

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +8 -8
  2. cheetah.jpg +0 -0
  3. run.ipynb +1 -0
  4. run.py +102 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Mcp Tools
3
- emoji:
4
- colorFrom: yellow
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 5.27.1
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: mcp_tools
4
+ 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
12
  ---
 
 
cheetah.jpg ADDED
run.ipynb ADDED
@@ -0,0 +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}
run.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ 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
+
41
+
42
+ def generate_cheetah_image():
43
+ """
44
+ Generate a cheetah image.
45
+
46
+ Returns:
47
+ The generated cheetah image.
48
+ """
49
+ return Path(os.path.dirname(__file__)) / "cheetah.jpg"
50
+
51
+
52
+ def image_orientation(image: Image.Image) -> str:
53
+ """
54
+ Returns whether image is portrait or landscape.
55
+
56
+ Args:
57
+ image (Image.Image): The image to check.
58
+
59
+ Returns:
60
+ str: "Portrait" if image is portrait, "Landscape" if image is landscape.
61
+ """
62
+ return "Portrait" if image.height > image.width else "Landscape"
63
+
64
+
65
+ def sepia(input_img):
66
+ """
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.
74
+ """
75
+ sepia_filter = np.array([
76
+ [0.393, 0.769, 0.189],
77
+ [0.349, 0.686, 0.168],
78
+ [0.272, 0.534, 0.131]
79
+ ])
80
+ sepia_img = input_img.dot(sepia_filter.T)
81
+ sepia_img /= sepia_img.max()
82
+ return sepia_img
83
+
84
+
85
+
86
+ demo = gr.TabbedInterface(
87
+ [
88
+ gr.Interface(prime_factors, gr.Textbox(), gr.Textbox(), api_name="prime_factors"),
89
+ gr.Interface(generate_cheetah_image, None, gr.Image(), api_name="generate_cheetah_image"),
90
+ gr.Interface(image_orientation, gr.Image(type="pil"), gr.Textbox(), api_name="image_orientation"),
91
+ gr.Interface(sepia, gr.Image(), gr.Image(), api_name="sepia"),
92
+ ],
93
+ [
94
+ "Prime Factors",
95
+ "Cheetah Image",
96
+ "Image Orientation Checker",
97
+ "Sepia Filter",
98
+ ]
99
+ )
100
+
101
+ if __name__ == "__main__":
102
+ demo.launch(mcp_server=True)