Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -284,9 +284,7 @@ def build_generative_core():
|
|
| 284 |
|
| 285 |
return GenerativeCore(model, tokenizer, gen_cfg)
|
| 286 |
|
| 287 |
-
# ========================
|
| 288 |
# 3) SOTA Enhanced Retrieval (EXACT SAME)
|
| 289 |
-
# ========================
|
| 290 |
|
| 291 |
class HybridCodeAssistant:
|
| 292 |
"""Main assistant class - EXACT SAME IMPLEMENTATION"""
|
|
@@ -475,45 +473,6 @@ Improved:"""
|
|
| 475 |
|
| 476 |
thread.join()
|
| 477 |
|
| 478 |
-
# 4) Gradio UI (Optimized for Hugging Face)
|
| 479 |
-
|
| 480 |
-
ASSISTANT: Optional[HybridCodeAssistant] = None
|
| 481 |
-
|
| 482 |
-
def initialize_assistant():
|
| 483 |
-
"""Initialize assistant"""
|
| 484 |
-
global ASSISTANT
|
| 485 |
-
if ASSISTANT is None:
|
| 486 |
-
ASSISTANT = HybridCodeAssistant()
|
| 487 |
-
return "Codey Bryant 3.0 Ready! Ask your Python questions below."
|
| 488 |
-
else:
|
| 489 |
-
return "Assistant already initialized!"
|
| 490 |
-
|
| 491 |
-
def chat(message: str, history):
|
| 492 |
-
"""Chat function with proper history handling"""
|
| 493 |
-
if ASSISTANT is None:
|
| 494 |
-
# If not initialized, return error message
|
| 495 |
-
history.append([message, "Please click 'Initialize Assistant' first!"])
|
| 496 |
-
return history
|
| 497 |
-
|
| 498 |
-
# Append user message to history
|
| 499 |
-
history.append([message, ""])
|
| 500 |
-
|
| 501 |
-
try:
|
| 502 |
-
# Stream the response
|
| 503 |
-
response_text = ""
|
| 504 |
-
for token in ASSISTANT.answer_stream(message):
|
| 505 |
-
response_text += token
|
| 506 |
-
# Update the last message in history
|
| 507 |
-
history[-1] = [message, response_text]
|
| 508 |
-
yield history
|
| 509 |
-
|
| 510 |
-
except Exception as e:
|
| 511 |
-
logger.error(f"Error generating response: {e}")
|
| 512 |
-
history[-1] = [message, f"Error: {str(e)}"]
|
| 513 |
-
yield history
|
| 514 |
-
|
| 515 |
-
# 4) Gradio UI
|
| 516 |
-
|
| 517 |
ASSISTANT: Optional[HybridCodeAssistant] = None
|
| 518 |
|
| 519 |
def initialize_assistant():
|
|
@@ -551,79 +510,104 @@ def chat(message: str, history: list):
|
|
| 551 |
history[-1][1] = f"Error: {str(e)}"
|
| 552 |
yield history
|
| 553 |
|
| 554 |
-
# 5) Main Entry Point
|
| 555 |
|
| 556 |
if __name__ == "__main__":
|
| 557 |
# Configure for Hugging Face Spaces
|
| 558 |
server_name = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 559 |
server_port = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
|
| 560 |
|
| 561 |
-
#
|
| 562 |
with gr.Blocks(title="Codey Bryant 3.0") as demo:
|
| 563 |
gr.Markdown("""
|
| 564 |
# 🤖 Codey Bryant 3.0
|
| 565 |
-
## SOTA RAG
|
| 566 |
|
| 567 |
-
**Features:** HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval
|
| 568 |
""")
|
| 569 |
|
| 570 |
# Status display
|
| 571 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
|
| 573 |
# Initialize button
|
| 574 |
init_btn = gr.Button("🚀 Initialize Assistant", variant="primary")
|
| 575 |
|
| 576 |
# Chat interface
|
| 577 |
-
chatbot = gr.Chatbot(height=500)
|
| 578 |
|
| 579 |
-
# Input and send
|
| 580 |
with gr.Row():
|
| 581 |
msg = gr.Textbox(
|
| 582 |
placeholder="Ask Python coding questions...",
|
| 583 |
label="Your Question",
|
|
|
|
| 584 |
scale=4
|
| 585 |
)
|
| 586 |
submit_btn = gr.Button("Send", variant="secondary", scale=1)
|
| 587 |
|
| 588 |
-
# Clear button
|
| 589 |
clear_btn = gr.Button("Clear Chat")
|
| 590 |
|
| 591 |
# Event handlers
|
| 592 |
def on_init():
|
| 593 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
|
| 595 |
-
init_btn.click(
|
|
|
|
|
|
|
|
|
|
| 596 |
|
| 597 |
def process_message(message, chat_history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
# Add user message
|
| 599 |
chat_history.append([message, ""])
|
| 600 |
return "", chat_history
|
| 601 |
|
| 602 |
def generate_response(message, chat_history):
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
|
| 607 |
# Connect submit button
|
| 608 |
submit_btn.click(
|
| 609 |
-
process_message,
|
| 610 |
-
[msg, chatbot],
|
| 611 |
-
[msg, chatbot]
|
| 612 |
).then(
|
| 613 |
-
generate_response,
|
| 614 |
-
[msg, chatbot],
|
| 615 |
-
chatbot
|
| 616 |
)
|
| 617 |
|
| 618 |
# Connect Enter key
|
| 619 |
msg.submit(
|
| 620 |
-
process_message,
|
| 621 |
-
[msg, chatbot],
|
| 622 |
-
[msg, chatbot]
|
| 623 |
).then(
|
| 624 |
-
generate_response,
|
| 625 |
-
[msg, chatbot],
|
| 626 |
-
chatbot
|
| 627 |
)
|
| 628 |
|
| 629 |
# Clear chat
|
|
@@ -631,8 +615,11 @@ if __name__ == "__main__":
|
|
| 631 |
|
| 632 |
# Launch the app
|
| 633 |
logger.info(f"Starting Codey Bryant 3.0 on {server_name}:{server_port}")
|
|
|
|
|
|
|
| 634 |
demo.launch(
|
| 635 |
server_name=server_name,
|
| 636 |
server_port=server_port,
|
| 637 |
-
share=False
|
|
|
|
| 638 |
)
|
|
|
|
| 284 |
|
| 285 |
return GenerativeCore(model, tokenizer, gen_cfg)
|
| 286 |
|
|
|
|
| 287 |
# 3) SOTA Enhanced Retrieval (EXACT SAME)
|
|
|
|
| 288 |
|
| 289 |
class HybridCodeAssistant:
|
| 290 |
"""Main assistant class - EXACT SAME IMPLEMENTATION"""
|
|
|
|
| 473 |
|
| 474 |
thread.join()
|
| 475 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
ASSISTANT: Optional[HybridCodeAssistant] = None
|
| 477 |
|
| 478 |
def initialize_assistant():
|
|
|
|
| 510 |
history[-1][1] = f"Error: {str(e)}"
|
| 511 |
yield history
|
| 512 |
|
| 513 |
+
# 5) Main Entry Point - SIMPLE WORKING UI
|
| 514 |
|
| 515 |
if __name__ == "__main__":
|
| 516 |
# Configure for Hugging Face Spaces
|
| 517 |
server_name = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 518 |
server_port = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
|
| 519 |
|
| 520 |
+
# SIMPLE, WORKING UI
|
| 521 |
with gr.Blocks(title="Codey Bryant 3.0") as demo:
|
| 522 |
gr.Markdown("""
|
| 523 |
# 🤖 Codey Bryant 3.0
|
| 524 |
+
## **SOTA RAG Coding Assistant**
|
| 525 |
|
| 526 |
+
**Advanced Features:** HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval
|
| 527 |
""")
|
| 528 |
|
| 529 |
# Status display
|
| 530 |
+
status_output = gr.Textbox(
|
| 531 |
+
label="Status",
|
| 532 |
+
value="Click 'Initialize Assistant' to start",
|
| 533 |
+
interactive=False
|
| 534 |
+
)
|
| 535 |
|
| 536 |
# Initialize button
|
| 537 |
init_btn = gr.Button("🚀 Initialize Assistant", variant="primary")
|
| 538 |
|
| 539 |
# Chat interface
|
| 540 |
+
chatbot = gr.Chatbot(label="Chat", height=500)
|
| 541 |
|
|
|
|
| 542 |
with gr.Row():
|
| 543 |
msg = gr.Textbox(
|
| 544 |
placeholder="Ask Python coding questions...",
|
| 545 |
label="Your Question",
|
| 546 |
+
lines=2,
|
| 547 |
scale=4
|
| 548 |
)
|
| 549 |
submit_btn = gr.Button("Send", variant="secondary", scale=1)
|
| 550 |
|
|
|
|
| 551 |
clear_btn = gr.Button("Clear Chat")
|
| 552 |
|
| 553 |
# Event handlers
|
| 554 |
def on_init():
|
| 555 |
+
"""Handle initialization and update status"""
|
| 556 |
+
status_text = ""
|
| 557 |
+
for status in initialize_assistant():
|
| 558 |
+
status_text = status
|
| 559 |
+
yield status
|
| 560 |
+
# Enable the chat interface after initialization
|
| 561 |
+
yield status_text
|
| 562 |
|
| 563 |
+
init_btn.click(
|
| 564 |
+
fn=on_init,
|
| 565 |
+
outputs=status_output
|
| 566 |
+
)
|
| 567 |
|
| 568 |
def process_message(message, chat_history):
|
| 569 |
+
"""Process a new message"""
|
| 570 |
+
if not message.strip():
|
| 571 |
+
return "", chat_history
|
| 572 |
+
|
| 573 |
# Add user message
|
| 574 |
chat_history.append([message, ""])
|
| 575 |
return "", chat_history
|
| 576 |
|
| 577 |
def generate_response(message, chat_history):
|
| 578 |
+
"""Generate response from assistant"""
|
| 579 |
+
if not message.strip():
|
| 580 |
+
yield chat_history
|
| 581 |
+
return
|
| 582 |
+
|
| 583 |
+
try:
|
| 584 |
+
# Get streaming response
|
| 585 |
+
for updated_history in chat(message, chat_history):
|
| 586 |
+
yield updated_history
|
| 587 |
+
except Exception as e:
|
| 588 |
+
chat_history[-1][1] = f"Error: {str(e)}"
|
| 589 |
+
yield chat_history
|
| 590 |
|
| 591 |
# Connect submit button
|
| 592 |
submit_btn.click(
|
| 593 |
+
fn=process_message,
|
| 594 |
+
inputs=[msg, chatbot],
|
| 595 |
+
outputs=[msg, chatbot]
|
| 596 |
).then(
|
| 597 |
+
fn=generate_response,
|
| 598 |
+
inputs=[msg, chatbot],
|
| 599 |
+
outputs=chatbot
|
| 600 |
)
|
| 601 |
|
| 602 |
# Connect Enter key
|
| 603 |
msg.submit(
|
| 604 |
+
fn=process_message,
|
| 605 |
+
inputs=[msg, chatbot],
|
| 606 |
+
outputs=[msg, chatbot]
|
| 607 |
).then(
|
| 608 |
+
fn=generate_response,
|
| 609 |
+
inputs=[msg, chatbot],
|
| 610 |
+
outputs=chatbot
|
| 611 |
)
|
| 612 |
|
| 613 |
# Clear chat
|
|
|
|
| 615 |
|
| 616 |
# Launch the app
|
| 617 |
logger.info(f"Starting Codey Bryant 3.0 on {server_name}:{server_port}")
|
| 618 |
+
logger.info("SOTA RAG Architecture: HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval")
|
| 619 |
+
|
| 620 |
demo.launch(
|
| 621 |
server_name=server_name,
|
| 622 |
server_port=server_port,
|
| 623 |
+
share=False,
|
| 624 |
+
debug=False
|
| 625 |
)
|