Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -476,149 +476,109 @@ def chat(message: str, history: list):
|
|
| 476 |
history[-1][1] = f"Error: {str(e)}"
|
| 477 |
yield history
|
| 478 |
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
|
| 484 |
-
with
|
| 485 |
-
|
| 486 |
-
theme=theme, # Pass theme here
|
| 487 |
-
css="""
|
| 488 |
-
.gradio-container { max-width: 1200px; margin: auto; }
|
| 489 |
-
.chatbot { min-height: 500px; }
|
| 490 |
-
.status-box { padding: 20px; border-radius: 10px; background: #f0f8ff; }
|
| 491 |
-
"""
|
| 492 |
-
) as demo:
|
| 493 |
gr.Markdown("""
|
| 494 |
# 🤖 Codey Bryant 3.0
|
| 495 |
## **SOTA RAG Coding Assistant**
|
| 496 |
|
| 497 |
-
|
| 498 |
-
- **HyDE** (Hypothetical Document Embeddings)
|
| 499 |
-
- **Query Rewriting** for vague queries
|
| 500 |
-
- **Multi-Query** retrieval
|
| 501 |
-
- **Answer-Space Retrieval**
|
| 502 |
|
| 503 |
-
|
| 504 |
-
- "it's not working"
|
| 505 |
-
- "help with error"
|
| 506 |
-
- "make it faster"
|
| 507 |
-
- "why error"
|
| 508 |
-
- "how to implement"
|
| 509 |
-
|
| 510 |
-
### **Powered by:**
|
| 511 |
-
- TinyLlama 1.1B (4-bit quantized)
|
| 512 |
-
- Hybrid retrieval (FAISS + BM25)
|
| 513 |
-
- OPC coding datasets
|
| 514 |
""")
|
| 515 |
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
init_btn = gr.Button(
|
| 519 |
-
"Initialize Assistant",
|
| 520 |
-
variant="primary",
|
| 521 |
-
size="lg"
|
| 522 |
-
)
|
| 523 |
-
clear_btn = gr.Button("Clear Chat", size="lg")
|
| 524 |
-
|
| 525 |
-
with gr.Column(scale=4):
|
| 526 |
-
status = gr.Markdown(
|
| 527 |
-
"### Status: Click 'Initialize Assistant' to start",
|
| 528 |
-
elem_classes="status-box"
|
| 529 |
-
)
|
| 530 |
|
| 531 |
-
chatbot = gr.Chatbot(
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
bubble_full_width=False
|
| 537 |
)
|
| 538 |
-
|
| 539 |
-
with gr.Row():
|
| 540 |
-
msg = gr.Textbox(
|
| 541 |
-
placeholder="Ask anything about Python coding...",
|
| 542 |
-
label="Your Question",
|
| 543 |
-
lines=3,
|
| 544 |
-
scale=5,
|
| 545 |
-
container=False
|
| 546 |
-
)
|
| 547 |
-
submit_btn = gr.Button("Send", variant="secondary", scale=1)
|
| 548 |
|
| 549 |
# Examples
|
| 550 |
gr.Examples(
|
| 551 |
examples=[
|
| 552 |
["How to read a CSV file in Python?"],
|
| 553 |
["Why am I getting 'list index out of range' error?"],
|
| 554 |
-
["Make this function faster..."],
|
| 555 |
["Help, my code isn't working!"],
|
| 556 |
-
["Best way to sort a dictionary by value?"]
|
| 557 |
],
|
| 558 |
-
inputs=msg
|
| 559 |
-
label="Try these examples:"
|
| 560 |
)
|
| 561 |
|
| 562 |
# Event handlers
|
| 563 |
-
init_btn.click(
|
| 564 |
-
initialize_assistant,
|
| 565 |
-
outputs=status
|
| 566 |
-
)
|
| 567 |
|
| 568 |
-
def
|
| 569 |
-
|
|
|
|
| 570 |
|
| 571 |
msg.submit(
|
| 572 |
-
|
| 573 |
[msg, chatbot],
|
| 574 |
-
[msg, chatbot]
|
| 575 |
-
queue=False
|
| 576 |
).then(
|
| 577 |
chat,
|
| 578 |
[msg, chatbot],
|
| 579 |
chatbot
|
| 580 |
)
|
| 581 |
|
| 582 |
-
|
| 583 |
-
submit_message,
|
| 584 |
-
[msg, chatbot],
|
| 585 |
-
[msg, chatbot],
|
| 586 |
-
queue=False
|
| 587 |
-
).then(
|
| 588 |
-
chat,
|
| 589 |
-
[msg, chatbot],
|
| 590 |
-
chatbot
|
| 591 |
-
)
|
| 592 |
-
|
| 593 |
-
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 594 |
-
|
| 595 |
-
# Footer
|
| 596 |
-
gr.Markdown("""
|
| 597 |
-
---
|
| 598 |
-
*Codey Bryant 3.0 uses TinyLlama 1.1B with 4-bit quantization. Responses may take a few seconds.*
|
| 599 |
-
""")
|
| 600 |
-
|
| 601 |
-
return demo
|
| 602 |
-
|
| 603 |
-
# ========================
|
| 604 |
-
# 5) Main Entry Point
|
| 605 |
-
# ========================
|
| 606 |
-
|
| 607 |
-
if __name__ == "__main__":
|
| 608 |
-
# Configure for Hugging Face Spaces
|
| 609 |
-
server_name = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 610 |
-
server_port = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
|
| 611 |
-
|
| 612 |
-
# Create and launch the demo
|
| 613 |
-
demo = create_ui()
|
| 614 |
|
|
|
|
| 615 |
logger.info(f"Starting Codey Bryant 3.0 on {server_name}:{server_port}")
|
| 616 |
logger.info("SOTA RAG Architecture: HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval")
|
| 617 |
|
| 618 |
demo.launch(
|
| 619 |
server_name=server_name,
|
| 620 |
server_port=server_port,
|
| 621 |
-
share=False
|
| 622 |
-
debug=False,
|
| 623 |
-
show_error=True
|
| 624 |
)
|
|
|
|
| 476 |
history[-1][1] = f"Error: {str(e)}"
|
| 477 |
yield history
|
| 478 |
|
| 479 |
+
# 4) Gradio UI
|
| 480 |
+
|
| 481 |
+
ASSISTANT: Optional[HybridCodeAssistant] = None
|
| 482 |
+
|
| 483 |
+
def initialize_assistant():
|
| 484 |
+
"""Initialize assistant with progress tracking"""
|
| 485 |
+
global ASSISTANT
|
| 486 |
+
if ASSISTANT is None:
|
| 487 |
+
yield "Initializing Codey Bryant 3.0..."
|
| 488 |
+
yield "Loading retrieval system..."
|
| 489 |
+
ASSISTANT = HybridCodeAssistant()
|
| 490 |
+
yield "Codey Bryant 3.0 Ready!"
|
| 491 |
+
yield "SOTA RAG Features: HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval"
|
| 492 |
+
yield "Ask coding questions like: 'it's not working', 'help with error', 'make it faster'"
|
| 493 |
+
else:
|
| 494 |
+
yield "Assistant already initialized!"
|
| 495 |
+
|
| 496 |
+
def chat(message: str, history: list):
|
| 497 |
+
"""Chat function with error handling"""
|
| 498 |
+
if ASSISTANT is None:
|
| 499 |
+
yield "Please click 'Initialize Assistant' first!"
|
| 500 |
+
return
|
| 501 |
+
|
| 502 |
+
# Append user message
|
| 503 |
+
history.append([message, ""])
|
| 504 |
+
yield history
|
| 505 |
+
|
| 506 |
+
# Stream response
|
| 507 |
+
try:
|
| 508 |
+
response = ""
|
| 509 |
+
for token in ASSISTANT.answer_stream(message):
|
| 510 |
+
response += token
|
| 511 |
+
history[-1][1] = response
|
| 512 |
+
yield history
|
| 513 |
+
except Exception as e:
|
| 514 |
+
logger.error(f"Chat error: {e}")
|
| 515 |
+
history[-1][1] = f"Error: {str(e)}"
|
| 516 |
+
yield history
|
| 517 |
+
|
| 518 |
+
# 5) Main Entry Point
|
| 519 |
+
|
| 520 |
+
if __name__ == "__main__":
|
| 521 |
+
# Configure for Hugging Face Spaces
|
| 522 |
+
server_name = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 523 |
+
server_port = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
|
| 524 |
|
| 525 |
+
# Create a SIMPLE Gradio UI that works with any version
|
| 526 |
+
with gr.Blocks(title="Codey Bryant 3.0 - SOTA RAG Coding Assistant") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
gr.Markdown("""
|
| 528 |
# 🤖 Codey Bryant 3.0
|
| 529 |
## **SOTA RAG Coding Assistant**
|
| 530 |
|
| 531 |
+
**Features:** HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
+
**Handles vague questions:** "it's not working", "help with error", "make it faster"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
""")
|
| 535 |
|
| 536 |
+
init_btn = gr.Button("Initialize Assistant", variant="primary")
|
| 537 |
+
status = gr.Markdown("**Status:** Click 'Initialize Assistant' to start")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
|
| 539 |
+
chatbot = gr.Chatbot(height=500)
|
| 540 |
+
msg = gr.Textbox(
|
| 541 |
+
placeholder="Ask anything about Python coding...",
|
| 542 |
+
label="Your Question",
|
| 543 |
+
lines=3
|
|
|
|
| 544 |
)
|
| 545 |
+
clear = gr.Button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
# Examples
|
| 548 |
gr.Examples(
|
| 549 |
examples=[
|
| 550 |
["How to read a CSV file in Python?"],
|
| 551 |
["Why am I getting 'list index out of range' error?"],
|
|
|
|
| 552 |
["Help, my code isn't working!"],
|
|
|
|
| 553 |
],
|
| 554 |
+
inputs=msg
|
|
|
|
| 555 |
)
|
| 556 |
|
| 557 |
# Event handlers
|
| 558 |
+
init_btn.click(initialize_assistant, outputs=status)
|
|
|
|
|
|
|
|
|
|
| 559 |
|
| 560 |
+
def respond(message, history):
|
| 561 |
+
history.append([message, ""])
|
| 562 |
+
return "", history
|
| 563 |
|
| 564 |
msg.submit(
|
| 565 |
+
respond,
|
| 566 |
[msg, chatbot],
|
| 567 |
+
[msg, chatbot]
|
|
|
|
| 568 |
).then(
|
| 569 |
chat,
|
| 570 |
[msg, chatbot],
|
| 571 |
chatbot
|
| 572 |
)
|
| 573 |
|
| 574 |
+
clear.click(lambda: [], None, chatbot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
|
| 576 |
+
# Launch the app
|
| 577 |
logger.info(f"Starting Codey Bryant 3.0 on {server_name}:{server_port}")
|
| 578 |
logger.info("SOTA RAG Architecture: HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval")
|
| 579 |
|
| 580 |
demo.launch(
|
| 581 |
server_name=server_name,
|
| 582 |
server_port=server_port,
|
| 583 |
+
share=False
|
|
|
|
|
|
|
| 584 |
)
|