Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,361 +1,211 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
ACTION_PROMPT,
|
| 11 |
-
ADD_PROMPT,
|
| 12 |
-
COMPRESS_HISTORY_PROMPT,
|
| 13 |
-
LOG_PROMPT,
|
| 14 |
-
LOG_RESPONSE,
|
| 15 |
-
MODIFY_PROMPT,
|
| 16 |
-
PREFIX,
|
| 17 |
-
SEARCH_QUERY,
|
| 18 |
-
READ_PROMPT,
|
| 19 |
-
TASK_PROMPT,
|
| 20 |
-
UNDERSTAND_TEST_RESULTS_PROMPT,
|
| 21 |
-
)
|
| 22 |
-
from utils import parse_action, parse_file_content, read_python_module_structure
|
| 23 |
-
from datetime import datetime
|
| 24 |
-
import json
|
| 25 |
-
|
| 26 |
-
# --- Global Variables for App State ---
|
| 27 |
-
app_state = {"components": []}
|
| 28 |
-
terminal_history = ""
|
| 29 |
-
|
| 30 |
-
# --- Component Library ---
|
| 31 |
-
components_registry = {
|
| 32 |
-
"Button": {
|
| 33 |
-
"properties": {"label": "Click Me", "onclick": ""},
|
| 34 |
-
"description": "A clickable button",
|
| 35 |
-
"code_snippet": 'gr.Button(value="{label}", variant="primary")',
|
| 36 |
-
},
|
| 37 |
-
"Text Input": {
|
| 38 |
-
"properties": {"value": "", "placeholder": "Enter text"},
|
| 39 |
-
"description": "A field for entering text",
|
| 40 |
-
"code_snippet": 'gr.Textbox(label="{placeholder}")',
|
| 41 |
-
},
|
| 42 |
-
"Image": {
|
| 43 |
-
"properties": {"src": "#", "alt": "Image"},
|
| 44 |
-
"description": "Displays an image",
|
| 45 |
-
"code_snippet": 'gr.Image(label="{alt}")',
|
| 46 |
-
},
|
| 47 |
-
"Dropdown": {
|
| 48 |
-
"properties": {"choices": ["Option 1", "Option 2"], "value": ""},
|
| 49 |
-
"description": "A dropdown menu for selecting options",
|
| 50 |
-
"code_snippet": 'gr.Dropdown(choices={choices}, label="Dropdown")',
|
| 51 |
-
},
|
| 52 |
-
# Add more components here...
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
# --- NLP Model (Example using Hugging Face) ---
|
| 56 |
nlp_model_name = "google/flan-t5-small"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
self.properties
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
"
|
| 82 |
-
"type": self.type,
|
| 83 |
-
"properties": self.properties,
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
def render(self):
|
| 87 |
-
# Properly format choices for Dropdown
|
| 88 |
-
if self.type == "Dropdown":
|
| 89 |
-
self.properties["choices"] = (
|
| 90 |
-
str(self.properties["choices"])
|
| 91 |
-
.replace("[", "")
|
| 92 |
-
.replace("]", "")
|
| 93 |
-
.replace("'", "")
|
| 94 |
-
)
|
| 95 |
-
return components_registry[self.type]["code_snippet"].format(
|
| 96 |
-
**self.properties
|
| 97 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
def
|
| 102 |
-
components_html = "".join(
|
| 103 |
-
[
|
| 104 |
-
f"<div>Component ID: {component['id']}, Type: {component['type']}, Properties: {component['properties']}</div>"
|
| 105 |
-
for component in app_state["components"]
|
| 106 |
-
]
|
| 107 |
-
)
|
| 108 |
-
return components_html
|
| 109 |
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
return (
|
| 117 |
-
update_app_canvas(),
|
| 118 |
-
f"System: Added component: {component_type}\n",
|
| 119 |
-
)
|
| 120 |
-
else:
|
| 121 |
-
return None, f"Error: Invalid component type: {component_type}\n"
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
# --- Function to handle terminal input ---
|
| 125 |
-
def run_terminal_command(command, history):
|
| 126 |
-
global terminal_history
|
| 127 |
-
output = ""
|
| 128 |
-
try:
|
| 129 |
-
# Basic command parsing (expand with NLP)
|
| 130 |
-
if command.startswith("add "):
|
| 131 |
-
component_type = command.split("add ", 1)[1].strip()
|
| 132 |
-
_, output = add_component(component_type)
|
| 133 |
-
elif command.startswith("set "):
|
| 134 |
-
_, output = set_component_property(command)
|
| 135 |
-
elif command.startswith("search "):
|
| 136 |
-
search_query = command.split("search ", 1)[1].strip()
|
| 137 |
-
output = i_s(search_query)
|
| 138 |
-
else:
|
| 139 |
-
# Attempt to execute command as Python code
|
| 140 |
-
try:
|
| 141 |
-
result = subprocess.check_output(
|
| 142 |
-
command, shell=True, stderr=subprocess.STDOUT, text=True
|
| 143 |
-
)
|
| 144 |
-
output = result
|
| 145 |
-
except Exception as e:
|
| 146 |
-
output = f"Error executing Python code: {str(e)}"
|
| 147 |
-
except Exception as e:
|
| 148 |
-
output = f"Error: {str(e)}"
|
| 149 |
-
finally:
|
| 150 |
-
terminal_history += f"User: {command}\n"
|
| 151 |
-
terminal_history += f"{output}\n"
|
| 152 |
-
return terminal_history
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
def set_component_property(command):
|
| 156 |
-
try:
|
| 157 |
-
# Improved 'set' command parsing
|
| 158 |
-
set_parts = command.split(" ", 2)[1:]
|
| 159 |
-
if len(set_parts) != 2:
|
| 160 |
-
raise ValueError("Invalid 'set' command format.")
|
| 161 |
-
|
| 162 |
-
component_id = int(set_parts[0]) # Use component ID
|
| 163 |
-
property_name, property_value = set_parts[1].split("=", 1)
|
| 164 |
-
|
| 165 |
-
# Find component by ID
|
| 166 |
-
component_found = False
|
| 167 |
-
for component in app_state["components"]:
|
| 168 |
-
if component["id"] == component_id:
|
| 169 |
-
if property_name in component["properties"]:
|
| 170 |
-
component["properties"][
|
| 171 |
-
property_name.strip()
|
| 172 |
-
] = property_value.strip()
|
| 173 |
-
component_found = True
|
| 174 |
-
return (
|
| 175 |
-
update_app_canvas(),
|
| 176 |
-
f"System: Property '{property_name}' set to '{property_value}' for component {component_id}\n",
|
| 177 |
-
)
|
| 178 |
-
else:
|
| 179 |
-
return (
|
| 180 |
-
None,
|
| 181 |
-
f"Error: Property '{property_name}' not found in component {component_id}\n",
|
| 182 |
-
)
|
| 183 |
-
if not component_found:
|
| 184 |
-
return (
|
| 185 |
-
None,
|
| 186 |
-
f"Error: Component with ID {component_id} not found.\n",
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
except Exception as e:
|
| 190 |
-
return None, f"Error: Invalid 'set' command format or error setting property: {str(e)}\n"
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
# --- Function to handle chat interaction ---
|
| 194 |
-
def run_chat(message, history):
|
| 195 |
-
global terminal_history
|
| 196 |
-
if message.startswith("!"):
|
| 197 |
-
command = message[1:]
|
| 198 |
-
terminal_history = run_terminal_command(command, history)
|
| 199 |
-
return history, terminal_history
|
| 200 |
-
else:
|
| 201 |
-
# ... (Your regular chat response generation)
|
| 202 |
-
return history, terminal_history
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
# --- Code Generation ---
|
| 206 |
-
def generate_python_code(app_name):
|
| 207 |
-
code = f"""
|
| 208 |
-
import gradio as gr
|
| 209 |
-
|
| 210 |
-
# Define your Gradio components here
|
| 211 |
-
with gr.Blocks() as {app_name}:
|
| 212 |
-
"""
|
| 213 |
for component in app_state["components"]:
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
try:
|
| 231 |
-
with open(filename, "r") as f:
|
| 232 |
-
app_state = json.load(f)
|
| 233 |
-
except FileNotFoundError:
|
| 234 |
-
print("App state file not found. Starting with a blank slate.")
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
# --- Hugging Face Deployment ---
|
| 238 |
-
def deploy_to_huggingface(app_name):
|
| 239 |
-
# Generate Python code
|
| 240 |
-
code = generate_python_code(app_name)
|
| 241 |
-
|
| 242 |
-
# Create requirements.txt
|
| 243 |
-
with open("requirements.txt", "w") as f:
|
| 244 |
-
f.write("gradio==3.32.0\n")
|
| 245 |
-
|
| 246 |
-
# Create the app.py file
|
| 247 |
-
with open("app.py", "w") as f:
|
| 248 |
-
f.write(code)
|
| 249 |
-
|
| 250 |
-
# Execute the deployment command
|
| 251 |
-
try:
|
| 252 |
-
subprocess.run(
|
| 253 |
-
[
|
| 254 |
-
"huggingface-cli",
|
| 255 |
-
"repo",
|
| 256 |
-
"create",
|
| 257 |
-
"--type",
|
| 258 |
-
"space",
|
| 259 |
-
"--space_sdk",
|
| 260 |
-
"gradio",
|
| 261 |
-
app_name,
|
| 262 |
-
],
|
| 263 |
-
check=True,
|
| 264 |
-
)
|
| 265 |
-
subprocess.run(
|
| 266 |
-
["git", "init"], cwd=f"./{app_name}", check=True
|
| 267 |
-
)
|
| 268 |
-
subprocess.run(
|
| 269 |
-
["git", "add", "."], cwd=f"./{app_name}", check=True
|
| 270 |
-
)
|
| 271 |
-
subprocess.run(
|
| 272 |
-
['git', 'commit', '-m', '"Initial commit"'], cwd=f"./{app_name}", check=True
|
| 273 |
-
)
|
| 274 |
-
subprocess.run(
|
| 275 |
-
["git", "push", "https://huggingface.co/spaces/" + app_name, "main"], cwd=f"./{app_name}", check=True
|
| 276 |
-
)
|
| 277 |
return (
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
except Exception as e:
|
| 281 |
-
return f"Error deploying to Hugging Face Spaces: {e}"
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
# --- Gradio Interface ---
|
| 285 |
-
with gr.Blocks() as iface:
|
| 286 |
-
with gr.Row():
|
| 287 |
-
# --- Chat Interface ---
|
| 288 |
-
chat_history = gr.Chatbot(label="Chat with Agent")
|
| 289 |
-
chat_input = gr.Textbox(label="Your Message")
|
| 290 |
-
chat_button = gr.Button("Send")
|
| 291 |
-
|
| 292 |
-
chat_button.click(
|
| 293 |
-
run_chat,
|
| 294 |
-
inputs=[chat_input, chat_history],
|
| 295 |
-
outputs=[chat_history, terminal_output],
|
| 296 |
)
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
)
|
| 303 |
-
with gr.Column():
|
| 304 |
-
component_list = gr.Dropdown(
|
| 305 |
-
choices=list(components_registry.keys()), label="Components"
|
| 306 |
-
)
|
| 307 |
-
add_button = gr.Button("Add Component")
|
| 308 |
-
|
| 309 |
-
add_button.click(
|
| 310 |
-
add_component,
|
| 311 |
-
inputs=component_list,
|
| 312 |
-
outputs=[app_canvas, terminal_output],
|
| 313 |
-
)
|
| 314 |
-
|
| 315 |
-
with gr.Row():
|
| 316 |
-
# --- Terminal ---
|
| 317 |
-
terminal_output = gr.Textbox(
|
| 318 |
-
lines=8, label="Terminal", value=terminal_history
|
| 319 |
-
)
|
| 320 |
-
terminal_input = gr.Textbox(label="Enter Command")
|
| 321 |
-
terminal_button = gr.Button("Run")
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
inputs=[terminal_input, terminal_output],
|
| 326 |
-
outputs=terminal_output,
|
| 327 |
-
)
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
)
|
|
|
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
save_button.click(save_app_state)
|
| 350 |
-
load_button.click(load_app_state)
|
| 351 |
-
|
| 352 |
-
with gr.Row():
|
| 353 |
-
# --- Deploy Button ---
|
| 354 |
-
deploy_button = gr.Button("Deploy to Hugging Face")
|
| 355 |
-
deploy_output = gr.Textbox(label="Deployment Output")
|
| 356 |
-
deploy_button.click(
|
| 357 |
-
deploy_to_huggingface,
|
| 358 |
-
inputs=[app_name_input],
|
| 359 |
-
outputs=[deploy_output],
|
| 360 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
iface.launch()
|
|
|
|
| 1 |
+
import os import subprocess import random from huggingface_hub import InferenceClient, cached_download, hf_hub_url import gradio as gr from safe_search import safe_search from i_search import google from i_search import i_search as i_s from agent import ( ACTION_PROMPT, ADD_PROMPT, COMPRESS_HISTORY_PROMPT, LOG_PROMPT, LOG_RESPONSE, MODIFY_PROMPT, PREFIX, SEARCH_QUERY, READ_PROMPT, TASK_PROMPT, UNDERSTAND_TEST_RESULTS_PROMPT, ) from utils import parse_action, parse_file_content, read_python_module_structure from datetime import datetime import json
|
| 2 |
+
|
| 3 |
+
--- Global Variables for App State ---
|
| 4 |
+
app_state = {"components": []} terminal_history = ""
|
| 5 |
+
|
| 6 |
+
--- Component Library ---
|
| 7 |
+
components_registry = { "Button": { "properties": {"label": "Click Me", "onclick": ""}, "description": "A clickable button", "code_snippet": 'gr.Button(value="{label}", variant="primary")', }, "Text Input": { "properties": {"value": "", "placeholder": "Enter text"}, "description": "A field for entering text", "code_snippet": 'gr.Textbox(label="{placeholder}")', }, "Image": { "properties": {"src": "#", "alt": "Image"}, "description": "Displays an image", "code_snippet": 'gr.Image(label="{alt}")', }, "Dropdown": { "properties": {"choices": ["Option 1", "Option 2"], "value": ""}, "description": "A dropdown menu for selecting options", "code_snippet": 'gr.Dropdown(choices={choices}, label="Dropdown")', }, # Add more components here... }
|
| 8 |
+
|
| 9 |
+
--- NLP Model (Example using Hugging Face) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
nlp_model_name = "google/flan-t5-small"
|
| 11 |
+
|
| 12 |
+
Check if the model exists in the cache
|
| 13 |
+
try: cached_download(hf_hub_url(nlp_model_name, revision="main")) nlp_model = InferenceClient(nlp_model_name) except: nlp_model = None
|
| 14 |
+
|
| 15 |
+
--- Function to get NLP model response ---
|
| 16 |
+
def get_nlp_response(input_text): if nlp_model: response = nlp_model.text_generation(input_text) return response.generated_text else: return "NLP model not available."
|
| 17 |
+
|
| 18 |
+
--- Component Class ---
|
| 19 |
+
class Component: def init(self, type, properties=None, id=None): self.id = id or random.randint(1000, 9999) self.type = type self.properties = properties or components_registry[type]["properties"].copy()
|
| 20 |
+
|
| 21 |
+
def to_dict(self):
|
| 22 |
+
return {
|
| 23 |
+
"id": self.id,
|
| 24 |
+
"type": self.type,
|
| 25 |
+
"properties": self.properties,
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def render(self):
|
| 29 |
+
# Properly format choices for Dropdown
|
| 30 |
+
if self.type == "Dropdown":
|
| 31 |
+
self.properties["choices"] = (
|
| 32 |
+
str(self.properties["choices"])
|
| 33 |
+
.replace("[", "")
|
| 34 |
+
.replace("]", "")
|
| 35 |
+
.replace("'", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
+
return components_registry[self.type]["code_snippet"].format(
|
| 38 |
+
**self.properties
|
| 39 |
+
)
|
| 40 |
+
--- Function to update the app canvas (for preview) ---
|
| 41 |
+
def update_app_canvas(): components_html = "".join( [ f"<div>Component ID: {component['id']}, Type: {component['type']}, Properties: {component['properties']}</div>" for component in app_state["components"] ] ) return components_html
|
| 42 |
|
| 43 |
+
--- Function to handle component addition ---
|
| 44 |
+
def add_component(component_type): if component_type in components_registry: new_component = Component(component_type) app_state["components"].append(new_component.to_dict()) return ( update_app_canvas(), f"System: Added component: {component_type}\n", ) else: return None, f"Error: Invalid component type: {component_type}\n"
|
| 45 |
|
| 46 |
+
--- Function to handle terminal input ---
|
| 47 |
+
def run_terminal_command(command, history): global terminal_history output = "" try: # Basic command parsing (expand with NLP) if command.startswith("add "): component_type = command.split("add ", 1)[1].strip() _, output = add_component(component_type) elif command.startswith("set "): _, output = set_component_property(command) elif command.startswith("search "): search_query = command.split("search ", 1)[1].strip() output = i_s(search_query) elif command.startswith("deploy "): app_name = command.split("deploy ", 1)[1].strip() output = deploy_to_huggingface(app_name) else: # Attempt to execute command as Python code try: result = subprocess.check_output( command, shell=True, stderr=subprocess.STDOUT, text=True ) output = result except Exception as e: output = f"Error executing Python code: {str(e)}" except Exception as e: output = f"Error: {str(e)}" finally: terminal_history += f"User: {command}\n" terminal_history += f"{output}\n" return terminal_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def set_component_property(command): try: # Improved 'set' command parsing set_parts = command.split(" ", 2)[1:] if len(set_parts) != 2: raise ValueError("Invalid 'set' command format.")
|
| 50 |
|
| 51 |
+
component_id = int(set_parts[0]) # Use component ID
|
| 52 |
+
property_name, property_value = set_parts[1].split("=", 1)
|
| 53 |
+
|
| 54 |
+
# Find component by ID
|
| 55 |
+
component_found = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
for component in app_state["components"]:
|
| 57 |
+
if component["id"] == component_id:
|
| 58 |
+
if property_name in component["properties"]:
|
| 59 |
+
component["properties"][
|
| 60 |
+
property_name.strip()
|
| 61 |
+
] = property_value.strip()
|
| 62 |
+
component_found = True
|
| 63 |
+
return (
|
| 64 |
+
update_app_canvas(),
|
| 65 |
+
f"System: Property '{property_name}' set to '{property_value}' for component {component_id}\n",
|
| 66 |
+
)
|
| 67 |
+
else:
|
| 68 |
+
return (
|
| 69 |
+
None,
|
| 70 |
+
f"Error: Property '{property_name}' not found in component {component_id}\n",
|
| 71 |
+
)
|
| 72 |
+
if not component_found:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return (
|
| 74 |
+
None,
|
| 75 |
+
f"Error: Component with ID {component_id} not found.\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return None, f"Error: Invalid 'set' command format or error setting property: {str(e)}\n"
|
| 80 |
+
--- Function to handle chat interaction ---
|
| 81 |
+
def run_chat(message, history): global terminal_history if message.startswith("!"): command = message[1:] terminal_history = run_terminal_command(command, history) return history, terminal_history else: # ... (Your regular chat response generation) return history, terminal_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
--- Code Generation ---
|
| 84 |
+
def generate_python_code(app_name): code = f""" import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
Define your Gradio components here
|
| 87 |
+
with gr.Blocks() as {app_name}: """ for component in app_state["components"]: code += " " + Component(**component).render() + "\n"
|
| 88 |
+
|
| 89 |
+
code += f"""
|
| 90 |
+
{app_name}.launch() """ return code
|
| 91 |
+
|
| 92 |
+
--- Save/Load App State ---
|
| 93 |
+
def save_app_state(filename="app_state.json"): with open(filename, "w") as f: json.dump(app_state, f)
|
| 94 |
+
|
| 95 |
+
def load_app_state(filename="app_state.json"): global app_state try: with open(filename, "r") as f: app_state = json.load(f) except FileNotFoundError: print("App state file not found. Starting with a blank slate.")
|
| 96 |
+
|
| 97 |
+
--- Hugging Face Deployment --- def deploy_to_huggingface(app_name): # Generate Python code code = generate_python_code(app_name)
|
| 98 |
+
|
| 99 |
+
# Create requirements.txt
|
| 100 |
+
with open("requirements.txt", "w") as f:
|
| 101 |
+
f.write("gradio==3.32.0\n")
|
| 102 |
+
|
| 103 |
+
# Create the app.py file
|
| 104 |
+
with open("app.py", "w") as f:
|
| 105 |
+
f.write(code)
|
| 106 |
+
|
| 107 |
+
# Execute the deployment command
|
| 108 |
+
try:
|
| 109 |
+
subprocess.run(
|
| 110 |
+
[
|
| 111 |
+
"huggingface-cli",
|
| 112 |
+
"repo",
|
| 113 |
+
"create",
|
| 114 |
+
"--type",
|
| 115 |
+
"space",
|
| 116 |
+
"--space_sdk",
|
| 117 |
+
"gradio",
|
| 118 |
+
app_name,
|
| 119 |
+
],
|
| 120 |
+
check=True,
|
| 121 |
+
)
|
| 122 |
+
subprocess.run(
|
| 123 |
+
["git", "init"], cwd=f"./{app_name}", check=True
|
| 124 |
+
)
|
| 125 |
+
subprocess.run(
|
| 126 |
+
["git", "add", "."], cwd=f"./{app_name}", check=True
|
| 127 |
+
)
|
| 128 |
+
subprocess.run(
|
| 129 |
+
['git', 'commit', '-m', '"Initial commit"'], cwd=f"./{app_name}", check=True
|
| 130 |
+
)
|
| 131 |
+
subprocess.run(
|
| 132 |
+
["git", "push", "https://huggingface.co/spaces/" + app_name, "main"], cwd=f"./{app_name}", check=True
|
| 133 |
+
)
|
| 134 |
+
return (
|
| 135 |
+
f"Successfully deployed to Hugging Face Spaces: https://huggingface.co/spaces/{app_name}"
|
| 136 |
+
)
|
| 137 |
+
except Exception as e:
|
| 138 |
+
return f"Error deploying to Hugging Face Spaces: {e}"
|
| 139 |
+
--- Gradio Interface ---
|
| 140 |
+
with gr.Blocks() as iface: with gr.Row(): # --- Chat Interface --- chat_history = gr.Chatbot(label="Chat with Agent") chat_input = gr.Textbox(label="Your Message") chat_button = gr.Button("Send")
|
| 141 |
+
|
| 142 |
+
chat_button.click(
|
| 143 |
+
run_chat,
|
| 144 |
+
inputs=[chat_input, chat_history],
|
| 145 |
+
outputs=[chat_history, terminal_output],
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
with gr.Row():
|
| 149 |
+
# --- App Builder Section ---
|
| 150 |
+
app_canvas = gr.HTML(
|
| 151 |
+
"<div>App Canvas Preview:</div>", label="App Canvas"
|
| 152 |
+
)
|
| 153 |
+
with gr.Column():
|
| 154 |
+
component_list = gr.Dropdown(
|
| 155 |
+
choices=list(components_registry.keys()), label="Components"
|
| 156 |
)
|
| 157 |
+
add_button = gr.Button("Add Component")
|
| 158 |
|
| 159 |
+
add_button.click(
|
| 160 |
+
add_component,
|
| 161 |
+
inputs=component_list,
|
| 162 |
+
outputs=[app_canvas, terminal_output],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
)
|
| 164 |
+
|
| 165 |
+
with gr.Row():
|
| 166 |
+
# --- Terminal ---
|
| 167 |
+
terminal_output = gr.Textbox(
|
| 168 |
+
lines=8, label="Terminal", value=terminal_history
|
| 169 |
+
)
|
| 170 |
+
terminal_input = gr.Textbox(label="Enter Command")
|
| 171 |
+
terminal_button = gr.Button("Run")
|
| 172 |
+
|
| 173 |
+
terminal_button.click(
|
| 174 |
+
run_terminal_command,
|
| 175 |
+
inputs=[terminal_input, terminal_output],
|
| 176 |
+
outputs=terminal_output,
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
with gr.Row():
|
| 180 |
+
# --- Code Generation ---
|
| 181 |
+
code_output = gr.Code(
|
| 182 |
+
generate_python_code("app_name"),
|
| 183 |
+
language="python",
|
| 184 |
+
label="Generated Code",
|
| 185 |
+
)
|
| 186 |
+
app_name_input = gr.Textbox(label="App Name")
|
| 187 |
+
generate_code_button = gr.Button("Generate Code")
|
| 188 |
+
generate_code_button.click(
|
| 189 |
+
generate_python_code,
|
| 190 |
+
inputs=[app_name_input],
|
| 191 |
+
outputs=code_output,
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
# --- Save/Load Buttons ---
|
| 196 |
+
save_button = gr.Button("Save App State")
|
| 197 |
+
load_button = gr.Button("Load App State")
|
| 198 |
+
|
| 199 |
+
save_button.click(save_app_state)
|
| 200 |
+
load_button.click(load_app_state)
|
| 201 |
+
|
| 202 |
+
with gr.Row():
|
| 203 |
+
# --- Deploy Button ---
|
| 204 |
+
deploy_button = gr.Button("Deploy to Hugging Face")
|
| 205 |
+
deploy_output = gr.Textbox(label="Deployment Output")
|
| 206 |
+
deploy_button.click(
|
| 207 |
+
deploy_to_huggingface,
|
| 208 |
+
inputs=[app_name_input],
|
| 209 |
+
outputs=[deploy_output],
|
| 210 |
+
)
|
| 211 |
iface.launch()
|