Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: 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 |
+
|
35 |
+
|
36 |
+
def generate_cheetah_image():
|
37 |
+
"""
|
38 |
+
Generate a cheetah image.
|
39 |
+
|
40 |
+
Returns:
|
41 |
+
The generated cheetah image.
|
42 |
+
"""
|
43 |
+
return Path(os.path.dirname(__file__)) / "cheetah.jpg"
|
44 |
+
|
45 |
+
|
46 |
+
def image_orientation(image: Image.Image) -> str:
|
47 |
+
"""
|
48 |
+
Returns whether image is portrait or landscape.
|
49 |
+
|
50 |
+
Args:
|
51 |
+
image (Image.Image): The image to check.
|
52 |
+
|
53 |
+
Returns:
|
54 |
+
str: "Portrait" if image is portrait, "Landscape" if image is landscape.
|
55 |
+
"""
|
56 |
+
return "Portrait" if image.height > image.width else "Landscape"
|
57 |
+
|
58 |
+
|
59 |
+
def sepia(input_img):
|
60 |
+
"""
|
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.
|
68 |
+
"""
|
69 |
+
sepia_filter = np.array([
|
70 |
+
[0.393, 0.769, 0.189],
|
71 |
+
[0.349, 0.686, 0.168],
|
72 |
+
[0.272, 0.534, 0.131]
|
73 |
+
])
|
74 |
+
sepia_img = input_img.dot(sepia_filter.T)
|
75 |
+
sepia_img /= sepia_img.max()
|
76 |
+
return sepia_img
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
demo = gr.TabbedInterface(
|
81 |
+
[
|
82 |
+
gr.Interface(prime_factors, gr.Textbox(), gr.Textbox(), api_name="prime_factors"),
|
83 |
+
gr.Interface(generate_cheetah_image, None, gr.Image(), api_name="generate_cheetah_image"),
|
84 |
+
gr.Interface(image_orientation, gr.Image(type="pil"), gr.Textbox(), api_name="image_orientation"),
|
85 |
+
gr.Interface(sepia, gr.Image(), gr.Image(), api_name="sepia"),
|
86 |
+
],
|
87 |
+
[
|
88 |
+
"Prime Factors",
|
89 |
+
"Cheetah Image",
|
90 |
+
"Image Orientation Checker",
|
91 |
+
"Sepia Filter",
|
92 |
+
]
|
93 |
+
)
|
94 |
+
|
95 |
+
if __name__ == "__main__":
|
96 |
+
demo.launch(mcp_server=True)
|