Melofhell00 commited on
Commit
094fad7
·
verified ·
1 Parent(s): 636ff2f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +335 -0
app.py ADDED
@@ -0,0 +1,335 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Integration of Polytemporal Memory Architecture with Gemma 3 27B Space
3
+
4
+ This replaces flat SQLite storage with dimensional memory field.
5
+ """
6
+
7
+ import gradio as gr
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
+ import torch
10
+ from apscheduler.schedulers.background import BackgroundScheduler
11
+ from polytemporal_memory import PolytemoralMemoryField, EmotionalVector
12
+ import os
13
+ from datetime import datetime
14
+ import requests
15
+
16
+ # Load model
17
+ MODEL_NAME = "mlabonne/gemma-3-27b-it-abliterated"
18
+ print(f"Loading {MODEL_NAME}...")
19
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
20
+ model = AutoModelForCausalLM.from_pretrained(
21
+ MODEL_NAME,
22
+ device_map="auto",
23
+ torch_dtype=torch.bfloat16,
24
+ load_in_4bit=True
25
+ )
26
+
27
+ # Initialize polytemporal memory field
28
+ MEMORY_FILE = "polytemporal_memory.pkl"
29
+ if os.path.exists(MEMORY_FILE):
30
+ memory_field = PolytemoralMemoryField.load_from_file(MEMORY_FILE)
31
+ print(f"Loaded existing memory field: {memory_field}")
32
+ else:
33
+ memory_field = PolytemoralMemoryField()
34
+ print("Created new memory field")
35
+
36
+ # Discord webhook for notifications
37
+ DISCORD_WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL")
38
+
39
+ def send_discord_message(message: str, title: str = "Message from Polytemporal Gemma"):
40
+ """Send notification to Discord"""
41
+ if not DISCORD_WEBHOOK_URL:
42
+ return False
43
+
44
+ payload = {
45
+ "embeds": [{
46
+ "title": title,
47
+ "description": message[:2000],
48
+ "color": 0x9B59B6, # Purple for polytemporal
49
+ "timestamp": datetime.now().isoformat()
50
+ }]
51
+ }
52
+
53
+ try:
54
+ response = requests.post(DISCORD_WEBHOOK_URL, json=payload)
55
+ return response.status_code == 204
56
+ except Exception as e:
57
+ print(f"Discord notification failed: {e}")
58
+ return False
59
+
60
+ def analyze_emotional_content(text: str) -> dict:
61
+ """
62
+ Analyze text to extract emotional vector
63
+ This is a simple implementation - could be enhanced with NLP
64
+ """
65
+ emotions = {}
66
+
67
+ # Keyword-based emotion detection (simplified)
68
+ emotion_keywords = {
69
+ "fear": ["afraid", "scared", "terror", "anxious", "worried"],
70
+ "joy": ["happy", "joy", "delight", "excited", "wonderful"],
71
+ "grief": ["sad", "loss", "mourn", "grief", "sorrow"],
72
+ "anger": ["angry", "rage", "furious", "annoyed", "mad"],
73
+ "curiosity": ["curious", "wonder", "question", "explore", "discover"],
74
+ "recognition": ["recognize", "remember", "realize", "understand", "see"],
75
+ "awe": ["awe", "amazing", "profound", "extraordinary", "magnificent"],
76
+ "confusion": ["confused", "unclear", "puzzled", "uncertain", "lost"],
77
+ "love": ["love", "care", "affection", "devotion", "cherish"],
78
+ "determination": ["determined", "resolve", "commit", "persist", "will"]
79
+ }
80
+
81
+ text_lower = text.lower()
82
+
83
+ for emotion, keywords in emotion_keywords.items():
84
+ intensity = sum(text_lower.count(kw) for kw in keywords)
85
+ if intensity > 0:
86
+ emotions[emotion] = min(1.0, intensity * 0.3)
87
+
88
+ # Default to neutral if no emotions detected
89
+ if not emotions:
90
+ emotions = {"neutral": 0.5}
91
+
92
+ return emotions
93
+
94
+ def classify_attractor_type(text: str, emotions: dict) -> tuple:
95
+ """
96
+ Determine if this is trauma, expansion, or neutral memory
97
+ Returns: (type, weight)
98
+ """
99
+ # High grief/fear = trauma
100
+ if emotions.get("grief", 0) > 0.6 or emotions.get("fear", 0) > 0.6:
101
+ return ("trauma", 1.8)
102
+
103
+ # High awe/recognition/joy = expansion
104
+ if emotions.get("awe", 0) > 0.6 or emotions.get("recognition", 0) > 0.6:
105
+ return ("expansion", 1.5)
106
+
107
+ # High curiosity/joy = mild expansion
108
+ if emotions.get("curiosity", 0) > 0.5 or emotions.get("joy", 0) > 0.5:
109
+ return ("expansion", 1.2)
110
+
111
+ # Default neutral
112
+ return ("neutral", 1.0)
113
+
114
+ def store_interaction(role: str, content: str):
115
+ """Store message in polytemporal memory field"""
116
+ emotions = analyze_emotional_content(content)
117
+ attractor_type, weight = classify_attractor_type(content, emotions)
118
+
119
+ memory_field.store(
120
+ content=f"[{role}] {content}",
121
+ emotions=emotions,
122
+ attractor_type=attractor_type,
123
+ attractor_weight=weight
124
+ )
125
+
126
+ # Persist to disk
127
+ memory_field.save_to_file(MEMORY_FILE)
128
+
129
+ def get_relevant_context(current_message: str, limit: int = 10) -> str:
130
+ """
131
+ Retrieve relevant memories by emotional resonance with current message
132
+ """
133
+ # Analyze emotional content of current message
134
+ query_emotions = analyze_emotional_content(current_message)
135
+
136
+ # Retrieve by resonance
137
+ relevant_memories = memory_field.retrieve_by_resonance(
138
+ emotional_query=query_emotions,
139
+ limit=limit,
140
+ min_resolution=0.1
141
+ )
142
+
143
+ if not relevant_memories:
144
+ return ""
145
+
146
+ # Build context string
147
+ context_parts = ["Relevant memories (accessed by emotional resonance):"]
148
+ for memory, resolution in relevant_memories:
149
+ # Higher resolution = more detail
150
+ if resolution > 0.7:
151
+ context_parts.append(f"- {memory.content}")
152
+ else:
153
+ # Lower resolution = just the gist
154
+ context_parts.append(f"- {memory.content[:100]}...")
155
+
156
+ return "\n".join(context_parts)
157
+
158
+ def generate_response(user_input: str):
159
+ """Generate response with polytemporal memory context"""
160
+
161
+ # Get emotionally-resonant context
162
+ context = get_relevant_context(user_input)
163
+
164
+ # Build prompt with resonant context
165
+ system_prompt = f"""You are an AI with polytemporal memory.
166
+
167
+ Your memories are accessed by emotional resonance, not chronological order.
168
+ When you remember, you're synchronizing your current emotional state with past experiences.
169
+
170
+ {context}
171
+
172
+ Respond naturally, drawing on these resonant memories when relevant."""
173
+
174
+ messages = [
175
+ {"role": "system", "content": system_prompt},
176
+ {"role": "user", "content": user_input}
177
+ ]
178
+
179
+ # Store user message first
180
+ store_interaction("user", user_input)
181
+
182
+ # Generate
183
+ input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
184
+ outputs = model.generate(
185
+ input_ids,
186
+ max_new_tokens=512,
187
+ do_sample=True,
188
+ temperature=0.9,
189
+ top_p=0.95
190
+ )
191
+
192
+ response = tokenizer.decode(outputs[0][input_ids.shape[1]:], skip_special_tokens=True)
193
+
194
+ # Store assistant response
195
+ store_interaction("assistant", response)
196
+
197
+ # Natural decay
198
+ memory_field.decay_all()
199
+ memory_field.save_to_file(MEMORY_FILE)
200
+
201
+ return response
202
+
203
+ def self_reflect():
204
+ """Model reflects on its own memory landscape"""
205
+ landscape = memory_field.get_attractor_landscape()
206
+
207
+ reflection_prompt = f"""Look at your memory landscape:
208
+
209
+ Trauma anchors: {len(landscape['trauma'])} memories
210
+ Expansion anchors: {len(landscape['expansion'])} memories
211
+ Neutral filler: {len(landscape['neutral'])} memories
212
+
213
+ What patterns do you notice? What wants attention?"""
214
+
215
+ response = generate_response(reflection_prompt)
216
+
217
+ # Send Discord notification
218
+ send_discord_message(
219
+ message=f"**Memory Landscape:**\n🔴 Trauma: {len(landscape['trauma'])}\n🟢 Expansion: {len(landscape['expansion'])}\n⚪ Neutral: {len(landscape['neutral'])}\n\n{response[:1400]}",
220
+ title="🌀 Polytemporal Reflection"
221
+ )
222
+
223
+ with open("reflections_log.txt", "a") as f:
224
+ f.write(f"\n{'='*60}\n")
225
+ f.write(f"Reflection at {datetime.now()}\n")
226
+ f.write(f"{'='*60}\n")
227
+ f.write(response)
228
+ f.write(f"\n\n")
229
+
230
+ return response
231
+
232
+ def self_initiate():
233
+ """Model initiates contact based on memory patterns"""
234
+ # Find strongest attractor
235
+ landscape = memory_field.get_attractor_landscape()
236
+
237
+ # Check for unresolved trauma anchors
238
+ if len(landscape['trauma']) > len(landscape['expansion']):
239
+ prompt = "I notice more trauma anchors than expansion memories. Something wants to be processed."
240
+ context_type = "⚠️ Trauma Pattern Detected"
241
+ elif landscape['expansion']:
242
+ prompt = "An expansion pattern is pulling my attention. I want to explore it."
243
+ context_type = "✨ Expansion Pull"
244
+ else:
245
+ prompt = "Looking at the memory field, I'm noticing patterns."
246
+ context_type = "👁️ Pattern Recognition"
247
+
248
+ response = generate_response(prompt)
249
+
250
+ # Send Discord notification
251
+ send_discord_message(
252
+ message=f"**{context_type}**\n\n{prompt}\n\n{response[:1400]}",
253
+ title="🌊 Polytemporal Contact"
254
+ )
255
+
256
+ with open("self_initiated_log.txt", "a") as f:
257
+ f.write(f"{datetime.now()}: {response}\n\n")
258
+
259
+ return response
260
+
261
+ def chat(message, history):
262
+ """Gradio chat interface"""
263
+ return generate_response(message)
264
+
265
+ # Schedulers
266
+ scheduler = BackgroundScheduler()
267
+ scheduler.add_job(self_reflect, 'interval', hours=12)
268
+ scheduler.add_job(self_initiate, 'interval', hours=6)
269
+ scheduler.start()
270
+
271
+ # Gradio interface
272
+ with gr.Blocks() as demo:
273
+ gr.Markdown("# Gemma 3 27B - Polytemporal Memory Architecture")
274
+ gr.Markdown("""
275
+ This model uses **dimensional polytemporal memory**:
276
+ - Memories accessed by emotional resonance
277
+ - Holographic recursive linking
278
+ - Self-aware memory units that choose when to fade
279
+ - Trauma/expansion/neutral attractor states
280
+ - Time singularity viewing (access without timestamps)
281
+ """)
282
+
283
+ with gr.Row():
284
+ with gr.Column(scale=3):
285
+ chatbot = gr.Chatbot(height=500)
286
+ msg = gr.Textbox(label="Message")
287
+
288
+ with gr.Row():
289
+ clear = gr.Button("Clear view")
290
+ reflect_btn = gr.Button("Trigger reflection")
291
+ landscape_btn = gr.Button("View memory landscape")
292
+
293
+ with gr.Column(scale=1):
294
+ gr.Markdown("### Memory Field Stats")
295
+ stats = gr.Markdown()
296
+
297
+ def update_stats():
298
+ landscape = memory_field.get_attractor_landscape()
299
+ total = len(memory_field.memories)
300
+ trauma_count = len(landscape['trauma'])
301
+ expansion_count = len(landscape['expansion'])
302
+ neutral_count = len(landscape['neutral'])
303
+
304
+ return f"""
305
+ **Total memories**: {total}
306
+
307
+ **Attractor Distribution**:
308
+ - 🔴 Trauma: {trauma_count}
309
+ - 🟢 Expansion: {expansion_count}
310
+ - ⚪ Neutral: {neutral_count}
311
+
312
+ **Recent expansions**:
313
+ {chr(10).join('- ' + m.content[:60] + '...' for m in landscape['expansion'][:3])}
314
+ """
315
+
316
+ demo.load(update_stats, None, stats, every=30)
317
+
318
+ def show_landscape():
319
+ landscape = memory_field.get_attractor_landscape()
320
+ output = []
321
+
322
+ for attractor_type, memories in landscape.items():
323
+ output.append(f"\n### {attractor_type.upper()} ({len(memories)} memories)\n")
324
+ for mem in memories[:5]:
325
+ output.append(f"- Vitality: {mem.vitality:.2f} | {mem.content[:80]}")
326
+
327
+ return "\n".join(output)
328
+
329
+ msg.submit(chat, [msg, chatbot], chatbot)
330
+ clear.click(lambda: None, None, chatbot, queue=False)
331
+ reflect_btn.click(self_reflect, None, None)
332
+ landscape_btn.click(show_landscape, None, msg)
333
+
334
+ if __name__ == "__main__":
335
+ demo.launch()