Javier-Jimenez99 commited on
Commit
a585ed2
·
1 Parent(s): ab18fdf

Agregar función para crear formas y mejorar la interfaz de Gradio

Browse files
Files changed (2) hide show
  1. app.py +18 -4
  2. tools.py +54 -1
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
- from tools import letter_counter, prime_factors, roll_dice, coin_flip
 
3
 
4
 
5
  # === GRADIO INTERFACE ===
@@ -8,14 +9,27 @@ demo = gr.TabbedInterface(
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
 
21
- demo.launch(mcp_server=True)
 
1
  import gradio as gr
2
+ from tools import letter_counter, prime_factors, roll_dice, coin_flip, create_shape
3
+
4
 
5
 
6
  # === GRADIO INTERFACE ===
 
9
  gr.Interface(letter_counter, [gr.Textbox(), gr.Textbox()], gr.Textbox(), api_name="letter_counter"),
10
  gr.Interface(prime_factors, gr.Number(), gr.JSON(), api_name="prime_factors"),
11
  gr.Interface(roll_dice, [gr.Number(label="Faces"), gr.Number(label="Rolls")], gr.JSON(), api_name="roll_dice"),
12
+ gr.Interface(coin_flip, gr.Number(label="Flips"), gr.JSON(), api_name="coin_flip"),
13
+ gr.Interface(
14
+ create_shape,
15
+ [
16
+ gr.Number(label="width", value=100),
17
+ gr.Number(label="height", value=100),
18
+ gr.Textbox(label="Shape Type", value="CIRCLE"),
19
+ gr.Textbox(label="Color (hex, e.g., #ff0000)", value="#ff0000"),
20
+ gr.Textbox(label="Tab ID"),
21
+ ],
22
+ gr.JSON(),
23
+ api_name="create_shape"
24
+ )
25
  ],
26
  [
27
  "Letter Counter",
28
  "Prime Factors",
29
  "Roll Dice",
30
+ "Coin Flip",
31
+ "Create Shape"
32
  ]
33
  )
34
 
35
+ demo.launch(mcp_server=True,server_port=7861)
tools.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  # === TOOLS ===
2
  def letter_counter(word, letter):
3
  """
@@ -71,4 +73,55 @@ def coin_flip(flips):
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)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests as request
2
+
3
  # === TOOLS ===
4
  def letter_counter(word, letter):
5
  """
 
73
  List[str]: A list of results from each flip ("Heads" or "Tails").
74
  """
75
  import random
76
+ return ["Heads" if random.randint(0, 1) == 0 else "Tails" for _ in range(flips)]
77
+
78
+ def execute_action(tab_id, action, params):
79
+ """
80
+ Execute an action on a specific tab.
81
+
82
+ Args:
83
+ tab_id (str): The ID of the tab where the action will be executed.
84
+ action (str): The action to perform (e.g., "createShape", "createText").
85
+ params (json): Parameters required for the action.
86
+
87
+ Returns:
88
+ dict: A dictionary containing the result of the action.
89
+ """
90
+ payload = {
91
+ "action": action,
92
+ "args": params
93
+ }
94
+ url = f"http://localhost:3000/execute/{tab_id}"
95
+ print(url)
96
+
97
+ response = request.post(
98
+ url,
99
+ json=payload,
100
+ headers={'Content-Type': 'application/json'}
101
+ )
102
+
103
+ # This is a placeholder for actual implementation
104
+ return response
105
+
106
+ def create_shape(width:int, height:int, shape_type:str, fill_color:str, tab_id:str):
107
+ """
108
+ Create a shape item with specified properties.
109
+
110
+ Args:
111
+ width (int): Width of the shape. Minimum value is 100.
112
+ height (int): Height of the shape. Minimum value is 100.
113
+ shape_type (str): Type of the shape. It can be "RECTANGLE" | "CIRCLE" | "TRIANGLE" | "HEXAGON".
114
+ fill_color (str): Fill color in hex format (e.g., "#ff0000").
115
+ tab_id (str): The ID of the tab where the shape will be created. This is the most important parameter.
116
+
117
+ Returns:
118
+ dict: A dictionary representing the created shape item.
119
+ """
120
+ params = {
121
+ "width": width,
122
+ "height": height,
123
+ "shapeType": shape_type,
124
+ "fillColor": fill_color
125
+ }
126
+
127
+ return execute_action(tab_id=tab_id, action="createShape", params=params)