Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import subprocess
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
from huggingface_hub import InferenceClient, cached_download, hf_hub_url
|
| 8 |
import gradio as gr
|
|
@@ -26,37 +27,6 @@ from agent import (
|
|
| 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": []}
|
|
@@ -127,7 +97,8 @@ class App:
|
|
| 127 |
try:
|
| 128 |
new_component = {
|
| 129 |
"type": component_type,
|
| 130 |
-
"properties": properties or {}
|
|
|
|
| 131 |
}
|
| 132 |
self.app_state["components"].append(new_component)
|
| 133 |
except Exception as e:
|
|
@@ -211,18 +182,18 @@ class App:
|
|
| 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
|
| 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
|
| 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
|
| 222 |
else:
|
| 223 |
return "", f"Error: Invalid action: {action}\n"
|
| 224 |
else:
|
| 225 |
-
return "", f"Error: Component with ID {component_id
|
| 226 |
else:
|
| 227 |
return "", f"Error: Failed to parse action from NLP response\n"
|
| 228 |
|
|
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
from datetime import datetime
|
| 6 |
+
import uuid # Import the uuid library
|
| 7 |
|
| 8 |
from huggingface_hub import InferenceClient, cached_download, hf_hub_url
|
| 9 |
import gradio as gr
|
|
|
|
| 27 |
|
| 28 |
from utils import parse_action, parse_file_content, read_python_module_structure
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
class App:
|
| 31 |
def __init__(self):
|
| 32 |
self.app_state = {"components": []}
|
|
|
|
| 97 |
try:
|
| 98 |
new_component = {
|
| 99 |
"type": component_type,
|
| 100 |
+
"properties": properties or {},
|
| 101 |
+
"id": str(uuid.uuid4()) # Assign a unique ID using UUID
|
| 102 |
}
|
| 103 |
self.app_state["components"].append(new_component)
|
| 104 |
except Exception as e:
|
|
|
|
| 182 |
response = self.get_nlp_response(input_text, model_index)
|
| 183 |
component_id, action, property_name, property_value = parse_action(response)
|
| 184 |
if component_id:
|
| 185 |
+
component = next((comp for comp in self.app_state["components"] if comp["id"] == component_id), None)
|
| 186 |
if component:
|
| 187 |
if action == "update":
|
| 188 |
component["properties"][property_name] = property_value
|
| 189 |
+
return self.update_app_canvas(), f"System: Updated property '{property_name}' of component with ID {component_id}\n"
|
| 190 |
elif action == "remove":
|
| 191 |
self.app_state["components"].remove(component)
|
| 192 |
+
return self.update_app_canvas(), f"System: Removed component with ID {component_id}\n"
|
| 193 |
else:
|
| 194 |
return "", f"Error: Invalid action: {action}\n"
|
| 195 |
else:
|
| 196 |
+
return "", f"Error: Component with ID {component_id} not found\n"
|
| 197 |
else:
|
| 198 |
return "", f"Error: Failed to parse action from NLP response\n"
|
| 199 |
|