acecalisto3 commited on
Commit
0843d32
·
verified ·
1 Parent(s): ee86491

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -63
app.py CHANGED
@@ -26,7 +26,6 @@ from agent import (
26
 
27
  from utils import parse_action, parse_file_content, read_python_module_structure
28
 
29
-
30
  class App:
31
  def __init__(self):
32
  self.app_state = {"components": []}
@@ -64,7 +63,7 @@ class App:
64
  "description": "A dropdown menu for selecting options",
65
  "code_snippet": "gr.Dropdown(choices={{choices}}, label='Dropdown')"
66
  }
67
- }
68
  self.nlp_model_names = [
69
  "google/flan-t5-small",
70
  "Qwen/CodeQwen1.5-7B-Chat-GGUF",
@@ -89,38 +88,19 @@ class App:
89
  else:
90
  return "NLP model not available."
91
 
92
- class Component:
93
- def __init__(self, type, properties=None, id=None):
94
- self.id = id or random.randint(1000, 9999)
95
- self.type = type
96
- self.properties = properties or self.components_registry[type]["properties"].copy()
97
-
98
- def to_dict(self):
99
- return {
100
- "id": self.id,
101
- "type": self.type,
102
- "properties": self.properties,
103
- }
104
-
105
- def render(self):
106
- if self.type == "Dropdown":
107
- self.properties["choices"] = str(self.properties["choices"]).replace("[", "").replace("]", "").replace("'", "")
108
- return self.components_registry[self.type]["code_snippet"].format(**self.properties)
109
-
110
  def update_app_canvas(self):
111
  components_html = "".join([f"<div>Component ID: {component['id']}, Type: {component['type']}, Properties: {component['properties']}</div>" for component in self.app_state["components"]])
112
  return components_html
113
 
114
- def add_component(self, component_type):
115
- if component_type in self.components_registry:
116
- new_component = self.Component(component_type)
117
- self.app_state["components"].append(new_component.to_dict())
118
- return (
119
- self.update_app_canvas(),
120
- f"System: Added component: {component_type}\n",
121
- )
122
- else:
123
- return None, f"Error: Invalid component type: {component_type}\n"
124
 
125
  def run_terminal_command(self, command, history):
126
  output = ""
@@ -170,7 +150,6 @@ class App:
170
  return compressed_history
171
 
172
  def understand_test_results(self, test_results):
173
- # Logic to understand test results
174
  return UNDERSTAND_TEST_RESULTS_PROMPT
175
 
176
  def get_help_message(self):
@@ -195,7 +174,7 @@ class App:
195
  command = input_text.strip().lstrip("/")
196
  output = self.run_terminal_command(command, self.terminal_history)
197
  self.terminal_history += f"{input_text}\n{output}\n"
198
- return output
199
  else:
200
  model_index = random.randint(0, len(self.nlp_models)-1)
201
  response = self.get_nlp_response(input_text, model_index)
@@ -205,22 +184,16 @@ class App:
205
  if component:
206
  if action == "update":
207
  component["properties"][property_name] = property_value
208
- return (
209
- self.update_app_canvas(),
210
- f"System: Updated property '{property_name}' of component with ID {component_id}\n",
211
- )
212
  elif action == "remove":
213
  self.app_state["components"].remove(component)
214
- return (
215
- self.update_app_canvas(),
216
- f"System: Removed component with ID {component_id}\n",
217
- )
218
  else:
219
- return None, f"Error: Invalid action: {action}\n"
220
  else:
221
- return None, f"Error: Component with ID {component_id} not found\n"
222
  else:
223
- return None, f"Error: Failed to parse action from NLP response\n"
224
 
225
  def build_app(self):
226
  with gr.Blocks() as demo:
@@ -237,15 +210,15 @@ class App:
237
  elif component_type == "Dropdown":
238
  gr.Dropdown(choices=properties["choices"], label="Dropdown")
239
 
240
- with gr.Tab("Components"):
241
- gr.Markdown("## Available Components")
242
- component_type = gr.Dropdown(label="Component Type", choices=list(self.components_registry.keys()))
243
- properties = gr.JSON(label="Properties")
244
- add_component_button = gr.Button("Add Component")
245
- add_component_button.click(
246
- self.add_component,
247
- inputs=[component_type, properties],
248
- outputs=[]
249
  )
250
 
251
  with gr.Tab("NLP Models"):
@@ -258,20 +231,64 @@ class App:
258
  inputs=[input_text, model_index],
259
  outputs="text"
260
  )
261
-
262
- with gr.Tab("Terminal"):
263
- gr.Markdown("## Terminal")
264
- terminal_input = gr.Textbox(label="Input")
265
- terminal_output = gr.Textbox(label="Output")
266
- run_terminal_command_button = gr.Button("Run Command")
267
- run_terminal_command_button.click(
268
- self.run_terminal_command,
269
- inputs=[terminal_input],
270
- outputs=[terminal_output]
271
- )
272
 
273
  return demo
274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  def run(self):
276
  self._print_welcome_message()
277
 
 
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": []}
 
63
  "description": "A dropdown menu for selecting options",
64
  "code_snippet": "gr.Dropdown(choices={{choices}}, label='Dropdown')"
65
  }
66
+ }
67
  self.nlp_model_names = [
68
  "google/flan-t5-small",
69
  "Qwen/CodeQwen1.5-7B-Chat-GGUF",
 
88
  else:
89
  return "NLP model not available."
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  def update_app_canvas(self):
92
  components_html = "".join([f"<div>Component ID: {component['id']}, Type: {component['type']}, Properties: {component['properties']}</div>" for component in self.app_state["components"]])
93
  return components_html
94
 
95
+ def add_component(self, component_type, properties=None):
96
+ try:
97
+ new_component = {
98
+ "type": component_type,
99
+ "properties": properties or {}
100
+ }
101
+ self.app_state["components"].append(new_component)
102
+ except Exception as e:
103
+ print(f"Error adding component: {e}")
 
104
 
105
  def run_terminal_command(self, command, history):
106
  output = ""
 
150
  return compressed_history
151
 
152
  def understand_test_results(self, test_results):
 
153
  return UNDERSTAND_TEST_RESULTS_PROMPT
154
 
155
  def get_help_message(self):
 
174
  command = input_text.strip().lstrip("/")
175
  output = self.run_terminal_command(command, self.terminal_history)
176
  self.terminal_history += f"{input_text}\n{output}\n"
177
+ return output, ""
178
  else:
179
  model_index = random.randint(0, len(self.nlp_models)-1)
180
  response = self.get_nlp_response(input_text, model_index)
 
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:
 
210
  elif component_type == "Dropdown":
211
  gr.Dropdown(choices=properties["choices"], label="Dropdown")
212
 
213
+ with gr.Tab("Terminal"):
214
+ gr.Markdown("## Terminal")
215
+ terminal_input = gr.Textbox(label="Input")
216
+ terminal_output = gr.Textbox(label="Output")
217
+ run_terminal_command_button = gr.Button("Run Command")
218
+ run_terminal_command_button.click(
219
+ self.run_terminal_command,
220
+ inputs=[terminal_input],
221
+ outputs=[terminal_output]
222
  )
223
 
224
  with gr.Tab("NLP Models"):
 
231
  inputs=[input_text, model_index],
232
  outputs="text"
233
  )
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  return demo
236
 
237
+ def run(self):
238
+ self._print_welcome_message()
239
+
240
+ while True:
241
+ try:
242
+ input_text = self._get_user_input()
243
+ if input_text.lower() == 'exit':
244
+ break
245
+ elif input_text.lower() == 'launch':
246
+ self.launch_app()
247
+ continue
248
+
249
+ output, system_message = self.process_input(input_text)
250
+ self._display_output(output, system_message)
251
+
252
+ except EOFError:
253
+ print("Error: Input reading interrupted. Please provide valid input.")
254
+ except KeyboardInterrupt:
255
+ print("\nApplication stopped by user.")
256
+ break
257
+ except Exception as e:
258
+ print(f"An error occurred: {str(e)}")
259
+
260
+ def _print_welcome_message(self):
261
+ print("Welcome to the Python App Builder!")
262
+ print("Type 'help' to see the available commands.")
263
+ print("Type 'launch' to build
264
+ print("Type 'launch' to build and launch the Gradio app.")
265
+ print("Type 'exit' to quit the application.")
266
+ print("-" * 50)
267
+
268
+ def _get_user_input(self):
269
+ return input("Enter input: ").strip()
270
+
271
+ def _display_output(self, output, system_message):
272
+ if output:
273
+ print(output)
274
+ if system_message:
275
+ print(system_message)
276
+
277
+ def launch_app(self):
278
+ demo = self.build_app()
279
+ demo.launch()
280
+
281
+ def main():
282
+ try:
283
+ app = App()
284
+ demo = app.build_app()
285
+ demo.launch()
286
+ except Exception as e:
287
+ print(f"Error launching app: {e}")
288
+
289
+ if __name__ == "__main__":
290
+ main()
291
+
292
  def run(self):
293
  self._print_welcome_message()
294