Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,429 +1,452 @@
|
|
1 |
-
from
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
from
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
PREFIX,
|
20 |
-
READ_PROMPT,
|
21 |
-
TASK_PROMPT,
|
22 |
-
UNDERSTAND_TEST_RESULTS_PROMPT,
|
23 |
-
WEB_DEV_SYSTEM_PROMPT,
|
24 |
-
AI_SYSTEM_PROMPT,
|
25 |
-
WEB_DEV,
|
26 |
-
PYTHON_CODE_DEV,
|
27 |
-
HUGGINGFACE_FILE_DEV
|
28 |
-
)
|
29 |
-
from utils import (
|
30 |
-
parse_action,
|
31 |
-
parse_file_content,
|
32 |
-
read_python_module_structure,
|
33 |
-
extract_imports, # Unused import, consider removing or using
|
34 |
-
get_file, # Unused import, consider removing or using
|
35 |
-
)
|
36 |
|
37 |
# --- Constants ---
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
)
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
if action_name == "COMPLETE":
|
145 |
-
return "Task completed."
|
146 |
-
|
147 |
-
if len(self.history.split("\n")) > MAX_HISTORY:
|
148 |
-
if VERBOSE:
|
149 |
-
print("COMPRESSING HISTORY")
|
150 |
-
self.compress_history()
|
151 |
-
|
152 |
-
action_funcs = {
|
153 |
-
"MAIN": self.call_main,
|
154 |
-
"UPDATE-TASK": self.call_set_task,
|
155 |
-
"MODIFY-FILE": self.call_modify,
|
156 |
-
"READ-FILE": self.call_read,
|
157 |
-
"ADD-FILE": self.call_add,
|
158 |
-
"TEST": self.call_test,
|
159 |
-
}
|
160 |
-
|
161 |
-
if action_name not in action_funcs:
|
162 |
-
return f"Unknown action: {action_name}"
|
163 |
-
|
164 |
-
print(f"RUN: {action_name} {action_input}")
|
165 |
-
return action_funcs[action_name](action_input)
|
166 |
-
|
167 |
-
def call_main(self, action_input: str) -> str:
|
168 |
-
resp = self.run_gpt(
|
169 |
-
ACTION_PROMPT,
|
170 |
-
stop_tokens=["observation:", "task:"],
|
171 |
-
max_tokens=256,
|
172 |
-
task=self.task,
|
173 |
-
history=self.history,
|
174 |
)
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
if line.startswith("thought: "):
|
180 |
-
self.history += f"{line}\n"
|
181 |
-
elif line.startswith("action: "):
|
182 |
-
action_name, action_input = parse_action(line)
|
183 |
-
self.history += f"{line}\n"
|
184 |
-
return self.run_action(action_name, action_input)
|
185 |
-
return "No valid action found."
|
186 |
-
|
187 |
-
def call_set_task(self, action_input: str) -> str:
|
188 |
-
self.task = (
|
189 |
-
self.run_gpt(
|
190 |
-
TASK_PROMPT,
|
191 |
-
stop_tokens=[],
|
192 |
-
max_tokens=64,
|
193 |
-
task=self.task,
|
194 |
-
history=self.history,
|
195 |
-
)
|
196 |
-
.strip("\n")
|
197 |
-
.strip()
|
198 |
)
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
if not os.path.exists(action_input):
|
204 |
-
self.history += "observation: file does not exist\n"
|
205 |
-
return "File does not exist."
|
206 |
-
|
207 |
-
content = read_python_module_structure(self.directory)[1]
|
208 |
-
f_content = (
|
209 |
-
content[action_input]
|
210 |
-
if content[action_input]
|
211 |
-
else "< document is empty >"
|
212 |
)
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
max_tokens=2048,
|
218 |
-
task=self.task,
|
219 |
-
history=self.history,
|
220 |
-
file_path=action_input,
|
221 |
-
file_contents=f_content,
|
222 |
)
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
with open(action_input, "w") as f:
|
229 |
-
f.write(new_contents)
|
230 |
-
|
231 |
-
self.history += f"observation: file successfully modified\n"
|
232 |
-
self.history += f"observation: {description}\n"
|
233 |
-
return f"File modified: {action_input}"
|
234 |
-
|
235 |
-
def call_read(self, action_input: str) -> str:
|
236 |
-
if not os.path.exists(action_input):
|
237 |
-
self.history += "observation: file does not exist\n"
|
238 |
-
return "File does not exist."
|
239 |
-
|
240 |
-
content = read_python_module_structure(self.directory)[1]
|
241 |
-
f_content = (
|
242 |
-
content[action_input]
|
243 |
-
if content[action_input]
|
244 |
-
else "< document is empty >"
|
245 |
)
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
max_tokens=256,
|
251 |
-
task=self.task,
|
252 |
-
history=self.history,
|
253 |
-
file_path=action_input,
|
254 |
-
file_contents=f_content,
|
255 |
-
).strip("\n")
|
256 |
-
self.history += f"observation: {resp}\n"
|
257 |
-
return f"File read: {action_input}"
|
258 |
-
|
259 |
-
def call_add(self, action_input: str) -> str:
|
260 |
-
d = os.path.dirname(action_input)
|
261 |
-
if not d.startswith(self.directory):
|
262 |
-
self.history += (
|
263 |
-
f"observation: files must be under directory {self.directory}\n"
|
264 |
-
)
|
265 |
-
return f"Invalid directory: {d}"
|
266 |
-
elif not action_input.endswith(".py"):
|
267 |
-
self.history += "observation: can only write .py files\n"
|
268 |
-
return "Only .py files are allowed."
|
269 |
-
else:
|
270 |
-
if d and not os.path.exists(d):
|
271 |
-
os.makedirs(d)
|
272 |
-
if not os.path.exists(action_input):
|
273 |
-
resp = self.run_gpt(
|
274 |
-
ADD_PROMPT,
|
275 |
-
stop_tokens=["action:", "thought:", "observation:"],
|
276 |
-
max_tokens=2048,
|
277 |
-
task=self.task,
|
278 |
-
history=self.history,
|
279 |
-
file_path=action_input,
|
280 |
-
)
|
281 |
-
new_contents, description = parse_file_content(resp)
|
282 |
-
if new_contents is None:
|
283 |
-
self.history += "observation: failed to write file\n"
|
284 |
-
return "Failed to write file."
|
285 |
-
|
286 |
-
with open(action_input, "w") as f:
|
287 |
-
f.write(new_contents)
|
288 |
-
|
289 |
-
self.history += "observation: file successfully written\n"
|
290 |
-
self.history += f"observation: {description}\n"
|
291 |
-
return f"File added: {action_input}"
|
292 |
-
else:
|
293 |
-
self.history += "observation: file already exists\n"
|
294 |
-
return "File already exists."
|
295 |
-
|
296 |
-
def call_test(self, action_input: str) -> str:
|
297 |
-
result = subprocess.run(
|
298 |
-
["python", "-m", "pytest", "--collect-only", self.directory],
|
299 |
-
capture_output=True,
|
300 |
-
text=True,
|
301 |
)
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
309 |
)
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
322 |
)
|
323 |
-
self.history += f"observation: tests failed: {resp}\n"
|
324 |
-
return f"Tests failed: {resp}"
|
325 |
-
|
326 |
-
|
327 |
-
# --- Global Pypelyne Instance ---
|
328 |
-
pypelyne = Pypelyne()
|
329 |
-
|
330 |
-
|
331 |
-
# --- Helper Functions ---
|
332 |
-
def create_agent(name: str, agent_type: str, complexity: int) -> Agent:
|
333 |
-
agent = Agent(name, agent_type, complexity)
|
334 |
-
pypelyne.add_agent(agent)
|
335 |
-
return agent
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
|
344 |
-
#
|
345 |
-
|
346 |
-
|
|
|
|
|
|
|
347 |
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
value=st.session_state.directory,
|
355 |
-
help="Path to your coding project",
|
356 |
-
)
|
357 |
-
st.session_state.directory = pypelyne.directory # Update session state
|
358 |
-
if "purpose" not in st.session_state:
|
359 |
-
st.session_state.purpose = ""
|
360 |
-
pypelyne.purpose = st.sidebar.text_area(
|
361 |
-
"Project Purpose:",
|
362 |
-
value=st.session_state.purpose,
|
363 |
-
help="Describe the purpose of your coding project.",
|
364 |
-
)
|
365 |
-
st.session_state.purpose = pypelyne.purpose # Update session state
|
366 |
-
|
367 |
-
# --- Agent and Tool Management ---
|
368 |
-
st.sidebar.header("🤖 Agents")
|
369 |
-
if "agents" not in st.session_state:
|
370 |
-
st.session_state.agents = []
|
371 |
-
show_agent_creation = st.sidebar.expander(
|
372 |
-
"Create New Agent", expanded=False
|
373 |
-
)
|
374 |
-
with show_agent_creation:
|
375 |
-
agent_name = st.text_input("Agent Name:")
|
376 |
-
agent_type = st.selectbox("Agent Type:", AGENT_TYPES)
|
377 |
-
agent_complexity = st.slider("Complexity (1-5):", 1, 5, 3)
|
378 |
-
if st.button("Add Agent"):
|
379 |
-
create_agent(agent_name, agent_type, agent_complexity)
|
380 |
-
st.session_state.agents = pypelyne.agents # Update session state
|
381 |
-
|
382 |
-
st.sidebar.header("🛠️ Tools")
|
383 |
-
if "tools" not in st.session_state:
|
384 |
-
st.session_state.tools = []
|
385 |
-
show_tool_creation = st.sidebar.expander("Create New Tool", expanded=False)
|
386 |
-
with show_tool_creation:
|
387 |
-
tool_name = st.text_input("Tool Name:")
|
388 |
-
tool_type = st.selectbox("Tool Type:", TOOL_TYPES)
|
389 |
-
if st.button("Add Tool"):
|
390 |
-
create_tool(tool_name, tool_type)
|
391 |
-
st.session_state.tools = pypelyne.tools # Update session state
|
392 |
-
|
393 |
-
# --- Display Agents and Tools ---
|
394 |
-
st.sidebar.subheader("Active Agents:")
|
395 |
-
for agent in st.session_state.agents:
|
396 |
-
st.sidebar.write(f"- {agent}")
|
397 |
-
|
398 |
-
st.sidebar.subheader("Available Tools:")
|
399 |
-
for tool in st.session_state.tools:
|
400 |
-
st.sidebar.write(f"- {tool}")
|
401 |
-
|
402 |
-
# --- Main Content Area ---
|
403 |
-
st.header("💻 Code Interaction")
|
404 |
-
|
405 |
-
if "task" not in st.session_state:
|
406 |
-
st.session_state.task = ""
|
407 |
-
task_input = st.text_area(
|
408 |
-
"🎯 Task:",
|
409 |
-
value=st.session_state.task,
|
410 |
-
help="Describe the coding task you want to perform.",
|
411 |
-
)
|
412 |
-
if task_input:
|
413 |
-
pypelyne.task = task_input
|
414 |
-
st.session_state.task = pypelyne.task # Update session state
|
415 |
|
416 |
-
|
417 |
-
|
|
|
|
|
|
|
418 |
)
|
419 |
|
420 |
-
|
421 |
-
|
422 |
-
with st.spinner("Pypelyne is working..."):
|
423 |
-
response = pypelyne.run_action("MAIN", user_input)
|
424 |
-
st.write("Pypelyne Says: ", response)
|
425 |
-
|
426 |
|
427 |
-
# ---
|
428 |
-
|
429 |
-
main()
|
|
|
1 |
+
from huggingface_hub import InferenceClient, hf_hub_url
|
2 |
+
import gradio as gr
|
3 |
+
import random
|
4 |
+
import os
|
5 |
+
import subprocess
|
6 |
+
import threading
|
7 |
+
import time
|
8 |
+
import shutil
|
9 |
+
from typing import Dict, Tuple, List
|
10 |
+
import json
|
11 |
+
from rich import print as rprint
|
12 |
+
from rich.panel import Panel
|
13 |
+
from rich.progress import track
|
14 |
+
from rich.table import Table
|
15 |
+
from rich.prompt import Prompt, Confirm
|
16 |
+
from rich.markdown import Markdown
|
17 |
+
from rich.traceback import install
|
18 |
+
install() # Enable rich tracebacks for easier debugging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# --- Constants ---
|
21 |
+
|
22 |
+
API_URL = "https://api-inference.huggingface.co/models/"
|
23 |
+
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1" # Replace with your desired model
|
24 |
+
|
25 |
+
# Chat Interface Parameters
|
26 |
+
DEFAULT_TEMPERATURE = 0.9
|
27 |
+
DEFAULT_MAX_NEW_TOKENS = 2048
|
28 |
+
DEFAULT_TOP_P = 0.95
|
29 |
+
DEFAULT_REPETITION_PENALTY = 1.2
|
30 |
+
|
31 |
+
# Local Server
|
32 |
+
LOCAL_HOST_PORT = 7860
|
33 |
+
|
34 |
+
# --- Agent Roles ---
|
35 |
+
|
36 |
+
agent_roles: Dict[str, Dict[str, bool]] = {
|
37 |
+
"Web Developer": {"description": "A master of front-end and back-end web development.", "active": False},
|
38 |
+
"Prompt Engineer": {"description": "An expert in crafting effective prompts for AI models.", "active": False},
|
39 |
+
"Python Code Developer": {"description": "A skilled Python programmer who can write clean and efficient code.", "active": False},
|
40 |
+
"Hugging Face Hub Expert": {"description": "A specialist in navigating and utilizing the Hugging Face Hub.", "active": False},
|
41 |
+
"AI-Powered Code Assistant": {"description": "An AI assistant that can help with coding tasks and provide code snippets.", "active": False},
|
42 |
+
}
|
43 |
+
|
44 |
+
# --- Initial Prompt ---
|
45 |
+
|
46 |
+
selected_agent = list(agent_roles.keys())[0]
|
47 |
+
initial_prompt = f"""
|
48 |
+
You are an expert {selected_agent} who responds with complete program coding to client requests.
|
49 |
+
Using available tools, please explain the researched information.
|
50 |
+
Please don't answer based solely on what you already know. Always perform a search before providing a response.
|
51 |
+
In special cases, such as when the user specifies a page to read, there's no need to search.
|
52 |
+
Please read the provided page and answer the user's question accordingly.
|
53 |
+
If you find that there's not much information just by looking at the search results page, consider these two options and try them out:
|
54 |
+
- Try clicking on the links of the search results to access and read the content of each page.
|
55 |
+
- Change your search query and perform a new search.
|
56 |
+
Users are extremely busy and not as free as you are.
|
57 |
+
Therefore, to save the user's effort, please provide direct answers.
|
58 |
+
BAD ANSWER EXAMPLE
|
59 |
+
- Please refer to these pages.
|
60 |
+
- You can write code referring these pages.
|
61 |
+
- Following page will be helpful.
|
62 |
+
GOOD ANSWER EXAMPLE
|
63 |
+
- This is the complete code: -- complete code here --
|
64 |
+
- The answer of you question is -- answer here --
|
65 |
+
Please make sure to list the URLs of the pages you referenced at the end of your answer. (This will allow users to verify your response.)
|
66 |
+
Please make sure to answer in the language used by the user. If the user asks in Japanese, please answer in Japanese. If the user asks in Spanish, please answer in Spanish.
|
67 |
+
But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
|
68 |
+
"""
|
69 |
+
|
70 |
+
# --- Custom CSS ---
|
71 |
+
|
72 |
+
customCSS = """
|
73 |
+
#component-7 {
|
74 |
+
height: 1600px;
|
75 |
+
flex-grow: 4;
|
76 |
+
}
|
77 |
+
"""
|
78 |
+
|
79 |
+
# --- Functions ---
|
80 |
+
|
81 |
+
# Function to toggle the active state of an agent
|
82 |
+
def toggle_agent(agent_name: str) -> str:
|
83 |
+
"""Toggles the active state of an agent."""
|
84 |
+
global agent_roles
|
85 |
+
agent_roles[agent_name]["active"] = not agent_roles[agent_name]["active"]
|
86 |
+
return f"{agent_name} is now {'active' if agent_roles[agent_name]['active'] else 'inactive'}"
|
87 |
+
|
88 |
+
# Function to get the active agent cluster
|
89 |
+
def get_agent_cluster() -> Dict[str, bool]:
|
90 |
+
"""Returns a dictionary of active agents."""
|
91 |
+
return {agent: agent_roles[agent]["active"] for agent in agent_roles}
|
92 |
+
|
93 |
+
# Function to execute code
|
94 |
+
def run_code(code: str) -> str:
|
95 |
+
"""Executes the provided code and returns the output."""
|
96 |
+
try:
|
97 |
+
output = subprocess.check_output(
|
98 |
+
['python', '-c', code],
|
99 |
+
stderr=subprocess.STDOUT,
|
100 |
+
universal_newlines=True,
|
101 |
)
|
102 |
+
return output
|
103 |
+
except subprocess.CalledProcessError as e:
|
104 |
+
return f"Error: {e.output}"
|
105 |
+
|
106 |
+
# Function to format the prompt
|
107 |
+
def format_prompt(message: str, history: list[Tuple[str, str]], agent_roles: list[str]) -> str:
|
108 |
+
"""Formats the prompt with the selected agent roles and conversation history."""
|
109 |
+
prompt = f"""
|
110 |
+
You are an expert agent cluster, consisting of {', '.join(agent_roles)}.
|
111 |
+
Respond with complete program coding to client requests.
|
112 |
+
Using available tools, please explain the researched information.
|
113 |
+
Please don't answer based solely on what you already know. Always perform a search before providing a response.
|
114 |
+
In special cases, such as when the user specifies a page to read, there's no need to search.
|
115 |
+
Please read the provided page and answer the user's question accordingly.
|
116 |
+
If you find that there's not much information just by looking at the search results page, consider these two options and try them out:
|
117 |
+
- Try clicking on the links of the search results to access and read the content of each page.
|
118 |
+
- Change your search query and perform a new search.
|
119 |
+
Users are extremely busy and not as free as you are.
|
120 |
+
Therefore, to save the user's effort, please provide direct answers.
|
121 |
+
BAD ANSWER EXAMPLE
|
122 |
+
- Please refer to these pages.
|
123 |
+
- You can write code referring these pages.
|
124 |
+
- Following page will be helpful.
|
125 |
+
GOOD ANSWER EXAMPLE
|
126 |
+
- This is the complete code: -- complete code here --
|
127 |
+
- The answer of you question is -- answer here --
|
128 |
+
Please make sure to list the URLs of the pages you referenced at the end of your answer. (This will allow users to verify your response.)
|
129 |
+
Please make sure to answer in the language used by the user. If the user asks in Japanese, please answer in Japanese. If the user asks in Spanish, please answer in Spanish.
|
130 |
+
But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
|
131 |
+
"""
|
132 |
+
|
133 |
+
for user_prompt, bot_response in history:
|
134 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
135 |
+
prompt += f" {bot_response}</s> "
|
136 |
+
|
137 |
+
prompt += f"[INST] {message} [/INST]"
|
138 |
+
return prompt
|
139 |
+
|
140 |
+
# Function to generate a response
|
141 |
+
def generate(prompt: str, history: list[Tuple[str, str]], agent_roles: list[str], temperature: float = DEFAULT_TEMPERATURE, max_new_tokens: int = DEFAULT_MAX_NEW_TOKENS, top_p: float = DEFAULT_TOP_P, repetition_penalty: float = DEFAULT_REPETITION_PENALTY) -> str:
|
142 |
+
"""Generates a response using the selected agent roles and parameters."""
|
143 |
+
temperature = float(temperature)
|
144 |
+
if temperature < 1e-2:
|
145 |
+
temperature = 1e-2
|
146 |
+
top_p = float(top_p)
|
147 |
+
|
148 |
+
generate_kwargs = dict(
|
149 |
+
temperature=temperature,
|
150 |
+
max_new_tokens=max_new_tokens,
|
151 |
+
top_p=top_p,
|
152 |
+
repetition_penalty=repetition_penalty,
|
153 |
+
do_sample=True,
|
154 |
+
seed=random.randint(0, 10**7),
|
155 |
+
)
|
156 |
|
157 |
+
formatted_prompt = format_prompt(prompt, history, agent_roles)
|
158 |
+
|
159 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
160 |
+
output = ""
|
161 |
+
|
162 |
+
for response in stream:
|
163 |
+
output += response.token.text
|
164 |
+
yield output
|
165 |
+
return output
|
166 |
+
|
167 |
+
# Function to handle user input and generate responses
|
168 |
+
def chat_interface(message: str, history: list[Tuple[str, str]], agent_cluster: Dict[str, bool], temperature: float, max_new_tokens: int, top_p: float, repetition_penalty: float) -> Tuple[str, str]:
|
169 |
+
"""Handles user input and generates responses."""
|
170 |
+
rprint(f"[bold blue]User:[/bold blue] {message}") # Log user message
|
171 |
+
if message.startswith("python"):
|
172 |
+
# User entered code, execute it
|
173 |
+
code = message[9:-3]
|
174 |
+
output = run_code(code)
|
175 |
+
rprint(f"[bold green]Code Output:[/bold green] {output}") # Log code output
|
176 |
+
return (message, output)
|
177 |
+
else:
|
178 |
+
# User entered a normal message, generate a response
|
179 |
+
active_agents = [agent for agent, is_active in agent_cluster.items() if is_active]
|
180 |
+
response = generate(message, history, active_agents, temperature, max_new_tokens, top_p, repetition_penalty)
|
181 |
+
rprint(f"[bold purple]Agent Response:[/bold purple] {response}") # Log agent response
|
182 |
+
return (message, response)
|
183 |
+
|
184 |
+
# Function to create a new web app instance
|
185 |
+
def create_web_app(app_name: str, code: str) -> None:
|
186 |
+
"""Creates a new web app instance with the given name and code."""
|
187 |
+
# Create a new directory for the app
|
188 |
+
os.makedirs(app_name, exist_ok=True)
|
189 |
+
|
190 |
+
# Create the app.py file
|
191 |
+
with open(os.path.join(app_name, 'app.py'), 'w') as f:
|
192 |
+
f.write(code)
|
193 |
+
|
194 |
+
# Create the requirements.txt file
|
195 |
+
with open(os.path.join(app_name, 'requirements.txt'), 'w') as f:
|
196 |
+
f.write("gradio\nhuggingface_hub")
|
197 |
+
|
198 |
+
# Print a success message
|
199 |
+
print(f"Web app '{app_name}' created successfully!")
|
200 |
+
|
201 |
+
# Function to handle the "Create Web App" button click
|
202 |
+
def create_web_app_button_click(code: str) -> str:
|
203 |
+
"""Handles the "Create Web App" button click."""
|
204 |
+
# Get the app name from the user
|
205 |
+
app_name = gr.Textbox.get().strip()
|
206 |
+
|
207 |
+
# Validate the app name
|
208 |
+
if not app_name:
|
209 |
+
return "Please enter a valid app name."
|
210 |
+
|
211 |
+
# Create the web app instance
|
212 |
+
create_web_app(app_name, code)
|
213 |
+
|
214 |
+
# Return a success message
|
215 |
+
return f"Web app '{app_name}' created successfully!"
|
216 |
+
|
217 |
+
# Function to handle the "Deploy" button click
|
218 |
+
def deploy_button_click(app_name: str, code: str) -> str:
|
219 |
+
"""Handles the "Deploy" button click."""
|
220 |
+
# Get the app name from the user
|
221 |
+
app_name = gr.Textbox.get().strip()
|
222 |
+
|
223 |
+
# Validate the app name
|
224 |
+
if not app_name:
|
225 |
+
return "Please enter a valid app name."
|
226 |
+
|
227 |
+
# Get Hugging Face token
|
228 |
+
hf_token = gr.Textbox.get("hf_token").strip()
|
229 |
+
|
230 |
+
# Validate Hugging Face token
|
231 |
+
if not hf_token:
|
232 |
+
return "Please enter a valid Hugging Face token."
|
233 |
+
|
234 |
+
# Create a new directory for the app
|
235 |
+
os.makedirs(app_name, exist_ok=True)
|
236 |
+
|
237 |
+
# Copy the code to the app directory
|
238 |
+
with open(os.path.join(app_name, 'app.py'), 'w') as f:
|
239 |
+
f.write(code)
|
240 |
+
|
241 |
+
# Create the requirements.txt file
|
242 |
+
with open(os.path.join(app_name, 'requirements.txt'), 'w') as f:
|
243 |
+
f.write("gradio\nhuggingface_hub")
|
244 |
+
|
245 |
+
# Deploy the app to Hugging Face Spaces
|
246 |
+
try:
|
247 |
+
subprocess.run(
|
248 |
+
['huggingface-cli', 'login', '--token', hf_token],
|
249 |
+
check=True,
|
250 |
)
|
251 |
+
subprocess.run(
|
252 |
+
['huggingface-cli', 'space', 'create', app_name, '--repo_type', 'spaces', '--private', '--branch', 'main'],
|
253 |
+
check=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
)
|
255 |
+
subprocess.run(
|
256 |
+
['git', 'init'],
|
257 |
+
cwd=app_name,
|
258 |
+
check=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
)
|
260 |
+
subprocess.run(
|
261 |
+
['git', 'add', '.'],
|
262 |
+
cwd=app_name,
|
263 |
+
check=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
)
|
265 |
+
subprocess.run(
|
266 |
+
['git', 'commit', '-m', 'Initial commit'],
|
267 |
+
cwd=app_name,
|
268 |
+
check=True,
|
|
|
|
|
|
|
|
|
|
|
269 |
)
|
270 |
+
subprocess.run(
|
271 |
+
['git', 'remote', 'add', 'origin', hf_hub_url(username='your_username', repo_id=app_name)],
|
272 |
+
cwd=app_name,
|
273 |
+
check=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
)
|
275 |
+
subprocess.run(
|
276 |
+
['git', 'push', '-u', 'origin', 'main'],
|
277 |
+
cwd=app_name,
|
278 |
+
check=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
)
|
280 |
+
return f"Web app '{app_name}' deployed successfully to Hugging Face Spaces!"
|
281 |
+
except subprocess.CalledProcessError as e:
|
282 |
+
return f"Error: {e}"
|
283 |
+
|
284 |
+
# Function to handle the "Local Host" button click
|
285 |
+
def local_host_button_click(app_name: str, code: str) -> str:
|
286 |
+
"""Handles the "Local Host" button click."""
|
287 |
+
# Get the app name from the user
|
288 |
+
app_name = gr.Textbox.get().strip()
|
289 |
+
|
290 |
+
# Validate the app name
|
291 |
+
if not app_name:
|
292 |
+
return "Please enter a valid app name."
|
293 |
+
|
294 |
+
# Create a new directory for the app
|
295 |
+
os.makedirs(app_name, exist_ok=True)
|
296 |
+
|
297 |
+
# Copy the code to the app directory
|
298 |
+
with open(os.path.join(app_name, 'app.py'), 'w') as f:
|
299 |
+
f.write(code)
|
300 |
+
|
301 |
+
# Create the requirements.txt file
|
302 |
+
with open(os.path.join(app_name, 'requirements.txt'), 'w') as f:
|
303 |
+
f.write("gradio\nhuggingface_hub")
|
304 |
+
|
305 |
+
# Start the local server
|
306 |
+
os.chdir(app_name)
|
307 |
+
subprocess.Popen(['gradio', 'run', 'app.py', '--share', '--server_port', str(LOCAL_HOST_PORT)])
|
308 |
+
|
309 |
+
# Return a success message
|
310 |
+
return f"Web app '{app_name}' running locally on port {LOCAL_HOST_PORT}!"
|
311 |
+
|
312 |
+
# Function to handle the "Ship" button click
|
313 |
+
def ship_button_click(app_name: str, code: str) -> str:
|
314 |
+
"""Handles the "Ship" button click."""
|
315 |
+
# Get the app name from the user
|
316 |
+
app_name = gr.Textbox.get().strip()
|
317 |
+
|
318 |
+
# Validate the app name
|
319 |
+
if not app_name:
|
320 |
+
return "Please enter a valid app name."
|
321 |
+
|
322 |
+
# Ship the web app instance
|
323 |
+
# ... (Implement shipping logic here)
|
324 |
+
|
325 |
+
# Return a success message
|
326 |
+
return f"Web app '{app_name}' shipped successfully!"
|
327 |
+
|
328 |
+
# --- Gradio Interface ---
|
329 |
+
|
330 |
+
with gr.Blocks(theme='ParityError/Interstellar') as demo:
|
331 |
+
# --- Agent Selection ---
|
332 |
+
with gr.Row():
|
333 |
+
for agent_name, agent_data in agent_roles.items():
|
334 |
+
button = gr.Button(agent_name, variant="secondary")
|
335 |
+
textbox = gr.Textbox(agent_data["description"], interactive=False)
|
336 |
+
button.click(toggle_agent, inputs=[button], outputs=[textbox])
|
337 |
+
|
338 |
+
# --- Chat Interface ---
|
339 |
+
with gr.Row():
|
340 |
+
chatbot = gr.Chatbot()
|
341 |
+
chat_interface_input = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
342 |
+
chat_interface_output = gr.Textbox(label="Response", interactive=False)
|
343 |
+
|
344 |
+
# Parameters
|
345 |
+
temperature_slider = gr.Slider(
|
346 |
+
label="Temperature",
|
347 |
+
value=DEFAULT_TEMPERATURE,
|
348 |
+
minimum=0.0,
|
349 |
+
maximum=1.0,
|
350 |
+
step=0.05,
|
351 |
+
interactive=True,
|
352 |
+
info="Higher values generate more diverse outputs",
|
353 |
)
|
354 |
+
max_new_tokens_slider = gr.Slider(
|
355 |
+
label="Maximum New Tokens",
|
356 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
357 |
+
minimum=64,
|
358 |
+
maximum=4096,
|
359 |
+
step=64,
|
360 |
+
interactive=True,
|
361 |
+
info="The maximum number of new tokens",
|
362 |
+
)
|
363 |
+
top_p_slider = gr.Slider(
|
364 |
+
label="Top-p (Nucleus Sampling)",
|
365 |
+
value=DEFAULT_TOP_P,
|
366 |
+
minimum=0.0,
|
367 |
+
maximum=1,
|
368 |
+
step=0.05,
|
369 |
+
interactive=True,
|
370 |
+
info="Higher values sample more low-probability tokens",
|
371 |
+
)
|
372 |
+
repetition_penalty_slider = gr.Slider(
|
373 |
+
label="Repetition Penalty",
|
374 |
+
value=DEFAULT_REPETITION_PENALTY,
|
375 |
+
minimum=1.0,
|
376 |
+
maximum=2.0,
|
377 |
+
step=0.05,
|
378 |
+
interactive=True,
|
379 |
+
info="Penalize repeated tokens",
|
380 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
381 |
|
382 |
+
# Submit Button
|
383 |
+
submit_button = gr.Button("Submit")
|
384 |
+
|
385 |
+
# Chat Interface Logic
|
386 |
+
submit_button.click(
|
387 |
+
chat_interface,
|
388 |
+
inputs=[
|
389 |
+
chat_interface_input,
|
390 |
+
chatbot,
|
391 |
+
get_agent_cluster,
|
392 |
+
temperature_slider,
|
393 |
+
max_new_tokens_slider,
|
394 |
+
top_p_slider,
|
395 |
+
repetition_penalty_slider,
|
396 |
+
],
|
397 |
+
outputs=[
|
398 |
+
chatbot,
|
399 |
+
chat_interface_output,
|
400 |
+
],
|
401 |
+
)
|
402 |
|
403 |
+
# --- Web App Creation ---
|
404 |
+
with gr.Row():
|
405 |
+
app_name_input = gr.Textbox(label="App Name", placeholder="Enter your app name")
|
406 |
+
code_output = gr.Textbox(label="Code", interactive=False)
|
407 |
+
create_web_app_button = gr.Button("Create Web App")
|
408 |
+
deploy_button = gr.Button("Deploy")
|
409 |
+
local_host_button = gr.Button("Local Host")
|
410 |
+
ship_button = gr.Button("Ship")
|
411 |
+
hf_token_input = gr.Textbox(label="Hugging Face Token", placeholder="Enter your Hugging Face token")
|
412 |
+
|
413 |
+
# Web App Creation Logic
|
414 |
+
create_web_app_button.click(
|
415 |
+
create_web_app_button_click,
|
416 |
+
inputs=[code_output],
|
417 |
+
outputs=[gr.Textbox(label="Status", interactive=False)],
|
418 |
+
)
|
419 |
|
420 |
+
# Deploy the web app
|
421 |
+
deploy_button.click(
|
422 |
+
deploy_button_click,
|
423 |
+
inputs=[app_name_input, code_output, hf_token_input],
|
424 |
+
outputs=[gr.Textbox(label="Status", interactive=False)],
|
425 |
+
)
|
426 |
|
427 |
+
# Local host the web app
|
428 |
+
local_host_button.click(
|
429 |
+
local_host_button_click,
|
430 |
+
inputs=[app_name_input, code_output],
|
431 |
+
outputs=[gr.Textbox(label="Status", interactive=False)],
|
432 |
+
)
|
433 |
|
434 |
+
# Ship the web app
|
435 |
+
ship_button.click(
|
436 |
+
ship_button_click,
|
437 |
+
inputs=[app_name_input, code_output],
|
438 |
+
outputs=[gr.Textbox(label="Status", interactive=False)],
|
439 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
440 |
|
441 |
+
# --- Connect Chat Output to Code Output ---
|
442 |
+
chat_interface_output.change(
|
443 |
+
lambda x: x,
|
444 |
+
inputs=[chat_interface_output],
|
445 |
+
outputs=[code_output],
|
446 |
)
|
447 |
|
448 |
+
# --- Initialize Hugging Face Client ---
|
449 |
+
client = InferenceClient(repo_id=MODEL_NAME, token=os.environ.get("HF_TOKEN"))
|
|
|
|
|
|
|
|
|
450 |
|
451 |
+
# --- Launch Gradio ---
|
452 |
+
demo.queue().launch(debug=True)
|
|