Spaces:
abidlabs
/
Running on CPU Upgrade

abidlabs HF Staff commited on
Commit
5910b93
·
verified ·
1 Parent(s): 12523ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -19
app.py CHANGED
@@ -1,29 +1,102 @@
 
1
  import gradio as gr
 
 
 
2
 
3
- def letter_counter(word, letter):
4
  """
5
- Count the number of occurrences of a letter in a word or text.
6
 
7
  Args:
8
- word (str): The input text to search through
9
- letter (str): The letter to search for
10
 
11
  Returns:
12
- str: A message indicating how many times the letter appears
13
- """
14
- word = word.lower()
15
- letter = letter.lower()
16
- count = word.count(letter)
17
- return count
18
-
19
- # Create and launch the Gradio interface
20
- demo = gr.Interface(
21
- fn=letter_counter,
22
- inputs=["textbox", "textbox"],
23
- outputs="number",
24
- title="Letter Counter",
25
- description="Enter text and a letter to count how many times the letter appears in the text."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  )
27
 
28
  if __name__ == "__main__":
29
- demo.launch(mcp_server=True, strict_cors=False)
 
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)