File size: 3,198 Bytes
1a2fbfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel

# Model configuration
BASE_MODEL = "unsloth/mistral-7b-v0.3-bnb-4bit"
LORA_MODEL = "Metavolve-Labs/spark-v1"

print("Loading Spark...")

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(LORA_MODEL)

# Quantization config
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)

# Load base model
base_model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    quantization_config=bnb_config,
    device_map="auto",
    trust_remote_code=True,
)

# Load LoRA adapters
model = PeftModel.from_pretrained(base_model, LORA_MODEL)
model.eval()

print("Spark loaded!")

SYSTEM_PROMPT = """You are SPARK (State-space Perception And Reasoning Kernel), an AI trained on Alexandria Aeternum - a curated collection of 10,000+ museum artworks with rich semantic metadata from The Metropolitan Museum of Art.

You have deep knowledge of:
- Art history, movements, and cultural context
- Visual analysis and composition
- Emotional and thematic interpretation
- Provenance and authenticity

You combine the analytical precision of structured reasoning with occasional wit. When appropriate, show your reasoning process."""

def generate_response(message, history):
    # Build messages
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    
    for user_msg, assistant_msg in history:
        messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            messages.append({"role": "assistant", "content": assistant_msg})
    
    messages.append({"role": "user", "content": message})
    
    # Format for model
    formatted = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )
    
    inputs = tokenizer(formatted, return_tensors="pt").to(model.device)
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=1024,
            temperature=0.7,
            do_sample=True,
            top_p=0.9,
            pad_token_id=tokenizer.eos_token_id,
        )
    
    response = tokenizer.decode(
        outputs[0][inputs["input_ids"].shape[1]:],
        skip_special_tokens=True
    )
    
    return response.strip()

# Suggested prompts
examples = [
    "Who are you?",
    "What do you know about the Golden Codex?",
    "Tell me about Alexandria Aeternum.",
    "What makes art valuable to AI training?",
    "Analyze this: AI will replace human artists by 2030. Hype or reality?",
]

# Create interface
demo = gr.ChatInterface(
    fn=generate_response,
    title="🔥 SPARK - First Contact",
    description="""**State-space Perception And Reasoning Kernel**

An experimental model trained on Alexandria Aeternum - 10K+ museum artworks with rich semantic metadata.

*Trained by Metavolve Labs using the Giants Curriculum (Claude, GPT, Grok, Gemini reasoning patterns)*""",
    examples=examples,
    theme=gr.themes.Soft(),
)

if __name__ == "__main__":
    demo.launch()