phxdev commited on
Commit
55db969
·
verified ·
1 Parent(s): 85f63f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -524
app.py CHANGED
@@ -1,26 +1,4 @@
1
- # Create wrapper function for proper chat handling
2
- def respond(message, history):
3
- """Response handler for Gradio messages format"""
4
- if not message.strip():
5
- return "", history
6
-
7
- # Convert messages format to simple tuples
8
- simple_history = []
9
- for i in range(0, len(history), 2):
10
- if i + 1 < len(history):
11
- user_msg = history[i].get('content', '') if isinstance(history[i], dict) else str(history[i])
12
- bot_msg = history[i + 1].get('content', '') if isinstance(history[i + 1], dict) else str(history[i + 1])
13
- if user_msg and bot_msg:
14
- simple_history.append([user_msg, bot_msg])
15
-
16
- # Generate response
17
- for response_chunk in creed_ai.generate_response(message, simple_history):
18
- # Create new history with the streaming response
19
- new_history = history + [
20
- {"role": "user", "content": message},
21
- {"role": "assistant", "content": response_chunk}
22
- ]
23
- yield "", new_history#!/usr/bin/env python3
24
  """
25
  🎸 Creed Bratton AI - Using phxdev/creed-qwen-0.5b-lora
26
  The REAL Creed, trained by Mark, not some knockoff prompt engineering
@@ -109,10 +87,6 @@ class CreedBrattonAI:
109
  trust_remote_code=True,
110
  low_cpu_mem_usage=True
111
  )
112
- # Explicitly ensure model is on CUDA
113
- if self.model.device.type != "cuda":
114
- print("🔧 Explicitly moving model to CUDA...")
115
- self.model = self.model.to(self.device)
116
  else:
117
  self.model = AutoModelForCausalLM.from_pretrained(
118
  model_name,
@@ -130,13 +104,9 @@ class CreedBrattonAI:
130
 
131
  self.model.eval()
132
 
133
- # Verify device placement
134
- model_device = next(self.model.parameters()).device
135
- print(f"🎯 Model is actually on: {model_device}")
136
-
137
  self.model_loaded = True
138
  self.loading = False
139
- print(f"✅ Creed's consciousness loaded on {model_device}!")
140
 
141
  # GPU memory info
142
  if self.device == "cuda" and torch.cuda.is_available():
@@ -160,8 +130,6 @@ class CreedBrattonAI:
160
  torch_dtype=torch.float16,
161
  device_map="auto"
162
  )
163
- if self.model.device.type != "cuda":
164
- self.model = self.model.to(self.device)
165
  else:
166
  self.model = AutoModelForCausalLM.from_pretrained(
167
  base_model,
@@ -172,8 +140,7 @@ class CreedBrattonAI:
172
 
173
  self.model.eval()
174
  self.model_loaded = True
175
- model_device = next(self.model.parameters()).device
176
- print(f"✅ Fallback model loaded on {model_device}")
177
  except Exception as fallback_error:
178
  print(f"❌ Fallback also failed: {fallback_error}")
179
  self.loading = False
@@ -294,9 +261,8 @@ Remember: You're not trying to be helpful in a traditional sense - you're being
294
  # Add conversation history
295
  conversation = system_prompt
296
  for user_msg, creed_msg in history[-4:]: # Keep recent context
297
- if user_msg and creed_msg: # Only add complete exchanges
298
- conversation += f"Human: {user_msg}\n"
299
- conversation += f"Creed: {creed_msg}\n"
300
 
301
  # Add current message
302
  conversation += f"Human: {message}\n"
@@ -341,491 +307,7 @@ Remember: You're not trying to be helpful in a traditional sense - you're being
341
 
342
  return final_response
343
 
344
- def cleanup_gpu_memory(self):
345
- """Clean up GPU memory if using CUDA"""
346
- if self.device == "cuda" and torch.cuda.is_available():
347
- torch.cuda.empty_cache()
348
- print(f"🧹 GPU Memory cleaned. Current: {torch.cuda.memory_allocated() // 1024**2} MB")
349
-
350
  def creed_story_tool(self, situation: str = "mysterious") -> str:
351
  """MCP tool: Get a Creed story"""
352
  if not self.model_loaded:
353
- return "🧠 Creed's consciousness is still loading..."
354
-
355
- prompt = f"Tell me a {situation} story from your past."
356
-
357
- # Generate a one-shot response
358
- final_response = ""
359
- for response in self.generate_response(prompt, []):
360
- final_response = response
361
-
362
- return final_response
363
-
364
- def main():
365
- """Initialize and launch the real Creed AI with modern styling"""
366
-
367
- print("🎸 Initializing REAL Creed Bratton AI...")
368
- print("📡 Loading Mark's trained model: phxdev/creed-qwen-0.5b-lora")
369
-
370
- # Initialize Creed AI
371
- creed_ai = CreedBrattonAI()
372
-
373
- if SPACES_AVAILABLE:
374
- gpu_placeholder()
375
- print("✅ Spaces GPU compatibility enabled")
376
-
377
- # Memory status if GPU available
378
- if torch.cuda.is_available() and creed_ai.model_loaded:
379
- print(f"🎯 Model device verification: {next(creed_ai.model.parameters()).device}")
380
- print(f"🔥 Final GPU Memory: {torch.cuda.memory_allocated() // 1024**2} MB allocated")
381
- print(f"📊 GPU Memory Reserved: {torch.cuda.memory_reserved() // 1024**2} MB reserved")
382
-
383
- # Modern glassmorphism CSS
384
- modern_css = """
385
- /* Creed AI - Modern Glassmorphism Design */
386
- :root {
387
- --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
388
- --secondary-gradient: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
389
- --glass-bg: rgba(255, 255, 255, 0.08);
390
- --glass-border: rgba(255, 255, 255, 0.18);
391
- --text-primary: #ffffff;
392
- --text-secondary: rgba(255, 255, 255, 0.8);
393
- --accent-purple: #8b5cf6;
394
- --accent-blue: #3b82f6;
395
- --shadow-glow: 0 8px 32px rgba(139, 92, 246, 0.3);
396
- }
397
-
398
- /* Main container with animated background */
399
- .gradio-container {
400
- min-height: 100vh !important;
401
- background: var(--primary-gradient) !important;
402
- background-attachment: fixed !important;
403
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
404
- color: var(--text-primary) !important;
405
- padding: 20px !important;
406
- position: relative !important;
407
- overflow-x: hidden !important;
408
- }
409
-
410
- .gradio-container::before {
411
- content: '';
412
- position: fixed;
413
- top: 0;
414
- left: 0;
415
- width: 100%;
416
- height: 100%;
417
- background:
418
- radial-gradient(circle at 20% 80%, rgba(139, 92, 246, 0.3) 0%, transparent 50%),
419
- radial-gradient(circle at 80% 20%, rgba(59, 130, 246, 0.3) 0%, transparent 50%),
420
- radial-gradient(circle at 40% 40%, rgba(167, 139, 250, 0.2) 0%, transparent 50%);
421
- pointer-events: none;
422
- z-index: -1;
423
- }
424
-
425
- /* Floating particles animation */
426
- .gradio-container::after {
427
- content: '';
428
- position: fixed;
429
- top: 0;
430
- left: 0;
431
- width: 100%;
432
- height: 100%;
433
- background-image:
434
- radial-gradient(2px 2px at 20px 30px, rgba(255, 255, 255, 0.3), transparent),
435
- radial-gradient(2px 2px at 40px 70px, rgba(139, 92, 246, 0.4), transparent),
436
- radial-gradient(1px 1px at 90px 40px, rgba(59, 130, 246, 0.3), transparent);
437
- background-size: 120px 120px;
438
- animation: float 20s ease-in-out infinite;
439
- pointer-events: none;
440
- z-index: -1;
441
- }
442
-
443
- @keyframes float {
444
- 0%, 100% { transform: translateY(0px) rotate(0deg); }
445
- 50% { transform: translateY(-20px) rotate(180deg); }
446
- }
447
-
448
- /* Header styling */
449
- .header {
450
- background: var(--glass-bg) !important;
451
- backdrop-filter: blur(20px) !important;
452
- border: 1px solid var(--glass-border) !important;
453
- border-radius: 24px !important;
454
- padding: 32px !important;
455
- margin-bottom: 24px !important;
456
- text-align: center !important;
457
- box-shadow: var(--shadow-glow) !important;
458
- position: relative !important;
459
- overflow: hidden !important;
460
- }
461
-
462
- .header::before {
463
- content: '';
464
- position: absolute;
465
- top: 0;
466
- left: 0;
467
- right: 0;
468
- height: 1px;
469
- background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.6), transparent);
470
- }
471
-
472
- .header h1 {
473
- font-size: 36px !important;
474
- font-weight: 700 !important;
475
- background: linear-gradient(135deg, #ffffff 0%, #a855f7 50%, #3b82f6 100%) !important;
476
- -webkit-background-clip: text !important;
477
- -webkit-text-fill-color: transparent !important;
478
- background-clip: text !important;
479
- margin: 0 0 12px 0 !important;
480
- text-shadow: 0 0 30px rgba(168, 85, 247, 0.5) !important;
481
- }
482
-
483
- .header p {
484
- font-size: 16px !important;
485
- color: var(--text-secondary) !important;
486
- margin: 0 !important;
487
- font-weight: 500 !important;
488
- }
489
-
490
- /* Info boxes with glass effect */
491
- .info-box {
492
- background: rgba(255, 255, 255, 0.06) !important;
493
- backdrop-filter: blur(16px) !important;
494
- border: 1px solid rgba(255, 255, 255, 0.12) !important;
495
- border-radius: 16px !important;
496
- padding: 20px !important;
497
- margin: 16px 0 !important;
498
- color: var(--text-secondary) !important;
499
- font-size: 14px !important;
500
- line-height: 1.6 !important;
501
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1) !important;
502
- }
503
-
504
- .status-box {
505
- background: rgba(16, 185, 129, 0.1) !important;
506
- backdrop-filter: blur(16px) !important;
507
- border: 1px solid rgba(16, 185, 129, 0.3) !important;
508
- border-radius: 16px !important;
509
- padding: 16px 20px !important;
510
- margin: 16px 0 !important;
511
- color: #10b981 !important;
512
- font-weight: 600 !important;
513
- box-shadow: 0 4px 20px rgba(16, 185, 129, 0.2) !important;
514
- }
515
-
516
- /* Chat area styling */
517
- .chat-area {
518
- background: var(--glass-bg) !important;
519
- backdrop-filter: blur(20px) !important;
520
- border: 1px solid var(--glass-border) !important;
521
- border-radius: 20px !important;
522
- margin: 16px 0 !important;
523
- overflow: hidden !important;
524
- box-shadow: var(--shadow-glow) !important;
525
- }
526
-
527
- /* Tools section */
528
- .tools-area {
529
- background: var(--glass-bg) !important;
530
- backdrop-filter: blur(20px) !important;
531
- border: 1px solid var(--glass-border) !important;
532
- border-radius: 20px !important;
533
- padding: 28px !important;
534
- margin: 24px 0 !important;
535
- box-shadow: var(--shadow-glow) !important;
536
- }
537
-
538
- .tools-title {
539
- font-size: 22px !important;
540
- font-weight: 600 !important;
541
- color: var(--text-primary) !important;
542
- margin: 0 0 20px 0 !important;
543
- padding-bottom: 12px !important;
544
- border-bottom: 1px solid rgba(255, 255, 255, 0.2) !important;
545
- background: linear-gradient(135deg, #ffffff 0%, #a855f7 100%) !important;
546
- -webkit-background-clip: text !important;
547
- -webkit-text-fill-color: transparent !important;
548
- }
549
-
550
- /* Form elements */
551
- .gradio-textbox input,
552
- .gradio-textbox textarea {
553
- background: rgba(255, 255, 255, 0.08) !important;
554
- backdrop-filter: blur(10px) !important;
555
- border: 1px solid rgba(255, 255, 255, 0.16) !important;
556
- color: var(--text-primary) !important;
557
- border-radius: 12px !important;
558
- padding: 12px 16px !important;
559
- transition: all 0.3s ease !important;
560
- font-size: 14px !important;
561
- }
562
-
563
- .gradio-textbox input:focus,
564
- .gradio-textbox textarea:focus {
565
- border-color: var(--accent-purple) !important;
566
- outline: none !important;
567
- box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.3) !important;
568
- background: rgba(255, 255, 255, 0.12) !important;
569
- }
570
-
571
- .gradio-textbox input::placeholder,
572
- .gradio-textbox textarea::placeholder {
573
- color: rgba(255, 255, 255, 0.5) !important;
574
- }
575
-
576
- /* Labels */
577
- .gradio-container label {
578
- color: var(--text-secondary) !important;
579
- font-weight: 500 !important;
580
- font-size: 14px !important;
581
- margin-bottom: 6px !important;
582
- display: block !important;
583
- }
584
-
585
- /* Buttons */
586
- .gradio-container button {
587
- background: linear-gradient(135deg, var(--accent-purple) 0%, var(--accent-blue) 100%) !important;
588
- color: var(--text-primary) !important;
589
- border: none !important;
590
- border-radius: 12px !important;
591
- padding: 12px 24px !important;
592
- font-weight: 600 !important;
593
- cursor: pointer !important;
594
- transition: all 0.3s ease !important;
595
- box-shadow: 0 4px 15px rgba(139, 92, 246, 0.4) !important;
596
- backdrop-filter: blur(10px) !important;
597
- min-height: 44px !important;
598
- display: flex !important;
599
- align-items: center !important;
600
- justify-content: center !important;
601
- }
602
-
603
- .gradio-container button:hover {
604
- transform: translateY(-2px) !important;
605
- box-shadow: 0 8px 25px rgba(139, 92, 246, 0.6) !important;
606
- background: linear-gradient(135deg, #9333ea 0%, #2563eb 100%) !important;
607
- }
608
-
609
- .gradio-container button:active {
610
- transform: translateY(0px) !important;
611
- }
612
-
613
- /* Send button specific styling */
614
- .gradio-container .gr-button {
615
- background: linear-gradient(135deg, var(--accent-purple) 0%, var(--accent-blue) 100%) !important;
616
- border: 1px solid rgba(255, 255, 255, 0.2) !important;
617
- color: white !important;
618
- font-weight: 600 !important;
619
- text-transform: none !important;
620
- letter-spacing: 0.5px !important;
621
- }
622
-
623
- /* Chatbot specific styling */
624
- .gradio-chatbot {
625
- background: transparent !important;
626
- border: none !important;
627
- }
628
-
629
- /* Footer */
630
- .footer {
631
- text-align: center !important;
632
- padding: 28px !important;
633
- color: var(--text-secondary) !important;
634
- background: var(--glass-bg) !important;
635
- backdrop-filter: blur(20px) !important;
636
- border: 1px solid var(--glass-border) !important;
637
- border-radius: 20px !important;
638
- margin-top: 32px !important;
639
- box-shadow: var(--shadow-glow) !important;
640
- }
641
-
642
- /* Scrollbar styling */
643
- ::-webkit-scrollbar {
644
- width: 8px;
645
- }
646
-
647
- ::-webkit-scrollbar-track {
648
- background: rgba(255, 255, 255, 0.05);
649
- border-radius: 4px;
650
- }
651
-
652
- ::-webkit-scrollbar-thumb {
653
- background: linear-gradient(135deg, var(--accent-purple), var(--accent-blue));
654
- border-radius: 4px;
655
- }
656
-
657
- ::-webkit-scrollbar-thumb:hover {
658
- background: linear-gradient(135deg, #9333ea, #2563eb);
659
- }
660
-
661
- /* Responsive design */
662
- @media (max-width: 768px) {
663
- .gradio-container {
664
- padding: 12px !important;
665
- }
666
-
667
- .header {
668
- padding: 20px !important;
669
- border-radius: 16px !important;
670
- }
671
-
672
- .header h1 {
673
- font-size: 28px !important;
674
- }
675
-
676
- .tools-area,
677
- .chat-area {
678
- border-radius: 16px !important;
679
- padding: 20px !important;
680
- }
681
- }
682
- """
683
-
684
- # Create wrapper function for proper chat handling
685
- def respond(message, history):
686
- """Response handler for Gradio messages format"""
687
- for response_chunk in creed_ai.generate_response(message, history):
688
- # Update the history with the current response in messages format
689
- updated_history = history + [
690
- {"role": "user", "content": message},
691
- {"role": "assistant", "content": response_chunk}
692
- ]
693
- yield "", updated_history
694
-
695
- # Create the interface with modern theme
696
- with gr.Blocks(
697
- title="🎸 Creed Bratton AI",
698
- css=modern_css,
699
- theme=gr.themes.Base() # Use base theme for better CSS control
700
- ) as demo:
701
-
702
- # Modern header
703
- gr.HTML(f"""
704
- <div class="header">
705
- <h1>🎸 Creed Bratton AI</h1>
706
- <p>Powered by phxdev/creed-qwen-0.5b-lora • Running on {'🚀 GPU' if creed_ai.device == 'cuda' else '🖥️ CPU'}</p>
707
- </div>
708
- """)
709
-
710
- # Model info with glass styling
711
- gr.HTML("""
712
- <div class="info-box">
713
- <strong>Model:</strong> phxdev/creed-qwen-0.5b-lora<br>
714
- <strong>Base:</strong> Qwen 0.5B + LoRA fine-tuning<br>
715
- <strong>Tokens:</strong> &lt;thinking&gt;, &lt;conspiracy&gt;, &lt;tangent&gt;
716
- </div>
717
- """)
718
-
719
- # MCP status
720
- if os.environ.get('GRADIO_MCP_ENABLED'):
721
- gr.HTML("""
722
- <div class="status-box">
723
- ✓ MCP Server Active • Available as tool for Claude Desktop
724
- </div>
725
- """)
726
-
727
- # Main chat interface with glass styling
728
- with gr.Row(elem_classes="chat-area"):
729
- chatbot = gr.Chatbot(
730
- type='messages', # Use messages format (modern)
731
- height=550,
732
- show_copy_button=True,
733
- show_share_button=False,
734
- avatar_images=["👤", "🎸"],
735
- bubble_full_width=False,
736
- show_label=False,
737
- placeholder="🎸 Creed is ready...",
738
- container=False
739
- )
740
-
741
- # Input with explicit send button
742
- with gr.Row():
743
- with gr.Column(scale=7):
744
- msg = gr.Textbox(
745
- placeholder="Ask Creed anything...",
746
- container=False,
747
- submit_btn=False, # Disable built-in submit
748
- stop_btn=False
749
- )
750
- with gr.Column(scale=1, min_width=100):
751
- send_btn = gr.Button("Send", variant="primary", size="lg")
752
-
753
- # Wire up the chat - both Enter key and Send button
754
- msg.submit(
755
- respond,
756
- inputs=[msg, chatbot],
757
- outputs=[msg, chatbot],
758
- show_progress="hidden"
759
- )
760
-
761
- send_btn.click(
762
- respond,
763
- inputs=[msg, chatbot],
764
- outputs=[msg, chatbot],
765
- show_progress="hidden"
766
- )
767
-
768
- # MCP Tools section with glass styling
769
- with gr.Row(elem_classes="tools-area"):
770
- gr.HTML('<div class="tools-title">🛠️ MCP Tools</div>')
771
-
772
- with gr.Row():
773
- with gr.Column():
774
- wisdom_topic = gr.Textbox(
775
- label="Wisdom Topic",
776
- placeholder="life, business, relationships..."
777
- )
778
- wisdom_output = gr.Textbox(
779
- label="Creed's Response",
780
- interactive=False,
781
- lines=3
782
- )
783
- wisdom_btn = gr.Button("Ask Creed", variant="primary")
784
-
785
- with gr.Column():
786
- story_situation = gr.Textbox(
787
- label="Story Request",
788
- placeholder="Tell me about..."
789
- )
790
- story_output = gr.Textbox(
791
- label="Creed's Story",
792
- interactive=False,
793
- lines=3
794
- )
795
- story_btn = gr.Button("Get Story", variant="primary")
796
-
797
- # Wire up the tools
798
- wisdom_btn.click(
799
- creed_ai.creed_wisdom_tool,
800
- inputs=[wisdom_topic],
801
- outputs=[wisdom_output]
802
- )
803
-
804
- story_btn.click(
805
- creed_ai.creed_story_tool,
806
- inputs=[story_situation],
807
- outputs=[story_output]
808
- )
809
-
810
- # Modern footer
811
- gr.HTML("""
812
- <div class="footer">
813
- <strong>Creed Bratton AI</strong><br>
814
- Model: phxdev/creed-qwen-0.5b-lora • Trained by Mark Scott<br>
815
- <em>"Sometimes a guy's gotta ride the bull, am I right?"</em>
816
- </div>
817
- """)
818
-
819
- # Launch with modern styling and public sharing
820
- print("🚀 Launching Real Creed AI with modern glassmorphism design...")
821
-
822
- demo.launch(
823
- ssr_mode=False,
824
- server_name="0.0.0.0",
825
- server_port=7860,
826
- share=True, # Create public link
827
- show_error=True
828
- )
829
-
830
- if __name__ == "__main__":
831
- main()
 
1
+ #!/usr/bin/env python3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  """
3
  🎸 Creed Bratton AI - Using phxdev/creed-qwen-0.5b-lora
4
  The REAL Creed, trained by Mark, not some knockoff prompt engineering
 
87
  trust_remote_code=True,
88
  low_cpu_mem_usage=True
89
  )
 
 
 
 
90
  else:
91
  self.model = AutoModelForCausalLM.from_pretrained(
92
  model_name,
 
104
 
105
  self.model.eval()
106
 
 
 
 
 
107
  self.model_loaded = True
108
  self.loading = False
109
+ print(f"✅ Creed's consciousness loaded on {self.device}!")
110
 
111
  # GPU memory info
112
  if self.device == "cuda" and torch.cuda.is_available():
 
130
  torch_dtype=torch.float16,
131
  device_map="auto"
132
  )
 
 
133
  else:
134
  self.model = AutoModelForCausalLM.from_pretrained(
135
  base_model,
 
140
 
141
  self.model.eval()
142
  self.model_loaded = True
143
+ print(f"✅ Fallback model loaded on {self.device}")
 
144
  except Exception as fallback_error:
145
  print(f"❌ Fallback also failed: {fallback_error}")
146
  self.loading = False
 
261
  # Add conversation history
262
  conversation = system_prompt
263
  for user_msg, creed_msg in history[-4:]: # Keep recent context
264
+ conversation += f"Human: {user_msg}\n"
265
+ conversation += f"Creed: {creed_msg}\n"
 
266
 
267
  # Add current message
268
  conversation += f"Human: {message}\n"
 
307
 
308
  return final_response
309
 
 
 
 
 
 
 
310
  def creed_story_tool(self, situation: str = "mysterious") -> str:
311
  """MCP tool: Get a Creed story"""
312
  if not self.model_loaded:
313
+ return "🧠 Creed's consciousness is still