Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,8 +14,7 @@ from agent import (
|
|
| 14 |
)
|
| 15 |
from utils import parse_action, parse_file_content, read_python_module_structure
|
| 16 |
|
| 17 |
-
# Global Variables
|
| 18 |
-
app_state = {"components": []}
|
| 19 |
terminal_history = ""
|
| 20 |
|
| 21 |
# Component Library
|
|
@@ -92,22 +91,80 @@ class Component:
|
|
| 92 |
)
|
| 93 |
return components_registry[self.type]["code_snippet"].format(**self.properties)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
new_component = Component(component_type)
|
| 107 |
-
|
| 108 |
-
return update_app_canvas()
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# Function to handle terminal input
|
| 113 |
def run_terminal_command(command, history):
|
|
@@ -117,15 +174,14 @@ def run_terminal_command(command, history):
|
|
| 117 |
# Basic command parsing (expand with NLP)
|
| 118 |
if command.startswith("add "):
|
| 119 |
component_type = command.split("add ", 1)[1].strip()
|
| 120 |
-
|
| 121 |
elif command.startswith("set "):
|
| 122 |
_, output = set_component_property(command)
|
| 123 |
elif command.startswith("search "):
|
| 124 |
search_query = command.split("search ", 1)[1].strip()
|
| 125 |
output = i_s(search_query)
|
| 126 |
elif command.startswith("deploy "):
|
| 127 |
-
|
| 128 |
-
output = deploy_to_huggingface(app_name)
|
| 129 |
else:
|
| 130 |
# Attempt to execute command as Python code
|
| 131 |
try:
|
|
@@ -149,18 +205,7 @@ def set_component_property(command):
|
|
| 149 |
raise ValueError("Invalid 'set' command format.")
|
| 150 |
component_id = int(set_parts[0]) # Use component ID
|
| 151 |
property_name, property_value = set_parts[1].split("=", 1)
|
| 152 |
-
|
| 153 |
-
component_found = False
|
| 154 |
-
for component in app_state["components"]:
|
| 155 |
-
if component["id"] == component_id:
|
| 156 |
-
if property_name in component["properties"]:
|
| 157 |
-
component["properties"][property_name.strip()] = property_value.strip()
|
| 158 |
-
component_found = True
|
| 159 |
-
return update_app_canvas(), f"System: Property '{property_name}' set to '{property_value}' for component {component_id}\n"
|
| 160 |
-
else:
|
| 161 |
-
return None, f"Error: Property '{property_name}' not found in component {component_id}\n"
|
| 162 |
-
if not component_found:
|
| 163 |
-
return None, f"Error: Component with ID {component_id} not found.\n"
|
| 164 |
except Exception as e:
|
| 165 |
return None, f"Error: {str(e)}\n"
|
| 166 |
|
|
@@ -178,47 +223,98 @@ def run_chat(message, history):
|
|
| 178 |
else:
|
| 179 |
return history, terminal_history + f"User: {message}\nAssistant: I'm sorry, I couldn't generate a response. Please try again.\n"
|
| 180 |
|
| 181 |
-
# Code Generation
|
| 182 |
-
def generate_python_code(app_name):
|
| 183 |
-
code = f"""import gradio as gr\n\nwith gr.Blocks() as {app_name}:\n"""
|
| 184 |
-
for component in app_state["components"]:
|
| 185 |
-
code += " " + Component(**component).render() + "\n"
|
| 186 |
-
code += f"\n{app_name}.launch()\n"
|
| 187 |
-
return code
|
| 188 |
-
|
| 189 |
-
# Hugging Face Deployment
|
| 190 |
-
def deploy_to_huggingface(app_name):
|
| 191 |
-
# Generate Python code
|
| 192 |
-
code = generate_python_code(app_name)
|
| 193 |
-
# Create requirements.txt
|
| 194 |
-
with open("requirements.txt", "w") as f:
|
| 195 |
-
f.write("gradio==3.32.0\n")
|
| 196 |
-
# Create the app.py file
|
| 197 |
-
with open("app.py", "w") as f:
|
| 198 |
-
f.write(code)
|
| 199 |
-
# Execute the deployment command
|
| 200 |
-
try:
|
| 201 |
-
subprocess.run(["huggingface-cli", "repo", "create", "--type", "space", "--space_sdk", "gradio", app_name], check=True)
|
| 202 |
-
subprocess.run(["git", "init"], cwd=f"./{app_name}", check=True)
|
| 203 |
-
subprocess.run(["git", "add", "."], cwd=f"./{app_name}", check=True)
|
| 204 |
-
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=f"./{app_name}", check=True)
|
| 205 |
-
subprocess.run(["git", "push", "https://huggingface.co/spaces/" + app_name, "main"], cwd=f"./{app_name}", check=True)
|
| 206 |
-
return f"Successfully deployed to Hugging Face Spaces: https://huggingface.co/spaces/{app_name}"
|
| 207 |
-
except Exception as e:
|
| 208 |
-
return f"Error deploying to Hugging Face Spaces: {e}"
|
| 209 |
-
|
| 210 |
# Gradio Interface
|
| 211 |
with gr.Blocks() as iface:
|
| 212 |
-
#
|
| 213 |
-
chat_history = gr.Chatbot(label="Chat with Agent")
|
| 214 |
-
chat_input = gr.Textbox(label="Your Message")
|
| 215 |
-
chat_button = gr.Button("Send")
|
| 216 |
-
chat_button.click(run_chat, inputs=[chat_input, chat_history], outputs=[chat_history, terminal_output])
|
| 217 |
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
iface.launch()
|
|
|
|
| 14 |
)
|
| 15 |
from utils import parse_action, parse_file_content, read_python_module_structure
|
| 16 |
|
| 17 |
+
# Global Variables
|
|
|
|
| 18 |
terminal_history = ""
|
| 19 |
|
| 20 |
# Component Library
|
|
|
|
| 91 |
)
|
| 92 |
return components_registry[self.type]["code_snippet"].format(**self.properties)
|
| 93 |
|
| 94 |
+
# App Creation Process Class
|
| 95 |
+
class AppCreationProcess:
|
| 96 |
+
def __init__(self):
|
| 97 |
+
self.current_step = 1
|
| 98 |
+
self.app_name = ""
|
| 99 |
+
self.components = []
|
| 100 |
+
|
| 101 |
+
def next_step(self):
|
| 102 |
+
self.current_step += 1
|
| 103 |
+
|
| 104 |
+
def previous_step(self):
|
| 105 |
+
if self.current_step > 1:
|
| 106 |
+
self.current_step -= 1
|
| 107 |
+
|
| 108 |
+
def get_current_step_info(self):
|
| 109 |
+
steps = {
|
| 110 |
+
1: "App Initialization",
|
| 111 |
+
2: "Component Addition",
|
| 112 |
+
3: "Property Configuration",
|
| 113 |
+
4: "Code Generation",
|
| 114 |
+
5: "Deployment"
|
| 115 |
+
}
|
| 116 |
+
return f"Step {self.current_step}: {steps[self.current_step]}"
|
| 117 |
+
|
| 118 |
+
def add_component(self, component_type):
|
| 119 |
new_component = Component(component_type)
|
| 120 |
+
self.components.append(new_component.to_dict())
|
| 121 |
+
return self.update_app_canvas()
|
| 122 |
+
|
| 123 |
+
def set_component_property(self, component_id, property_name, property_value):
|
| 124 |
+
for component in self.components:
|
| 125 |
+
if component['id'] == component_id:
|
| 126 |
+
if property_name in component['properties']:
|
| 127 |
+
component['properties'][property_name.strip()] = property_value.strip()
|
| 128 |
+
return self.update_app_canvas(), f"Property '{property_name}' set to '{property_value}' for component {component_id}"
|
| 129 |
+
else:
|
| 130 |
+
return None, f"Error: Property '{property_name}' not found in component {component_id}"
|
| 131 |
+
return None, f"Error: Component with ID {component_id} not found."
|
| 132 |
+
|
| 133 |
+
def update_app_canvas(self):
|
| 134 |
+
components_html = "".join([
|
| 135 |
+
f"<div>Component ID: {component['id']}, Type: {component['type']}, Properties: {component['properties']}</div>"
|
| 136 |
+
for component in self.components
|
| 137 |
+
])
|
| 138 |
+
return components_html
|
| 139 |
+
|
| 140 |
+
def generate_python_code(self):
|
| 141 |
+
code = f"""import gradio as gr\n\nwith gr.Blocks() as {self.app_name}:\n"""
|
| 142 |
+
for component in self.components:
|
| 143 |
+
code += " " + Component(**component).render() + "\n"
|
| 144 |
+
code += f"\n{self.app_name}.launch()\n"
|
| 145 |
+
return code
|
| 146 |
+
|
| 147 |
+
def deploy_to_huggingface(self):
|
| 148 |
+
# Generate Python code
|
| 149 |
+
code = self.generate_python_code()
|
| 150 |
+
# Create requirements.txt
|
| 151 |
+
with open("requirements.txt", "w") as f:
|
| 152 |
+
f.write("gradio==3.32.0\n")
|
| 153 |
+
# Create the app.py file
|
| 154 |
+
with open("app.py", "w") as f:
|
| 155 |
+
f.write(code)
|
| 156 |
+
# Execute the deployment command
|
| 157 |
+
try:
|
| 158 |
+
subprocess.run(["huggingface-cli", "repo", "create", "--type", "space", "--space_sdk", "gradio", self.app_name], check=True)
|
| 159 |
+
subprocess.run(["git", "init"], cwd=f"./{self.app_name}", check=True)
|
| 160 |
+
subprocess.run(["git", "add", "."], cwd=f"./{self.app_name}", check=True)
|
| 161 |
+
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=f"./{self.app_name}", check=True)
|
| 162 |
+
subprocess.run(["git", "push", "https://huggingface.co/spaces/" + self.app_name, "main"], cwd=f"./{self.app_name}", check=True)
|
| 163 |
+
return f"Successfully deployed to Hugging Face Spaces: https://huggingface.co/spaces/{self.app_name}"
|
| 164 |
+
except Exception as e:
|
| 165 |
+
return f"Error deploying to Hugging Face Spaces: {e}"
|
| 166 |
+
|
| 167 |
+
app_process = AppCreationProcess()
|
| 168 |
|
| 169 |
# Function to handle terminal input
|
| 170 |
def run_terminal_command(command, history):
|
|
|
|
| 174 |
# Basic command parsing (expand with NLP)
|
| 175 |
if command.startswith("add "):
|
| 176 |
component_type = command.split("add ", 1)[1].strip()
|
| 177 |
+
output = app_process.add_component(component_type)
|
| 178 |
elif command.startswith("set "):
|
| 179 |
_, output = set_component_property(command)
|
| 180 |
elif command.startswith("search "):
|
| 181 |
search_query = command.split("search ", 1)[1].strip()
|
| 182 |
output = i_s(search_query)
|
| 183 |
elif command.startswith("deploy "):
|
| 184 |
+
output = app_process.deploy_to_huggingface()
|
|
|
|
| 185 |
else:
|
| 186 |
# Attempt to execute command as Python code
|
| 187 |
try:
|
|
|
|
| 205 |
raise ValueError("Invalid 'set' command format.")
|
| 206 |
component_id = int(set_parts[0]) # Use component ID
|
| 207 |
property_name, property_value = set_parts[1].split("=", 1)
|
| 208 |
+
return app_process.set_component_property(component_id, property_name, property_value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
except Exception as e:
|
| 210 |
return None, f"Error: {str(e)}\n"
|
| 211 |
|
|
|
|
| 223 |
else:
|
| 224 |
return history, terminal_history + f"User: {message}\nAssistant: I'm sorry, I couldn't generate a response. Please try again.\n"
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
# Gradio Interface
|
| 227 |
with gr.Blocks() as iface:
|
| 228 |
+
gr.Markdown("# Sequential App Builder")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
with gr.Row():
|
| 231 |
+
current_step = gr.Markdown(app_process.get_current_step_info())
|
| 232 |
+
|
| 233 |
+
with gr.Row():
|
| 234 |
+
prev_button = gr.Button("Previous Step")
|
| 235 |
+
next_button = gr.Button("Next Step")
|
| 236 |
+
|
| 237 |
+
# Step 1: App Initialization
|
| 238 |
+
with gr.Group() as step1:
|
| 239 |
+
app_name_input = gr.Textbox(label="Enter App Name")
|
| 240 |
+
init_app_button = gr.Button("Initialize App")
|
| 241 |
+
|
| 242 |
+
# Step 2: Component Addition
|
| 243 |
+
with gr.Group() as step2:
|
| 244 |
+
component_type = gr.Dropdown(choices=list(components_registry.keys()), label="Select Component Type")
|
| 245 |
+
add_component_button = gr.Button("Add Component")
|
| 246 |
+
components_display = gr.HTML()
|
| 247 |
+
|
| 248 |
+
# Step 3: Property Configuration
|
| 249 |
+
with gr.Group() as step3:
|
| 250 |
+
component_id = gr.Number(label="Component ID")
|
| 251 |
+
property_name = gr.Textbox(label="Property Name")
|
| 252 |
+
property_value = gr.Textbox(label="Property Value")
|
| 253 |
+
set_property_button = gr.Button("Set Property")
|
| 254 |
+
|
| 255 |
+
# Step 4: Code Generation
|
| 256 |
+
with gr.Group() as step4:
|
| 257 |
+
generated_code = gr.Code(language="python")
|
| 258 |
+
generate_code_button = gr.Button("Generate Code")
|
| 259 |
+
|
| 260 |
+
# Step 5: Deployment
|
| 261 |
+
with gr.Group() as step5:
|
| 262 |
+
deploy_button = gr.Button("Deploy to Hugging Face Spaces")
|
| 263 |
+
deployment_status = gr.Markdown()
|
| 264 |
+
|
| 265 |
+
# Chat and Terminal (optional, can be hidden or shown based on preference)
|
| 266 |
+
with gr.Accordion("Advanced", open=False):
|
| 267 |
+
chat_history = gr.Chatbot(label="Chat with Agent")
|
| 268 |
+
chat_input = gr.Textbox(label="Your Message")
|
| 269 |
+
chat_button = gr.Button("Send")
|
| 270 |
+
|
| 271 |
+
terminal_output = gr.Textbox(lines=8, label="Terminal", value=terminal_history)
|
| 272 |
+
terminal_input = gr.Textbox(label="Enter Command")
|
| 273 |
+
terminal_button = gr.Button("Run")
|
| 274 |
+
|
| 275 |
+
# Function to update visibility based on current step
|
| 276 |
+
def update_visibility(step):
|
| 277 |
+
return {
|
| 278 |
+
step1: gr.update(visible=(step == 1)),
|
| 279 |
+
step2: gr.update(visible=(step == 2)),
|
| 280 |
+
step3: gr.update(visible=(step == 3)),
|
| 281 |
+
step4: gr.update(visible=(step == 4)),
|
| 282 |
+
step5: gr.update(visible=(step == 5)),
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
# Event handlers
|
| 286 |
+
def next_step():
|
| 287 |
+
app_process.next_step()
|
| 288 |
+
return app_process.get_current_step_info(), update_visibility(app_process.current_step)
|
| 289 |
+
|
| 290 |
+
def prev_step():
|
| 291 |
+
app_process.previous_step()
|
| 292 |
+
return app_process.get_current_step_info(), update_visibility(app_process.current_step)
|
| 293 |
+
|
| 294 |
+
next_button.click(next_step, outputs=[current_step, step1, step2, step3, step4, step5])
|
| 295 |
+
prev_button.click(prev_step, outputs=[current_step, step1, step2, step3, step4, step5])
|
| 296 |
+
|
| 297 |
+
# Step 1: Initialize App
|
| 298 |
+
def init_app(name):
|
| 299 |
+
app_process.app_name = name
|
| 300 |
+
return f"App '{name}' initialized."
|
| 301 |
+
|
| 302 |
+
init_app_button.click(init_app, inputs=[app_name_input], outputs=[components_display])
|
| 303 |
+
|
| 304 |
+
# Step 2: Add Component
|
| 305 |
+
add_component_button.click(app_process.add_component, inputs=[component_type], outputs=[components_display])
|
| 306 |
+
|
| 307 |
+
# Step 3: Set Property
|
| 308 |
+
set_property_button.click(app_process.set_component_property, inputs=[component_id, property_name, property_value], outputs=[components_display])
|
| 309 |
+
|
| 310 |
+
# Step 4: Generate Code
|
| 311 |
+
generate_code_button.click(app_process.generate_python_code, outputs=[generated_code])
|
| 312 |
+
|
| 313 |
+
# Step 5: Deploy
|
| 314 |
+
deploy_button.click(app_process.deploy_to_huggingface, outputs=[deployment_status])
|
| 315 |
+
|
| 316 |
+
# Existing chat and terminal functionality
|
| 317 |
+
chat_button.click(run_chat, inputs=[chat_input, chat_history], outputs=[chat_history, terminal_output])
|
| 318 |
+
terminal_button.click(run_terminal_command, inputs=[terminal_input, terminal_output], outputs=[terminal_output])
|
| 319 |
|
| 320 |
iface.launch()
|