acecalisto3 commited on
Commit
2d0ade1
·
verified ·
1 Parent(s): 12548f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -141
app.py CHANGED
@@ -10,10 +10,11 @@ client = InferenceClient(
10
  "mistralai/Mixtral-8x7B-Instruct-v0.1"
11
  )
12
 
13
- def format_prompt(message, history, agent_role):
14
- """Formats the prompt with the selected agent role and conversation history."""
15
  prompt = f"""
16
- You are an expert {agent_role} who responds with complete program coding to client requests.
 
17
  Using available tools, please explain the researched information.
18
  Please don't answer based solely on what you already know. Always perform a search before providing a response.
19
  In special cases, such as when the user specifies a page to read, there's no need to search.
@@ -42,8 +43,8 @@ But, you can go ahead and search in English, especially for programming-related
42
  prompt += f"[INST] {message} [/INST]"
43
  return prompt
44
 
45
- def generate(prompt, history, agent_role, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
46
- """Generates a response using the selected agent role and parameters."""
47
  temperature = float(temperature)
48
  if temperature < 1e-2:
49
  temperature = 1e-2
@@ -58,7 +59,7 @@ def generate(prompt, history, agent_role, temperature=0.9, max_new_tokens=2048,
58
  seed=random.randint(0, 10**7),
59
  )
60
 
61
- formatted_prompt = format_prompt(prompt, history, agent_role)
62
 
63
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
64
  output = ""
@@ -75,55 +76,16 @@ def change_agent(agent_name):
75
  return f"Agent switched to: {agent_name}"
76
 
77
  # Define the available agent roles
78
- agent_roles = [
79
- "Web Developer",
80
- "Prompt Engineer",
81
- "Python Code Developer",
82
- "Hugging Face Hub Expert",
83
- "AI-Powered Code Assistant" # Agent 3
84
- ]
85
 
86
  # Initialize the selected agent
87
- selected_agent = agent_roles[0]
88
-
89
- additional_inputs=[
90
- gr.Slider(
91
- label="Temperatur",
92
- value=0.9,
93
- minimum=0.0,
94
- maximum=1.0,
95
- step=0.05,
96
- interactive=True,
97
- info="Höhere Werte erzeugen vielfältigere Ausgaben",
98
- ),
99
- gr.Slider(
100
- label="Maximale neue Tokens",
101
- value=2048,
102
- minimum=64,
103
- maximum=4096,
104
- step=64,
105
- interactive=True,
106
- info="Die maximale Anzahl neuer Tokens",
107
- ),
108
- gr.Slider(
109
- label="Top-p (Nukleus-Sampling)",
110
- value=0.90,
111
- minimum=0.0,
112
- maximum=1,
113
- step=0.05,
114
- interactive=True,
115
- info="Höhere Werte probieren mehr niedrigwahrscheinliche Tokens aus",
116
- ),
117
- gr.Slider(
118
- label="Wiederholungsstrafe",
119
- value=1.2,
120
- minimum=1.0,
121
- maximum=2.0,
122
- step=0.05,
123
- interactive=True,
124
- info="Bestrafe wiederholte Tokens",
125
- )
126
- ]
127
 
128
  # Define the initial prompt for the selected agent
129
  initial_prompt = f"""
@@ -156,6 +118,16 @@ customCSS = """
156
  }
157
  """
158
 
 
 
 
 
 
 
 
 
 
 
159
  def run_code(code):
160
  """Executes the provided code and returns the output."""
161
  try:
@@ -168,96 +140,57 @@ def run_code(code):
168
  except subprocess.CalledProcessError as e:
169
  return f"Error: {e.output}"
170
 
171
- def chat_interface(message, history, agent_role, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
172
  """Handles user input and generates responses."""
173
  if message.startswith("
174
 
175
 
176
- python"): # User entered code, execute it code = message[9:-3] output = run_code(code) return (message, output) else: # User entered a normal message, generate a response response = generate(message, history, agent_role, temperature, max_new_tokens, top_p, repetition_penalty) return (message, response)
177
-
178
- Define the available agent roles
179
- agent_roles = { "Web Developer": {"description": "A master of front-end and back-end web development.", "active": False}, "Prompt Engineer": {"description": "An expert in crafting effective prompts for AI models.", "active": False}, "Python Code Developer": {"description": "A skilled Python programmer who can write clean and efficient code.", "active": False}, "Hugging Face Hub Expert": {"description": "A specialist in navigating and utilizing the Hugging Face Hub.", "active": False}, "AI-Powered Code Assistant": {"description": "An AI assistant that can help with coding tasks and provide code snippets.", "active": False}, }
180
-
181
- Initialize the selected agent
182
- selected_agent = list(agent_roles.keys())[0]
183
-
184
- Define the initial prompt for the selected agent
185
- initial_prompt = f""" You are an expert {selected_agent} who responds with complete program coding to client requests. Using available tools, please explain the researched information. Please don't answer based solely on what you already know. Always perform a search before providing a response. In special cases, such as when the user specifies a page to read, there's no need to search. Please read the provided page and answer the user's question accordingly. 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:
186
-
187
- Try clicking on the links of the search results to access and read the content of each page.
188
- Change your search query and perform a new search. Users are extremely busy and not as free as you are. Therefore, to save the user's effort, please provide direct answers. BAD ANSWER EXAMPLE
189
- Please refer to these pages.
190
- You can write code referring these pages.
191
- Following page will be helpful. GOOD ANSWER EXAMPLE
192
- This is the complete code: -- complete code here --
193
- The answer of you question is -- answer here -- 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.) 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. But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE. """
194
- customCSS = """ #component-7 { # dies ist die Standardelement-ID des Chatkomponenten height: 1600px; # passen Sie die Höhe nach Bedarf an flex-grow: 4; } """
195
-
196
- def toggle_agent(agent_name): """Toggles the active state of an agent.""" global agent_roles agent_roles[agent_name]["active"] = not agent_roles[agent_name]["active"] return f"{agent_name} is now {'active' if agent_roles[agent_name]['active'] else 'inactive'}"
197
-
198
- def get_agent_cluster(): """Returns a dictionary of active agents.""" return {agent: agent_roles[agent]["active"] for agent in agent_roles}
199
-
200
- def run_code(code): """Executes the provided code and returns the output.""" try: output = subprocess.check_output( ['python', '-c', code], stderr=subprocess.STDOUT, universal_newlines=True, ) return output except subprocess.CalledProcessError as e: return f"Error: {e.output}"
201
-
202
- def chat_interface(message, history, agent_role, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0): """Handles user input and generates responses.""" if message.startswith("
203
-
204
- # User entered code, execute it
205
- code = message[9:-3]
206
- output = run_code(code)
207
- return (message, output)
208
- else:
209
- # User entered a normal message, generate a response
210
- response = generate(message, history, agent_role, temperature, max_new_tokens, top_p, repetition_penalty)
211
- return (message, response)
212
-
213
- with gr.Blocks(theme='ParityError/Interstellar') as demo:
214
- with gr.Row():
215
- for agent_name, agent_data in agent_roles.items():
216
- gr.Button(agent_name, variant="secondary").click(toggle_agent, inputs=[gr.Button], outputs=[gr.Textbox])
217
- gr.Textbox(agent_data["description"], interactive=False)
218
-
219
- with gr.Row():
220
- gr.ChatInterface(
221
- chat_interface,
222
- additional_inputs=[
223
- gr.Slider(
224
- label="Temperature",
225
- value=0.9,
226
- minimum=0.0,
227
- maximum=1.0,
228
- step=0.05,
229
- interactive=True,
230
- info="Higher values generate more diverse outputs",
231
- ),
232
- gr.Slider(
233
- label="Maximum New Tokens",
234
- value=2048,
235
- minimum=64,
236
- maximum=4096,
237
- step=64,
238
- interactive=True,
239
- info="The maximum number of new tokens",
240
- ),
241
- gr.Slider(
242
- label="Top-p (Nucleus Sampling)",
243
- value=0.90,
244
- minimum=0.0,
245
- maximum=1,
246
- step=0.05,
247
- interactive=True,
248
- info="Higher values sample more low-probability tokens",
249
- ),
250
- gr.Slider(
251
- label="Repetition Penalty",
252
- value=1.2,
253
- minimum=1.0,
254
- maximum=2.0,
255
- step=0.05,
256
- interactive=True,
257
- info="Penalize repeated tokens",
258
- )
259
- ],
260
- inputs=[gr.Textbox, gr.Chatbot, get_agent_cluster],
261
- )
262
 
263
- demo.queue().launch(debug=True)
 
10
  "mistralai/Mixtral-8x7B-Instruct-v0.1"
11
  )
12
 
13
+ def format_prompt(message, history, agent_roles):
14
+ """Formats the prompt with the selected agent roles and conversation history."""
15
  prompt = f"""
16
+ You are an expert agent cluster, consisting of {', '.join(agent_roles)}.
17
+ Respond with complete program coding to client requests.
18
  Using available tools, please explain the researched information.
19
  Please don't answer based solely on what you already know. Always perform a search before providing a response.
20
  In special cases, such as when the user specifies a page to read, there's no need to search.
 
43
  prompt += f"[INST] {message} [/INST]"
44
  return prompt
45
 
46
+ def generate(prompt, history, agent_roles, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
47
+ """Generates a response using the selected agent roles and parameters."""
48
  temperature = float(temperature)
49
  if temperature < 1e-2:
50
  temperature = 1e-2
 
59
  seed=random.randint(0, 10**7),
60
  )
61
 
62
+ formatted_prompt = format_prompt(prompt, history, agent_roles)
63
 
64
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
65
  output = ""
 
76
  return f"Agent switched to: {agent_name}"
77
 
78
  # Define the available agent roles
79
+ agent_roles = {
80
+ "Web Developer": {"description": "A master of front-end and back-end web development.", "active": False},
81
+ "Prompt Engineer": {"description": "An expert in crafting effective prompts for AI models.", "active": False},
82
+ "Python Code Developer": {"description": "A skilled Python programmer who can write clean and efficient code.", "active": False},
83
+ "Hugging Face Hub Expert": {"description": "A specialist in navigating and utilizing the Hugging Face Hub.", "active": False},
84
+ "AI-Powered Code Assistant": {"description": "An AI assistant that can help with coding tasks and provide code snippets.", "active": False},
85
+ }
86
 
87
  # Initialize the selected agent
88
+ selected_agent = list(agent_roles.keys())[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Define the initial prompt for the selected agent
91
  initial_prompt = f"""
 
118
  }
119
  """
120
 
121
+ def toggle_agent(agent_name):
122
+ """Toggles the active state of an agent."""
123
+ global agent_roles
124
+ agent_roles[agent_name]["active"] = not agent_roles[agent_name]["active"]
125
+ return f"{agent_name} is now {'active' if agent_roles[agent_name]['active'] else 'inactive'}"
126
+
127
+ def get_agent_cluster():
128
+ """Returns a dictionary of active agents."""
129
+ return {agent: agent_roles[agent]["active"] for agent in agent_roles}
130
+
131
  def run_code(code):
132
  """Executes the provided code and returns the output."""
133
  try:
 
140
  except subprocess.CalledProcessError as e:
141
  return f"Error: {e.output}"
142
 
143
+ def chat_interface(message, history, agent_cluster, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
144
  """Handles user input and generates responses."""
145
  if message.startswith("
146
 
147
 
148
+ python"): # User entered code, execute it code = message[9:-3] output = run_code(code) return (message, output) else: # User entered a normal message, generate a response active_agents = [agent for agent, is_active in agent_cluster.items() if is_active] response = generate(message, history, active_agents, temperature, max_new_tokens, top_p, repetition_penalty) return (message, response)
149
+
150
+ with gr.Blocks(theme='ParityError/Interstellar') as demo: with gr.Row(): for agent_name, agent_data in agent_roles.items(): gr.Button(agent_name, variant="secondary").click(toggle_agent, inputs=[gr.Button], outputs=[gr.Textbox]) gr.Textbox(agent_data["description"], interactive=False)
151
+
152
+ with gr.Row():
153
+ gr.ChatInterface(
154
+ chat_interface,
155
+ additional_inputs=[
156
+ gr.Slider(
157
+ label="Temperature",
158
+ value=0.9,
159
+ minimum=0.0,
160
+ maximum=1.0,
161
+ step=0.05,
162
+ interactive=True,
163
+ info="Higher values generate more diverse outputs",
164
+ ),
165
+ gr.Slider(
166
+ label="Maximum New Tokens",
167
+ value=2048,
168
+ minimum=64,
169
+ maximum=4096,
170
+ step=64,
171
+ interactive=True,
172
+ info="The maximum number of new tokens",
173
+ ),
174
+ gr.Slider(
175
+ label="Top-p (Nucleus Sampling)",
176
+ value=0.90,
177
+ minimum=0.0,
178
+ maximum=1,
179
+ step=0.05,
180
+ interactive=True,
181
+ info="Higher values sample more low-probability tokens",
182
+ ),
183
+ gr.Slider(
184
+ label="Repetition Penalty",
185
+ value=1.2,
186
+ minimum=1.0,
187
+ maximum=2.0,
188
+ step=0.05,
189
+ interactive=True,
190
+ info="Penalize repeated tokens",
191
+ )
192
+ ],
193
+ inputs=[gr.Textbox, gr.Chatbot, get_agent_cluster],
194
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
+ demo.queue().launch(debug=True)