acecalisto3 commited on
Commit
20e0ec5
1 Parent(s): 23513ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +443 -194
app.py CHANGED
@@ -1,203 +1,452 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
- import logging
4
- import torch
5
- import numpy as np
6
- from huggingface_hub import InferenceClient
7
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
10
 
11
- def format_prompt(message, history):
12
- prompt = "[Instuction]:\n"
13
- for user_prompt, bot_response in history:
14
- prompt += f"\n{user_prompt}: {bot_response}"
15
-
16
- prompt += f"\n{message}<|endoftext|>\n"
17
- return prompt
18
 
19
- def generate(input_text, history, system_prompt, temperature, max_length, top_p, repetition_penalty):
20
- input_text = input_text.strip()
21
- history = history[:-1] if input_text == '[continue conversation]' else history
22
- history = list(filter(lambda x: x[0] != '', history))
 
23
 
24
- system_prompt = system_prompt.replace("[INST]", "[Instruction]").strip() + "\n"
25
- prompt = format_prompt(input_text, history)
26
-
27
- result = client.generate(
28
- prompt,
29
- max_length=int(max_length),
30
- temperature=float(temperature),
31
- top_p=float(top_p),
32
- repetition_penalty=float(repetition_penalty),
33
- num_return_sequences=1,
34
- do_sample=True
35
- )[0]['generated_text'].strip()
36
-
37
- return {"history": history + [(input_text, result)], "result": result}
38
-
39
- iface = gr.Interface(fn=generate,
40
- inputs=gr.Inputs(text="input_text",
41
- textarea="system_prompt",
42
- sliders={"temperature": (0.0, 1.0, 0.1),
43
- "max_length": (20, 256, 1),
44
- "top_p": (0.1, 1.0, 0.1),
45
- "repetition_penalty": (1.0, 2.0, 0.1)}),
46
- outputs="markdown",
47
- interpretation="notext",
48
- examples="""{"My first question"}, {"input_text"}, {"How old are you?"}, {"system_prompt"}, {"temperature":(0.5)}, {"max_length":(max_legnth)}, {"top_p":(0.5)}, {"repetition_penalty":(1)}"""
49
- allow_flagging="never"),
50
-
51
-
52
- # Logging Setup
53
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
54
-
55
- # Initialize session state
56
- if 'generated_codes' not in st.session_state:
57
- st.session_state.generated_codes = []
58
-
59
- # Function to get the model pipeline
60
- @st.cache_resource
61
- def get_model_pipeline(model_name):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  try:
63
- code_pipeline = pipeline("text-generation", model=model_name, device=0 if torch.cuda.is_available() else -1)
64
- return code_pipeline
65
- except Exception as e:
66
- logging.error(f"Error loading model pipeline: {e}")
67
- return None
68
-
69
- # Beam search implementation
70
- def beam_search(model, prompt, beam_width=3, max_length=20):
71
- sequences = [[list(prompt), 0.0]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- for _ in range(max_length):
74
- all_candidates = list()
75
-
76
- for seq, score in sequences:
77
- if len(seq) > 0 and seq[-1] == model.tokenizer.eos_token_id:
78
- all_candidates.append((seq, score))
79
- continue
80
-
81
- inputs = model.tokenizer(seq, return_tensors='pt')
82
- outputs = model.model(**inputs)
83
- logits = outputs.logits[0, -1, :]
84
- probabilities = torch.nn.functional.softmax(logits, dim=-1).detach().cpu().numpy()
85
-
86
- candidates = np.argsort(probabilities)[-beam_width:]
87
-
88
- for candidate in candidates:
89
- new_seq = seq + [candidate]
90
- new_score = score + np.log(probabilities[candidate])
91
- all_candidates.append((new_seq, new_score))
92
-
93
- ordered = sorted(all_candidates, key=lambda tup: tup[1], reverse=True)
94
- sequences = ordered[:beam_width]
95
-
96
- return sequences[0][0]
97
-
98
- # Function to generate code
99
- @st.cache_data
100
- def generate_code(task_description, max_length, temperature, num_return_sequences, model_name, beam_width=3):
101
- code_pipeline = get_model_pipeline(model_name)
102
- if code_pipeline is None:
103
- return ["Error: Failed to load model pipeline."]
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  try:
106
- logging.info(f"Generating code with input: {task_description}")
107
- prompt = f"Develop code for the following task: {task_description}"
108
-
109
- # Tokenize prompt for beam search
110
- inputs = code_pipeline.tokenizer(prompt, return_tensors='pt')
111
- input_ids = inputs['input_ids'][0].tolist()
112
-
113
- outputs = []
114
- for _ in range(num_return_sequences):
115
- output_tokens = beam_search(code_pipeline, input_ids, beam_width=beam_width, max_length=max_length)
116
- output_text = code_pipeline.tokenizer.decode(output_tokens, skip_special_tokens=True)
117
- outputs.append({'generated_text': output_text})
118
-
119
- codes = [output['generated_text'] for output in outputs]
120
-
121
- logging.info("Code generation completed successfully.")
122
- return codes
123
- except Exception as e:
124
- logging.error(f"Error generating code: {e}")
125
- return [f"Error generating code: {e}"]
126
-
127
- # Function to save code to a file
128
- def save_code(code, file_name):
129
- try:
130
- with open(file_name, "w") as file:
131
- file.write(code)
132
- return True
133
- except Exception as e:
134
- logging.error(f"Error saving code: {e}")
135
- return False
136
-
137
- # Main function for Streamlit app
138
- def main():
139
- st.set_page_config(page_title="Advanced Code Generator", layout="wide")
140
-
141
- st.title("Advanced Code Generator")
142
- st.markdown("This application generates code based on the given task description using a text-generation model.")
143
-
144
- # Model Selection
145
- model_name = st.selectbox("Select Model", [
146
- "EleutherAI/gpt-neo-2.7B",
147
- "EleutherAI/gpt-j-6B",
148
- "bigscience/bloom-1b7",
149
- "openai-gpt",
150
- "gpt2-xl"
151
- ], help="Choose the model for code generation.")
152
-
153
- # Input Section
154
- st.header("Task Description")
155
- task_description = st.text_area("Describe the task for which you need code:", height=150)
156
-
157
- # Options Section
158
- st.header("Options")
159
- col1, col2, col3, col4 = st.columns(4)
160
- with col1:
161
- max_length = st.slider("Max Length", min_value=50, max_value=2048, value=250, step=50, help="Maximum length of the generated code.")
162
- with col2:
163
- temperature = st.slider("Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.1, help="Controls the creativity of the generated code.")
164
- with col3:
165
- num_return_sequences = st.slider("Number of Sequences", min_value=1, max_value=5, value=1, step=1, help="Number of code snippets to generate.")
166
- with col4:
167
- beam_width = st.slider("Beam Width", min_value=1, max_value=10, value=3, step=1, help="Beam width for beam search.")
168
-
169
- # Generate Code Button
170
- if st.button("Generate Code"):
171
- if task_description.strip():
172
- # Clear previous generated codes
173
- st.session_state.generated_codes = []
174
- with st.spinner("Generating code..."):
175
- st.session_state.generated_codes = generate_code(task_description, max_length, temperature, num_return_sequences, model_name, beam_width)
176
- st.header("Generated Code")
177
- for idx, code in enumerate(st.session_state.generated_codes):
178
- with st.expander(f"Generated Code {idx + 1}", expanded=True):
179
- st.code(code, language='python')
180
- else:
181
- st.error("Please enter a task description.")
182
-
183
- # Save Code Section
184
- if st.session_state.generated_codes:
185
- st.header("Save Code")
186
- selected_code_idx = st.selectbox("Select which code to save:", range(1, len(st.session_state.generated_codes) + 1)) - 1
187
- col1, col2 = st.columns(2)
188
- with col1:
189
- file_name = st.text_input("Enter file name to save:", value="generated_code.py")
190
- with col2:
191
- save_button = st.button("Save", key="save_code")
192
-
193
- if save_button:
194
- if file_name:
195
- if save_code(st.session_state.generated_codes[selected_code_idx], file_name):
196
- st.success(f"Code saved to {file_name}")
197
- else:
198
- st.error("Failed to save the code. Please try again.")
199
- else:
200
- st.error("Please enter a valid file name.")
201
-
202
- if __name__ == "__main__":
203
- 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)