Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ from i_search import i_search as i_s
|
|
| 9 |
from datetime import datetime
|
| 10 |
import logging
|
| 11 |
import json
|
| 12 |
-
import nltk # Import nltk for sentence tokenization
|
| 13 |
|
| 14 |
now = datetime.now()
|
| 15 |
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -25,7 +24,7 @@ logging.basicConfig(
|
|
| 25 |
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 26 |
)
|
| 27 |
|
| 28 |
-
agents =
|
| 29 |
"WEB_DEV",
|
| 30 |
"AI_SYSTEM_PROMPT",
|
| 31 |
"PYTHON_CODE_DEV"
|
|
@@ -81,12 +80,12 @@ thought:
|
|
| 81 |
"""
|
| 82 |
|
| 83 |
def format_prompt(message, history, max_history_turns=2):
|
| 84 |
-
prompt = "
|
| 85 |
# Keep only the last 'max_history_turns' turns
|
| 86 |
for user_prompt, bot_response in history[-max_history_turns:]:
|
| 87 |
-
prompt += f"[INST] {user_prompt} [/
|
| 88 |
-
prompt += f" {bot_response}
|
| 89 |
-
prompt += f"[INST] {message} [/
|
| 90 |
return prompt
|
| 91 |
|
| 92 |
def run_gpt(
|
|
@@ -146,7 +145,7 @@ def compress_history(purpose, task, history, directory):
|
|
| 146 |
def call_search(purpose, task, history, directory, action_input):
|
| 147 |
logging.info(f"CALLING SEARCH: {action_input}")
|
| 148 |
try:
|
| 149 |
-
|
| 150 |
if "http" in action_input:
|
| 151 |
if "<" in action_input:
|
| 152 |
action_input = action_input.strip("<")
|
|
@@ -160,7 +159,7 @@ def call_search(purpose, task, history, directory, action_input):
|
|
| 160 |
else:
|
| 161 |
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
| 162 |
except Exception as e:
|
| 163 |
-
history += "observation: {}
|
| 164 |
return "MAIN", None, history, task
|
| 165 |
|
| 166 |
def call_main(purpose, task, history, directory, action_input):
|
|
@@ -291,236 +290,13 @@ def run(purpose,history):
|
|
| 291 |
################################################
|
| 292 |
|
| 293 |
def format_prompt(message, history, max_history_turns=5):
|
| 294 |
-
prompt = "
|
| 295 |
# Keep only the last 'max_history_turns' turns
|
| 296 |
for user_prompt, bot_response in history[-max_history_turns:]:
|
| 297 |
-
prompt += f"[INST] {user_prompt} [/
|
| 298 |
-
prompt += f" {bot_response}
|
| 299 |
-
prompt += f"[INST] {message} [/
|
| 300 |
return prompt
|
| 301 |
-
agents =[
|
| 302 |
-
"WEB_DEV",
|
| 303 |
-
"AI_SYSTEM_PROMPT",
|
| 304 |
-
"PYTHON_CODE_DEV"
|
| 305 |
-
]
|
| 306 |
-
def generate(
|
| 307 |
-
prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0,
|
| 308 |
-
):
|
| 309 |
-
seed = random.randint(1,1111111111111111)
|
| 310 |
-
|
| 311 |
-
# Correct the line:
|
| 312 |
-
if agent_name == "WEB_DEV":
|
| 313 |
-
agent = "You are a helpful AI assistant. You are a web developer."
|
| 314 |
-
if agent_name == "AI_SYSTEM_PROMPT":
|
| 315 |
-
agent = "You are a helpful AI assistant. You are an AI system."
|
| 316 |
-
if agent_name == "PYTHON_CODE_DEV":
|
| 317 |
-
agent = "You are a helpful AI assistant. You are a Python code developer."
|
| 318 |
-
system_prompt = agent
|
| 319 |
-
temperature = float(temperature)
|
| 320 |
-
if temperature < 1e-2:
|
| 321 |
-
temperature = 1e-2
|
| 322 |
-
top_p = float(top_p)
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
def generate_text_chunked(input_text, model, generation_parameters, max_tokens_to_generate):
|
| 327 |
-
"""Generates text in chunks to avoid token limit errors."""
|
| 328 |
-
sentences = nltk.sent_tokenize(input_text)
|
| 329 |
-
generated_text = []
|
| 330 |
-
generator = pipeline('text-generation', model=model)
|
| 331 |
-
|
| 332 |
-
for sentence in sentences:
|
| 333 |
-
# Tokenize the sentence and check if it's within the limit
|
| 334 |
-
tokens = generator.tokenizer(sentence).input_ids
|
| 335 |
-
if len(tokens) + max_tokens_to_generate <= 32768:
|
| 336 |
-
# Generate text for this chunk
|
| 337 |
-
response = generator(sentence, max_length=max_tokens_to_generate, **generation_parameters)
|
| 338 |
-
generated_text.append(response[0]['generated_text'])
|
| 339 |
-
else:
|
| 340 |
-
# Handle cases where the sentence is too long
|
| 341 |
-
# You could split the sentence further or skip it
|
| 342 |
-
print(f"Sentence too long: {sentence}")
|
| 343 |
-
|
| 344 |
-
return ''.join(generated_text)
|
| 345 |
-
|
| 346 |
-
formatted_prompt = format_prompt(prompt, history, max_history_turns=5) # Truncated history
|
| 347 |
-
logging.info(f"Formatted Prompt: {formatted_prompt}")
|
| 348 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 349 |
-
output = ""
|
| 350 |
-
|
| 351 |
-
for response in stream:
|
| 352 |
-
output += response.token.text
|
| 353 |
-
yield output
|
| 354 |
-
return output
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
additional_inputs=[
|
| 358 |
-
gr.Dropdown(
|
| 359 |
-
label="Agents",
|
| 360 |
-
choices=[s for s in agents],
|
| 361 |
-
value=agents[0],
|
| 362 |
-
interactive=True,
|
| 363 |
-
),
|
| 364 |
-
gr.Textbox(
|
| 365 |
-
label="System Prompt",
|
| 366 |
-
max_lines=1,
|
| 367 |
-
interactive=True,
|
| 368 |
-
),
|
| 369 |
-
gr.Slider(
|
| 370 |
-
label="Temperature",
|
| 371 |
-
value=0.9,
|
| 372 |
-
minimum=0.0,
|
| 373 |
-
maximum=1.0,
|
| 374 |
-
step=0.05,
|
| 375 |
-
interactive=True,
|
| 376 |
-
info="Higher values produce more diverse outputs",
|
| 377 |
-
),
|
| 378 |
-
|
| 379 |
-
gr.Slider(
|
| 380 |
-
label="Max new tokens",
|
| 381 |
-
value=1048*10,
|
| 382 |
-
minimum=0,
|
| 383 |
-
maximum=1048*10,
|
| 384 |
-
step=64,
|
| 385 |
-
interactive=True,
|
| 386 |
-
info="The maximum numbers of new tokens",
|
| 387 |
-
),
|
| 388 |
-
gr.Slider(
|
| 389 |
-
label="Top-p (nucleus sampling)",
|
| 390 |
-
value=0.90,
|
| 391 |
-
minimum=0.0,
|
| 392 |
-
maximum=1,
|
| 393 |
-
step=0.05,
|
| 394 |
-
interactive=True,
|
| 395 |
-
info="Higher values sample more low-probability tokens",
|
| 396 |
-
),
|
| 397 |
-
gr.Slider(
|
| 398 |
-
label="Repetition penalty",
|
| 399 |
-
value=1.2,
|
| 400 |
-
minimum=1.0,
|
| 401 |
-
maximum=2.0,
|
| 402 |
-
step=0.05,
|
| 403 |
-
interactive=True,
|
| 404 |
-
info="Penalize repeated tokens",
|
| 405 |
-
),
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
]
|
| 409 |
-
|
| 410 |
-
examples = [
|
| 411 |
-
["Help me set up TypeScript configurations and integrate ts-loader in my existing React project.",
|
| 412 |
-
"Update Webpack Configurations",
|
| 413 |
-
"Install Dependencies",
|
| 414 |
-
"Configure Ts-Loader",
|
| 415 |
-
"TypeChecking Rules Setup",
|
| 416 |
-
"React Specific Settings",
|
| 417 |
-
"Compilation Options",
|
| 418 |
-
"Test Runner Configuration"],
|
| 419 |
-
|
| 420 |
-
["Guide me through building a serverless microservice using AWS Lambda and API Gateway, connecting to DynamoDB for storage.",
|
| 421 |
-
"Set Up AWS Account",
|
| 422 |
-
"Create Lambda Function",
|
| 423 |
-
"APIGateway Integration",
|
| 424 |
-
"Define DynamoDB Table Scheme",
|
| 425 |
-
"Connect Service To DB",
|
| 426 |
-
"Add Authentication Layers",
|
| 427 |
-
"Monitor Metrics and Set Alarms"],
|
| 428 |
-
|
| 429 |
-
["Migrate our current monolithic PHP application towards containerized services using Docker and Kubernetes for scalability.",
|
| 430 |
-
"Architectural Restructuring Plan",
|
| 431 |
-
"Containerisation Process With Docker",
|
| 432 |
-
"Service Orchestration With Kubernetes",
|
| 433 |
-
"Load Balancing Strategies",
|
| 434 |
-
"Persistent Storage Solutions",
|
| 435 |
-
"Network Policies Enforcement",
|
| 436 |
-
"Continuous Integration / Continuous Delivery"],
|
| 437 |
-
|
| 438 |
-
["Provide guidance on integrating WebAssembly modules compiled from C++ source files into an ongoing web project.",
|
| 439 |
-
"Toolchain Selection (Emscripten vs. LLVM)",
|
| 440 |
-
"Setting Up Compiler Environment",
|
| 441 |
-
".cpp Source Preparation",
|
| 442 |
-
"Module Building Approach",
|
| 443 |
-
"Memory Management Considerations",
|
| 444 |
-
"Performance Tradeoffs",
|
| 445 |
-
"Seamless Web Assembly Embedding"]
|
| 446 |
-
]
|
| 447 |
-
|
| 448 |
-
def parse_action(line):
|
| 449 |
-
action_name, action_input = line.strip("action: ").split("=")
|
| 450 |
-
action_input = action_input.strip()
|
| 451 |
-
return action_name, action_input
|
| 452 |
-
|
| 453 |
-
def get_file_tree(path):
|
| 454 |
-
"""
|
| 455 |
-
Recursively explores a directory and returns a nested dictionary representing its file tree.
|
| 456 |
-
"""
|
| 457 |
-
tree = {}
|
| 458 |
-
for item in os.listdir(path):
|
| 459 |
-
item_path = os.path.join(path, item)
|
| 460 |
-
if os.path.isdir(item_path):
|
| 461 |
-
tree[item] = get_file_tree(item_path)
|
| 462 |
-
else:
|
| 463 |
-
tree[item] = None
|
| 464 |
-
return tree
|
| 465 |
-
|
| 466 |
-
def display_file_tree(tree, indent=0):
|
| 467 |
-
"""
|
| 468 |
-
Prints a formatted representation of the file tree.
|
| 469 |
-
"""
|
| 470 |
-
for name, subtree in tree.items():
|
| 471 |
-
print(f"{' ' * indent}{name}")
|
| 472 |
-
if subtree is not None:
|
| 473 |
-
display_file_tree(subtree, indent + 1)
|
| 474 |
-
|
| 475 |
-
def project_explorer(path):
|
| 476 |
-
"""
|
| 477 |
-
Displays the file tree of a given path in a Streamlit app.
|
| 478 |
-
"""
|
| 479 |
-
tree = get_file_tree(path)
|
| 480 |
-
display_file_tree(tree)
|
| 481 |
-
|
| 482 |
-
def chat_app_logic(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty):
|
| 483 |
-
# Your existing code here
|
| 484 |
-
|
| 485 |
-
try:
|
| 486 |
-
# Attempt to join the generator output
|
| 487 |
-
response = ''.join(generate(
|
| 488 |
-
model=model,
|
| 489 |
-
messages=messages,
|
| 490 |
-
stream=True,
|
| 491 |
-
temperature=0.7,
|
| 492 |
-
max_tokens=1500
|
| 493 |
-
))
|
| 494 |
-
except TypeError:
|
| 495 |
-
# If joining fails, collect the output in a list
|
| 496 |
-
response_parts = []
|
| 497 |
-
for part in generate(
|
| 498 |
-
model=model,
|
| 499 |
-
messages=messages,
|
| 500 |
-
stream=True,
|
| 501 |
-
temperature=0.7,
|
| 502 |
-
max_tokens=1500
|
| 503 |
-
):
|
| 504 |
-
if isinstance(part, str):
|
| 505 |
-
response_parts.append(part)
|
| 506 |
-
elif isinstance(part, dict) and 'content' in part:
|
| 507 |
-
response_parts.append(part['content']),
|
| 508 |
-
|
| 509 |
-
response = ''.join(response_parts,
|
| 510 |
-
# Run the model and get the response (convert generator to string)
|
| 511 |
-
prompt=message,
|
| 512 |
-
history=history,
|
| 513 |
-
agent_name=agent_name,
|
| 514 |
-
sys_prompt=sys_prompt,
|
| 515 |
-
temperature=temperature,
|
| 516 |
-
max_new_tokens=max_new_tokens,
|
| 517 |
-
top_p=top_p,
|
| 518 |
-
repetition_penalty=repetition_penalty,
|
| 519 |
-
)
|
| 520 |
-
history.append((message, response))
|
| 521 |
-
return history
|
| 522 |
-
|
| 523 |
-
return history
|
| 524 |
|
| 525 |
def main():
|
| 526 |
with gr.Blocks() as demo:
|
|
@@ -554,11 +330,11 @@ def main():
|
|
| 554 |
with gr.Tab("Chat App"):
|
| 555 |
history = gr.State([])
|
| 556 |
for example in examples:
|
| 557 |
-
gr.Button(
|
| 558 |
|
| 559 |
# Connect components to the chat app logic
|
| 560 |
-
submit_button.click(
|
| 561 |
-
message.submit(
|
| 562 |
# Connect components to the project explorer
|
| 563 |
explore_button.click(project_explorer, inputs=[project_path], outputs=project_output)
|
| 564 |
|
|
|
|
| 9 |
from datetime import datetime
|
| 10 |
import logging
|
| 11 |
import json
|
|
|
|
| 12 |
|
| 13 |
now = datetime.now()
|
| 14 |
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 24 |
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 25 |
)
|
| 26 |
|
| 27 |
+
agents =[
|
| 28 |
"WEB_DEV",
|
| 29 |
"AI_SYSTEM_PROMPT",
|
| 30 |
"PYTHON_CODE_DEV"
|
|
|
|
| 80 |
"""
|
| 81 |
|
| 82 |
def format_prompt(message, history, max_history_turns=2):
|
| 83 |
+
prompt = " "
|
| 84 |
# Keep only the last 'max_history_turns' turns
|
| 85 |
for user_prompt, bot_response in history[-max_history_turns:]:
|
| 86 |
+
prompt += f"[INST] {user_prompt} [/ "
|
| 87 |
+
prompt += f" {bot_response}"
|
| 88 |
+
prompt += f"[INST] {message} [/ "
|
| 89 |
return prompt
|
| 90 |
|
| 91 |
def run_gpt(
|
|
|
|
| 145 |
def call_search(purpose, task, history, directory, action_input):
|
| 146 |
logging.info(f"CALLING SEARCH: {action_input}")
|
| 147 |
try:
|
| 148 |
+
|
| 149 |
if "http" in action_input:
|
| 150 |
if "<" in action_input:
|
| 151 |
action_input = action_input.strip("<")
|
|
|
|
| 159 |
else:
|
| 160 |
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
| 161 |
except Exception as e:
|
| 162 |
+
history += "observation: {}\n".format(e)
|
| 163 |
return "MAIN", None, history, task
|
| 164 |
|
| 165 |
def call_main(purpose, task, history, directory, action_input):
|
|
|
|
| 290 |
################################################
|
| 291 |
|
| 292 |
def format_prompt(message, history, max_history_turns=5):
|
| 293 |
+
prompt = " "
|
| 294 |
# Keep only the last 'max_history_turns' turns
|
| 295 |
for user_prompt, bot_response in history[-max_history_turns:]:
|
| 296 |
+
prompt += f"[INST]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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
def main():
|
| 302 |
with gr.Blocks() as demo:
|
|
|
|
| 330 |
with gr.Tab("Chat App"):
|
| 331 |
history = gr.State([])
|
| 332 |
for example in examples:
|
| 333 |
+
gr.Button(example[0]).click(lambda event, x=example[0]: chat_app_logic, inputs=[x, message, purpose], outputs=chatbot)
|
| 334 |
|
| 335 |
# Connect components to the chat app logic
|
| 336 |
+
submit_button.click(lambda event, x=message, h=history: chat_app_logic, inputs=[x, h], outputs=chatbot)
|
| 337 |
+
message.submit(lambda event, x=message, h=history: chat_app_logic, inputs=[x, h], outputs=chatbot)
|
| 338 |
# Connect components to the project explorer
|
| 339 |
explore_button.click(project_explorer, inputs=[project_path], outputs=project_output)
|
| 340 |
|