Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import sys
|
|
| 9 |
import tempfile
|
| 10 |
import zipfile
|
| 11 |
from datetime import datetime
|
|
|
|
| 12 |
|
| 13 |
class CodingCopilot:
|
| 14 |
def __init__(self):
|
|
@@ -113,8 +114,26 @@ class CodingCopilot:
|
|
| 113 |
|
| 114 |
return combined_content, status
|
| 115 |
|
| 116 |
-
def
|
| 117 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
if history is None:
|
| 119 |
history = []
|
| 120 |
|
|
@@ -132,12 +151,10 @@ class CodingCopilot:
|
|
| 132 |
context = ""
|
| 133 |
if history:
|
| 134 |
if is_continuation:
|
| 135 |
-
# For continuation, include more context (last 5 exchanges)
|
| 136 |
context = "\n\nFull conversation context for continuation:\n"
|
| 137 |
for i, (user_msg, assistant_msg) in enumerate(history[-5:]):
|
| 138 |
context += f"Exchange {i+1}:\nUser: {user_msg}\nAssistant: {assistant_msg}\n\n"
|
| 139 |
else:
|
| 140 |
-
# For new topics, include recent context (last 3 exchanges)
|
| 141 |
context = "\n\nRecent conversation context:\n"
|
| 142 |
for i, (user_msg, assistant_msg) in enumerate(history[-3:]):
|
| 143 |
context += f"User: {user_msg}\nAssistant: {assistant_msg}\n\n"
|
|
@@ -171,7 +188,6 @@ User's continuation request: {message}
|
|
| 171 |
{final_instruction}"""
|
| 172 |
|
| 173 |
else:
|
| 174 |
-
# Determine if this is a code generation request
|
| 175 |
generation_keywords = ['create', 'generate', 'build', 'make', 'develop', 'write code', 'implement', 'design']
|
| 176 |
is_generation = any(keyword in message.lower() for keyword in generation_keywords)
|
| 177 |
|
|
@@ -204,7 +220,6 @@ User's request: {message}
|
|
| 204 |
{final_instruction}"""
|
| 205 |
|
| 206 |
else:
|
| 207 |
-
# Regular analysis/chat prompt
|
| 208 |
file_content_section = f"\nCode files content:\n```\n{file_content}\n```" if file_content else ""
|
| 209 |
|
| 210 |
prompt = f"""You are an advanced coding assistant. Provide detailed, comprehensive responses.
|
|
@@ -219,511 +234,578 @@ User's request: {message}
|
|
| 219 |
|
| 220 |
Provide a thorough and helpful response. If suggesting code changes, provide complete implementations."""
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
def create_download_file(self, content: str, filename: str = "code_output.py") -> str:
|
| 225 |
-
"""Create a downloadable file with the given content."""
|
| 226 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 227 |
-
filename = f"{timestamp}_{filename}"
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
# Initialize the copilot
|
| 236 |
copilot = CodingCopilot()
|
| 237 |
|
| 238 |
-
# Professional
|
| 239 |
-
|
| 240 |
/* Global Styles */
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
.gradio-container {
|
| 242 |
-
max-width:
|
| 243 |
margin: 0 auto !important;
|
| 244 |
-
background: #
|
| 245 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif !important;
|
|
|
|
| 246 |
}
|
| 247 |
|
| 248 |
/* Header Styles */
|
| 249 |
.header-container {
|
| 250 |
-
background: linear-gradient(
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
| 254 |
}
|
| 255 |
|
| 256 |
.header-title {
|
| 257 |
-
color:
|
| 258 |
-
font-size:
|
| 259 |
-
font-weight:
|
| 260 |
margin: 0 !important;
|
| 261 |
text-align: center !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
}
|
| 263 |
|
| 264 |
.header-subtitle {
|
| 265 |
-
color:
|
| 266 |
-
font-size:
|
| 267 |
text-align: center !important;
|
| 268 |
margin: 0.5rem 0 0 0 !important;
|
|
|
|
| 269 |
}
|
| 270 |
|
| 271 |
-
/*
|
| 272 |
-
.
|
| 273 |
-
background: #
|
| 274 |
-
border-radius:
|
| 275 |
-
border: 1px solid #
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
padding: 0 !important;
|
| 279 |
}
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
}
|
| 285 |
|
| 286 |
-
.
|
| 287 |
-
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
}
|
| 290 |
|
| 291 |
-
.
|
| 292 |
-
|
|
|
|
|
|
|
| 293 |
}
|
| 294 |
|
| 295 |
-
.
|
| 296 |
-
|
| 297 |
-
margin: 0.5rem 0 !important;
|
| 298 |
-
padding: 0.75rem 1rem !important;
|
| 299 |
-
border-radius: 8px !important;
|
| 300 |
-
border: 1px solid #e9ecef !important;
|
| 301 |
}
|
| 302 |
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
background: transparent !important;
|
| 305 |
-
|
| 306 |
-
padding: 0.75rem 1rem !important;
|
| 307 |
}
|
| 308 |
|
| 309 |
/* Input Area */
|
| 310 |
-
.input-
|
| 311 |
-
background: #
|
| 312 |
-
border: 1px solid #
|
| 313 |
-
|
| 314 |
-
padding: 0.5rem !important;
|
| 315 |
-
margin-top: 1rem !important;
|
| 316 |
}
|
| 317 |
|
| 318 |
.input-row {
|
| 319 |
display: flex !important;
|
|
|
|
| 320 |
align-items: flex-end !important;
|
| 321 |
-
gap: 0.5rem !important;
|
| 322 |
}
|
| 323 |
|
| 324 |
-
.
|
| 325 |
-
|
| 326 |
-
border:
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
padding: 0.5rem !important;
|
| 331 |
font-size: 1rem !important;
|
| 332 |
line-height: 1.5 !important;
|
|
|
|
|
|
|
| 333 |
}
|
| 334 |
|
| 335 |
-
.
|
| 336 |
outline: none !important;
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
}
|
| 339 |
|
| 340 |
/* Buttons */
|
| 341 |
-
.
|
| 342 |
-
background: #
|
| 343 |
color: white !important;
|
| 344 |
border: none !important;
|
| 345 |
-
border-radius:
|
| 346 |
-
padding:
|
| 347 |
-
font-weight:
|
|
|
|
| 348 |
cursor: pointer !important;
|
| 349 |
-
transition:
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
.send-button:hover {
|
| 354 |
-
background: #0d8c6c !important;
|
| 355 |
}
|
| 356 |
|
| 357 |
-
.
|
| 358 |
-
background: #
|
| 359 |
-
|
|
|
|
| 360 |
}
|
| 361 |
|
| 362 |
.secondary-button {
|
| 363 |
-
background: #
|
| 364 |
-
color: #
|
| 365 |
-
border: 1px solid #
|
| 366 |
-
border-radius:
|
| 367 |
-
padding: 0.5rem
|
| 368 |
font-weight: 500 !important;
|
| 369 |
cursor: pointer !important;
|
| 370 |
-
transition: all 0.
|
|
|
|
|
|
|
|
|
|
| 371 |
}
|
| 372 |
|
| 373 |
.secondary-button:hover {
|
| 374 |
-
background: #
|
| 375 |
-
border-color: #
|
|
|
|
| 376 |
}
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
border:
|
| 382 |
border-radius: 8px !important;
|
| 383 |
-
padding: 1rem !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
text-align: center !important;
|
| 385 |
-
transition: all 0.
|
| 386 |
-
|
| 387 |
}
|
| 388 |
|
| 389 |
-
.file-upload
|
| 390 |
-
border-color: #
|
| 391 |
-
background: #
|
| 392 |
}
|
| 393 |
|
| 394 |
.file-upload-text {
|
| 395 |
-
color: #
|
| 396 |
font-size: 0.9rem !important;
|
| 397 |
}
|
| 398 |
|
| 399 |
-
/*
|
| 400 |
-
.
|
| 401 |
-
background: #
|
| 402 |
-
border: 1px solid #
|
| 403 |
-
border-radius:
|
| 404 |
-
padding:
|
| 405 |
-
margin:
|
| 406 |
-
font-size: 0.9rem !important;
|
| 407 |
-
color: #0c4a6e !important;
|
| 408 |
}
|
| 409 |
|
| 410 |
-
.
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
margin: 0.5rem 0 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
font-size: 0.9rem !important;
|
| 417 |
-
|
| 418 |
}
|
| 419 |
|
| 420 |
-
/* Code
|
| 421 |
-
|
| 422 |
-
background: #
|
| 423 |
-
border: 1px solid #
|
| 424 |
-
border-radius:
|
| 425 |
padding: 1rem !important;
|
| 426 |
-
|
|
|
|
| 427 |
font-size: 0.9rem !important;
|
| 428 |
line-height: 1.4 !important;
|
| 429 |
overflow-x: auto !important;
|
| 430 |
}
|
| 431 |
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
}
|
| 440 |
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
}
|
| 447 |
|
| 448 |
/* Mobile Responsiveness */
|
| 449 |
@media (max-width: 768px) {
|
| 450 |
.gradio-container {
|
| 451 |
-
|
| 452 |
-
padding: 0.5rem !important;
|
| 453 |
}
|
| 454 |
|
| 455 |
-
.header-
|
| 456 |
-
|
| 457 |
-
padding: 1rem !important;
|
| 458 |
}
|
| 459 |
|
| 460 |
-
.
|
| 461 |
-
|
|
|
|
| 462 |
}
|
| 463 |
|
| 464 |
.input-row {
|
| 465 |
flex-direction: column !important;
|
| 466 |
-
gap:
|
| 467 |
}
|
| 468 |
|
| 469 |
-
.
|
| 470 |
width: 100% !important;
|
| 471 |
}
|
| 472 |
}
|
| 473 |
|
| 474 |
-
/*
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
height: 20px !important;
|
| 479 |
-
border: 3px solid #f3f3f3 !important;
|
| 480 |
-
border-top: 3px solid #10a37f !important;
|
| 481 |
-
border-radius: 50% !important;
|
| 482 |
-
animation: spin 1s linear infinite !important;
|
| 483 |
}
|
| 484 |
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
100% { transform: rotate(360deg); }
|
| 488 |
}
|
| 489 |
|
| 490 |
-
/*
|
| 491 |
-
.
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
}
|
| 499 |
-
|
| 500 |
-
.chat-container::-webkit-scrollbar-track {
|
| 501 |
-
background: #f1f1f1 !important;
|
| 502 |
-
}
|
| 503 |
-
|
| 504 |
-
.chat-container::-webkit-scrollbar-thumb {
|
| 505 |
-
background: #c1c1c1 !important;
|
| 506 |
-
border-radius: 3px !important;
|
| 507 |
}
|
| 508 |
|
| 509 |
-
.
|
| 510 |
-
|
| 511 |
}
|
| 512 |
"""
|
| 513 |
|
| 514 |
def process_chat_message(message, files, history):
|
| 515 |
-
"""Process a new chat message with
|
| 516 |
if not message.strip() and not files:
|
| 517 |
-
return history, history, ""
|
| 518 |
|
| 519 |
-
# Add user message to history
|
| 520 |
if history is None:
|
| 521 |
history = []
|
| 522 |
|
| 523 |
user_msg = message
|
| 524 |
if files:
|
| 525 |
file_names = [os.path.basename(f.name) for f in files]
|
| 526 |
-
user_msg += f" π *[
|
| 527 |
|
| 528 |
-
#
|
| 529 |
-
|
| 530 |
-
is_continuation = any(keyword in message.lower() for keyword in continue_keywords)
|
| 531 |
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
status_msg = "π **Continuing from previous code...** (Analyzing context and extending the implementation)"
|
| 535 |
-
temp_history = history + [[user_msg, status_msg]]
|
| 536 |
-
|
| 537 |
-
# Get AI response with full context
|
| 538 |
-
ai_response = copilot.process_message(message, files, history)
|
| 539 |
-
|
| 540 |
-
# Add both messages to history
|
| 541 |
-
history.append([user_msg, ai_response])
|
| 542 |
-
|
| 543 |
-
return history, history, ""
|
| 544 |
-
else:
|
| 545 |
-
# Regular processing
|
| 546 |
-
ai_response = copilot.process_message(message, files, history)
|
| 547 |
-
|
| 548 |
-
# Add both messages to history
|
| 549 |
-
history.append([user_msg, ai_response])
|
| 550 |
-
|
| 551 |
-
return history, history, ""
|
| 552 |
-
|
| 553 |
-
def smart_continue_suggestion(history):
|
| 554 |
-
"""Suggest continuation if the last response seems incomplete."""
|
| 555 |
-
if not history:
|
| 556 |
-
return ""
|
| 557 |
|
| 558 |
-
|
|
|
|
|
|
|
|
|
|
| 559 |
|
| 560 |
-
|
| 561 |
-
incomplete_indicators = [
|
| 562 |
-
"```" in last_response and last_response.count("```") % 2 != 0, # Unclosed code block
|
| 563 |
-
last_response.endswith("..."),
|
| 564 |
-
"# TODO" in last_response,
|
| 565 |
-
"# Continue" in last_response.lower(),
|
| 566 |
-
len(last_response) > 3000 # Very long response might be cut off
|
| 567 |
-
]
|
| 568 |
-
|
| 569 |
-
if any(incomplete_indicators):
|
| 570 |
-
return "π‘ **Tip**: Type 'continue' to extend this code or add more functionality!"
|
| 571 |
-
|
| 572 |
-
return ""
|
| 573 |
|
| 574 |
def clear_chat():
|
| 575 |
"""Clear the chat history."""
|
| 576 |
-
return [], []
|
| 577 |
-
|
| 578 |
-
def
|
| 579 |
-
"""
|
| 580 |
-
|
| 581 |
-
# Extract code blocks
|
| 582 |
-
import re
|
| 583 |
-
code_blocks = re.findall(r'```(?:\w+)?\n(.*?)\n```', message, re.DOTALL)
|
| 584 |
-
if code_blocks:
|
| 585 |
-
combined_code = "\n\n".join(code_blocks)
|
| 586 |
-
return copilot.create_download_file(combined_code)
|
| 587 |
-
return None
|
| 588 |
|
| 589 |
# Create the main interface
|
| 590 |
-
with gr.Blocks(css=
|
|
|
|
| 591 |
# Header
|
| 592 |
gr.HTML("""
|
| 593 |
<div class="header-container">
|
| 594 |
-
<h1 class="header-title">
|
| 595 |
-
<p class="header-subtitle">
|
| 596 |
</div>
|
| 597 |
""")
|
| 598 |
|
| 599 |
-
# Main
|
| 600 |
-
with gr.Row():
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
gr.HTML('<
|
|
|
|
|
|
|
| 604 |
gr.Markdown("""
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
β’
|
| 609 |
-
β’
|
| 610 |
-
β’
|
| 611 |
-
β’
|
| 612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
-
**
|
| 615 |
-
β’
|
| 616 |
-
β’
|
| 617 |
-
β’
|
|
|
|
| 618 |
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
β’ Continue incomplete code
|
| 622 |
-
β’ Debug existing code
|
| 623 |
-
β’ Explain complex algorithms
|
| 624 |
-
β’ Refactor & optimize
|
| 625 |
-
β’ Add comprehensive documentation
|
| 626 |
|
| 627 |
-
|
| 628 |
-
β’ Say "continue" to extend code
|
| 629 |
-
β’ Upload files for context
|
| 630 |
-
β’ Ask for "complete implementation"
|
| 631 |
-
β’ Request "production-ready code"
|
| 632 |
-
""")
|
| 633 |
|
| 634 |
-
# Quick actions
|
| 635 |
-
gr.HTML('<h3 class="sidebar-title">π Quick Actions</h3>')
|
| 636 |
with gr.Column():
|
| 637 |
-
quick_review = gr.Button("π
|
| 638 |
-
quick_debug = gr.Button("π
|
| 639 |
-
quick_optimize = gr.Button("β‘
|
| 640 |
-
quick_explain = gr.Button("π
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
|
|
|
|
| 642 |
with gr.Column(scale=3):
|
|
|
|
|
|
|
| 643 |
# Chat interface
|
| 644 |
chatbot = gr.Chatbot(
|
| 645 |
-
height=
|
| 646 |
show_label=False,
|
| 647 |
-
container=
|
| 648 |
bubble_full_width=False,
|
| 649 |
-
avatar_images=("
|
|
|
|
| 650 |
)
|
| 651 |
|
| 652 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
with gr.Row():
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
)
|
| 673 |
|
| 674 |
-
with gr.Column(scale=1
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
|
|
|
| 679 |
|
| 680 |
-
# Hidden state
|
| 681 |
chat_history = gr.State([])
|
| 682 |
|
| 683 |
# Event handlers
|
| 684 |
-
def
|
| 685 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
quick_explain.click(lambda: "Please provide a detailed explanation of what this code does, how it works, and its key components with examples.", outputs=msg)
|
| 692 |
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
return result_history, result_state, cleared_msg, suggestion
|
| 698 |
|
| 699 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 700 |
submit.click(
|
| 701 |
-
|
| 702 |
inputs=[msg, files, chat_history],
|
| 703 |
-
outputs=[chatbot, chat_history, msg,
|
| 704 |
).then(
|
| 705 |
-
lambda:
|
| 706 |
-
outputs=
|
| 707 |
)
|
| 708 |
|
| 709 |
-
# Enter key submission with continuation support
|
| 710 |
msg.submit(
|
| 711 |
-
|
| 712 |
inputs=[msg, files, chat_history],
|
| 713 |
-
outputs=[chatbot, chat_history, msg,
|
| 714 |
).then(
|
| 715 |
-
lambda:
|
| 716 |
-
outputs=
|
| 717 |
)
|
| 718 |
|
| 719 |
-
|
| 720 |
-
|
|
|
|
|
|
|
| 721 |
|
| 722 |
# Footer
|
| 723 |
gr.HTML("""
|
| 724 |
-
<div
|
| 725 |
-
<p><strong>
|
| 726 |
-
<p>Upload your code files and start
|
| 727 |
</div>
|
| 728 |
""")
|
| 729 |
|
|
@@ -734,5 +816,6 @@ if __name__ == "__main__":
|
|
| 734 |
server_port=7860,
|
| 735 |
share=True,
|
| 736 |
show_error=True,
|
| 737 |
-
show_api=False
|
|
|
|
| 738 |
)
|
|
|
|
| 9 |
import tempfile
|
| 10 |
import zipfile
|
| 11 |
from datetime import datetime
|
| 12 |
+
import re
|
| 13 |
|
| 14 |
class CodingCopilot:
|
| 15 |
def __init__(self):
|
|
|
|
| 114 |
|
| 115 |
return combined_content, status
|
| 116 |
|
| 117 |
+
def extract_code_blocks(self, text: str) -> List[str]:
|
| 118 |
+
"""Extract code blocks from response text."""
|
| 119 |
+
code_blocks = re.findall(r'```(?:\w+)?\n(.*?)\n```', text, re.DOTALL)
|
| 120 |
+
return code_blocks
|
| 121 |
+
|
| 122 |
+
def create_download_file(self, content: str, filename: str = "generated_code.py") -> str:
|
| 123 |
+
"""Create a downloadable file with the given content."""
|
| 124 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 125 |
+
base_name = os.path.splitext(filename)[0]
|
| 126 |
+
extension = os.path.splitext(filename)[1] or '.py'
|
| 127 |
+
filename = f"{base_name}_{timestamp}{extension}"
|
| 128 |
+
|
| 129 |
+
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix=extension, delete=False)
|
| 130 |
+
temp_file.write(content)
|
| 131 |
+
temp_file.close()
|
| 132 |
+
|
| 133 |
+
return temp_file.name
|
| 134 |
+
|
| 135 |
+
def process_message(self, message: str, files: List = None, history: List = None) -> Tuple[str, Optional[str]]:
|
| 136 |
+
"""Process user message and return response with optional download file."""
|
| 137 |
if history is None:
|
| 138 |
history = []
|
| 139 |
|
|
|
|
| 151 |
context = ""
|
| 152 |
if history:
|
| 153 |
if is_continuation:
|
|
|
|
| 154 |
context = "\n\nFull conversation context for continuation:\n"
|
| 155 |
for i, (user_msg, assistant_msg) in enumerate(history[-5:]):
|
| 156 |
context += f"Exchange {i+1}:\nUser: {user_msg}\nAssistant: {assistant_msg}\n\n"
|
| 157 |
else:
|
|
|
|
| 158 |
context = "\n\nRecent conversation context:\n"
|
| 159 |
for i, (user_msg, assistant_msg) in enumerate(history[-3:]):
|
| 160 |
context += f"User: {user_msg}\nAssistant: {assistant_msg}\n\n"
|
|
|
|
| 188 |
{final_instruction}"""
|
| 189 |
|
| 190 |
else:
|
|
|
|
| 191 |
generation_keywords = ['create', 'generate', 'build', 'make', 'develop', 'write code', 'implement', 'design']
|
| 192 |
is_generation = any(keyword in message.lower() for keyword in generation_keywords)
|
| 193 |
|
|
|
|
| 220 |
{final_instruction}"""
|
| 221 |
|
| 222 |
else:
|
|
|
|
| 223 |
file_content_section = f"\nCode files content:\n```\n{file_content}\n```" if file_content else ""
|
| 224 |
|
| 225 |
prompt = f"""You are an advanced coding assistant. Provide detailed, comprehensive responses.
|
|
|
|
| 234 |
|
| 235 |
Provide a thorough and helpful response. If suggesting code changes, provide complete implementations."""
|
| 236 |
|
| 237 |
+
# Get response from Claude
|
| 238 |
+
response = self.call_claude(prompt, max_tokens=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
+
# Extract code blocks for download
|
| 241 |
+
code_blocks = self.extract_code_blocks(response)
|
| 242 |
+
download_file = None
|
| 243 |
|
| 244 |
+
if code_blocks:
|
| 245 |
+
combined_code = "\n\n".join(code_blocks)
|
| 246 |
+
if len(combined_code.strip()) > 50: # Only create download if substantial code
|
| 247 |
+
download_file = self.create_download_file(combined_code)
|
| 248 |
+
|
| 249 |
+
return response, download_file
|
| 250 |
|
| 251 |
# Initialize the copilot
|
| 252 |
copilot = CodingCopilot()
|
| 253 |
|
| 254 |
+
# Professional Black & Red CSS Theme
|
| 255 |
+
professional_css = """
|
| 256 |
/* Global Styles */
|
| 257 |
+
* {
|
| 258 |
+
box-sizing: border-box;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
.gradio-container {
|
| 262 |
+
max-width: 1400px !important;
|
| 263 |
margin: 0 auto !important;
|
| 264 |
+
background: #0a0a0a !important;
|
| 265 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif !important;
|
| 266 |
+
color: #ffffff !important;
|
| 267 |
}
|
| 268 |
|
| 269 |
/* Header Styles */
|
| 270 |
.header-container {
|
| 271 |
+
background: linear-gradient(135deg, #1a1a1a 0%, #2d1b1b 50%, #1a1a1a 100%) !important;
|
| 272 |
+
border: 1px solid #dc2626 !important;
|
| 273 |
+
border-radius: 12px !important;
|
| 274 |
+
padding: 2rem !important;
|
| 275 |
+
margin-bottom: 2rem !important;
|
| 276 |
+
box-shadow: 0 8px 32px rgba(220, 38, 38, 0.1) !important;
|
| 277 |
}
|
| 278 |
|
| 279 |
.header-title {
|
| 280 |
+
color: #ffffff !important;
|
| 281 |
+
font-size: 2.5rem !important;
|
| 282 |
+
font-weight: 700 !important;
|
| 283 |
margin: 0 !important;
|
| 284 |
text-align: center !important;
|
| 285 |
+
background: linear-gradient(135deg, #ffffff 0%, #dc2626 100%) !important;
|
| 286 |
+
-webkit-background-clip: text !important;
|
| 287 |
+
-webkit-text-fill-color: transparent !important;
|
| 288 |
+
background-clip: text !important;
|
| 289 |
}
|
| 290 |
|
| 291 |
.header-subtitle {
|
| 292 |
+
color: #a1a1aa !important;
|
| 293 |
+
font-size: 1.1rem !important;
|
| 294 |
text-align: center !important;
|
| 295 |
margin: 0.5rem 0 0 0 !important;
|
| 296 |
+
font-weight: 400 !important;
|
| 297 |
}
|
| 298 |
|
| 299 |
+
/* Main Layout */
|
| 300 |
+
.main-container {
|
| 301 |
+
background: #111111 !important;
|
| 302 |
+
border-radius: 16px !important;
|
| 303 |
+
border: 1px solid #262626 !important;
|
| 304 |
+
overflow: hidden !important;
|
| 305 |
+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3) !important;
|
|
|
|
| 306 |
}
|
| 307 |
|
| 308 |
+
/* Sidebar */
|
| 309 |
+
.sidebar {
|
| 310 |
+
background: linear-gradient(180deg, #1a1a1a 0%, #0f0f0f 100%) !important;
|
| 311 |
+
border-right: 1px solid #dc2626 !important;
|
| 312 |
+
padding: 2rem !important;
|
| 313 |
+
height: 100% !important;
|
| 314 |
+
min-height: 700px !important;
|
| 315 |
}
|
| 316 |
|
| 317 |
+
.sidebar-title {
|
| 318 |
+
color: #dc2626 !important;
|
| 319 |
+
font-weight: 700 !important;
|
| 320 |
+
font-size: 1.2rem !important;
|
| 321 |
+
margin-bottom: 1.5rem !important;
|
| 322 |
+
text-transform: uppercase !important;
|
| 323 |
+
letter-spacing: 0.5px !important;
|
| 324 |
}
|
| 325 |
|
| 326 |
+
.sidebar-content {
|
| 327 |
+
color: #d1d5db !important;
|
| 328 |
+
font-size: 0.9rem !important;
|
| 329 |
+
line-height: 1.6 !important;
|
| 330 |
}
|
| 331 |
|
| 332 |
+
.sidebar-content strong {
|
| 333 |
+
color: #ffffff !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
}
|
| 335 |
|
| 336 |
+
/* Chat Interface */
|
| 337 |
+
.chat-container {
|
| 338 |
+
background: #0f0f0f !important;
|
| 339 |
+
border-radius: 0 !important;
|
| 340 |
+
border: none !important;
|
| 341 |
+
min-height: 600px !important;
|
| 342 |
+
max-height: 600px !important;
|
| 343 |
+
overflow-y: auto !important;
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
.chatbot {
|
| 347 |
background: transparent !important;
|
| 348 |
+
border: none !important;
|
|
|
|
| 349 |
}
|
| 350 |
|
| 351 |
/* Input Area */
|
| 352 |
+
.input-section {
|
| 353 |
+
background: #1a1a1a !important;
|
| 354 |
+
border-top: 1px solid #262626 !important;
|
| 355 |
+
padding: 1.5rem !important;
|
|
|
|
|
|
|
| 356 |
}
|
| 357 |
|
| 358 |
.input-row {
|
| 359 |
display: flex !important;
|
| 360 |
+
gap: 1rem !important;
|
| 361 |
align-items: flex-end !important;
|
|
|
|
| 362 |
}
|
| 363 |
|
| 364 |
+
.message-input {
|
| 365 |
+
background: #262626 !important;
|
| 366 |
+
border: 1px solid #404040 !important;
|
| 367 |
+
border-radius: 12px !important;
|
| 368 |
+
color: #ffffff !important;
|
| 369 |
+
padding: 1rem !important;
|
|
|
|
| 370 |
font-size: 1rem !important;
|
| 371 |
line-height: 1.5 !important;
|
| 372 |
+
resize: vertical !important;
|
| 373 |
+
transition: all 0.3s ease !important;
|
| 374 |
}
|
| 375 |
|
| 376 |
+
.message-input:focus {
|
| 377 |
outline: none !important;
|
| 378 |
+
border-color: #dc2626 !important;
|
| 379 |
+
box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1) !important;
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
.message-input::placeholder {
|
| 383 |
+
color: #6b7280 !important;
|
| 384 |
}
|
| 385 |
|
| 386 |
/* Buttons */
|
| 387 |
+
.primary-button {
|
| 388 |
+
background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%) !important;
|
| 389 |
color: white !important;
|
| 390 |
border: none !important;
|
| 391 |
+
border-radius: 12px !important;
|
| 392 |
+
padding: 1rem 2rem !important;
|
| 393 |
+
font-weight: 600 !important;
|
| 394 |
+
font-size: 1rem !important;
|
| 395 |
cursor: pointer !important;
|
| 396 |
+
transition: all 0.3s ease !important;
|
| 397 |
+
box-shadow: 0 4px 16px rgba(220, 38, 38, 0.2) !important;
|
| 398 |
+
text-transform: uppercase !important;
|
| 399 |
+
letter-spacing: 0.5px !important;
|
|
|
|
|
|
|
| 400 |
}
|
| 401 |
|
| 402 |
+
.primary-button:hover {
|
| 403 |
+
background: linear-gradient(135deg, #b91c1c 0%, #991b1b 100%) !important;
|
| 404 |
+
transform: translateY(-2px) !important;
|
| 405 |
+
box-shadow: 0 8px 24px rgba(220, 38, 38, 0.3) !important;
|
| 406 |
}
|
| 407 |
|
| 408 |
.secondary-button {
|
| 409 |
+
background: #262626 !important;
|
| 410 |
+
color: #ffffff !important;
|
| 411 |
+
border: 1px solid #404040 !important;
|
| 412 |
+
border-radius: 12px !important;
|
| 413 |
+
padding: 0.75rem 1.5rem !important;
|
| 414 |
font-weight: 500 !important;
|
| 415 |
cursor: pointer !important;
|
| 416 |
+
transition: all 0.3s ease !important;
|
| 417 |
+
text-transform: uppercase !important;
|
| 418 |
+
letter-spacing: 0.5px !important;
|
| 419 |
+
font-size: 0.9rem !important;
|
| 420 |
}
|
| 421 |
|
| 422 |
.secondary-button:hover {
|
| 423 |
+
background: #404040 !important;
|
| 424 |
+
border-color: #dc2626 !important;
|
| 425 |
+
transform: translateY(-1px) !important;
|
| 426 |
}
|
| 427 |
|
| 428 |
+
.quick-action-button {
|
| 429 |
+
background: #1a1a1a !important;
|
| 430 |
+
color: #dc2626 !important;
|
| 431 |
+
border: 1px solid #dc2626 !important;
|
| 432 |
border-radius: 8px !important;
|
| 433 |
+
padding: 0.75rem 1rem !important;
|
| 434 |
+
font-weight: 500 !important;
|
| 435 |
+
cursor: pointer !important;
|
| 436 |
+
transition: all 0.3s ease !important;
|
| 437 |
+
margin-bottom: 0.5rem !important;
|
| 438 |
+
width: 100% !important;
|
| 439 |
+
text-align: left !important;
|
| 440 |
+
font-size: 0.9rem !important;
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
.quick-action-button:hover {
|
| 444 |
+
background: #dc2626 !important;
|
| 445 |
+
color: #ffffff !important;
|
| 446 |
+
transform: translateX(4px) !important;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
/* File Upload */
|
| 450 |
+
.file-upload {
|
| 451 |
+
background: #1a1a1a !important;
|
| 452 |
+
border: 2px dashed #404040 !important;
|
| 453 |
+
border-radius: 12px !important;
|
| 454 |
+
padding: 1.5rem !important;
|
| 455 |
text-align: center !important;
|
| 456 |
+
transition: all 0.3s ease !important;
|
| 457 |
+
color: #d1d5db !important;
|
| 458 |
}
|
| 459 |
|
| 460 |
+
.file-upload:hover {
|
| 461 |
+
border-color: #dc2626 !important;
|
| 462 |
+
background: #262626 !important;
|
| 463 |
}
|
| 464 |
|
| 465 |
.file-upload-text {
|
| 466 |
+
color: #9ca3af !important;
|
| 467 |
font-size: 0.9rem !important;
|
| 468 |
}
|
| 469 |
|
| 470 |
+
/* Download Section */
|
| 471 |
+
.download-section {
|
| 472 |
+
background: #1a1a1a !important;
|
| 473 |
+
border: 1px solid #262626 !important;
|
| 474 |
+
border-radius: 12px !important;
|
| 475 |
+
padding: 1rem !important;
|
| 476 |
+
margin-top: 1rem !important;
|
|
|
|
|
|
|
| 477 |
}
|
| 478 |
|
| 479 |
+
.download-title {
|
| 480 |
+
color: #dc2626 !important;
|
| 481 |
+
font-weight: 600 !important;
|
| 482 |
+
margin-bottom: 0.5rem !important;
|
| 483 |
+
font-size: 1rem !important;
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
/* Status Messages */
|
| 487 |
+
.status-success {
|
| 488 |
+
background: linear-gradient(135deg, #065f46 0%, #047857 100%) !important;
|
| 489 |
+
border: 1px solid #10b981 !important;
|
| 490 |
+
border-radius: 8px !important;
|
| 491 |
+
padding: 1rem !important;
|
| 492 |
+
color: #ffffff !important;
|
| 493 |
+
font-size: 0.9rem !important;
|
| 494 |
margin: 0.5rem 0 !important;
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
.status-error {
|
| 498 |
+
background: linear-gradient(135deg, #7f1d1d 0%, #991b1b 100%) !important;
|
| 499 |
+
border: 1px solid #dc2626 !important;
|
| 500 |
+
border-radius: 8px !important;
|
| 501 |
+
padding: 1rem !important;
|
| 502 |
+
color: #ffffff !important;
|
| 503 |
font-size: 0.9rem !important;
|
| 504 |
+
margin: 0.5rem 0 !important;
|
| 505 |
}
|
| 506 |
|
| 507 |
+
/* Code Blocks */
|
| 508 |
+
pre {
|
| 509 |
+
background: #0a0a0a !important;
|
| 510 |
+
border: 1px solid #262626 !important;
|
| 511 |
+
border-radius: 8px !important;
|
| 512 |
padding: 1rem !important;
|
| 513 |
+
color: #ffffff !important;
|
| 514 |
+
font-family: 'JetBrains Mono', 'Fira Code', monospace !important;
|
| 515 |
font-size: 0.9rem !important;
|
| 516 |
line-height: 1.4 !important;
|
| 517 |
overflow-x: auto !important;
|
| 518 |
}
|
| 519 |
|
| 520 |
+
code {
|
| 521 |
+
background: #1a1a1a !important;
|
| 522 |
+
color: #dc2626 !important;
|
| 523 |
+
padding: 0.2rem 0.4rem !important;
|
| 524 |
+
border-radius: 4px !important;
|
| 525 |
+
font-family: 'JetBrains Mono', 'Fira Code', monospace !important;
|
| 526 |
+
font-size: 0.9rem !important;
|
| 527 |
}
|
| 528 |
|
| 529 |
+
/* Scrollbar */
|
| 530 |
+
::-webkit-scrollbar {
|
| 531 |
+
width: 8px !important;
|
| 532 |
+
height: 8px !important;
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
::-webkit-scrollbar-track {
|
| 536 |
+
background: #1a1a1a !important;
|
| 537 |
+
}
|
| 538 |
+
|
| 539 |
+
::-webkit-scrollbar-thumb {
|
| 540 |
+
background: #404040 !important;
|
| 541 |
+
border-radius: 4px !important;
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
::-webkit-scrollbar-thumb:hover {
|
| 545 |
+
background: #dc2626 !important;
|
| 546 |
}
|
| 547 |
|
| 548 |
/* Mobile Responsiveness */
|
| 549 |
@media (max-width: 768px) {
|
| 550 |
.gradio-container {
|
| 551 |
+
padding: 1rem !important;
|
|
|
|
| 552 |
}
|
| 553 |
|
| 554 |
+
.header-title {
|
| 555 |
+
font-size: 2rem !important;
|
|
|
|
| 556 |
}
|
| 557 |
|
| 558 |
+
.sidebar {
|
| 559 |
+
min-height: auto !important;
|
| 560 |
+
padding: 1rem !important;
|
| 561 |
}
|
| 562 |
|
| 563 |
.input-row {
|
| 564 |
flex-direction: column !important;
|
| 565 |
+
gap: 1rem !important;
|
| 566 |
}
|
| 567 |
|
| 568 |
+
.primary-button {
|
| 569 |
width: 100% !important;
|
| 570 |
}
|
| 571 |
}
|
| 572 |
|
| 573 |
+
/* Animations */
|
| 574 |
+
@keyframes fadeIn {
|
| 575 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 576 |
+
to { opacity: 1; transform: translateY(0); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
}
|
| 578 |
|
| 579 |
+
.chat-message {
|
| 580 |
+
animation: fadeIn 0.3s ease-out !important;
|
|
|
|
| 581 |
}
|
| 582 |
|
| 583 |
+
/* Footer */
|
| 584 |
+
.footer {
|
| 585 |
+
background: #0a0a0a !important;
|
| 586 |
+
border-top: 1px solid #262626 !important;
|
| 587 |
+
padding: 2rem !important;
|
| 588 |
+
text-align: center !important;
|
| 589 |
+
color: #6b7280 !important;
|
| 590 |
+
margin-top: 2rem !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
}
|
| 592 |
|
| 593 |
+
.footer strong {
|
| 594 |
+
color: #dc2626 !important;
|
| 595 |
}
|
| 596 |
"""
|
| 597 |
|
| 598 |
def process_chat_message(message, files, history):
|
| 599 |
+
"""Process a new chat message with download support."""
|
| 600 |
if not message.strip() and not files:
|
| 601 |
+
return history, history, "", None, ""
|
| 602 |
|
|
|
|
| 603 |
if history is None:
|
| 604 |
history = []
|
| 605 |
|
| 606 |
user_msg = message
|
| 607 |
if files:
|
| 608 |
file_names = [os.path.basename(f.name) for f in files]
|
| 609 |
+
user_msg += f" π *[Files: {', '.join(file_names)}]*"
|
| 610 |
|
| 611 |
+
# Get AI response with potential download file
|
| 612 |
+
ai_response, download_file = copilot.process_message(message, files, history)
|
|
|
|
| 613 |
|
| 614 |
+
# Add to history
|
| 615 |
+
history.append([user_msg, ai_response])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
|
| 617 |
+
# Return download file info
|
| 618 |
+
download_info = ""
|
| 619 |
+
if download_file:
|
| 620 |
+
download_info = "π₯ **Code generated successfully!** Download button available below."
|
| 621 |
|
| 622 |
+
return history, history, "", download_file, download_info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
|
| 624 |
def clear_chat():
|
| 625 |
"""Clear the chat history."""
|
| 626 |
+
return [], [], None, ""
|
| 627 |
+
|
| 628 |
+
def quick_action(action_text):
|
| 629 |
+
"""Handle quick action buttons."""
|
| 630 |
+
return action_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
|
| 632 |
# Create the main interface
|
| 633 |
+
with gr.Blocks(css=professional_css, title="Coding Copilot Pro", theme=gr.themes.Base()) as app:
|
| 634 |
+
|
| 635 |
# Header
|
| 636 |
gr.HTML("""
|
| 637 |
<div class="header-container">
|
| 638 |
+
<h1 class="header-title">β‘ CODING COPILOT PRO</h1>
|
| 639 |
+
<p class="header-subtitle">Advanced AI-Powered Development Assistant β’ Powered by Claude 3 Haiku</p>
|
| 640 |
</div>
|
| 641 |
""")
|
| 642 |
|
| 643 |
+
# Main interface
|
| 644 |
+
with gr.Row(equal_height=True):
|
| 645 |
+
# Sidebar
|
| 646 |
+
with gr.Column(scale=1, min_width=300):
|
| 647 |
+
gr.HTML('<div class="sidebar">')
|
| 648 |
+
|
| 649 |
+
gr.HTML('<h3 class="sidebar-title">π CAPABILITIES</h3>')
|
| 650 |
gr.Markdown("""
|
| 651 |
+
<div class="sidebar-content">
|
| 652 |
+
|
| 653 |
+
**π» Code Generation:**
|
| 654 |
+
β’ Complete applications & modules
|
| 655 |
+
β’ Production-ready implementations
|
| 656 |
+
β’ Custom algorithms & data structures
|
| 657 |
+
β’ API integrations & web services
|
| 658 |
+
|
| 659 |
+
**π§ Code Analysis:**
|
| 660 |
+
β’ Comprehensive code reviews
|
| 661 |
+
β’ Performance optimization
|
| 662 |
+
β’ Security vulnerability detection
|
| 663 |
+
β’ Architecture recommendations
|
| 664 |
+
|
| 665 |
+
**π Debugging & Testing:**
|
| 666 |
+
β’ Error detection & fixes
|
| 667 |
+
β’ Unit test generation
|
| 668 |
+
β’ Code refactoring
|
| 669 |
+
β’ Best practices enforcement
|
| 670 |
|
| 671 |
+
**π Documentation:**
|
| 672 |
+
β’ Comprehensive docstrings
|
| 673 |
+
β’ API documentation
|
| 674 |
+
β’ Code explanations
|
| 675 |
+
β’ Technical specifications
|
| 676 |
|
| 677 |
+
</div>
|
| 678 |
+
""", elem_classes=["sidebar-content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
|
| 680 |
+
gr.HTML('<h3 class="sidebar-title">β‘ QUICK ACTIONS</h3>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
|
|
|
|
|
|
|
| 682 |
with gr.Column():
|
| 683 |
+
quick_review = gr.Button("π COMPREHENSIVE REVIEW", elem_classes=["quick-action-button"])
|
| 684 |
+
quick_debug = gr.Button("π DEBUG & FIX", elem_classes=["quick-action-button"])
|
| 685 |
+
quick_optimize = gr.Button("β‘ OPTIMIZE CODE", elem_classes=["quick-action-button"])
|
| 686 |
+
quick_explain = gr.Button("π EXPLAIN & DOCUMENT", elem_classes=["quick-action-button"])
|
| 687 |
+
quick_generate = gr.Button("π₯ GENERATE NEW CODE", elem_classes=["quick-action-button"])
|
| 688 |
+
quick_test = gr.Button("π§ͺ CREATE TESTS", elem_classes=["quick-action-button"])
|
| 689 |
+
|
| 690 |
+
gr.HTML('</div>')
|
| 691 |
|
| 692 |
+
# Main chat area
|
| 693 |
with gr.Column(scale=3):
|
| 694 |
+
gr.HTML('<div class="main-container">')
|
| 695 |
+
|
| 696 |
# Chat interface
|
| 697 |
chatbot = gr.Chatbot(
|
| 698 |
+
height=600,
|
| 699 |
show_label=False,
|
| 700 |
+
container=False,
|
| 701 |
bubble_full_width=False,
|
| 702 |
+
avatar_images=("π¨βπ»", "π€"),
|
| 703 |
+
elem_classes=["chat-container"]
|
| 704 |
)
|
| 705 |
|
| 706 |
+
# Download section
|
| 707 |
+
download_status = gr.HTML("", visible=True)
|
| 708 |
+
download_file = gr.File(label="π₯ Download Generated Code", visible=False)
|
| 709 |
+
|
| 710 |
+
# Input section
|
| 711 |
+
gr.HTML('<div class="input-section">')
|
| 712 |
+
|
| 713 |
+
# File upload
|
| 714 |
with gr.Row():
|
| 715 |
+
files = gr.File(
|
| 716 |
+
label="π Upload Code Files (.py, .ipynb)",
|
| 717 |
+
file_count="multiple",
|
| 718 |
+
file_types=[".py", ".ipynb"],
|
| 719 |
+
elem_classes=["file-upload"]
|
| 720 |
+
)
|
| 721 |
+
|
| 722 |
+
# Message input and buttons
|
| 723 |
+
with gr.Row(elem_classes=["input-row"]):
|
| 724 |
+
msg = gr.Textbox(
|
| 725 |
+
label="",
|
| 726 |
+
placeholder="Describe what you want to build, analyze, or improve... Type 'continue' to extend previous code.",
|
| 727 |
+
show_label=False,
|
| 728 |
+
lines=2,
|
| 729 |
+
max_lines=4,
|
| 730 |
+
scale=4,
|
| 731 |
+
elem_classes=["message-input"]
|
| 732 |
+
)
|
|
|
|
| 733 |
|
| 734 |
+
with gr.Column(scale=1):
|
| 735 |
+
submit = gr.Button("SEND", variant="primary", elem_classes=["primary-button"])
|
| 736 |
+
clear = gr.Button("CLEAR", variant="secondary", elem_classes=["secondary-button"])
|
| 737 |
+
|
| 738 |
+
gr.HTML('</div>')
|
| 739 |
+
gr.HTML('</div>')
|
| 740 |
|
| 741 |
+
# Hidden state
|
| 742 |
chat_history = gr.State([])
|
| 743 |
|
| 744 |
# Event handlers
|
| 745 |
+
def handle_submit(message, files, history):
|
| 746 |
+
result = process_chat_message(message, files, history)
|
| 747 |
+
return result
|
| 748 |
+
|
| 749 |
+
# Quick actions
|
| 750 |
+
quick_review.click(
|
| 751 |
+
lambda: "Please perform a comprehensive code review of the uploaded files. Analyze code quality, identify potential bugs, suggest performance improvements, check for security vulnerabilities, and provide detailed recommendations with examples.",
|
| 752 |
+
outputs=msg
|
| 753 |
+
)
|
| 754 |
+
|
| 755 |
+
quick_debug.click(
|
| 756 |
+
lambda: "Please debug the uploaded code thoroughly. Identify any errors, bugs, logical issues, or potential runtime problems. Provide detailed explanations and complete fixed versions of the code.",
|
| 757 |
+
outputs=msg
|
| 758 |
+
)
|
| 759 |
+
|
| 760 |
+
quick_optimize.click(
|
| 761 |
+
lambda: "Please optimize the uploaded code for better performance, memory usage, and readability. Provide the complete optimized version with explanations of the improvements made.",
|
| 762 |
+
outputs=msg
|
| 763 |
+
)
|
| 764 |
|
| 765 |
+
quick_explain.click(
|
| 766 |
+
lambda: "Please provide a detailed explanation of the uploaded code. Break down its functionality, explain the algorithms used, document all functions and classes, and create comprehensive documentation.",
|
| 767 |
+
outputs=msg
|
| 768 |
+
)
|
|
|
|
| 769 |
|
| 770 |
+
quick_generate.click(
|
| 771 |
+
lambda: "Please generate a complete, production-ready application based on the requirements. Include all necessary components, error handling, documentation, and best practices.",
|
| 772 |
+
outputs=msg
|
| 773 |
+
)
|
|
|
|
| 774 |
|
| 775 |
+
quick_test.click(
|
| 776 |
+
lambda: "Please create comprehensive unit tests for the uploaded code. Include test cases for all functions, edge cases, error handling, and integration tests where applicable.",
|
| 777 |
+
outputs=msg
|
| 778 |
+
)
|
| 779 |
+
|
| 780 |
+
# Main functionality
|
| 781 |
submit.click(
|
| 782 |
+
handle_submit,
|
| 783 |
inputs=[msg, files, chat_history],
|
| 784 |
+
outputs=[chatbot, chat_history, msg, download_file, download_status]
|
| 785 |
).then(
|
| 786 |
+
lambda: None,
|
| 787 |
+
outputs=files
|
| 788 |
)
|
| 789 |
|
|
|
|
| 790 |
msg.submit(
|
| 791 |
+
handle_submit,
|
| 792 |
inputs=[msg, files, chat_history],
|
| 793 |
+
outputs=[chatbot, chat_history, msg, download_file, download_status]
|
| 794 |
).then(
|
| 795 |
+
lambda: None,
|
| 796 |
+
outputs=files
|
| 797 |
)
|
| 798 |
|
| 799 |
+
clear.click(
|
| 800 |
+
clear_chat,
|
| 801 |
+
outputs=[chatbot, chat_history, download_file, download_status]
|
| 802 |
+
)
|
| 803 |
|
| 804 |
# Footer
|
| 805 |
gr.HTML("""
|
| 806 |
+
<div class="footer">
|
| 807 |
+
<p><strong>CODING COPILOT PRO</strong> β’ Professional Development Assistant</p>
|
| 808 |
+
<p>Upload your code files and start building β’ Advanced AI β’ Secure & Private</p>
|
| 809 |
</div>
|
| 810 |
""")
|
| 811 |
|
|
|
|
| 816 |
server_port=7860,
|
| 817 |
share=True,
|
| 818 |
show_error=True,
|
| 819 |
+
show_api=False,
|
| 820 |
+
inbrowser=True
|
| 821 |
)
|