Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,10 +10,11 @@ client = InferenceClient(
|
|
| 10 |
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
def format_prompt(message, history,
|
| 14 |
-
"""Formats the prompt with the selected agent
|
| 15 |
prompt = f"""
|
| 16 |
-
You are an expert
|
|
|
|
| 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,
|
| 46 |
-
"""Generates a response using the selected agent
|
| 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,
|
| 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"
|
| 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,
|
| 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,
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 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 |
-
|
|
|
|
| 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)
|