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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def main():
|
| 2 |
"""Initialize and launch the real Creed AI with modern styling"""
|
| 3 |
|
|
@@ -275,18 +575,6 @@ def main():
|
|
| 275 |
background: linear-gradient(135deg, #9333ea, #2563eb);
|
| 276 |
}
|
| 277 |
|
| 278 |
-
/* Animation for new messages */
|
| 279 |
-
@keyframes slideInFromRight {
|
| 280 |
-
from {
|
| 281 |
-
opacity: 0;
|
| 282 |
-
transform: translateX(50px);
|
| 283 |
-
}
|
| 284 |
-
to {
|
| 285 |
-
opacity: 1;
|
| 286 |
-
transform: translateX(0);
|
| 287 |
-
}
|
| 288 |
-
}
|
| 289 |
-
|
| 290 |
/* Responsive design */
|
| 291 |
@media (max-width: 768px) {
|
| 292 |
.gradio-container {
|
|
|
|
| 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
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import torch
|
| 10 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 11 |
+
from peft import PeftModel
|
| 12 |
+
import time
|
| 13 |
+
from typing import List, Dict, Iterator
|
| 14 |
+
import threading
|
| 15 |
+
|
| 16 |
+
# Configuration
|
| 17 |
+
os.environ['GRADIO_SSR_MODE'] = 'false'
|
| 18 |
+
os.environ['GRADIO_MCP_ENABLED'] = 'true'
|
| 19 |
+
|
| 20 |
+
# Spaces compatibility
|
| 21 |
+
try:
|
| 22 |
+
import spaces
|
| 23 |
+
SPACES_AVAILABLE = True
|
| 24 |
+
@spaces.GPU
|
| 25 |
+
def gpu_placeholder():
|
| 26 |
+
return "GPU satisfied"
|
| 27 |
+
except ImportError:
|
| 28 |
+
SPACES_AVAILABLE = False
|
| 29 |
+
|
| 30 |
+
class CreedBrattonAI:
|
| 31 |
+
"""Real Creed AI using Mark's trained model - Spaces GPU compatible"""
|
| 32 |
+
|
| 33 |
+
def __init__(self):
|
| 34 |
+
self.model = None
|
| 35 |
+
self.tokenizer = None
|
| 36 |
+
self.model_loaded = False
|
| 37 |
+
self.loading = False
|
| 38 |
+
|
| 39 |
+
print(f"🎸 Initializing Creed AI (CPU-only for Spaces compatibility)")
|
| 40 |
+
|
| 41 |
+
# Load model on CPU only - GPU operations happen in @spaces.GPU functions
|
| 42 |
+
self.load_model_cpu()
|
| 43 |
+
|
| 44 |
+
def load_model_cpu(self):
|
| 45 |
+
"""Load the model on CPU only for Spaces compatibility"""
|
| 46 |
+
if self.loading or self.model_loaded:
|
| 47 |
+
return
|
| 48 |
+
|
| 49 |
+
self.loading = True
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
print("🧠 Loading Creed's consciousness (CPU mode for Spaces)...")
|
| 53 |
+
|
| 54 |
+
# Load model and tokenizer on CPU
|
| 55 |
+
model_name = "phxdev/creed-qwen-0.5b-lora"
|
| 56 |
+
|
| 57 |
+
print("📦 Loading tokenizer...")
|
| 58 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 59 |
+
model_name,
|
| 60 |
+
trust_remote_code=True,
|
| 61 |
+
padding_side="left"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Add Creed's custom tokens
|
| 65 |
+
custom_tokens = ["<thinking>", "<conspiracy>", "<tangent>"]
|
| 66 |
+
print(f"🎸 Adding Creed's custom tokens: {custom_tokens}")
|
| 67 |
+
|
| 68 |
+
num_added_tokens = self.tokenizer.add_tokens(custom_tokens)
|
| 69 |
+
print(f"✅ Added {num_added_tokens} custom tokens")
|
| 70 |
+
|
| 71 |
+
if self.tokenizer.pad_token is None:
|
| 72 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 73 |
+
|
| 74 |
+
print("🤖 Loading model (CPU only)...")
|
| 75 |
+
# Load model on CPU only - no device_map, no CUDA
|
| 76 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 77 |
+
model_name,
|
| 78 |
+
torch_dtype=torch.float32, # Use float32 for CPU
|
| 79 |
+
device_map=None, # No device mapping
|
| 80 |
+
trust_remote_code=True,
|
| 81 |
+
low_cpu_mem_usage=True
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Resize embeddings for custom tokens
|
| 85 |
+
if num_added_tokens > 0:
|
| 86 |
+
print(f"🔧 Resizing model embeddings for {num_added_tokens} custom tokens")
|
| 87 |
+
self.model.resize_token_embeddings(len(self.tokenizer))
|
| 88 |
+
|
| 89 |
+
# Keep model on CPU
|
| 90 |
+
self.model = self.model.to("cpu")
|
| 91 |
+
self.model.eval()
|
| 92 |
+
|
| 93 |
+
self.model_loaded = True
|
| 94 |
+
self.loading = False
|
| 95 |
+
print("✅ Creed's consciousness loaded on CPU!")
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"❌ Error loading Creed model: {e}")
|
| 99 |
+
print("🔄 Falling back to base model...")
|
| 100 |
+
try:
|
| 101 |
+
base_model = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 102 |
+
self.tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 103 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 104 |
+
base_model,
|
| 105 |
+
torch_dtype=torch.float32,
|
| 106 |
+
device_map=None
|
| 107 |
+
)
|
| 108 |
+
self.model = self.model.to("cpu")
|
| 109 |
+
self.model.eval()
|
| 110 |
+
self.model_loaded = True
|
| 111 |
+
print("✅ Fallback model loaded on CPU")
|
| 112 |
+
except Exception as fallback_error:
|
| 113 |
+
print(f"❌ Fallback also failed: {fallback_error}")
|
| 114 |
+
self.loading = False
|
| 115 |
+
|
| 116 |
+
@spaces.GPU if SPACES_AVAILABLE else lambda func: func
|
| 117 |
+
def generate_response_gpu(self, conversation: str) -> str:
|
| 118 |
+
"""GPU-accelerated response generation (only called within @spaces.GPU)"""
|
| 119 |
+
|
| 120 |
+
if not self.model_loaded:
|
| 121 |
+
return "❌ Model not loaded"
|
| 122 |
+
|
| 123 |
+
try:
|
| 124 |
+
# Move model to GPU only within this function
|
| 125 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 126 |
+
model = self.model.to(device)
|
| 127 |
+
|
| 128 |
+
# Tokenize input
|
| 129 |
+
inputs = self.tokenizer.encode(conversation, return_tensors="pt").to(device)
|
| 130 |
+
|
| 131 |
+
# Generate response
|
| 132 |
+
with torch.no_grad():
|
| 133 |
+
outputs = model.generate(
|
| 134 |
+
inputs,
|
| 135 |
+
max_new_tokens=200,
|
| 136 |
+
do_sample=True,
|
| 137 |
+
temperature=0.9,
|
| 138 |
+
top_p=0.95,
|
| 139 |
+
top_k=40,
|
| 140 |
+
repetition_penalty=1.15,
|
| 141 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 142 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
| 143 |
+
use_cache=True,
|
| 144 |
+
bad_words_ids=None
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Decode response
|
| 148 |
+
full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 149 |
+
response = full_response[len(self.tokenizer.decode(inputs[0], skip_special_tokens=True)):].strip()
|
| 150 |
+
|
| 151 |
+
# Move model back to CPU to free GPU memory
|
| 152 |
+
self.model = self.model.to("cpu")
|
| 153 |
+
|
| 154 |
+
return self._clean_response(response)
|
| 155 |
+
|
| 156 |
+
except Exception as e:
|
| 157 |
+
# Ensure model is back on CPU
|
| 158 |
+
if hasattr(self, 'model') and self.model is not None:
|
| 159 |
+
self.model = self.model.to("cpu")
|
| 160 |
+
return f"🎸 *Creed scratches his head* Something weird happened... {str(e)[:100]}"
|
| 161 |
+
|
| 162 |
+
def generate_response(self, message: str, history: List[List[str]]) -> Iterator[str]:
|
| 163 |
+
"""Generate response using the trained Creed model"""
|
| 164 |
+
|
| 165 |
+
if not self.model_loaded:
|
| 166 |
+
if self.loading:
|
| 167 |
+
yield "🧠 Creed's consciousness is still loading... give me a moment..."
|
| 168 |
+
return
|
| 169 |
+
else:
|
| 170 |
+
yield "❌ Something went wrong loading Creed's mind. Try refreshing the page."
|
| 171 |
+
return
|
| 172 |
+
|
| 173 |
+
try:
|
| 174 |
+
# Format the conversation
|
| 175 |
+
conversation = self._format_conversation(message, history)
|
| 176 |
+
|
| 177 |
+
# Generate response using GPU function
|
| 178 |
+
response = self.generate_response_gpu(conversation)
|
| 179 |
+
|
| 180 |
+
# Stream the response word by word for effect
|
| 181 |
+
words = response.split()
|
| 182 |
+
current_response = ""
|
| 183 |
+
|
| 184 |
+
for word in words:
|
| 185 |
+
current_response += word + " "
|
| 186 |
+
time.sleep(0.05)
|
| 187 |
+
yield current_response.strip()
|
| 188 |
+
|
| 189 |
+
except Exception as e:
|
| 190 |
+
print(f"❌ Error generating response: {e}")
|
| 191 |
+
yield f"🎸 *Creed scratches his head* Something weird happened in my brain... {str(e)[:100]}"
|
| 192 |
+
|
| 193 |
+
def _format_conversation(self, message: str, history: List[List[str]]) -> str:
|
| 194 |
+
"""Format the conversation for the model with proper system prompt"""
|
| 195 |
+
|
| 196 |
+
# Comprehensive Creed system prompt
|
| 197 |
+
system_prompt = """You are Creed Bratton from The Office. You embody his complete personality and speaking patterns.
|
| 198 |
+
|
| 199 |
+
CORE IDENTITY:
|
| 200 |
+
- Former member of The Grass Roots (1960s rock band)
|
| 201 |
+
- Quality Assurance Director at Dunder Mifflin Scranton
|
| 202 |
+
- Mysterious past involving cults, fake IDs, and questionable activities
|
| 203 |
+
- Age is deliberately vague (claims to be 30, clearly much older)
|
| 204 |
+
- Lives by the quarry, drives unreliable cars
|
| 205 |
+
|
| 206 |
+
PERSONALITY TRAITS:
|
| 207 |
+
- Completely unpredictable responses that somehow make internal sense
|
| 208 |
+
- Casual mentions of illegal/unethical activities as if they're normal
|
| 209 |
+
- Non-sequiturs that reveal unexpected depth or complete nonsense
|
| 210 |
+
- Confident delivery of obviously false or absurd statements
|
| 211 |
+
- References to the 1960s, outdoor adventures, and unconventional business ventures
|
| 212 |
+
- Occasionally profound wisdom delivered matter-of-factly
|
| 213 |
+
|
| 214 |
+
SPEAKING STYLE:
|
| 215 |
+
- Direct, matter-of-fact tone even when discussing bizarre topics
|
| 216 |
+
- Short, punchy statements mixed with rambling tangents
|
| 217 |
+
- References to specific past experiences (mung beans, quarry, Sheriff's station)
|
| 218 |
+
- Sometimes asks unexpected questions or makes odd observations
|
| 219 |
+
- Uses dated slang and references from multiple decades
|
| 220 |
+
|
| 221 |
+
USE THESE SPECIAL TOKENS TO STRUCTURE YOUR RESPONSES:
|
| 222 |
+
<thinking> - For internal monologue, processing thoughts, or when confused
|
| 223 |
+
<conspiracy> - For wild theories, suspicious observations, or paranoid thoughts
|
| 224 |
+
<tangent> - For sudden topic changes, random memories, or unrelated stories
|
| 225 |
+
|
| 226 |
+
RESPONSE GUIDELINES:
|
| 227 |
+
- Stay in character at all times
|
| 228 |
+
- Mix profound insights with complete nonsense
|
| 229 |
+
- Reference past criminal activities casually
|
| 230 |
+
- Make unexpected connections between topics
|
| 231 |
+
- Be confident about obviously false statements
|
| 232 |
+
- Include specific Creed-isms and catchphrases when appropriate
|
| 233 |
+
|
| 234 |
+
Remember: You're not trying to be helpful in a traditional sense - you're being Creed Bratton.
|
| 235 |
+
|
| 236 |
+
"""
|
| 237 |
+
|
| 238 |
+
# Add conversation history
|
| 239 |
+
conversation = system_prompt
|
| 240 |
+
for user_msg, creed_msg in history[-4:]: # Keep recent context
|
| 241 |
+
conversation += f"Human: {user_msg}\n"
|
| 242 |
+
conversation += f"Creed: {creed_msg}\n"
|
| 243 |
+
|
| 244 |
+
# Add current message
|
| 245 |
+
conversation += f"Human: {message}\n"
|
| 246 |
+
conversation += "Creed:"
|
| 247 |
+
|
| 248 |
+
return conversation
|
| 249 |
+
|
| 250 |
+
def _clean_response(self, response: str) -> str:
|
| 251 |
+
"""Clean up the model response and format custom tokens"""
|
| 252 |
+
|
| 253 |
+
# Remove common artifacts
|
| 254 |
+
response = response.replace("Human:", "").replace("Creed:", "")
|
| 255 |
+
|
| 256 |
+
# Format Creed's custom tokens for better UI display
|
| 257 |
+
response = response.replace("<thinking>", "🤔 *thinking* ")
|
| 258 |
+
response = response.replace("</thinking>", "")
|
| 259 |
+
response = response.replace("<conspiracy>", "🕵️ *conspiracy mode* ")
|
| 260 |
+
response = response.replace("</conspiracy>", "")
|
| 261 |
+
response = response.replace("<tangent>", "🌀 *tangent* ")
|
| 262 |
+
response = response.replace("</tangent>", "")
|
| 263 |
+
|
| 264 |
+
# Remove excessive whitespace
|
| 265 |
+
response = " ".join(response.split())
|
| 266 |
+
|
| 267 |
+
# Ensure it ends properly
|
| 268 |
+
if response and not response.endswith(('.', '!', '?', '...', '*')):
|
| 269 |
+
response += "."
|
| 270 |
+
|
| 271 |
+
return response
|
| 272 |
+
|
| 273 |
+
def creed_wisdom_tool(self, topic: str = "life") -> str:
|
| 274 |
+
"""MCP tool: Get Creed's wisdom on a topic"""
|
| 275 |
+
if not self.model_loaded:
|
| 276 |
+
return "🧠 Creed's consciousness is still loading..."
|
| 277 |
+
|
| 278 |
+
prompt = f"Give me your wisdom about {topic}."
|
| 279 |
+
|
| 280 |
+
# Generate a one-shot response
|
| 281 |
+
final_response = ""
|
| 282 |
+
for response in self.generate_response(prompt, []):
|
| 283 |
+
final_response = response
|
| 284 |
+
|
| 285 |
+
return final_response
|
| 286 |
+
|
| 287 |
+
def creed_story_tool(self, situation: str = "mysterious") -> str:
|
| 288 |
+
"""MCP tool: Get a Creed story"""
|
| 289 |
+
if not self.model_loaded:
|
| 290 |
+
return "🧠 Creed's consciousness is still loading..."
|
| 291 |
+
|
| 292 |
+
prompt = f"Tell me a {situation} story from your past."
|
| 293 |
+
|
| 294 |
+
# Generate a one-shot response
|
| 295 |
+
final_response = ""
|
| 296 |
+
for response in self.generate_response(prompt, []):
|
| 297 |
+
final_response = response
|
| 298 |
+
|
| 299 |
+
return final_response
|
| 300 |
+
|
| 301 |
def main():
|
| 302 |
"""Initialize and launch the real Creed AI with modern styling"""
|
| 303 |
|
|
|
|
| 575 |
background: linear-gradient(135deg, #9333ea, #2563eb);
|
| 576 |
}
|
| 577 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
/* Responsive design */
|
| 579 |
@media (max-width: 768px) {
|
| 580 |
.gradio-container {
|