acecalisto3 commited on
Commit
19ac016
·
verified ·
1 Parent(s): dba26ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +475 -234
app.py CHANGED
@@ -1,239 +1,480 @@
1
  import os
2
  import subprocess
3
- import streamlit as st
4
- from transformers.pipelines import pipeline
5
- from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel, RagRetriever, AutoModelForSeq2SeqLM
6
- import black
7
- from pylint import lint
8
- from io import StringIO
9
- import sys
10
- import torch
11
- from huggingface_hub import hf_hub_url, cached_download, HfApi
12
- from datetime import datetime
13
- import requests
14
  import random
15
- from huggingface_hub.hf_api import Repository # Assuming this is how you import the Repository class
16
-
17
- # Set your Hugging Face API key here
18
- # hf_token = "YOUR_HUGGING_FACE_API_KEY" # Replace with your actual token
19
- # Get Hugging Face token from secrets.toml - this line should already be in the main code
20
- hf_token = st.secrets["huggingface"]["hf_token"]
21
-
22
- HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit"
23
- PROJECT_ROOT = "projects"
24
- AGENT_DIRECTORY = "agents"
25
-
26
- # Global state to manage communication between Tool Box and Workspace Chat App
27
- if 'chat_history' not in st.session_state:
28
- st.session_state.chat_history = []
29
- if 'terminal_history' not in st.session_state:
30
- st.session_state.terminal_history = []
31
- if 'workspace_projects' not in st.session_state:
32
- st.session_state.workspace_projects = {}
33
- if 'available_agents' not in st.session_state:
34
- st.session_state.available_agents = []
35
- if 'current_state' not in st.session_state:
36
- st.session_state.current_state = {
37
- 'toolbox': {},
38
- 'workspace_chat': {}
39
- }
40
-
41
- # List of top downloaded free code-generative models from Hugging Face Hub
42
- AVAILABLE_CODE_GENERATIVE_MODELS = [
43
- "bigcode/starcoder", # Popular and powerful
44
- "Salesforce/codegen-350M-mono", # Smaller, good for quick tasks
45
- "microsoft/CodeGPT-small", # Smaller, good for quick tasks
46
- "google/flan-t5-xl", # Powerful, good for complex tasks
47
- "facebook/bart-large-cnn", # Good for text-to-code tasks
48
- ]
 
 
 
 
 
 
 
 
 
49
 
50
- # Load pre-trained RAG retriever
51
- rag_retriever = RagRetriever.from_pretrained("facebook/rag-token-base") # Use a Hugging Face RAG model
52
-
53
- # Load pre-trained chat model
54
- chat_model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/DialoGPT-medium") # Use a Hugging Face chat model
55
-
56
- # Load tokenizer
57
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
58
-
59
- def process_input(user_input):
60
- # Input pipeline: Tokenize and preprocess user input
61
- input_ids = tokenizer(user_input, return_tensors="pt").input_ids
62
- attention_mask = tokenizer(user_input, return_tensors="pt").attention_mask
63
-
64
- # RAG model: Generate response
65
- with torch.no_grad():
66
- output = rag_retriever(input_ids, attention_mask=attention_mask)
67
- response = output.generator_outputs[0].sequences[0]
68
-
69
- # Chat model: Refine response
70
- chat_input = tokenizer(response, return_tensors="pt")
71
- chat_input["input_ids"] = chat_input["input_ids"].unsqueeze(0)
72
- chat_input["attention_mask"] = chat_input["attention_mask"].unsqueeze(0)
73
- with torch.no_grad():
74
- chat_output = chat_model(**chat_input)
75
- refined_response = chat_output.sequences[0]
76
-
77
- # Output pipeline: Return final response
78
- return refined_response
79
-
80
- class AIAgent:
81
- def __init__(self, name, description, skills, hf_api=None):
82
- self.name = name
83
- self.description = description
84
- self.skills = skills
85
- self._hf_api = hf_api
86
- self._hf_token = hf_token # Store the token here
87
-
88
- @property
89
- def hf_api(self):
90
- if not self._hf_api and self.has_valid_hf_token():
91
- self._hf_api = HfApi(token=self._hf_token)
92
- return self._hf_api
93
-
94
- def has_valid_hf_token(self):
95
- return bool(self._hf_token)
96
-
97
- async def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model, hf_token):
98
- self._hf_token = hf_token
99
- # Continuation of previous methods
100
- summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
101
- summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
102
-
103
- # Analyze chat history and workspace projects to suggest actions
104
- # Example:
105
- # - Check if the user has requested to create a new file
106
- # - Check if the user has requested to install a package
107
- # - Check if the user has requested to run a command
108
- # - Check if the user has requested to generate code
109
- # - Check if the user has requested to translate code
110
- # - Check if the user has requested to summarize text
111
- # - Check if the user has requested to analyze sentiment
112
-
113
- # Generate a response based on the analysis
114
- next_step = "Based on the current state, the next logical step is to implement the main application logic."
115
-
116
- # Ensure project folder exists
117
- project_path = os.path.join(PROJECT_ROOT, project_name)
118
- if not os.path.exists(project_path):
119
- os.makedirs(project_path)
120
-
121
- # Create requirements.txt if it doesn't exist
122
- requirements_file = os.path.join(project_path, "requirements.txt")
123
- if not os.path.exists(requirements_file):
124
- with open(requirements_file, "w") as f:
125
- f.write("# Add your project's dependencies here\n")
126
-
127
- # Create app.py if it doesn't exist
128
- app_file = os.path.join(project_path, "app.py")
129
- if not os.path.exists(app_file):
130
- with open(app_file, "w") as f:
131
- f.write("# Your project's main application logic goes here\n")
132
-
133
- # Generate GUI code for app.py if requested
134
- if "create a gui" in summary.lower():
135
- gui_code = generate_code("Create a simple GUI for this application", selected_model)
136
- with open(app_file, "a") as f:
137
- f.write(gui_code)
138
-
139
- # Run the default build process
140
- build_command = "pip install -r requirements.txt && python app.py"
141
- try:
142
- result = subprocess.run(build_command, shell=True, capture_output=True, text=True, cwd=project_path)
143
- st.write(f"Build Output:\n{result.stdout}")
144
- if result.stderr:
145
- st.error(f"Build Errors:\n{result.stderr}")
146
- except Exception as e:
147
- st.error(f"Build Error: {e}")
148
-
149
- return summary, next_step
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
- def deploy_built_space_to_hf(self):
152
- if not self._hf_api or not self._hf_token:
153
- raise ValueError("Cannot deploy the Space since no valid Hugoging Face API connection was established.")
154
-
155
- # Assuming you have a function to get the files for your Space
156
- repository_name = f"my-awesome-space_{datetime.now().timestamp()}"
157
- files = get_built_space_files() # Placeholder - you'll need to define this function
158
-
159
- # Create the Space
160
- create_space(self.hf_api, repository_name, "Description", True, files)
161
-
162
- st.markdown("## Congratulations! Successfully deployed Space 🚀 ##")
163
- st.markdown(f"[Check out your new Space here](https://huggingface.co/spaces/{repository_name})")
164
-
165
-
166
- # Add any missing functions from your original code (e.g., get_built_space_files)
167
- def get_built_space_files():
168
- # Replace with your logic to gather the files you want to deploy
169
- return {
170
- "app.py": "# Your Streamlit app code here",
171
- "requirements.txt": "streamlit\ntransformers"
172
- # Add other files as needed
173
- }
174
-
175
- # ... (Rest of your existing functions: save_agent_to_file, load_agent_prompt,
176
- # create_agent_from_text, chat_interface_with_agent, terminal_interface,
177
- # code_editor_interface, summarize_text, sentiment_analysis, translate_code,
178
- # generate_code, chat_interface, workspace_interface, add_code_to_workspace)
179
-
180
- def create_space(api, name, description, public, files, entrypoint="launch.py"):
181
- url = f"{hf_hub_url()}spaces/{name}/prepare-repo"
182
- headers = {"Authorization": f"Bearer {api.access_token}"}
183
- payload = {
184
- "public": public,
185
- "gitignore_template": "web",
186
- "default_branch": "main",
187
- "archived": False,
188
- "files": []
189
- }
190
- for filename, contents in files.items():
191
- data = {
192
- "content": contents,
193
- "path": filename,
194
- "encoding": "utf-8",
195
- "mode": "overwrite" if "#\{random.randint(0, 1)\}" not in contents else "merge",
196
- }
197
- payload["files"].append(data)
198
- response = requests.post(url, json=payload, headers=headers)
199
- response.raise_for_status()
200
- location = response.headers.get("Location")
201
- # wait_for_processing(location, api) # You might need to implement this if it's not already defined
202
-
203
- return Repository(name=name, api=api)
204
-
205
- # Streamlit App
206
- st.title("AI Agent Creator")
207
-
208
- # Sidebar navigation
209
- st.sidebar.title("Navigation")
210
- app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
211
-
212
- # ... (Rest of your Streamlit app logic, including the 'Automate' button callback)
213
-
214
- if app_mode == "AI Agent Creator":
215
- # AI Agent Creator
216
- st.header("Create an AI Agent from Text")
217
-
218
- st.subheader("From Text")
219
- agent_name = st.text_input("Enter agent name:")
220
- text_input = st.text_area("Enter skills (one per line):")
221
- if st.button("Create Agent"):
222
- agent_prompt = create_agent_from_text(agent_name, text_input)
223
- st.success(f"Agent '{agent_name}' created and saved successfully.")
224
- st.session_state.available_agents.append(agent_name)
225
-
226
- # ... (Rest of your Streamlit app logic for other app modes)
227
-
228
- # Using the modified and extended class and functions, update the callback for the 'Automate' button in the Streamlit UI:
229
- if st.button("Automate", args=(hf_token,)):
230
- agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
231
- summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name, selected_model, hf_token)
232
- st.write("Autonomous Build Summary:")
233
- st.write(summary)
234
- st.write("Next Step:")
235
- st.write(next_step)
236
-
237
- # If everything went well, proceed to deploy the Space
238
- if agent._hf_api and agent.has_valid_hf_token():
239
- agent.deploy_built_space_to_hf()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import subprocess
 
 
 
 
 
 
 
 
 
 
 
3
  import random
4
+ from huggingface_hub import InferenceClient
5
+ import gradio as gr
6
+ from i_search import google
7
+ from i_search import i_search as i_s
8
+ from agent import (
9
+ ACTION_PROMPT,
10
+ ADD_PROMPT,
11
+ COMPRESS_HISTORY_PROMPT,
12
+ LOG_PROMPT,
13
+ LOG_RESPONSE,
14
+ MODIFY_PROMPT,
15
+ PREFIX,
16
+ SEARCH_QUERY,
17
+ READ_PROMPT,
18
+ TASK_PROMPT,
19
+ UNDERSTAND_TEST_RESULTS_PROMPT,
20
+ )
21
+ from utils import parse_action, parse_file_content, read_python_module_structure
22
+ from datetime import datetime
23
+ now = datetime.now()
24
+ date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
25
+
26
+ client = InferenceClient(
27
+ "mistralai/Mixtral-8x7B-Instruct-v0.1"
28
+ )
29
+
30
+ ############################################
31
+
32
+
33
+ VERBOSE = True
34
+ MAX_HISTORY = 100
35
+ #MODEL = "gpt-3.5-turbo" # "gpt-4"
36
+
37
+
38
+ def format_prompt(message, history):
39
+ prompt = "<s>"
40
+ for user_prompt, bot_response in history:
41
+ prompt += f"[INST] {user_prompt} [/INST]"
42
+ prompt += f" {bot_response}</s> "
43
+ prompt += f"[INST] {message} [/INST]"
44
+ return prompt
45
+
46
+
47
 
48
+ def run_gpt(
49
+ prompt_template,
50
+ stop_tokens,
51
+ max_tokens,
52
+ module_summary,
53
+ purpose,
54
+ **prompt_kwargs,
55
+ ):
56
+ seed = random.randint(1,1111111111111111)
57
+
58
+ generate_kwargs = dict(
59
+ temperature=0.9,
60
+ max_new_tokens=1048,
61
+ top_p=0.95,
62
+ repetition_penalty=1.0,
63
+ do_sample=True,
64
+ seed=seed,
65
+ )
66
+
67
+
68
+ content = PREFIX.format(
69
+ date_time_str=date_time_str,
70
+ purpose=purpose,
71
+ ) + prompt_template.format(**prompt_kwargs)
72
+ if VERBOSE:
73
+ print(LOG_PROMPT.format(content))
74
+
75
+
76
+ #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
77
+ #formatted_prompt = format_prompt(f'{content}', history)
78
+
79
+ stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
80
+ resp = ""
81
+ for response in stream:
82
+ resp += response.token.text
83
+
84
+ if VERBOSE:
85
+ print(LOG_RESPONSE.format(resp))
86
+ return resp
87
+
88
+
89
+ def compress_history(purpose, task, history, directory):
90
+ module_summary, _, _ = read_python_module_structure(directory)
91
+ resp = run_gpt(
92
+ COMPRESS_HISTORY_PROMPT,
93
+ stop_tokens=["observation:", "task:", "action:", "thought:"],
94
+ max_tokens=512,
95
+ module_summary=module_summary,
96
+ purpose=purpose,
97
+ task=task,
98
+ history=history,
99
+ )
100
+ history = "observation: {}\n".format(resp)
101
+ return history
102
+
103
+ def call_search(purpose, task, history, directory, action_input):
104
+ print("CALLING SEARCH")
105
+ try:
106
+ if "http" in action_input:
107
+ if "<" in action_input:
108
+ action_input = action_input.strip("<")
109
+ if ">" in action_input:
110
+ action_input = action_input.strip(">")
111
+ response = i_s(action_input)
112
+ #response = google(search_return)
113
+ print(response)
114
+ history += "observation: search result is: {}\n".format(response)
115
+ else:
116
+ history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=URL'\n"
117
+ except Exception as e:
118
+ history += "observation: {}'\n".format(e)
119
+ return "MAIN", None, history, task
120
+
121
+ def call_main(purpose, task, history, directory, action_input):
122
+ module_summary, _, _ = read_python_module_structure(directory)
123
+ resp = run_gpt(
124
+ ACTION_PROMPT,
125
+ stop_tokens=["observation:", "task:"],
126
+ max_tokens=256,
127
+ module_summary=module_summary,
128
+ purpose=purpose,
129
+ task=task,
130
+ history=history,
131
+ )
132
+ lines = resp.strip().strip("\n").split("\n")
133
+ for line in lines:
134
+ if line == "":
135
+ continue
136
+ if line.startswith("thought: "):
137
+ history += "{}\n".format(line)
138
+ elif line.startswith("action: "):
139
+
140
+ action_name, action_input = parse_action(line)
141
+ print (f'ACTION_NAME :: {action_name}')
142
+ print (f'ACTION_INPUT :: {action_input}')
143
+
144
+ history += "{}\n".format(line)
145
+ if "COMPLETE" in action_name or "COMPLETE" in action_input:
146
+ task = "END"
147
+ return action_name, action_input, history, task
148
+ else:
149
+ return action_name, action_input, history, task
150
+ else:
151
+ history += "{}\n".format(line)
152
+ #history += "observation: the following command did not produce any useful output: '{}', I need to check the commands syntax, or use a different command\n".format(line)
153
+
154
+ #return action_name, action_input, history, task
155
+ #assert False, "unknown action: {}".format(line)
156
+ return "MAIN", None, history, task
157
+
158
+
159
+ def call_test(purpose, task, history, directory, action_input):
160
+ result = subprocess.run(
161
+ ["python", "-m", "pytest", "--collect-only", directory],
162
+ capture_output=True,
163
+ text=True,
164
+ )
165
+ if result.returncode != 0:
166
+ history += "observation: there are no tests! Test should be written in a test folder under {}\n".format(
167
+ directory
168
+ )
169
+ return "MAIN", None, history, task
170
+ result = subprocess.run(
171
+ ["python", "-m", "pytest", directory], capture_output=True, text=True
172
+ )
173
+ if result.returncode == 0:
174
+ history += "observation: tests pass\n"
175
+ return "MAIN", None, history, task
176
+ module_summary, content, _ = read_python_module_structure(directory)
177
+ resp = run_gpt(
178
+ UNDERSTAND_TEST_RESULTS_PROMPT,
179
+ stop_tokens=[],
180
+ max_tokens=256,
181
+ module_summary=module_summary,
182
+ purpose=purpose,
183
+ task=task,
184
+ history=history,
185
+ stdout=result.stdout[:5000], # limit amount of text
186
+ stderr=result.stderr[:5000], # limit amount of text
187
+ )
188
+ history += "observation: tests failed: {}\n".format(resp)
189
+ return "MAIN", None, history, task
190
+
191
+
192
+ def call_set_task(purpose, task, history, directory, action_input):
193
+ module_summary, content, _ = read_python_module_structure(directory)
194
+ task = run_gpt(
195
+ TASK_PROMPT,
196
+ stop_tokens=[],
197
+ max_tokens=64,
198
+ module_summary=module_summary,
199
+ purpose=purpose,
200
+ task=task,
201
+ history=history,
202
+ ).strip("\n")
203
+ history += "observation: task has been updated to: {}\n".format(task)
204
+ return "MAIN", None, history, task
205
+
206
+
207
+ def call_read(purpose, task, history, directory, action_input):
208
+ if not os.path.exists(action_input):
209
+ history += "observation: file does not exist\n"
210
+ return "MAIN", None, history, task
211
+ module_summary, content, _ = read_python_module_structure(directory)
212
+ f_content = (
213
+ content[action_input] if content[action_input] else "< document is empty >"
214
+ )
215
+ resp = run_gpt(
216
+ READ_PROMPT,
217
+ stop_tokens=[],
218
+ max_tokens=256,
219
+ module_summary=module_summary,
220
+ purpose=purpose,
221
+ task=task,
222
+ history=history,
223
+ file_path=action_input,
224
+ file_contents=f_content,
225
+ ).strip("\n")
226
+ history += "observation: {}\n".format(resp)
227
+ return "MAIN", None, history, task
228
+
229
+
230
+ def call_modify(purpose, task, history, directory, action_input):
231
+ if not os.path.exists(action_input):
232
+ history += "observation: file does not exist\n"
233
+ return "MAIN", None, history, task
234
+ (
235
+ module_summary,
236
+ content,
237
+ _,
238
+ ) = read_python_module_structure(directory)
239
+ f_content = (
240
+ content[action_input] if content[action_input] else "< document is empty >"
241
+ )
242
+ resp = run_gpt(
243
+ MODIFY_PROMPT,
244
+ stop_tokens=["action:", "thought:", "observation:"],
245
+ max_tokens=2048,
246
+ module_summary=module_summary,
247
+ purpose=purpose,
248
+ task=task,
249
+ history=history,
250
+ file_path=action_input,
251
+ file_contents=f_content,
252
+ )
253
+ new_contents, description = parse_file_content(resp)
254
+ if new_contents is None:
255
+ history += "observation: failed to modify file\n"
256
+ return "MAIN", None, history, task
257
+
258
+ with open(action_input, "w") as f:
259
+ f.write(new_contents)
260
+
261
+ history += "observation: file successfully modified\n"
262
+ history += "observation: {}\n".format(description)
263
+ return "MAIN", None, history, task
264
+
265
+
266
+ def call_add(purpose, task, history, directory, action_input):
267
+ d = os.path.dirname(action_input)
268
+ if not d.startswith(directory):
269
+ history += "observation: files must be under directory {}\n".format(directory)
270
+ elif not action_input.endswith(".py"):
271
+ history += "observation: can only write .py files\n"
272
+ else:
273
+ if d and not os.path.exists(d):
274
+ os.makedirs(d)
275
+ if not os.path.exists(action_input):
276
+ module_summary, _, _ = read_python_module_structure(directory)
277
+ resp = run_gpt(
278
+ ADD_PROMPT,
279
+ stop_tokens=["action:", "thought:", "observation:"],
280
+ max_tokens=2048,
281
+ module_summary=module_summary,
282
+ purpose=purpose,
283
+ task=task,
284
+ history=history,
285
+ file_path=action_input,
286
+ )
287
+ new_contents, description = parse_file_content(resp)
288
+ if new_contents is None:
289
+ history += "observation: failed to write file\n"
290
+ return "MAIN", None, history, task
291
+
292
+ with open(action_input, "w") as f:
293
+ f.write(new_contents)
294
+
295
+ history += "observation: file successfully written\n"
296
+ history += "obsertation: {}\n".format(description)
297
+ else:
298
+ history += "observation: file already exists\n"
299
+ return "MAIN", None, history, task
300
+ def end_fn(purpose, task, history, directory, action_input):
301
+ task = "END"
302
+ return "COMPLETE", None, history, task
303
+ NAME_TO_FUNC = {
304
+ "MAIN": call_main,
305
+ "UPDATE-TASK": call_set_task,
306
+ "SEARCH": call_search,
307
+ "COMPLETE": end_fn,
308
+
309
+ }
310
+
311
+
312
+ def run_action(purpose, task, history, directory, action_name, action_input):
313
+ if "RESPONSE" in action_name:
314
+ task="END"
315
+ return action_name, action_input, history, task
316
+
317
+ # compress the history when it is long
318
+ if len(history.split("\n")) > MAX_HISTORY:
319
+ if VERBOSE:
320
+ print("COMPRESSING HISTORY")
321
+ history = compress_history(purpose, task, history, directory)
322
+
323
+ assert action_name in NAME_TO_FUNC
324
+
325
+ print("RUN: ", action_name, action_input)
326
+ return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
327
+
328
+
329
+ def run(purpose,hist):
330
 
331
+ print(purpose)
332
+ print(hist)
333
+ task=None
334
+ directory="./"
335
+ history = ""
336
+ action_name = "UPDATE-TASK" if task is None else "MAIN"
337
+ action_input = None
338
+ while True:
339
+ print("")
340
+ print("")
341
+ print("---")
342
+ print("purpose:", purpose)
343
+ print("task:", task)
344
+ print("---")
345
+ print(history)
346
+ print("---")
347
+
348
+ action_name, action_input, history, task = run_action(
349
+ purpose,
350
+ task,
351
+ history,
352
+ directory,
353
+ action_name,
354
+ action_input,
355
+ )
356
+ if task == "END":
357
+ return history
358
+
359
+
360
+
361
+ ################################################
362
+
363
+ def format_prompt(message, history):
364
+ prompt = "<s>"
365
+ for user_prompt, bot_response in history:
366
+ prompt += f"[INST] {user_prompt} [/INST]"
367
+ prompt += f" {bot_response}</s> "
368
+ prompt += f"[INST] {message} [/INST]"
369
+ return prompt
370
+ agents =[
371
+ "WEB_DEV",
372
+ "AI_SYSTEM_PROMPT",
373
+ "PYTHON_CODE_DEV"
374
+ ]
375
+ def generate(
376
+ prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
377
+ ):
378
+ seed = random.randint(1,1111111111111111)
379
+
380
+ agent=prompts.WEB_DEV
381
+ if agent_name == "WEB_DEV":
382
+ agent = prompts.WEB_DEV
383
+ if agent_name == "AI_SYSTEM_PROMPT":
384
+ agent = prompts.AI_SYSTEM_PROMPT
385
+ if agent_name == "PYTHON_CODE_DEV":
386
+ agent = prompts.PYTHON_CODE_DEV
387
+ system_prompt=agent
388
+ temperature = float(temperature)
389
+ if temperature < 1e-2:
390
+ temperature = 1e-2
391
+ top_p = float(top_p)
392
+
393
+ generate_kwargs = dict(
394
+ temperature=temperature,
395
+ max_new_tokens=max_new_tokens,
396
+ top_p=top_p,
397
+ repetition_penalty=repetition_penalty,
398
+ do_sample=True,
399
+ seed=seed,
400
+ )
401
+
402
+ formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
403
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
404
+ output = ""
405
+
406
+ for response in stream:
407
+ output += response.token.text
408
+ yield output
409
+ return output
410
+
411
+
412
+ additional_inputs=[
413
+ gr.Dropdown(
414
+ label="Agents",
415
+ choices=[s for s in agents],
416
+ value=agents[0],
417
+ interactive=True,
418
+ ),
419
+ gr.Textbox(
420
+ label="System Prompt",
421
+ max_lines=1,
422
+ interactive=True,
423
+ ),
424
+ gr.Slider(
425
+ label="Temperature",
426
+ value=0.9,
427
+ minimum=0.0,
428
+ maximum=1.0,
429
+ step=0.05,
430
+ interactive=True,
431
+ info="Higher values produce more diverse outputs",
432
+ ),
433
+
434
+ gr.Slider(
435
+ label="Max new tokens",
436
+ value=1048*10,
437
+ minimum=0,
438
+ maximum=1048*10,
439
+ step=64,
440
+ interactive=True,
441
+ info="The maximum numbers of new tokens",
442
+ ),
443
+ gr.Slider(
444
+ label="Top-p (nucleus sampling)",
445
+ value=0.90,
446
+ minimum=0.0,
447
+ maximum=1,
448
+ step=0.05,
449
+ interactive=True,
450
+ info="Higher values sample more low-probability tokens",
451
+ ),
452
+ gr.Slider(
453
+ label="Repetition penalty",
454
+ value=1.2,
455
+ minimum=1.0,
456
+ maximum=2.0,
457
+ step=0.05,
458
+ interactive=True,
459
+ info="Penalize repeated tokens",
460
+ ),
461
+
462
+
463
+ ]
464
+
465
+ examples=[["I'm planning a vacation to Japan. Can you suggest a one-week itinerary including must-visit places and local cuisines to try?", None, None, None, None, None, ],
466
+ ["Can you write a short story about a time-traveling detective who solves historical mysteries?", None, None, None, None, None,],
467
+ ["I'm trying to learn French. Can you provide some common phrases that would be useful for a beginner, along with their pronunciations?", None, None, None, None, None,],
468
+ ["I have chicken, rice, and bell peppers in my kitchen. Can you suggest an easy recipe I can make with these ingredients?", None, None, None, None, None,],
469
+ ["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
470
+ ["What are some unique features of Rust that make it stand out compared to other systems programming languages like C++?", None, None, None, None, None,],
471
+ ]
472
+
473
+
474
+ gr.ChatInterface(
475
+ fn=run,
476
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
477
+ title="Mixtral 46.7B\nMicro-Agent\nInternet Search",
478
+ examples=examples,
479
+ concurrency_limit=20,
480
+ ).launch(show_api=False)