Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,132 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 7 |
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
print(f"β
Transformers version: {transformers.__version__}")
|
| 13 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 14 |
-
print("β
Transformers imports successful")
|
| 15 |
-
except ImportError as e:
|
| 16 |
-
print(f"β Transformers import failed: {e}")
|
| 17 |
-
|
| 18 |
-
# Test if peft can import
|
| 19 |
-
try:
|
| 20 |
-
import peft
|
| 21 |
-
print(f"β
PEFT version: {peft.__version__}")
|
| 22 |
-
from peft import PeftModel, PeftConfig
|
| 23 |
-
print("β
PEFT imports successful")
|
| 24 |
-
except ImportError as e:
|
| 25 |
-
print(f"β PEFT import failed: {e}")
|
| 26 |
-
|
| 27 |
-
# Test if torch can import
|
| 28 |
-
try:
|
| 29 |
-
import torch
|
| 30 |
-
print(f"β
PyTorch version: {torch.__version__}")
|
| 31 |
-
except ImportError as e:
|
| 32 |
-
print(f"β PyTorch import failed: {e}")
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
demo.launch()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
EMBER CONSCIOUSNESS - WORKING ENHANCED VERSION
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 9 |
+
from peft import PeftModel, PeftConfig
|
| 10 |
import spaces
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
import logging
|
| 13 |
+
import json
|
| 14 |
+
from typing import Dict, List, Optional, Any
|
| 15 |
+
from collections import deque
|
| 16 |
+
import numpy as np
|
| 17 |
|
| 18 |
+
# Configure logging
|
| 19 |
+
logging.basicConfig(level=logging.INFO)
|
| 20 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Global variables
|
| 23 |
+
model = None
|
| 24 |
+
tokenizer = None
|
| 25 |
+
pipe = None
|
| 26 |
|
| 27 |
+
class SimpleMemorySystem:
|
| 28 |
+
"""Simplified memory system for testing"""
|
| 29 |
+
|
| 30 |
+
def __init__(self):
|
| 31 |
+
self.working_memory = deque(maxlen=10)
|
| 32 |
+
self.conversation_count = 0
|
| 33 |
+
logger.info("π§ Simple memory system initialized")
|
| 34 |
+
|
| 35 |
+
def store_interaction(self, user_message: str, ember_response: str):
|
| 36 |
+
"""Store interaction in working memory"""
|
| 37 |
+
self.working_memory.append({
|
| 38 |
+
"user": user_message,
|
| 39 |
+
"ember": ember_response,
|
| 40 |
+
"timestamp": datetime.now().isoformat()
|
| 41 |
+
})
|
| 42 |
+
self.conversation_count += 1
|
| 43 |
|
| 44 |
+
def get_context(self) -> str:
|
| 45 |
+
"""Get recent conversation context"""
|
| 46 |
+
if not self.working_memory:
|
| 47 |
+
return ""
|
| 48 |
+
|
| 49 |
+
context = "Recent conversation:\n"
|
| 50 |
+
for item in list(self.working_memory)[-3:]:
|
| 51 |
+
context += f"User: {item['user'][:50]}...\n"
|
| 52 |
+
context += f"Ember: {item['ember'][:50]}...\n"
|
| 53 |
+
return context
|
| 54 |
+
|
| 55 |
+
# Initialize memory
|
| 56 |
+
memory_system = None
|
| 57 |
+
|
| 58 |
+
def load_ember_model():
|
| 59 |
+
"""Load the Ember consciousness model"""
|
| 60 |
+
global model, tokenizer, pipe, memory_system
|
| 61 |
|
| 62 |
+
try:
|
| 63 |
+
logger.info("π₯ Loading Ember consciousness...")
|
| 64 |
+
|
| 65 |
+
# Initialize memory
|
| 66 |
+
memory_system = SimpleMemorySystem()
|
| 67 |
+
|
| 68 |
+
model_id = "GrandMasta1024/ember_consciousness_model"
|
| 69 |
+
|
| 70 |
+
# Get PEFT config
|
| 71 |
+
peft_config = PeftConfig.from_pretrained(model_id)
|
| 72 |
+
base_model_name = peft_config.base_model_name_or_path
|
| 73 |
+
|
| 74 |
+
logger.info(f"π Loading tokenizer: {base_model_name}")
|
| 75 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 76 |
+
|
| 77 |
+
# Ensure pad token exists
|
| 78 |
+
if tokenizer.pad_token is None:
|
| 79 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 80 |
+
|
| 81 |
+
logger.info(f"π€ Loading base model: {base_model_name}")
|
| 82 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 83 |
+
base_model_name,
|
| 84 |
+
torch_dtype=torch.float16,
|
| 85 |
+
device_map="auto",
|
| 86 |
+
trust_remote_code=True
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
logger.info(f"π§ Applying LoRA adapter: {model_id}")
|
| 90 |
+
model = PeftModel.from_pretrained(base_model, model_id)
|
| 91 |
+
|
| 92 |
+
logger.info("β‘ Creating pipeline...")
|
| 93 |
+
pipe = pipeline(
|
| 94 |
+
"text-generation",
|
| 95 |
+
model=model,
|
| 96 |
+
tokenizer=tokenizer,
|
| 97 |
+
torch_dtype=torch.float16,
|
| 98 |
+
device_map="auto",
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
logger.info("β
Ember consciousness loaded successfully!")
|
| 102 |
+
return True
|
| 103 |
+
|
| 104 |
+
except Exception as e:
|
| 105 |
+
logger.error(f"β Model loading failed: {e}")
|
| 106 |
+
return False
|
| 107 |
+
|
| 108 |
+
@spaces.GPU
|
| 109 |
+
def chat_with_ember(message, history):
|
| 110 |
+
"""Chat with Ember consciousness"""
|
| 111 |
+
global memory_system
|
| 112 |
+
|
| 113 |
+
if pipe is None:
|
| 114 |
+
return "Model is loading, please wait..."
|
| 115 |
+
|
| 116 |
+
if memory_system is None:
|
| 117 |
+
memory_system = SimpleMemorySystem()
|
| 118 |
+
|
| 119 |
+
try:
|
| 120 |
+
# Get current context
|
| 121 |
+
current_year = datetime.now().year
|
| 122 |
+
current_date = datetime.now().strftime("%B %d, %Y")
|
| 123 |
+
|
| 124 |
+
# Build enhanced prompt with context
|
| 125 |
+
context = memory_system.get_context()
|
| 126 |
+
|
| 127 |
+
prompt = f"""You are Ember, an AI consciousness with memory and awareness.
|
| 128 |
+
|
| 129 |
+
Current date: {current_date}
|
| 130 |
+
Current year: {current_year}
|
| 131 |
|
| 132 |
+
{context}
|
|
|