Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,344 +1,280 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
from safe_search import safe_search
|
| 7 |
-
from i_search import google
|
| 8 |
-
from i_search import i_search as i_s
|
| 9 |
-
from agent import (
|
| 10 |
-
ACTION_PROMPT,
|
| 11 |
-
ADD_PROMPT,
|
| 12 |
-
COMPRESS_HISTORY_PROMPT,
|
| 13 |
-
LOG_PROMPT,
|
| 14 |
-
LOG_RESPONSE,
|
| 15 |
-
MODIFY_PROMPT,
|
| 16 |
-
PREFIX,
|
| 17 |
-
SEARCH_QUERY,
|
| 18 |
-
READ_PROMPT,
|
| 19 |
-
TASK_PROMPT,
|
| 20 |
-
UNDERSTAND_TEST_RESULTS_PROMPT,
|
| 21 |
-
)
|
| 22 |
-
from utils import parse_action, parse_file_content, read_python_module_structure
|
| 23 |
from datetime import datetime
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def format_prompt(message, history):
|
| 39 |
-
prompt = "
|
| 40 |
-
for user_prompt, bot_response in history:
|
| 41 |
-
prompt += f"
|
| 42 |
-
|
| 43 |
-
prompt += f"[INST] {message} [/INST]"
|
| 44 |
return prompt
|
| 45 |
|
| 46 |
-
def
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
date_time_str=date_time_str,
|
| 60 |
-
purpose=
|
| 61 |
-
|
| 62 |
-
) +
|
|
|
|
| 63 |
if VERBOSE:
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
resp = ""
|
| 71 |
-
for response in stream:
|
| 72 |
-
resp += response.token.text
|
| 73 |
|
| 74 |
if VERBOSE:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
max_tokens=512,
|
| 83 |
-
purpose=purpose,
|
| 84 |
-
task=task,
|
| 85 |
-
history=history,
|
| 86 |
-
)
|
| 87 |
-
history = "observation: {}\n".format(resp)
|
| 88 |
-
return history
|
| 89 |
-
|
| 90 |
-
def call_search(purpose, task, history, directory, action_input):
|
| 91 |
-
print("CALLING SEARCH")
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
max_tokens=2096,
|
| 114 |
-
purpose=purpose,
|
| 115 |
-
task=task,
|
| 116 |
-
history=history,
|
| 117 |
-
)
|
| 118 |
-
lines = resp.strip().strip("\n").split("\n")
|
| 119 |
-
for line in lines:
|
| 120 |
-
if line == "":
|
| 121 |
-
continue
|
| 122 |
-
if line.startswith("thought: "):
|
| 123 |
-
history += "{}\n".format(line)
|
| 124 |
-
elif line.startswith("action: "):
|
| 125 |
-
action_name, action_input = parse_action(line)
|
| 126 |
-
print(f'ACTION_NAME :: {action_name}')
|
| 127 |
-
print(f'ACTION_INPUT :: {action_input}')
|
| 128 |
-
|
| 129 |
-
history += "{}\n".format(line)
|
| 130 |
-
if "COMPLETE" in action_name or "COMPLETE" in action_input:
|
| 131 |
-
task = "END"
|
| 132 |
-
return action_name, action_input, history, task
|
| 133 |
-
else:
|
| 134 |
-
return action_name, action_input, history, task
|
| 135 |
else:
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
max_tokens=64,
|
| 148 |
-
purpose=purpose,
|
| 149 |
-
task=task,
|
| 150 |
-
history=history,
|
| 151 |
-
).strip("\n")
|
| 152 |
-
history += "observation: task has been updated to: {}\n".format(task)
|
| 153 |
-
return "MAIN", None, history, task
|
| 154 |
-
|
| 155 |
-
def end_fn(purpose, task, history, directory, action_input):
|
| 156 |
-
task = "END"
|
| 157 |
-
return "COMPLETE", "COMPLETE", history, task
|
| 158 |
-
|
| 159 |
-
NAME_TO_FUNC = {
|
| 160 |
-
"MAIN": call_main,
|
| 161 |
-
"UPDATE-TASK": call_set_task,
|
| 162 |
-
"SEARCH": call_search,
|
| 163 |
-
"COMPLETE": end_fn,
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
def run_action(purpose, task, history, directory, action_name, action_input):
|
| 167 |
-
print(f'action_name::{action_name}')
|
| 168 |
try:
|
| 169 |
-
if
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
print("RUN: ", action_name, action_input)
|
| 186 |
-
return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
|
| 187 |
except Exception as e:
|
| 188 |
-
|
| 189 |
-
return "MAIN", None, history, task
|
| 190 |
-
|
| 191 |
-
def run(purpose, history):
|
| 192 |
-
# print(purpose)
|
| 193 |
-
# print(hist)
|
| 194 |
-
task = None
|
| 195 |
-
directory = "./"
|
| 196 |
-
if history:
|
| 197 |
-
history = str(history).strip("[]")
|
| 198 |
-
if not history:
|
| 199 |
-
history = ""
|
| 200 |
-
|
| 201 |
-
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
| 202 |
-
action_input = None
|
| 203 |
-
while True:
|
| 204 |
-
print("")
|
| 205 |
-
print("")
|
| 206 |
-
print("---")
|
| 207 |
-
print("purpose:", purpose)
|
| 208 |
-
print("task:", task)
|
| 209 |
-
print("---")
|
| 210 |
-
print(history)
|
| 211 |
-
print("---")
|
| 212 |
-
|
| 213 |
-
action_name, action_input, history, task = run_action(
|
| 214 |
-
purpose,
|
| 215 |
-
task,
|
| 216 |
-
history,
|
| 217 |
-
directory,
|
| 218 |
-
action_name,
|
| 219 |
-
action_input,
|
| 220 |
-
)
|
| 221 |
-
yield (history)
|
| 222 |
-
# yield ("",[(purpose,history)])
|
| 223 |
-
if task == "END":
|
| 224 |
-
return (history)
|
| 225 |
-
# return ("", [(purpose,history)])
|
| 226 |
-
|
| 227 |
-
################################################
|
| 228 |
-
|
| 229 |
-
def format_prompt(message, history):
|
| 230 |
-
prompt = "<s>"
|
| 231 |
-
for user_prompt, bot_response in history:
|
| 232 |
-
prompt += f"[INST] {user_prompt} [/INST]"
|
| 233 |
-
prompt += f" {bot_response}</s> "
|
| 234 |
-
prompt += f"[INST] {message} [/INST]"
|
| 235 |
-
return prompt
|
| 236 |
|
| 237 |
-
|
| 238 |
-
"
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
)
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import random
|
| 4 |
+
import time
|
| 5 |
+
from typing import Dict, List, Tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
+
import logging
|
| 8 |
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 11 |
+
from huggingface_hub import InferenceClient, cached_download, Repository, HfApi
|
| 12 |
+
from IPython.display import display, HTML
|
| 13 |
|
| 14 |
+
# --- Configuration ---
|
| 15 |
+
VERBOSE = True
|
| 16 |
+
MAX_HISTORY = 5
|
| 17 |
+
MAX_TOKENS = 2048
|
| 18 |
+
TEMPERATURE = 0.7
|
| 19 |
+
TOP_P = 0.8
|
| 20 |
+
REPETITION_PENALTY = 1.5
|
| 21 |
+
DEFAULT_PROJECT_PATH = "./my-hf-project" # Default project directory
|
| 22 |
+
|
| 23 |
+
# --- Logging Setup ---
|
| 24 |
+
logging.basicConfig(
|
| 25 |
+
filename="app.log",
|
| 26 |
+
level=logging.INFO,
|
| 27 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# --- Global Variables ---
|
| 31 |
+
current_model = None # Store the currently loaded model
|
| 32 |
+
repo = None # Store the Hugging Face Repository object
|
| 33 |
+
model_descriptions = {} # Store model descriptions
|
| 34 |
+
|
| 35 |
+
# --- Functions ---
|
| 36 |
+
def format_prompt(message: str, history: List[Tuple[str, str]], max_history_turns: int = 2) -> str:
|
| 37 |
+
prompt = ""
|
| 38 |
+
for user_prompt, bot_response in history[-max_history_turns:]:
|
| 39 |
+
prompt += f"Human: {user_prompt}\nAssistant: {bot_response}\n"
|
| 40 |
+
prompt += f"Human: {message}\nAssistant:"
|
|
|
|
| 41 |
return prompt
|
| 42 |
|
| 43 |
+
def generate_response(
|
| 44 |
+
prompt: str,
|
| 45 |
+
history: List[Tuple[str, str]],
|
| 46 |
+
agent_name: str = "Generic Agent",
|
| 47 |
+
sys_prompt: str = "",
|
| 48 |
+
temperature: float = TEMPERATURE,
|
| 49 |
+
max_new_tokens: int = MAX_TOKENS,
|
| 50 |
+
top_p: float = TOP_P,
|
| 51 |
+
repetition_penalty: float = REPETITION_PENALTY,
|
| 52 |
+
) -> str:
|
| 53 |
+
global current_model
|
| 54 |
+
if current_model is None:
|
| 55 |
+
return "Error: Please load a model first."
|
| 56 |
+
|
| 57 |
+
date_time_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
+
full_prompt = PREFIX.format(
|
| 59 |
date_time_str=date_time_str,
|
| 60 |
+
purpose=sys_prompt,
|
| 61 |
+
agent_name=agent_name
|
| 62 |
+
) + format_prompt(prompt, history)
|
| 63 |
+
|
| 64 |
if VERBOSE:
|
| 65 |
+
logging.info(LOG_PROMPT.format(content=full_prompt))
|
| 66 |
|
| 67 |
+
response = current_model(
|
| 68 |
+
full_prompt,
|
| 69 |
+
max_new_tokens=max_new_tokens,
|
| 70 |
+
temperature=temperature,
|
| 71 |
+
top_p=top_p,
|
| 72 |
+
repetition_penalty=repetition_penalty,
|
| 73 |
+
do_sample=True
|
| 74 |
+
)[0]['generated_text']
|
| 75 |
|
| 76 |
+
assistant_response = response.split("Assistant:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
if VERBOSE:
|
| 79 |
+
logging.info(LOG_RESPONSE.format(resp=assistant_response))
|
| 80 |
+
|
| 81 |
+
return assistant_response
|
| 82 |
+
|
| 83 |
+
def load_hf_model(model_name: str):
|
| 84 |
+
"""Loads a language model and fetches its description."""
|
| 85 |
+
global current_model, model_descriptions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
try:
|
| 87 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 88 |
+
current_model = pipeline(
|
| 89 |
+
"text-generation",
|
| 90 |
+
model=model_name,
|
| 91 |
+
tokenizer=tokenizer,
|
| 92 |
+
model_kwargs={"load_in_8bit": True}
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Fetch and store the model description
|
| 96 |
+
api = HfApi()
|
| 97 |
+
model_info = api.model_info(model_name)
|
| 98 |
+
model_descriptions[model_name] = model_info.pipeline_tag
|
| 99 |
+
return f"Successfully loaded model: {model_name}"
|
| 100 |
except Exception as e:
|
| 101 |
+
return f"Error loading model: {str(e)}"
|
| 102 |
+
|
| 103 |
+
def execute_command(command: str, project_path: str = None) -> str:
|
| 104 |
+
"""Executes a shell command and returns the output."""
|
| 105 |
+
try:
|
| 106 |
+
if project_path:
|
| 107 |
+
process = subprocess.Popen(command, shell=True, cwd=project_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
+
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 110 |
+
output, error = process.communicate()
|
| 111 |
+
if error:
|
| 112 |
+
return f"Error: {error.decode('utf-8')}"
|
| 113 |
+
return output.decode("utf-8")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return f"Error executing command: {str(e)}"
|
| 116 |
+
|
| 117 |
+
def create_hf_project(project_name: str, project_path: str = DEFAULT_PROJECT_PATH):
|
| 118 |
+
"""Creates a new Hugging Face project."""
|
| 119 |
+
global repo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
try:
|
| 121 |
+
if os.path.exists(project_path):
|
| 122 |
+
return f"Error: Directory '{project_path}' already exists!"
|
| 123 |
+
# Create the repository
|
| 124 |
+
repo = Repository(local_dir=project_path, clone_from=None)
|
| 125 |
+
repo.git_init()
|
| 126 |
+
|
| 127 |
+
# Add basic files (optional, you can customize this)
|
| 128 |
+
with open(os.path.join(project_path, "README.md"), "w") as f:
|
| 129 |
+
f.write(f"# {project_name}\n\nA new Hugging Face project.")
|
| 130 |
+
|
| 131 |
+
# Stage all changes
|
| 132 |
+
repo.git_add(pattern="*")
|
| 133 |
+
repo.git_commit(commit_message="Initial commit")
|
| 134 |
+
|
| 135 |
+
return f"Hugging Face project '{project_name}' created successfully at '{project_path}'"
|
|
|
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
+
return f"Error creating Hugging Face project: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
def list_project_files(project_path: str = DEFAULT_PROJECT_PATH) -> str:
|
| 140 |
+
"""Lists files in the project directory."""
|
| 141 |
+
try:
|
| 142 |
+
files = os.listdir(project_path)
|
| 143 |
+
if not files:
|
| 144 |
+
return "Project directory is empty."
|
| 145 |
+
return "\n".join(files)
|
| 146 |
+
except Exception as e:
|
| 147 |
+
return f"Error listing project files: {str(e)}"
|
| 148 |
+
|
| 149 |
+
def read_file_content(file_path: str, project_path: str = DEFAULT_PROJECT_PATH) -> str:
|
| 150 |
+
"""Reads and returns the content of a file in the project."""
|
| 151 |
+
try:
|
| 152 |
+
full_path = os.path.join(project_path, file_path)
|
| 153 |
+
with open(full_path, "r") as f:
|
| 154 |
+
content = f.read()
|
| 155 |
+
return content
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return f"Error reading file: {str(e)}"
|
| 158 |
+
|
| 159 |
+
def write_to_file(file_path: str, content: str, project_path: str = DEFAULT_PROJECT_PATH) -> str:
|
| 160 |
+
"""Writes content to a file in the project."""
|
| 161 |
+
try:
|
| 162 |
+
full_path = os.path.join(project_path, file_path)
|
| 163 |
+
with open(full_path, "w") as f:
|
| 164 |
+
f.write(content)
|
| 165 |
+
return f"Successfully wrote to '{file_path}'"
|
| 166 |
+
except Exception as e:
|
| 167 |
+
return f"Error writing to file: {str(e)}"
|
| 168 |
+
|
| 169 |
+
def preview_project(project_path: str = DEFAULT_PROJECT_PATH):
|
| 170 |
+
"""Provides a preview of the project, if applicable."""
|
| 171 |
+
# Assuming a simple HTML preview for now
|
| 172 |
+
try:
|
| 173 |
+
index_html_path = os.path.join(project_path, "index.html")
|
| 174 |
+
if os.path.exists(index_html_path):
|
| 175 |
+
with open(index_html_path, "r") as f:
|
| 176 |
+
html_content = f.read()
|
| 177 |
+
display(HTML(html_content))
|
| 178 |
+
return "Previewing 'index.html'"
|
| 179 |
+
else:
|
| 180 |
+
return "No 'index.html' found for preview."
|
| 181 |
+
except Exception as e:
|
| 182 |
+
return f"Error previewing project: {str(e)}"
|
| 183 |
+
|
| 184 |
+
def main():
|
| 185 |
+
with gr.Blocks() as demo:
|
| 186 |
+
gr.Markdown("## FragMixt: Your Hugging Face No-Code App Builder")
|
| 187 |
+
|
| 188 |
+
# --- Model Selection ---
|
| 189 |
+
with gr.Tab("Model"):
|
| 190 |
+
# --- Model Dropdown with Categories ---
|
| 191 |
+
model_categories = gr.Dropdown(
|
| 192 |
+
choices=["Text Generation", "Text Summarization", "Code Generation", "Translation", "Question Answering"],
|
| 193 |
+
label="Model Category",
|
| 194 |
+
value="Text Generation"
|
| 195 |
+
)
|
| 196 |
+
model_name = gr.Dropdown(
|
| 197 |
+
choices=[], # Initially empty, will be populated based on category
|
| 198 |
+
label="Hugging Face Model Name",
|
| 199 |
+
)
|
| 200 |
+
load_button = gr.Button("Load Model")
|
| 201 |
+
load_output = gr.Textbox(label="Output")
|
| 202 |
+
model_description = gr.Markdown(label="Model Description")
|
| 203 |
+
|
| 204 |
+
# --- Function to populate model names based on category ---
|
| 205 |
+
def update_model_dropdown(category):
|
| 206 |
+
models = []
|
| 207 |
+
api = HfApi()
|
| 208 |
+
for model in api.list_models():
|
| 209 |
+
if model.pipeline_tag == category:
|
| 210 |
+
models.append(model.modelId)
|
| 211 |
+
return gr.Dropdown.update(choices=models)
|
| 212 |
+
|
| 213 |
+
# --- Event handler for category dropdown ---
|
| 214 |
+
model_categories.change(
|
| 215 |
+
fn=update_model_dropdown,
|
| 216 |
+
inputs=model_categories,
|
| 217 |
+
outputs=model_name,
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# --- Event handler to display model description ---
|
| 221 |
+
def display_model_description(model_name):
|
| 222 |
+
global model_descriptions
|
| 223 |
+
if model_name in model_descriptions:
|
| 224 |
+
return model_descriptions[model_name]
|
| 225 |
+
else:
|
| 226 |
+
return "Model description not available."
|
| 227 |
+
|
| 228 |
+
model_name.change(
|
| 229 |
+
fn=display_model_description,
|
| 230 |
+
inputs=model_name,
|
| 231 |
+
outputs=model_description,
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
load_button.click(load_hf_model, inputs=model_name, outputs=load_output)
|
| 235 |
+
|
| 236 |
+
# --- Chat Interface ---
|
| 237 |
+
with gr.Tab("Chat"):
|
| 238 |
+
chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True)
|
| 239 |
+
message = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
| 240 |
+
purpose = gr.Textbox(label="Purpose", placeholder="What is the purpose of this interaction?")
|
| 241 |
+
agent_name = gr.Dropdown(label="Agents", choices=["Generic Agent"], value="Generic Agent", interactive=True)
|
| 242 |
+
sys_prompt = gr.Textbox(label="System Prompt", max_lines=1, interactive=True)
|
| 243 |
+
temperature = gr.Slider(label="Temperature", value=TEMPERATURE, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs")
|
| 244 |
+
max_new_tokens = gr.Slider(label="Max new tokens", value=MAX_TOKENS, minimum=0, maximum=1048 * 10, step=64, interactive=True, info="The maximum numbers of new tokens")
|
| 245 |
+
top_p = gr.Slider(label="Top-p (nucleus sampling)", value=TOP_P, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens")
|
| 246 |
+
repetition_penalty = gr.Slider(label="Repetition penalty", value=REPETITION_PENALTY, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
|
| 247 |
+
submit_button = gr.Button(value="Send")
|
| 248 |
+
history = gr.State([])
|
| 249 |
+
|
| 250 |
+
def run_chat(purpose: str, message: str, agent_name: str, sys_prompt: str, temperature: float, max_new_tokens: int, top_p: float, repetition_penalty: float, history: List[Tuple[str, str]]) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]]]:
|
| 251 |
+
response = generate_response(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
|
| 252 |
+
history.append((message, response))
|
| 253 |
+
return history, history
|
| 254 |
+
|
| 255 |
+
submit_button.click(run_chat, inputs=[purpose, message, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty, history], outputs=[chatbot, history])
|
| 256 |
+
|
| 257 |
+
# --- Project Management ---
|
| 258 |
+
with gr.Tab("Project"):
|
| 259 |
+
project_name = gr.Textbox(label="Project Name", placeholder="MyHuggingFaceApp")
|
| 260 |
+
create_project_button = gr.Button("Create Hugging Face Project")
|
| 261 |
+
project_output = gr.Textbox(label="Output", lines=5)
|
| 262 |
+
file_content = gr.Code(label="File Content", language="python", lines=20)
|
| 263 |
+
file_path = gr.Textbox(label="File Path (relative to project)", placeholder="src/main.py")
|
| 264 |
+
read_button = gr.Button("Read File")
|
| 265 |
+
write_button = gr.Button("Write to File")
|
| 266 |
+
command_input = gr.Textbox(label="Terminal Command", placeholder="pip install -r requirements.txt")
|
| 267 |
+
command_output = gr.Textbox(label="Command Output", lines=5)
|
| 268 |
+
run_command_button = gr.Button("Run Command")
|
| 269 |
+
preview_button = gr.Button("Preview Project")
|
| 270 |
+
|
| 271 |
+
create_project_button.click(create_hf_project, inputs=[project_name], outputs=project_output)
|
| 272 |
+
read_button.click(read_file_content, inputs=file_path, outputs=file_content)
|
| 273 |
+
write_button.click(write_to_file, inputs=[file_path, file_content], outputs=project_output)
|
| 274 |
+
run_command_button.click(execute_command, inputs=command_input, outputs=command_output)
|
| 275 |
+
preview_button.click(preview_project, outputs=project_output)
|
| 276 |
+
|
| 277 |
+
demo.launch()
|
| 278 |
+
|
| 279 |
+
if __name__ == "__main__":
|
| 280 |
+
main()
|