Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,22 +4,16 @@ import random
|
|
4 |
import json
|
5 |
from datetime import datetime
|
6 |
|
7 |
-
from huggingface_hub import
|
8 |
-
InferenceClient,
|
9 |
-
cached_download,
|
10 |
-
hf_hub_url
|
11 |
-
)
|
12 |
import gradio as gr
|
13 |
|
14 |
from safe_search import safe_search
|
15 |
-
from i_search import google
|
16 |
-
from i_search import i_search as i_s
|
17 |
|
18 |
from agent import (
|
19 |
ACTION_PROMPT,
|
20 |
ADD_PROMPT,
|
21 |
COMPRESS_HISTORY_PROMPT,
|
22 |
-
|
23 |
LOG_PROMPT,
|
24 |
LOG_RESPONSE,
|
25 |
MODIFY_PROMPT,
|
@@ -28,278 +22,219 @@ from agent import (
|
|
28 |
READ_PROMPT,
|
29 |
TASK_PROMPT,
|
30 |
UNDERSTAND_TEST_RESULTS_PROMPT,
|
31 |
-
)
|
32 |
-
|
33 |
-
from utils import (
|
34 |
-
parse_action,
|
35 |
-
parse_file_content,
|
36 |
-
read_python_module_structure
|
37 |
-
)
|
38 |
-
from datetime import datetime
|
39 |
-
import json
|
40 |
-
|
41 |
-
#--- Global Variables for App State ---
|
42 |
-
app_state = {"components": []}
|
43 |
-
|
44 |
-
terminal_history = ""
|
45 |
-
#--- Component Library ---
|
46 |
-
components_registry = {
|
47 |
-
"Button": {
|
48 |
-
"properties": {
|
49 |
-
"label": "Click Me",
|
50 |
-
"onclick": ""
|
51 |
-
},
|
52 |
-
"description": "A clickable button",
|
53 |
-
"code_snippet": "gr.Button(value='{{label}}', variant='primary')"
|
54 |
-
},
|
55 |
-
"Text Input": {
|
56 |
-
"properties": {
|
57 |
-
"value": "",
|
58 |
-
"placeholder": "Enter text"
|
59 |
-
},
|
60 |
-
"description": "A field for entering text",
|
61 |
-
"code_snippet": "gr.Textbox(label='{{placeholder}}')"
|
62 |
-
},
|
63 |
-
"Image": {
|
64 |
-
"properties": {
|
65 |
-
"src": "#",
|
66 |
-
"alt": "Image"
|
67 |
-
},
|
68 |
-
"description": "Displays an image",
|
69 |
-
"code_snippet": "gr.Image(label='{{alt}}')"
|
70 |
-
},
|
71 |
-
"Dropdown": {
|
72 |
-
"properties": {
|
73 |
-
"choices": ["Option 1", "Option 2"],
|
74 |
-
"value": ""
|
75 |
-
},
|
76 |
-
"description": "A dropdown menu for selecting options",
|
77 |
-
"code_snippet": "gr.Dropdown(choices={{choices}}, label='Dropdown')"
|
78 |
-
}
|
79 |
-
}
|
80 |
-
|
81 |
-
#--- NLP Model (Example using Hugging Face) ---
|
82 |
-
nlp_model_names = [
|
83 |
-
"google/flan-t5-small",
|
84 |
-
"Qwen/CodeQwen1.5-7B-Chat-GGUF",
|
85 |
-
"bartowski/Codestral-22B-v0.1-GGUF",
|
86 |
-
"bartowski/AutoCoder-GGUF"
|
87 |
-
]
|
88 |
-
|
89 |
-
nlp_models = []
|
90 |
-
|
91 |
-
for nlp_model_name in nlp_model_names:
|
92 |
-
try:
|
93 |
-
cached_download(hf_hub_url(nlp_model_name, revision="main"))
|
94 |
-
nlp_models.append(InferenceClient(nlp_model_name))
|
95 |
-
except:
|
96 |
-
nlp_models.append(None)
|
97 |
-
|
98 |
-
#--- Function to get NLP model response ---
|
99 |
-
def get_nlp_response(input_text, model_index):
|
100 |
-
if nlp_models[model_index]:
|
101 |
-
response = nlp_models[model_index].text_generation(input_text)
|
102 |
-
return response.generated_text
|
103 |
-
else:
|
104 |
-
return "NLP model not available."
|
105 |
-
|
106 |
-
# --- Component Class ---
|
107 |
-
class Component:
|
108 |
-
def __init__(self, type, properties=None, id=None):
|
109 |
-
self.id = id or random.randint(1000, 9999)
|
110 |
-
self.type = type
|
111 |
-
self.properties = properties or components_registry[type]["properties"].copy()
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
}
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
**self.properties
|
131 |
-
)
|
132 |
-
# --- Function to update the app canvas (for preview) ---
|
133 |
-
def update_app_canvas():
|
134 |
-
components_html = "".join( [ f"<div>Component ID: {component['id']}, Type: {component['type']}, Properties: {component['properties']}</div>" for component in app_state["components"] ] )
|
135 |
-
return components_html
|
136 |
-
|
137 |
-
# --- Function to handle component addition ---
|
138 |
-
def add_component(component_type):
|
139 |
-
if component_type in components_registry:
|
140 |
-
new_component = Component(component_type)
|
141 |
-
app_state["components"].append(new_component.to_dict())
|
142 |
-
return (
|
143 |
-
update_app_canvas(),
|
144 |
-
f"System: Added component: {component_type}\n",
|
145 |
-
)
|
146 |
-
else:
|
147 |
-
return None, f"Error: Invalid component type: {component_type}\n"
|
148 |
-
|
149 |
-
# --- Function to handle terminal input ---
|
150 |
-
def run_terminal_command(command, history):
|
151 |
-
global terminal_history
|
152 |
-
output = ""
|
153 |
-
try:
|
154 |
-
# Basic command parsing (expand with NLP)
|
155 |
-
if command.startswith("add "):
|
156 |
-
component_type = command.split("add ", 1)[1].strip()
|
157 |
-
_, output = add_component(component_type)
|
158 |
-
elif command.startswith("set "):
|
159 |
-
_, output = set_component_property(command)
|
160 |
-
elif command.startswith("search "):
|
161 |
-
search_query = command.split("search ", 1)[1].strip()
|
162 |
-
output = i_s(search_query)
|
163 |
-
elif command.startswith("deploy "):
|
164 |
-
app_name = command.split("deploy ", 1)[1].strip()
|
165 |
-
output = deploy_to_huggingface(app_name)
|
166 |
-
else:
|
167 |
-
# Attempt to execute command as Python code
|
168 |
try:
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
def
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
if
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
None,
|
208 |
-
f"Error: Property '{property_name}' not found in component {component_id}\n",
|
209 |
-
)
|
210 |
-
if not component_found:
|
211 |
return (
|
212 |
-
|
213 |
-
f"
|
214 |
)
|
215 |
-
|
216 |
-
except Exception as e:
|
217 |
-
return None, f"Error: Invalid 'set' command format or error setting property: {str(e)}\n"
|
218 |
-
|
219 |
-
#--- Function to handle chat interaction ---
|
220 |
-
def run_chat(message, history):
|
221 |
-
global terminal_history
|
222 |
-
if message.startswith("!"):
|
223 |
-
command = message[1:]
|
224 |
-
terminal_history = run_terminal_command(command, history)
|
225 |
-
else:
|
226 |
-
model_index = 0 # Select the model to use for chat response
|
227 |
-
response = get_nlp_response(message, model_index)
|
228 |
-
if response:
|
229 |
-
return history, terminal_history + f"User: {message}\nAssistant: {response}"
|
230 |
else:
|
231 |
-
return
|
232 |
-
|
233 |
-
def
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
)
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
)
|
304 |
-
|
305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
9 |
|
10 |
from safe_search import safe_search
|
11 |
+
from i_search import google, i_search as i_s
|
|
|
12 |
|
13 |
from agent import (
|
14 |
ACTION_PROMPT,
|
15 |
ADD_PROMPT,
|
16 |
COMPRESS_HISTORY_PROMPT,
|
|
|
17 |
LOG_PROMPT,
|
18 |
LOG_RESPONSE,
|
19 |
MODIFY_PROMPT,
|
|
|
22 |
READ_PROMPT,
|
23 |
TASK_PROMPT,
|
24 |
UNDERSTAND_TEST_RESULTS_PROMPT,
|
25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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": []}
|
33 |
+
self.terminal_history = ""
|
34 |
+
self.components_registry = {
|
35 |
+
"Button": {
|
36 |
+
"properties": {
|
37 |
+
"label": "Click Me",
|
38 |
+
"onclick": ""
|
39 |
+
},
|
40 |
+
"description": "A clickable button",
|
41 |
+
"code_snippet": "gr.Button(value='{{label}}', variant='primary')"
|
42 |
+
},
|
43 |
+
"Text Input": {
|
44 |
+
"properties": {
|
45 |
+
"value": "",
|
46 |
+
"placeholder": "Enter text"
|
47 |
+
},
|
48 |
+
"description": "A field for entering text",
|
49 |
+
"code_snippet": "gr.Textbox(label='{{placeholder}}')"
|
50 |
+
},
|
51 |
+
"Image": {
|
52 |
+
"properties": {
|
53 |
+
"src": "#",
|
54 |
+
"alt": "Image"
|
55 |
+
},
|
56 |
+
"description": "Displays an image",
|
57 |
+
"code_snippet": "gr.Image(label='{{alt}}')"
|
58 |
+
},
|
59 |
+
"Dropdown": {
|
60 |
+
"properties": {
|
61 |
+
"choices": ["Option 1", "Option 2"],
|
62 |
+
"value": ""
|
63 |
+
},
|
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",
|
71 |
+
"bartowski/Codestral-22B-v0.1-GGUF",
|
72 |
+
"bartowski/AutoCoder-GGUF"
|
73 |
+
]
|
74 |
+
self.nlp_models = []
|
75 |
+
self.initialize_nlp_models()
|
76 |
+
|
77 |
+
def initialize_nlp_models(self):
|
78 |
+
for nlp_model_name in self.nlp_model_names:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
try:
|
80 |
+
cached_download(hf_hub_url(nlp_model_name, revision="main"))
|
81 |
+
self.nlp_models.append(InferenceClient(nlp_model_name))
|
82 |
+
except:
|
83 |
+
self.nlp_models.append(None)
|
84 |
+
|
85 |
+
def get_nlp_response(self, input_text, model_index):
|
86 |
+
if self.nlp_models[model_index]:
|
87 |
+
response = self.nlp_models[model_index].text_generation(input_text)
|
88 |
+
return response.generated_text
|
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 = ""
|
127 |
+
try:
|
128 |
+
if command.startswith("add "):
|
129 |
+
component_type = command.split("add ")[1]
|
130 |
+
return self.add_component(component_type)
|
131 |
+
elif command.startswith("search "):
|
132 |
+
query = command.split("search ")[1]
|
133 |
+
return google(query)
|
134 |
+
elif command.startswith("i search "):
|
135 |
+
query = command.split("i search ")[1]
|
136 |
+
return i_s(query)
|
137 |
+
elif command.startswith("safe search "):
|
138 |
+
query = command.split("safesearch ")[1]
|
139 |
+
return safe_search(query)
|
140 |
+
elif command.startswith("read "):
|
141 |
+
file_path = command.split("read ")[1]
|
142 |
+
return parse_file_content(file_path)
|
143 |
+
elif command == "task":
|
144 |
+
return TASK_PROMPT
|
145 |
+
elif command == "modify":
|
146 |
+
return MODIFY_PROMPT
|
147 |
+
elif command == "log":
|
148 |
+
return LOG_PROMPT
|
149 |
+
elif command.startswith("understand test results "):
|
150 |
+
test_results = command.split("understand test results ")[1]
|
151 |
+
return self.understand_test_results(test_results)
|
152 |
+
elif command.startswith("compress history"):
|
153 |
+
return self.compress_history(history)
|
154 |
+
elif command == "help":
|
155 |
+
return self.get_help_message()
|
156 |
+
elif command == "exit":
|
157 |
+
exit()
|
158 |
+
else:
|
159 |
+
output = subprocess.check_output(command, shell=True).decode("utf-8")
|
160 |
+
except Exception as e:
|
161 |
+
output = str(e)
|
162 |
+
return output or "No output\n"
|
163 |
+
|
164 |
+
def compress_history(self, history):
|
165 |
+
compressed_history = ""
|
166 |
+
lines = history.strip().split("\n")
|
167 |
+
for line in lines:
|
168 |
+
if not line.strip().startswith("#"):
|
169 |
+
compressed_history += line + "\n"
|
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):
|
177 |
+
return """
|
178 |
+
Available commands:
|
179 |
+
- add [component_type]: Add a component to the app canvas
|
180 |
+
- search [query]: Perform a Google search
|
181 |
+
- i search [query]: Perform an intelligent search
|
182 |
+
- safe search [query]: Perform a safe search
|
183 |
+
- read [file_path]: Read and parse the content of a Python module
|
184 |
+
- task: Prompt for a task to perform
|
185 |
+
- modify: Prompt to modify a component property
|
186 |
+
- log: Prompt to log a response
|
187 |
+
- understand test results [test_results]: Understand test results
|
188 |
+
- compress history: Compress the terminal history by removing comments
|
189 |
+
- help: Show this help message
|
190 |
+
- exit: Exit the program
|
191 |
+
"""
|
192 |
+
|
193 |
+
def process_input(self, input_text):
|
194 |
+
if input_text.strip().startswith("/"):
|
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)
|
202 |
+
component_id, action, property_name, property_value = parse_action(response)
|
203 |
+
if component_id:
|
204 |
+
component = next((comp for comp in self.app_state["components"] if comp["id"] == component_id), None)
|
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 run(self):
|
226 |
+
print("Welcome to the Python App Builder!")
|
227 |
+
print("Type 'help' to see the available commands.")
|
228 |
+
print("-" * 50)
|
229 |
+
while True:
|
230 |
+
input_text = input("Enter input: ")
|
231 |
+
output, system_message = self.process_input(input_text)
|
232 |
+
if output:
|
233 |
+
print(output)
|
234 |
+
if system_message:
|
235 |
+
print(system_message)
|
236 |
+
|
237 |
+
|
238 |
+
if __name__ == "__main__":
|
239 |
+
app = App()
|
240 |
+
app.run()
|