Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,612 +1,48 @@
|
|
| 1 |
-
|
| 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 |
-
|
| 34 |
-
self.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
for response in self.generate_response(prompt, []):
|
| 282 |
-
final_response = response
|
| 283 |
-
|
| 284 |
-
return final_response
|
| 285 |
-
|
| 286 |
-
def creed_story_tool(self, situation: str = "mysterious") -> str:
|
| 287 |
-
"""MCP tool: Get a Creed story"""
|
| 288 |
-
if not self.model_loaded:
|
| 289 |
-
return "🧠 Creed's consciousness is still loading..."
|
| 290 |
-
|
| 291 |
-
prompt = f"Tell me a {situation} story from your past."
|
| 292 |
-
|
| 293 |
-
# Generate a one-shot response
|
| 294 |
-
for response in self.generate_response(prompt, []):
|
| 295 |
-
final_response = response
|
| 296 |
-
|
| 297 |
-
return final_response
|
| 298 |
-
|
| 299 |
-
def main():
|
| 300 |
-
"""Initialize and launch the real Creed AI"""
|
| 301 |
-
|
| 302 |
-
print("🎸 Initializing REAL Creed Bratton AI...")
|
| 303 |
-
print("📡 Loading Mark's trained model: phxdev/creed-qwen-0.5b-lora")
|
| 304 |
-
|
| 305 |
-
# Initialize Creed AI
|
| 306 |
-
creed_ai = CreedBrattonAI()
|
| 307 |
-
|
| 308 |
-
if SPACES_AVAILABLE:
|
| 309 |
-
gpu_placeholder()
|
| 310 |
-
print("✅ Spaces compatibility enabled")
|
| 311 |
-
|
| 312 |
-
# Simple, readable CSS that actually works
|
| 313 |
-
simple_css = """
|
| 314 |
-
/* Basic reset */
|
| 315 |
-
.gradio-container {
|
| 316 |
-
max-width: 1200px !important;
|
| 317 |
-
margin: 0 auto !important;
|
| 318 |
-
background: #ffffff !important;
|
| 319 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif !important;
|
| 320 |
-
color: #1f2937 !important;
|
| 321 |
-
padding: 20px !important;
|
| 322 |
-
}
|
| 323 |
-
|
| 324 |
-
/* Header */
|
| 325 |
-
.header {
|
| 326 |
-
background: #ffffff !important;
|
| 327 |
-
border: 2px solid #e5e7eb !important;
|
| 328 |
-
border-radius: 8px !important;
|
| 329 |
-
padding: 24px !important;
|
| 330 |
-
margin-bottom: 24px !important;
|
| 331 |
-
text-align: center !important;
|
| 332 |
-
}
|
| 333 |
-
|
| 334 |
-
.header h1 {
|
| 335 |
-
font-size: 32px !important;
|
| 336 |
-
font-weight: 600 !important;
|
| 337 |
-
color: #111827 !important;
|
| 338 |
-
margin: 0 0 8px 0 !important;
|
| 339 |
-
}
|
| 340 |
-
|
| 341 |
-
.header p {
|
| 342 |
-
font-size: 16px !important;
|
| 343 |
-
color: #6b7280 !important;
|
| 344 |
-
margin: 0 !important;
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
/* Info boxes */
|
| 348 |
-
.info-box {
|
| 349 |
-
background: #f9fafb !important;
|
| 350 |
-
border: 1px solid #d1d5db !important;
|
| 351 |
-
border-radius: 6px !important;
|
| 352 |
-
padding: 16px !important;
|
| 353 |
-
margin: 16px 0 !important;
|
| 354 |
-
color: #374151 !important;
|
| 355 |
-
font-size: 14px !important;
|
| 356 |
-
line-height: 1.5 !important;
|
| 357 |
-
}
|
| 358 |
-
|
| 359 |
-
.status-box {
|
| 360 |
-
background: #ecfdf5 !important;
|
| 361 |
-
border: 1px solid #a7f3d0 !important;
|
| 362 |
-
border-radius: 6px !important;
|
| 363 |
-
padding: 12px 16px !important;
|
| 364 |
-
margin: 16px 0 !important;
|
| 365 |
-
color: #065f46 !important;
|
| 366 |
-
font-weight: 500 !important;
|
| 367 |
-
}
|
| 368 |
-
|
| 369 |
-
/* Chat area */
|
| 370 |
-
.chat-area {
|
| 371 |
-
background: #ffffff !important;
|
| 372 |
-
border: 2px solid #e5e7eb !important;
|
| 373 |
-
border-radius: 8px !important;
|
| 374 |
-
margin: 16px 0 !important;
|
| 375 |
-
overflow: hidden !important;
|
| 376 |
-
}
|
| 377 |
-
|
| 378 |
-
/* Tools section */
|
| 379 |
-
.tools-area {
|
| 380 |
-
background: #ffffff !important;
|
| 381 |
-
border: 2px solid #e5e7eb !important;
|
| 382 |
-
border-radius: 8px !important;
|
| 383 |
-
padding: 24px !important;
|
| 384 |
-
margin: 24px 0 !important;
|
| 385 |
-
}
|
| 386 |
-
|
| 387 |
-
.tools-title {
|
| 388 |
-
font-size: 20px !important;
|
| 389 |
-
font-weight: 600 !important;
|
| 390 |
-
color: #111827 !important;
|
| 391 |
-
margin: 0 0 16px 0 !important;
|
| 392 |
-
padding-bottom: 8px !important;
|
| 393 |
-
border-bottom: 1px solid #e5e7eb !important;
|
| 394 |
-
}
|
| 395 |
-
|
| 396 |
-
/* Form elements - force readable colors */
|
| 397 |
-
.gradio-textbox input,
|
| 398 |
-
.gradio-textbox textarea {
|
| 399 |
-
background: #ffffff !important;
|
| 400 |
-
border: 1px solid #d1d5db !important;
|
| 401 |
-
color: #111827 !important;
|
| 402 |
-
border-radius: 4px !important;
|
| 403 |
-
padding: 8px 12px !important;
|
| 404 |
-
}
|
| 405 |
-
|
| 406 |
-
.gradio-textbox input:focus,
|
| 407 |
-
.gradio-textbox textarea:focus {
|
| 408 |
-
border-color: #3b82f6 !important;
|
| 409 |
-
outline: none !important;
|
| 410 |
-
box-shadow: 0 0 0 1px #3b82f6 !important;
|
| 411 |
-
}
|
| 412 |
-
|
| 413 |
-
/* Labels - force readable */
|
| 414 |
-
.gradio-container label {
|
| 415 |
-
color: #374151 !important;
|
| 416 |
-
font-weight: 500 !important;
|
| 417 |
-
font-size: 14px !important;
|
| 418 |
-
margin-bottom: 4px !important;
|
| 419 |
-
display: block !important;
|
| 420 |
-
}
|
| 421 |
-
|
| 422 |
-
/* Buttons */
|
| 423 |
-
.gradio-container button {
|
| 424 |
-
background: #3b82f6 !important;
|
| 425 |
-
color: #ffffff !important;
|
| 426 |
-
border: none !important;
|
| 427 |
-
border-radius: 4px !important;
|
| 428 |
-
padding: 8px 16px !important;
|
| 429 |
-
font-weight: 500 !important;
|
| 430 |
-
cursor: pointer !important;
|
| 431 |
-
}
|
| 432 |
-
|
| 433 |
-
.gradio-container button:hover {
|
| 434 |
-
background: #2563eb !important;
|
| 435 |
-
}
|
| 436 |
-
|
| 437 |
-
/* Chat messages - ensure readability */
|
| 438 |
-
.message {
|
| 439 |
-
color: #111827 !important;
|
| 440 |
-
background: #ffffff !important;
|
| 441 |
-
}
|
| 442 |
-
|
| 443 |
-
/* Footer */
|
| 444 |
-
.footer {
|
| 445 |
-
text-align: center !important;
|
| 446 |
-
padding: 24px !important;
|
| 447 |
-
color: #6b7280 !important;
|
| 448 |
-
border-top: 1px solid #e5e7eb !important;
|
| 449 |
-
margin-top: 32px !important;
|
| 450 |
-
background: #ffffff !important;
|
| 451 |
-
}
|
| 452 |
-
|
| 453 |
-
/* Force text to be readable everywhere */
|
| 454 |
-
.gradio-container * {
|
| 455 |
-
color: inherit !important;
|
| 456 |
-
}
|
| 457 |
-
|
| 458 |
-
.gradio-container .prose {
|
| 459 |
-
color: #111827 !important;
|
| 460 |
-
max-width: none !important;
|
| 461 |
-
}
|
| 462 |
-
|
| 463 |
-
.gradio-container .prose h1,
|
| 464 |
-
.gradio-container .prose h2,
|
| 465 |
-
.gradio-container .prose h3,
|
| 466 |
-
.gradio-container .prose p {
|
| 467 |
-
color: #111827 !important;
|
| 468 |
-
}
|
| 469 |
-
|
| 470 |
-
/* Remove any dark mode for now */
|
| 471 |
-
@media (prefers-color-scheme: dark) {
|
| 472 |
-
/* Override dark mode completely */
|
| 473 |
-
.gradio-container {
|
| 474 |
-
background: #ffffff !important;
|
| 475 |
-
color: #1f2937 !important;
|
| 476 |
-
}
|
| 477 |
-
}
|
| 478 |
-
"""
|
| 479 |
-
|
| 480 |
-
# Create the interface
|
| 481 |
-
with gr.Blocks(
|
| 482 |
-
title="🎸 Creed Bratton AI",
|
| 483 |
-
css=simple_css,
|
| 484 |
-
theme=gr.themes.Default()
|
| 485 |
-
) as demo:
|
| 486 |
-
|
| 487 |
-
# Clean header
|
| 488 |
-
gr.HTML("""
|
| 489 |
-
<div class="header">
|
| 490 |
-
<h1>🎸 Creed Bratton AI</h1>
|
| 491 |
-
<p>Powered by phxdev/creed-qwen-0.5b-lora • Enterprise-grade unpredictability</p>
|
| 492 |
-
</div>
|
| 493 |
-
""")
|
| 494 |
-
|
| 495 |
-
# Model info
|
| 496 |
-
gr.HTML("""
|
| 497 |
-
<div class="info-box">
|
| 498 |
-
<strong>Model:</strong> phxdev/creed-qwen-0.5b-lora<br>
|
| 499 |
-
<strong>Base:</strong> Qwen 0.5B + LoRA fine-tuning<br>
|
| 500 |
-
<strong>Tokens:</strong> <thinking>, <conspiracy>, <tangent>
|
| 501 |
-
</div>
|
| 502 |
-
""")
|
| 503 |
-
|
| 504 |
-
# MCP status
|
| 505 |
-
if os.environ.get('GRADIO_MCP_ENABLED'):
|
| 506 |
-
gr.HTML("""
|
| 507 |
-
<div class="status-box">
|
| 508 |
-
✓ MCP Server Active • Available as tool for Claude Desktop
|
| 509 |
-
</div>
|
| 510 |
-
""")
|
| 511 |
-
|
| 512 |
-
# Main chat interface
|
| 513 |
-
with gr.Row(elem_classes="chat-area"):
|
| 514 |
-
chatbot = gr.Chatbot(
|
| 515 |
-
type='messages',
|
| 516 |
-
height=550,
|
| 517 |
-
show_copy_button=True,
|
| 518 |
-
show_share_button=False,
|
| 519 |
-
avatar_images=["👤", "🎸"],
|
| 520 |
-
bubble_full_width=False,
|
| 521 |
-
show_label=False,
|
| 522 |
-
placeholder="🎸 Creed is ready..."
|
| 523 |
-
)
|
| 524 |
-
|
| 525 |
-
# Input
|
| 526 |
-
msg = gr.Textbox(
|
| 527 |
-
placeholder="Ask Creed anything...",
|
| 528 |
-
container=False,
|
| 529 |
-
scale=7,
|
| 530 |
-
submit_btn=True,
|
| 531 |
-
stop_btn=True
|
| 532 |
-
)
|
| 533 |
-
|
| 534 |
-
# MCP Tools section
|
| 535 |
-
with gr.Row(elem_classes="tools-area"):
|
| 536 |
-
gr.HTML('<div class="tools-title">🛠️ MCP Tools</div>')
|
| 537 |
-
|
| 538 |
-
with gr.Row():
|
| 539 |
-
with gr.Column():
|
| 540 |
-
wisdom_topic = gr.Textbox(
|
| 541 |
-
label="Wisdom Topic",
|
| 542 |
-
placeholder="life, business, relationships..."
|
| 543 |
-
)
|
| 544 |
-
wisdom_output = gr.Textbox(
|
| 545 |
-
label="Creed's Response",
|
| 546 |
-
interactive=False
|
| 547 |
-
)
|
| 548 |
-
wisdom_btn = gr.Button("Ask Creed", variant="primary")
|
| 549 |
-
|
| 550 |
-
with gr.Column():
|
| 551 |
-
story_situation = gr.Textbox(
|
| 552 |
-
label="Story Request",
|
| 553 |
-
placeholder="Tell me about..."
|
| 554 |
-
)
|
| 555 |
-
story_output = gr.Textbox(
|
| 556 |
-
label="Creed's Story",
|
| 557 |
-
interactive=False
|
| 558 |
-
)
|
| 559 |
-
story_btn = gr.Button("Get Story", variant="primary")
|
| 560 |
-
|
| 561 |
-
# Wire up the tools
|
| 562 |
-
wisdom_btn.click(
|
| 563 |
-
creed_ai.creed_wisdom_tool,
|
| 564 |
-
inputs=[wisdom_topic],
|
| 565 |
-
outputs=[wisdom_output]
|
| 566 |
-
)
|
| 567 |
-
|
| 568 |
-
story_btn.click(
|
| 569 |
-
creed_ai.creed_story_tool,
|
| 570 |
-
inputs=[story_situation],
|
| 571 |
-
outputs=[story_output]
|
| 572 |
-
)
|
| 573 |
-
|
| 574 |
-
# Chat interface
|
| 575 |
-
chat_interface = gr.ChatInterface(
|
| 576 |
-
fn=creed_ai.generate_response,
|
| 577 |
-
type='messages',
|
| 578 |
-
chatbot=chatbot,
|
| 579 |
-
textbox=msg,
|
| 580 |
-
title=None,
|
| 581 |
-
description=None,
|
| 582 |
-
examples=[
|
| 583 |
-
"What's your philosophy on life?",
|
| 584 |
-
"Tell me about your business ventures",
|
| 585 |
-
"Share a story from the 60s",
|
| 586 |
-
"What's your advice for success?"
|
| 587 |
-
],
|
| 588 |
-
cache_examples=False
|
| 589 |
-
)
|
| 590 |
-
|
| 591 |
-
# Footer
|
| 592 |
-
gr.HTML("""
|
| 593 |
-
<div class="footer">
|
| 594 |
-
<strong>Creed Bratton AI</strong><br>
|
| 595 |
-
Model: phxdev/creed-qwen-0.5b-lora • Trained by Mark Scott<br>
|
| 596 |
-
<em>"Sometimes a guy's gotta ride the bull, am I right?"</em>
|
| 597 |
-
</div>
|
| 598 |
-
""")
|
| 599 |
-
|
| 600 |
-
# Launch
|
| 601 |
-
print("🚀 Launching Real Creed AI...")
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
if
|
| 612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def generate_response(self, message: Dict, history: List[Dict]) -> Iterator[str]:
|
| 2 |
+
"""Generate response using the trained Creed model - Fixed for ChatInterface"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
if not self.model_loaded:
|
| 5 |
+
if self.loading:
|
| 6 |
+
yield "🧠 Creed's consciousness is still loading... give me a moment..."
|
| 7 |
+
return
|
| 8 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
try:
|
| 13 |
+
# Extract the actual message content from the message dict
|
| 14 |
+
user_message = message.get('text', '') if isinstance(message, dict) else str(message)
|
| 15 |
+
|
| 16 |
+
# Convert message format history to the format your model expects
|
| 17 |
+
formatted_history = []
|
| 18 |
+
if history:
|
| 19 |
+
for msg_pair in history:
|
| 20 |
+
if isinstance(msg_pair, dict):
|
| 21 |
+
# Handle message format
|
| 22 |
+
if msg_pair.get('role') == 'user':
|
| 23 |
+
user_msg = msg_pair.get('content', '')
|
| 24 |
+
# Find corresponding assistant message
|
| 25 |
+
assistant_msg = ""
|
| 26 |
+
formatted_history.append([user_msg, assistant_msg])
|
| 27 |
+
else:
|
| 28 |
+
# Handle legacy format
|
| 29 |
+
formatted_history.append(msg_pair)
|
| 30 |
+
|
| 31 |
+
# Format the conversation
|
| 32 |
+
conversation = self._format_conversation(user_message, formatted_history)
|
| 33 |
+
|
| 34 |
+
# Generate response using GPU function
|
| 35 |
+
response = self.generate_response_gpu(conversation)
|
| 36 |
+
|
| 37 |
+
# Stream the response word by word for effect
|
| 38 |
+
words = response.split()
|
| 39 |
+
current_response = ""
|
| 40 |
+
|
| 41 |
+
for word in words:
|
| 42 |
+
current_response += word + " "
|
| 43 |
+
time.sleep(0.05)
|
| 44 |
+
yield current_response.strip()
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"❌ Error generating response: {e}")
|
| 48 |
+
yield f"🎸 *Creed scratches his head* Something weird happened in my brain... {str(e)[:100]}"
|