Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,19 +8,22 @@ import time
|
|
8 |
import shutil
|
9 |
from typing import Dict, Tuple
|
10 |
|
11 |
-
# Constants
|
|
|
12 |
API_URL = "https://api-inference.huggingface.co/models/"
|
13 |
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1" # Replace with your desired model
|
|
|
|
|
14 |
DEFAULT_TEMPERATURE = 0.9
|
15 |
DEFAULT_MAX_NEW_TOKENS = 2048
|
16 |
DEFAULT_TOP_P = 0.95
|
17 |
DEFAULT_REPETITION_PENALTY = 1.2
|
|
|
|
|
18 |
LOCAL_HOST_PORT = 7860
|
19 |
|
20 |
-
#
|
21 |
-
client = InferenceClient(MODEL_NAME)
|
22 |
|
23 |
-
# Define agent roles and their initial states
|
24 |
agent_roles: Dict[str, Dict[str, bool]] = {
|
25 |
"Web Developer": {"description": "A master of front-end and back-end web development.", "active": False},
|
26 |
"Prompt Engineer": {"description": "An expert in crafting effective prompts for AI models.", "active": False},
|
@@ -29,10 +32,9 @@ agent_roles: Dict[str, Dict[str, bool]] = {
|
|
29 |
"AI-Powered Code Assistant": {"description": "An AI assistant that can help with coding tasks and provide code snippets.", "active": False},
|
30 |
}
|
31 |
|
32 |
-
#
|
33 |
-
selected_agent = list(agent_roles.keys())[0]
|
34 |
|
35 |
-
|
36 |
initial_prompt = f"""
|
37 |
You are an expert {selected_agent} who responds with complete program coding to client requests.
|
38 |
Using available tools, please explain the researched information.
|
@@ -56,14 +58,17 @@ Please make sure to answer in the language used by the user. If the user asks in
|
|
56 |
But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
|
57 |
"""
|
58 |
|
59 |
-
# Custom CSS
|
|
|
60 |
customCSS = """
|
61 |
-
#component-7 {
|
62 |
-
height: 1600px;
|
63 |
flex-grow: 4;
|
64 |
}
|
65 |
"""
|
66 |
|
|
|
|
|
67 |
# Function to toggle the active state of an agent
|
68 |
def toggle_agent(agent_name: str) -> str:
|
69 |
"""Toggles the active state of an agent."""
|
@@ -151,7 +156,7 @@ def generate(prompt: str, history: list[Tuple[str, str]], agent_roles: list[str]
|
|
151 |
return output
|
152 |
|
153 |
# Function to handle user input and generate responses
|
154 |
-
def chat_interface(message: str, history: list[Tuple[str, str]], agent_cluster: Dict[str, bool], temperature: float
|
155 |
"""Handles user input and generates responses."""
|
156 |
if message.startswith("python"):
|
157 |
# User entered code, execute it
|
@@ -246,22 +251,23 @@ def ship_button_click(app_name: str, code: str) -> str:
|
|
246 |
# Return a success message
|
247 |
return f"Web app '{app_name}' shipped successfully!"
|
248 |
|
249 |
-
#
|
|
|
250 |
with gr.Blocks(theme='ParityError/Interstellar') as demo:
|
251 |
-
# Agent
|
252 |
with gr.Row():
|
253 |
for agent_name, agent_data in agent_roles.items():
|
254 |
button = gr.Button(agent_name, variant="secondary")
|
255 |
textbox = gr.Textbox(agent_data["description"], interactive=False)
|
256 |
button.click(toggle_agent, inputs=[button], outputs=[textbox])
|
257 |
|
258 |
-
# Chat
|
259 |
with gr.Row():
|
260 |
chatbot = gr.Chatbot()
|
261 |
chat_interface_input = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
262 |
chat_interface_output = gr.Textbox(label="Response", interactive=False)
|
263 |
|
264 |
-
# Parameters
|
265 |
temperature_slider = gr.Slider(
|
266 |
label="Temperature",
|
267 |
value=DEFAULT_TEMPERATURE,
|
@@ -299,15 +305,15 @@ with gr.Blocks(theme='ParityError/Interstellar') as demo:
|
|
299 |
info="Penalize repeated tokens",
|
300 |
)
|
301 |
|
302 |
-
# Submit
|
303 |
submit_button = gr.Button("Submit")
|
304 |
|
305 |
-
#
|
306 |
submit_button.click(
|
307 |
chat_interface,
|
308 |
inputs=[
|
309 |
-
chat_interface_input,
|
310 |
-
chatbot,
|
311 |
get_agent_cluster,
|
312 |
temperature_slider,
|
313 |
max_new_tokens_slider,
|
@@ -320,7 +326,7 @@ with gr.Blocks(theme='ParityError/Interstellar') as demo:
|
|
320 |
],
|
321 |
)
|
322 |
|
323 |
-
# Web
|
324 |
with gr.Row():
|
325 |
app_name_input = gr.Textbox(label="App Name", placeholder="Enter your app name")
|
326 |
code_output = gr.Textbox(label="Code", interactive=False)
|
@@ -329,7 +335,7 @@ with gr.Blocks(theme='ParityError/Interstellar') as demo:
|
|
329 |
local_host_button = gr.Button("Local Host")
|
330 |
ship_button = gr.Button("Ship")
|
331 |
|
332 |
-
#
|
333 |
create_web_app_button.click(
|
334 |
create_web_app_button_click,
|
335 |
inputs=[code_output],
|
@@ -357,12 +363,12 @@ with gr.Blocks(theme='ParityError/Interstellar') as demo:
|
|
357 |
outputs=[gr.Textbox(label="Status", interactive=False)],
|
358 |
)
|
359 |
|
360 |
-
# Connect
|
361 |
chat_interface_output.change(
|
362 |
lambda x: x,
|
363 |
inputs=[chat_interface_output],
|
364 |
outputs=[code_output],
|
365 |
)
|
366 |
|
367 |
-
# Launch
|
368 |
demo.queue().launch(debug=True)
|
|
|
8 |
import shutil
|
9 |
from typing import Dict, Tuple
|
10 |
|
11 |
+
# --- Constants ---
|
12 |
+
|
13 |
API_URL = "https://api-inference.huggingface.co/models/"
|
14 |
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1" # Replace with your desired model
|
15 |
+
|
16 |
+
# Chat Interface Parameters
|
17 |
DEFAULT_TEMPERATURE = 0.9
|
18 |
DEFAULT_MAX_NEW_TOKENS = 2048
|
19 |
DEFAULT_TOP_P = 0.95
|
20 |
DEFAULT_REPETITION_PENALTY = 1.2
|
21 |
+
|
22 |
+
# Local Server
|
23 |
LOCAL_HOST_PORT = 7860
|
24 |
|
25 |
+
# --- Agent Roles ---
|
|
|
26 |
|
|
|
27 |
agent_roles: Dict[str, Dict[str, bool]] = {
|
28 |
"Web Developer": {"description": "A master of front-end and back-end web development.", "active": False},
|
29 |
"Prompt Engineer": {"description": "An expert in crafting effective prompts for AI models.", "active": False},
|
|
|
32 |
"AI-Powered Code Assistant": {"description": "An AI assistant that can help with coding tasks and provide code snippets.", "active": False},
|
33 |
}
|
34 |
|
35 |
+
# --- Initial Prompt ---
|
|
|
36 |
|
37 |
+
selected_agent = list(agent_roles.keys())[0]
|
38 |
initial_prompt = f"""
|
39 |
You are an expert {selected_agent} who responds with complete program coding to client requests.
|
40 |
Using available tools, please explain the researched information.
|
|
|
58 |
But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
|
59 |
"""
|
60 |
|
61 |
+
# --- Custom CSS ---
|
62 |
+
|
63 |
customCSS = """
|
64 |
+
#component-7 {
|
65 |
+
height: 1600px;
|
66 |
flex-grow: 4;
|
67 |
}
|
68 |
"""
|
69 |
|
70 |
+
# --- Functions ---
|
71 |
+
|
72 |
# Function to toggle the active state of an agent
|
73 |
def toggle_agent(agent_name: str) -> str:
|
74 |
"""Toggles the active state of an agent."""
|
|
|
156 |
return output
|
157 |
|
158 |
# Function to handle user input and generate responses
|
159 |
+
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]:
|
160 |
"""Handles user input and generates responses."""
|
161 |
if message.startswith("python"):
|
162 |
# User entered code, execute it
|
|
|
251 |
# Return a success message
|
252 |
return f"Web app '{app_name}' shipped successfully!"
|
253 |
|
254 |
+
# --- Gradio Interface ---
|
255 |
+
|
256 |
with gr.Blocks(theme='ParityError/Interstellar') as demo:
|
257 |
+
# --- Agent Selection ---
|
258 |
with gr.Row():
|
259 |
for agent_name, agent_data in agent_roles.items():
|
260 |
button = gr.Button(agent_name, variant="secondary")
|
261 |
textbox = gr.Textbox(agent_data["description"], interactive=False)
|
262 |
button.click(toggle_agent, inputs=[button], outputs=[textbox])
|
263 |
|
264 |
+
# --- Chat Interface ---
|
265 |
with gr.Row():
|
266 |
chatbot = gr.Chatbot()
|
267 |
chat_interface_input = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
268 |
chat_interface_output = gr.Textbox(label="Response", interactive=False)
|
269 |
|
270 |
+
# Parameters
|
271 |
temperature_slider = gr.Slider(
|
272 |
label="Temperature",
|
273 |
value=DEFAULT_TEMPERATURE,
|
|
|
305 |
info="Penalize repeated tokens",
|
306 |
)
|
307 |
|
308 |
+
# Submit Button
|
309 |
submit_button = gr.Button("Submit")
|
310 |
|
311 |
+
# Chat Interface Logic
|
312 |
submit_button.click(
|
313 |
chat_interface,
|
314 |
inputs=[
|
315 |
+
chat_interface_input._id,
|
316 |
+
chatbot._id,
|
317 |
get_agent_cluster,
|
318 |
temperature_slider,
|
319 |
max_new_tokens_slider,
|
|
|
326 |
],
|
327 |
)
|
328 |
|
329 |
+
# --- Web App Creation ---
|
330 |
with gr.Row():
|
331 |
app_name_input = gr.Textbox(label="App Name", placeholder="Enter your app name")
|
332 |
code_output = gr.Textbox(label="Code", interactive=False)
|
|
|
335 |
local_host_button = gr.Button("Local Host")
|
336 |
ship_button = gr.Button("Ship")
|
337 |
|
338 |
+
# Web App Creation Logic
|
339 |
create_web_app_button.click(
|
340 |
create_web_app_button_click,
|
341 |
inputs=[code_output],
|
|
|
363 |
outputs=[gr.Textbox(label="Status", interactive=False)],
|
364 |
)
|
365 |
|
366 |
+
# --- Connect Chat Output to Code Output ---
|
367 |
chat_interface_output.change(
|
368 |
lambda x: x,
|
369 |
inputs=[chat_interface_output],
|
370 |
outputs=[code_output],
|
371 |
)
|
372 |
|
373 |
+
# --- Launch Gradio ---
|
374 |
demo.queue().launch(debug=True)
|