Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -76,20 +76,6 @@ function sendMessage() {
|
|
76 |
######
|
77 |
|
78 |
import asyncio
|
79 |
-
import queue
|
80 |
-
import threading
|
81 |
-
import random
|
82 |
-
import time
|
83 |
-
from fastapi import FastAPI, WebSocket
|
84 |
-
from fastapi.responses import HTMLResponse
|
85 |
-
import uvicorn
|
86 |
-
|
87 |
-
# FastAPI App
|
88 |
-
app = FastAPI()
|
89 |
-
|
90 |
-
import asyncio
|
91 |
-
import queue
|
92 |
-
import threading
|
93 |
import random
|
94 |
import time
|
95 |
from fastapi import FastAPI, WebSocket
|
@@ -99,128 +85,82 @@ import uvicorn
|
|
99 |
class AIStateManager:
|
100 |
def __init__(self):
|
101 |
self.state = "awake"
|
102 |
-
self.
|
|
|
103 |
self.heartbeat_count = 0
|
104 |
-
self.
|
105 |
-
self.clients = set() # Set to store WebSocket clients
|
106 |
|
107 |
async def set_state(self, new_state):
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
async def receive_message(self, sender, message):
|
114 |
-
"""Adds messages to queue and resets heartbeat if human sends input."""
|
115 |
-
async with self.lock:
|
116 |
-
await self.msg_queue.put((sender, message))
|
117 |
-
if sender == "human":
|
118 |
-
self.heartbeat_count = 0
|
119 |
-
if self.state != "awake":
|
120 |
-
await self.set_state("awake")
|
121 |
-
|
122 |
-
async def add_message(self, sender, message):
|
123 |
-
"""AI or System can add messages to queue."""
|
124 |
-
await self.msg_queue.put((sender, message))
|
125 |
-
|
126 |
-
async def process_messages(self):
|
127 |
-
"""Processes messages in queue."""
|
128 |
-
while not self.msg_queue.empty():
|
129 |
-
sender, message = await self.msg_queue.get()
|
130 |
-
print(f"[{sender.upper()}] {message}")
|
131 |
-
await self.broadcast(f"[{sender.upper()}] {message}")
|
132 |
|
133 |
async def broadcast(self, message):
|
134 |
-
"""
|
135 |
-
|
136 |
-
for client in list(self.clients): # Create a copy to avoid runtime modification
|
137 |
try:
|
138 |
await client.send_text(message)
|
139 |
except Exception as e:
|
140 |
-
print(f"
|
141 |
self.clients.remove(client)
|
142 |
|
143 |
-
async def
|
144 |
-
"""
|
145 |
while True:
|
146 |
-
await asyncio.sleep(
|
147 |
-
|
|
|
|
|
148 |
self.heartbeat_count += 1
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
if self.heartbeat_count >= 20:
|
170 |
-
await self.set_state("research") # Restart research after sleep
|
171 |
-
asyncio.create_task(self.run_research())
|
172 |
-
print(self.state)
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
self.current_task = self.research_tasks.pop(0)
|
179 |
-
self.add_message("ai", f"Researching: {self.current_task}")
|
180 |
-
if random.random() < 0.3: # AI might ask a follow-up question
|
181 |
-
self.add_message("ai", f"Question: What do you think about {self.current_task}?")
|
182 |
-
self.process_messages()
|
183 |
-
|
184 |
-
async def run_sleeping(self):
|
185 |
-
"""Runs self-training until state changes."""
|
186 |
-
while self.state == "sleeping":
|
187 |
-
await asyncio.sleep(5)
|
188 |
-
self.add_message("system", "Self-training in progress...")
|
189 |
-
self.process_messages()
|
190 |
-
|
191 |
-
def modify_research_tasks(self):
|
192 |
-
"""Background process to edit research tasks dynamically (subconscious)."""
|
193 |
-
while True:
|
194 |
-
time.sleep(10) # Runs every 10 seconds
|
195 |
-
with self.lock:
|
196 |
-
if self.state == "research" and random.random() < 0.5:
|
197 |
-
new_task = f"Investigate {random.choice(['AI Bias', 'Neural Networks', 'Data Privacy'])}"
|
198 |
-
self.research_tasks.append(new_task)
|
199 |
-
self.add_message("system", f"New research task added: {new_task}")
|
200 |
-
|
201 |
-
# Initialize AI Manager
|
202 |
-
ai_manager = AIStateManager()
|
203 |
|
204 |
-
#
|
205 |
-
|
206 |
-
|
207 |
-
threading.Thread(target=ai_manager.modify_research_tasks, daemon=True).start()
|
208 |
|
209 |
@app.websocket("/ws")
|
210 |
async def websocket_endpoint(websocket: WebSocket):
|
211 |
await websocket.accept()
|
212 |
-
|
213 |
-
|
214 |
-
ai_manager.clients.add(websocket) # Add client to set
|
215 |
try:
|
216 |
while True:
|
217 |
data = await websocket.receive_text()
|
218 |
-
|
219 |
-
await ai_manager.receive_message("human", data)
|
220 |
except Exception as e:
|
221 |
print(f"WebSocket error: {e}")
|
222 |
finally:
|
223 |
-
ai_manager.clients.remove(websocket)
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
@app.get("/")
|
226 |
async def get():
|
|
|
76 |
######
|
77 |
|
78 |
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
import random
|
80 |
import time
|
81 |
from fastapi import FastAPI, WebSocket
|
|
|
85 |
class AIStateManager:
|
86 |
def __init__(self):
|
87 |
self.state = "awake"
|
88 |
+
self.research_tasks = ["Explore AI Ethics", "Find latest AI models", "Investigate quantum computing"]
|
89 |
+
self.current_task = None
|
90 |
self.heartbeat_count = 0
|
91 |
+
self.clients = set()
|
|
|
92 |
|
93 |
async def set_state(self, new_state):
|
94 |
+
"""Thread-safe state change with logging."""
|
95 |
+
print(f"[STATE CHANGE] {self.state} → {new_state}")
|
96 |
+
self.state = new_state
|
97 |
+
await self.broadcast(f"State changed to {new_state}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
async def broadcast(self, message):
|
100 |
+
"""Broadcast message to all connected clients."""
|
101 |
+
for client in list(self.clients):
|
|
|
102 |
try:
|
103 |
await client.send_text(message)
|
104 |
except Exception as e:
|
105 |
+
print(f"Broadcast error: {e}")
|
106 |
self.clients.remove(client)
|
107 |
|
108 |
+
async def research_cycle(self):
|
109 |
+
"""Manages research state tasks."""
|
110 |
while True:
|
111 |
+
await asyncio.sleep(5) # Longer interval for visibility
|
112 |
+
|
113 |
+
# State-based logic
|
114 |
+
if self.state == "awake":
|
115 |
self.heartbeat_count += 1
|
116 |
+
if self.heartbeat_count >= 3:
|
117 |
+
await self.set_state("research")
|
118 |
+
|
119 |
+
elif self.state == "research":
|
120 |
+
if self.research_tasks:
|
121 |
+
task = self.research_tasks.pop(0)
|
122 |
+
await self.broadcast(f"Researching: {task}")
|
123 |
+
|
124 |
+
# Simulate task work
|
125 |
+
await asyncio.sleep(3)
|
126 |
+
|
127 |
+
# Random chance of generating follow-up
|
128 |
+
if random.random() < 0.3:
|
129 |
+
await self.broadcast(f"Question about {task}")
|
130 |
+
else:
|
131 |
+
await self.set_state("sleeping")
|
132 |
+
|
133 |
+
elif self.state == "sleeping":
|
134 |
+
await self.broadcast("System in sleep mode")
|
135 |
+
await asyncio.sleep(10) # Longer sleep
|
|
|
|
|
|
|
|
|
136 |
|
137 |
+
# Reset after sleep
|
138 |
+
self.research_tasks = ["Explore AI Ethics", "Find latest AI models", "Investigate quantum computing"]
|
139 |
+
await self.set_state("awake")
|
140 |
+
self.heartbeat_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
+
# FastAPI Setup
|
143 |
+
app = FastAPI()
|
144 |
+
ai_manager = AIStateManager()
|
|
|
145 |
|
146 |
@app.websocket("/ws")
|
147 |
async def websocket_endpoint(websocket: WebSocket):
|
148 |
await websocket.accept()
|
149 |
+
ai_manager.clients.add(websocket)
|
|
|
|
|
150 |
try:
|
151 |
while True:
|
152 |
data = await websocket.receive_text()
|
153 |
+
await ai_manager.broadcast(f"Received: {data}")
|
|
|
154 |
except Exception as e:
|
155 |
print(f"WebSocket error: {e}")
|
156 |
finally:
|
157 |
+
ai_manager.clients.remove(websocket)
|
158 |
+
|
159 |
+
@app.on_event("startup")
|
160 |
+
async def startup_event():
|
161 |
+
"""Start research cycle on app startup."""
|
162 |
+
asyncio.create_task(ai_manager.research_cycle())
|
163 |
+
|
164 |
|
165 |
@app.get("/")
|
166 |
async def get():
|