Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,6 +26,37 @@ from agent import (
|
|
26 |
|
27 |
from utils import parse_action, parse_file_content, read_python_module_structure
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
class App:
|
30 |
def __init__(self):
|
31 |
self.app_state = {"components": []}
|
@@ -180,40 +211,35 @@ class App:
|
|
180 |
response = self.get_nlp_response(input_text, model_index)
|
181 |
component_id, action, property_name, property_value = parse_action(response)
|
182 |
if component_id:
|
183 |
-
component = next((comp for comp in self.app_state["components"] if comp["id"] == component_id), None)
|
184 |
if component:
|
185 |
if action == "update":
|
186 |
component["properties"][property_name] = property_value
|
187 |
-
return self.update_app_canvas(), f"System: Updated property '{property_name}' of component with ID {component_id}\n"
|
188 |
elif action == "remove":
|
189 |
self.app_state["components"].remove(component)
|
190 |
-
return self.update_app_canvas(), f"System: Removed component with ID {component_id}\n"
|
191 |
else:
|
192 |
return "", f"Error: Invalid action: {action}\n"
|
193 |
else:
|
194 |
-
return "", f"Error: Component with ID {component_id} not found\n"
|
195 |
else:
|
196 |
return "", f"Error: Failed to parse action from NLP response\n"
|
197 |
|
198 |
def build_app(self):
|
199 |
with gr.Blocks() as demo:
|
200 |
-
self.components = [] # Store component objects
|
201 |
for component in self.app_state["components"]:
|
202 |
component_type = component["type"]
|
203 |
properties = component["properties"]
|
204 |
|
205 |
if component_type == "Button":
|
206 |
-
|
207 |
-
self.components.append(button) # Store the button object
|
208 |
elif component_type == "Text Input":
|
209 |
-
|
210 |
-
self.components.append(textbox)
|
211 |
elif component_type == "Image":
|
212 |
-
|
213 |
-
self.components.append(image)
|
214 |
elif component_type == "Dropdown":
|
215 |
-
|
216 |
-
self.components.append(dropdown)
|
217 |
|
218 |
with gr.Tab("Terminal"):
|
219 |
gr.Markdown("## Terminal")
|
|
|
26 |
|
27 |
from utils import parse_action, parse_file_content, read_python_module_structure
|
28 |
|
29 |
+
def parse_action(response):
|
30 |
+
"""Parses the NLP response for action and component information.
|
31 |
+
|
32 |
+
Args:
|
33 |
+
response (str): The NLP model's response.
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
tuple: A tuple containing the parsed information:
|
37 |
+
- component_id (dict): The ID of the component, as an object with an '_id' attribute.
|
38 |
+
- action (str): The action to perform on the component (e.g., "update", "remove").
|
39 |
+
- property_name (str): The name of the property to modify.
|
40 |
+
- property_value (str): The new value for the property.
|
41 |
+
"""
|
42 |
+
component_id = None
|
43 |
+
action = None
|
44 |
+
property_name = None
|
45 |
+
property_value = None
|
46 |
+
|
47 |
+
# Example parsing logic (adjust based on your NLP model's response format)
|
48 |
+
if "Component ID:" in response:
|
49 |
+
component_id = response.split("Component ID:")[1].split(",")[0].strip()
|
50 |
+
if "Action:" in response:
|
51 |
+
action = response.split("Action:")[1].split(",")[0].strip()
|
52 |
+
if "Property:" in response:
|
53 |
+
property_name = response.split("Property:")[1].split(",")[0].strip()
|
54 |
+
if "Value:" in response:
|
55 |
+
property_value = response.split("Value:")[1].split(",")[0].strip()
|
56 |
+
|
57 |
+
# Return an object with an '_id' attribute
|
58 |
+
return {"_id": component_id}, action, property_name, property_value
|
59 |
+
|
60 |
class App:
|
61 |
def __init__(self):
|
62 |
self.app_state = {"components": []}
|
|
|
211 |
response = self.get_nlp_response(input_text, model_index)
|
212 |
component_id, action, property_name, property_value = parse_action(response)
|
213 |
if component_id:
|
214 |
+
component = next((comp for comp in self.app_state["components"] if comp["id"] == component_id["_id"]), None)
|
215 |
if component:
|
216 |
if action == "update":
|
217 |
component["properties"][property_name] = property_value
|
218 |
+
return self.update_app_canvas(), f"System: Updated property '{property_name}' of component with ID {component_id['_id']}\n"
|
219 |
elif action == "remove":
|
220 |
self.app_state["components"].remove(component)
|
221 |
+
return self.update_app_canvas(), f"System: Removed component with ID {component_id['_id']}\n"
|
222 |
else:
|
223 |
return "", f"Error: Invalid action: {action}\n"
|
224 |
else:
|
225 |
+
return "", f"Error: Component with ID {component_id['_id']} not found\n"
|
226 |
else:
|
227 |
return "", f"Error: Failed to parse action from NLP response\n"
|
228 |
|
229 |
def build_app(self):
|
230 |
with gr.Blocks() as demo:
|
|
|
231 |
for component in self.app_state["components"]:
|
232 |
component_type = component["type"]
|
233 |
properties = component["properties"]
|
234 |
|
235 |
if component_type == "Button":
|
236 |
+
gr.Button(value=properties["label"], variant="primary")
|
|
|
237 |
elif component_type == "Text Input":
|
238 |
+
gr.Textbox(label=properties["placeholder"])
|
|
|
239 |
elif component_type == "Image":
|
240 |
+
gr.Image(label=properties["alt"])
|
|
|
241 |
elif component_type == "Dropdown":
|
242 |
+
gr.Dropdown(choices=properties["choices"], label="Dropdown")
|
|
|
243 |
|
244 |
with gr.Tab("Terminal"):
|
245 |
gr.Markdown("## Terminal")
|