pasxalisag commited on
Commit
d75ea89
·
verified ·
1 Parent(s): 9f0aafb

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +117 -60
app.py CHANGED
@@ -410,7 +410,7 @@ Improved:"""
410
  return final[:k]
411
 
412
  def answer_stream(self, text: str) -> Iterator[str]:
413
- """Stream answer - same implementation"""
414
  retrieved = self.retrieve_enhanced(text, k=3)
415
 
416
  context = ""
@@ -419,22 +419,60 @@ Improved:"""
419
  ans = meta["answer"][:200]
420
  context = f"Reference example:\nQ: {q}\nA: {ans}\n\n"
421
 
 
 
 
 
422
  messages = [
423
- {"role": "system", "content": "You are a concise, accurate Python coding assistant. Use the reference if helpful." + context},
424
  {"role": "user", "content": text}
425
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426
 
427
- prompt = self.generator.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
428
  inputs = self.generator.tokenizer(prompt, return_tensors="pt").to(DEVICE)
429
 
430
- streamer = TextIteratorStreamer(self.generator.tokenizer, skip_prompt=True, skip_special_tokens=True)
431
- thread = Thread(target=self.generator.model.generate, kwargs=dict(
432
- **inputs, streamer=streamer, generation_config=self.generator.generation_config
433
- ))
 
 
 
 
 
 
 
 
 
 
434
  thread.start()
435
 
436
  for token in streamer:
437
  yield token
 
438
  thread.join()
439
 
440
  # 4) Gradio UI (Optimized for Hugging Face)
@@ -442,38 +480,36 @@ Improved:"""
442
  ASSISTANT: Optional[HybridCodeAssistant] = None
443
 
444
  def initialize_assistant():
445
- """Initialize assistant with progress tracking"""
446
  global ASSISTANT
447
  if ASSISTANT is None:
448
- yield "Initializing Codey Bryant 3.0..."
449
- yield "Loading retrieval system..."
450
  ASSISTANT = HybridCodeAssistant()
451
- yield "Codey Bryant 3.0 Ready!"
452
- yield "SOTA RAG Features: HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval"
453
- yield "Ask coding questions like: 'it's not working', 'help with error', 'make it faster'"
454
  else:
455
- yield "Assistant already initialized!"
456
 
457
- def chat(message: str, history: list):
458
- """Chat function with error handling"""
459
  if ASSISTANT is None:
460
- yield "Please click 'Initialize Assistant' first!"
461
- return
462
-
463
- # Append user message
 
464
  history.append([message, ""])
465
- yield history
466
-
467
- # Stream response
468
  try:
469
- response = ""
 
470
  for token in ASSISTANT.answer_stream(message):
471
- response += token
472
- history[-1][1] = response
 
473
  yield history
 
474
  except Exception as e:
475
- logger.error(f"Chat error: {e}")
476
- history[-1][1] = f"Error: {str(e)}"
477
  yield history
478
 
479
  # 4) Gradio UI
@@ -522,60 +558,81 @@ if __name__ == "__main__":
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
- # SIMPLE, WORKING UI
526
- with gr.Blocks() as demo:
527
- gr.Markdown("# 🤖 Codey Bryant 3.0")
528
- gr.Markdown("Python Coding Assistant with SOTA RAG")
 
529
 
530
- # Status and initialization
531
- status = gr.Textbox("Click 'Initialize' to start", label="Status", interactive=False)
532
- init_btn = gr.Button("🚀 Initialize", variant="primary")
 
 
 
 
 
533
 
534
  # Chat interface
535
- chatbot = gr.Chatbot(label="Conversation")
536
- msg = gr.Textbox(placeholder="Type your Python question here...", label="Your Question")
537
- submit = gr.Button("Send")
538
- clear = gr.Button("Clear")
 
 
 
 
 
 
 
 
 
539
 
540
  # Event handlers
541
- def init_and_enable():
542
- yield from initialize_assistant()
543
- return gr.update(interactive=True)
544
 
545
- init_btn.click(
546
- init_and_enable,
547
- outputs=[status]
548
- )
549
 
550
- def add_message(message, history):
551
- return "", history + [[message, None]]
 
 
552
 
553
- def respond(message, history):
554
- for response in chat(message, history):
555
- yield response
 
556
 
557
- # Connect the send button
558
- submit.click(
559
- add_message,
560
  [msg, chatbot],
561
  [msg, chatbot]
562
  ).then(
563
- respond,
564
  [msg, chatbot],
565
  chatbot
566
  )
567
 
568
- # Also connect Enter key
569
  msg.submit(
570
- add_message,
571
  [msg, chatbot],
572
  [msg, chatbot]
573
  ).then(
574
- respond,
575
  [msg, chatbot],
576
  chatbot
577
  )
578
 
579
- clear.click(lambda: [], None, chatbot)
 
580
 
581
- demo.launch(server_name=server_name, server_port=server_port, share=False)
 
 
 
 
 
 
 
410
  return final[:k]
411
 
412
  def answer_stream(self, text: str) -> Iterator[str]:
413
+ """Stream answer with proper message formatting"""
414
  retrieved = self.retrieve_enhanced(text, k=3)
415
 
416
  context = ""
 
419
  ans = meta["answer"][:200]
420
  context = f"Reference example:\nQ: {q}\nA: {ans}\n\n"
421
 
422
+ # Create properly formatted messages
423
+ system_content = "You are a concise, accurate Python coding assistant. " + context.strip()
424
+
425
+ # Format messages for TinyLlama chat template
426
  messages = [
 
427
  {"role": "user", "content": text}
428
  ]
429
+
430
+ # Add system message if context exists
431
+ if context:
432
+ messages.insert(0, {"role": "system", "content": system_content})
433
+
434
+ # Debug: Print messages format
435
+ logger.debug(f"Messages format: {messages}")
436
+
437
+ try:
438
+ # Apply chat template
439
+ prompt = self.generator.tokenizer.apply_chat_template(
440
+ messages,
441
+ tokenize=False,
442
+ add_generation_prompt=True
443
+ )
444
+
445
+ logger.debug(f"Generated prompt length: {len(prompt)}")
446
+
447
+ except Exception as e:
448
+ logger.error(f"Error applying chat template: {e}")
449
+ # Fallback: Use simple formatting
450
+ if context:
451
+ prompt = f"<|system|>\n{system_content}</s>\n<|user|>\n{text}</s>\n<|assistant|>\n"
452
+ else:
453
+ prompt = f"<|user|>\n{text}</s>\n<|assistant|>\n"
454
 
 
455
  inputs = self.generator.tokenizer(prompt, return_tensors="pt").to(DEVICE)
456
 
457
+ streamer = TextIteratorStreamer(
458
+ self.generator.tokenizer,
459
+ skip_prompt=True,
460
+ skip_special_tokens=True
461
+ )
462
+
463
+ generation_kwargs = dict(
464
+ **inputs,
465
+ streamer=streamer,
466
+ generation_config=self.generator.generation_config,
467
+ max_new_tokens=300
468
+ )
469
+
470
+ thread = Thread(target=self.generator.model.generate, kwargs=generation_kwargs)
471
  thread.start()
472
 
473
  for token in streamer:
474
  yield token
475
+
476
  thread.join()
477
 
478
  # 4) Gradio UI (Optimized for Hugging Face)
 
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
 
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
+ # Create a simple, robust UI
562
+ with gr.Blocks(title="Codey Bryant 3.0") as demo:
563
+ gr.Markdown("""
564
+ # 🤖 Codey Bryant 3.0
565
+ ## SOTA RAG Python Coding Assistant
566
 
567
+ **Features:** HyDE + Query Rewriting + Multi-Query + Answer-Space Retrieval
568
+ """)
569
+
570
+ # Status display
571
+ status = gr.Markdown("### Status: Click 'Initialize Assistant' to start")
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
+ return initialize_assistant()
 
594
 
595
+ init_btn.click(on_init, outputs=status)
 
 
 
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
+ # Generate assistant response
604
+ for updated_history in chat(message, chat_history):
605
+ yield updated_history
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
630
+ clear_btn.click(lambda: [], None, chatbot)
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
+ )