Commit
·
ab18fdf
1
Parent(s):
92cc575
Agregar funciones de herramientas: contador de letras, factores primos, lanzamiento de dados y voltereta de moneda
Browse files- __pycache__/tools.cpython-311.pyc +0 -0
- __pycache__/websocket_manager.cpython-311.pyc +0 -0
- __pycache__/websockets.cpython-311.pyc +0 -0
- app.py +6 -48
- tools.py +74 -0
__pycache__/tools.cpython-311.pyc
ADDED
Binary file (3.29 kB). View file
|
|
__pycache__/websocket_manager.cpython-311.pyc
ADDED
Binary file (1.92 kB). View file
|
|
__pycache__/websockets.cpython-311.pyc
ADDED
Binary file (1.91 kB). View file
|
|
app.py
CHANGED
@@ -1,62 +1,20 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
# === TOOLS ===
|
4 |
-
def letter_counter(word, letter):
|
5 |
-
"""
|
6 |
-
Count the number of occurrences of a letter in a word or text.
|
7 |
-
|
8 |
-
Args:
|
9 |
-
word (str): The input text to search through
|
10 |
-
letter (str): The letter to search for
|
11 |
-
|
12 |
-
Returns:
|
13 |
-
str: A message indicating how many times the letter appears
|
14 |
-
"""
|
15 |
-
word = word.lower()
|
16 |
-
letter = letter.lower()
|
17 |
-
count = word.count(letter)
|
18 |
-
return count
|
19 |
-
|
20 |
-
def prime_factors(n):
|
21 |
-
"""
|
22 |
-
Compute the prime factorization of a positive integer.
|
23 |
-
Args:
|
24 |
-
n (int): The integer to factorize. Must be greater than 1.
|
25 |
-
Returns:
|
26 |
-
List[int]: A list of prime factors in ascending order.
|
27 |
-
Raises:
|
28 |
-
ValueError: If n is not greater than 1.
|
29 |
-
"""
|
30 |
-
n = int(n)
|
31 |
-
if n <= 1:
|
32 |
-
raise ValueError("Input must be an integer greater than 1.")
|
33 |
-
|
34 |
-
factors = []
|
35 |
-
while n % 2 == 0:
|
36 |
-
factors.append(2)
|
37 |
-
n //= 2
|
38 |
-
|
39 |
-
divisor = 3
|
40 |
-
while divisor * divisor <= n:
|
41 |
-
while n % divisor == 0:
|
42 |
-
factors.append(divisor)
|
43 |
-
n //= divisor
|
44 |
-
divisor += 2
|
45 |
-
|
46 |
-
if n > 1:
|
47 |
-
factors.append(n)
|
48 |
-
|
49 |
-
return factors
|
50 |
|
51 |
# === GRADIO INTERFACE ===
|
52 |
demo = gr.TabbedInterface(
|
53 |
[
|
54 |
gr.Interface(letter_counter, [gr.Textbox(), gr.Textbox()], gr.Textbox(), api_name="letter_counter"),
|
55 |
gr.Interface(prime_factors, gr.Number(), gr.JSON(), api_name="prime_factors"),
|
|
|
|
|
56 |
],
|
57 |
[
|
58 |
"Letter Counter",
|
59 |
-
"Prime Factors"
|
|
|
|
|
60 |
]
|
61 |
)
|
62 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from tools import letter_counter, prime_factors, roll_dice, coin_flip
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# === GRADIO INTERFACE ===
|
6 |
demo = gr.TabbedInterface(
|
7 |
[
|
8 |
gr.Interface(letter_counter, [gr.Textbox(), gr.Textbox()], gr.Textbox(), api_name="letter_counter"),
|
9 |
gr.Interface(prime_factors, gr.Number(), gr.JSON(), api_name="prime_factors"),
|
10 |
+
gr.Interface(roll_dice, [gr.Number(label="Faces"), gr.Number(label="Rolls")], gr.JSON(), api_name="roll_dice"),
|
11 |
+
gr.Interface(coin_flip, gr.Number(label="Flips"), gr.JSON(), api_name="coin_flip")
|
12 |
],
|
13 |
[
|
14 |
"Letter Counter",
|
15 |
+
"Prime Factors",
|
16 |
+
"Roll Dice",
|
17 |
+
"Coin Flip"
|
18 |
]
|
19 |
)
|
20 |
|
tools.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# === TOOLS ===
|
2 |
+
def letter_counter(word, letter):
|
3 |
+
"""
|
4 |
+
Count the number of occurrences of a letter in a word or text.
|
5 |
+
|
6 |
+
Args:
|
7 |
+
word (str): The input text to search through
|
8 |
+
letter (str): The letter to search for
|
9 |
+
|
10 |
+
Returns:
|
11 |
+
str: A message indicating how many times the letter appears
|
12 |
+
"""
|
13 |
+
word = word.lower()
|
14 |
+
letter = letter.lower()
|
15 |
+
count = word.count(letter)
|
16 |
+
return count
|
17 |
+
|
18 |
+
def prime_factors(n):
|
19 |
+
"""
|
20 |
+
Compute the prime factorization of a positive integer.
|
21 |
+
Args:
|
22 |
+
n (int): The integer to factorize. Must be greater than 1.
|
23 |
+
Returns:
|
24 |
+
List[int]: A list of prime factors in ascending order.
|
25 |
+
Raises:
|
26 |
+
ValueError: If n is not greater than 1.
|
27 |
+
"""
|
28 |
+
n = int(n)
|
29 |
+
if n <= 1:
|
30 |
+
raise ValueError("Input must be an integer greater than 1.")
|
31 |
+
|
32 |
+
factors = []
|
33 |
+
while n % 2 == 0:
|
34 |
+
factors.append(2)
|
35 |
+
n //= 2
|
36 |
+
|
37 |
+
divisor = 3
|
38 |
+
while divisor * divisor <= n:
|
39 |
+
while n % divisor == 0:
|
40 |
+
factors.append(divisor)
|
41 |
+
n //= divisor
|
42 |
+
divisor += 2
|
43 |
+
|
44 |
+
if n > 1:
|
45 |
+
factors.append(n)
|
46 |
+
|
47 |
+
return factors
|
48 |
+
|
49 |
+
def roll_dice(faces, rolls):
|
50 |
+
"""
|
51 |
+
Simulate rolling dice.
|
52 |
+
|
53 |
+
Args:
|
54 |
+
faces (int): Number of faces on the die.
|
55 |
+
rolls (int): Number of times to roll the die.
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
List[int]: A list of results from each roll.
|
59 |
+
"""
|
60 |
+
import random
|
61 |
+
return [random.randint(1, faces) for _ in range(rolls)]
|
62 |
+
|
63 |
+
def coin_flip(flips):
|
64 |
+
"""
|
65 |
+
Simulate flipping a coin multiple times.
|
66 |
+
|
67 |
+
Args:
|
68 |
+
flips (int): Number of times to flip the coin.
|
69 |
+
|
70 |
+
Returns:
|
71 |
+
List[str]: A list of results from each flip ("Heads" or "Tails").
|
72 |
+
"""
|
73 |
+
import random
|
74 |
+
return ["Heads" if random.randint(0, 1) == 0 else "Tails" for _ in range(flips)]
|