Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,88 +71,243 @@ def index():
|
|
| 71 |
})
|
| 72 |
.catch(error => {
|
| 73 |
console.error('Fetch error:', error);
|
| 74 |
-
});
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
loadFrame(); // Load first frame immediately
|
| 80 |
-
</script>
|
| 81 |
-
</body>
|
| 82 |
-
</html>
|
| 83 |
-
'''
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
pixels[:HEIGHT//2, :WIDTH//2, 0] = 255
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
if
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
y_pos = 10 + dy
|
| 119 |
-
x_pos = 10 + dx
|
| 120 |
-
if y_pos < HEIGHT and x_pos < WIDTH:
|
| 121 |
-
pixels[y_pos, x_pos] = [255, 200, 100]
|
| 122 |
|
| 123 |
# Convert to bytes
|
| 124 |
-
raw_bytes =
|
| 125 |
|
| 126 |
-
print(f"π€ Flask generated frame {frame_count}: {len(raw_bytes)} bytes")
|
| 127 |
frame_count += 1
|
|
|
|
|
|
|
| 128 |
|
| 129 |
return Response(
|
| 130 |
raw_bytes,
|
| 131 |
mimetype='application/octet-stream',
|
| 132 |
headers={
|
| 133 |
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
| 134 |
-
'
|
|
|
|
| 135 |
'X-Frame-Count': str(frame_count)
|
| 136 |
}
|
| 137 |
)
|
| 138 |
|
| 139 |
@app.route('/test')
|
| 140 |
def test():
|
| 141 |
-
"""
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
print("="*60)
|
| 151 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
print("="*60)
|
| 153 |
-
print(
|
| 154 |
-
print("
|
| 155 |
-
print("
|
|
|
|
| 156 |
print("="*60)
|
| 157 |
|
| 158 |
-
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 71 |
})
|
| 72 |
.catch(error => {
|
| 73 |
console.error('Fetch error:', error);
|
| 74 |
+
});#!/usr/bin/env python3
|
| 75 |
+
# static_noise.py - Flask generates random noise
|
| 76 |
+
import numpy as np
|
| 77 |
+
import time
|
| 78 |
+
from flask import Flask, Response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
app = Flask(__name__)
|
| 81 |
+
|
| 82 |
+
WIDTH, HEIGHT = 400, 300
|
| 83 |
+
frame_count = 0
|
| 84 |
+
|
| 85 |
+
@app.route('/')
|
| 86 |
+
def index():
|
| 87 |
+
return '''
|
| 88 |
+
<!DOCTYPE html>
|
| 89 |
+
<html>
|
| 90 |
+
<head>
|
| 91 |
+
<title>Static Noise Animation</title>
|
| 92 |
+
<style>
|
| 93 |
+
body { margin: 0; padding: 20px; background: #111; color: white; font-family: monospace; text-align: center; }
|
| 94 |
+
canvas { border: 3px solid #0af; background: black; display: block; margin: 20px auto; }
|
| 95 |
+
.stats { background: #222; padding: 15px; border-radius: 8px; display: inline-block; margin: 10px; }
|
| 96 |
+
button { background: #0af; color: white; border: none; padding: 10px 20px; margin: 5px; border-radius: 5px; cursor: pointer; }
|
| 97 |
+
</style>
|
| 98 |
+
</head>
|
| 99 |
+
<body>
|
| 100 |
+
<h1 style="color: #0af;">πΊ Static Noise Animation</h1>
|
| 101 |
|
| 102 |
+
<canvas id="canvas" width="400" height="300"></canvas>
|
|
|
|
| 103 |
|
| 104 |
+
<div class="stats">
|
| 105 |
+
<div>Frames: <span id="frameCount">0</span></div>
|
| 106 |
+
<div>FPS: <span id="fps">0</span></div>
|
| 107 |
+
<div>Status: <span id="status" style="color: #0af;">Loading...</span></div>
|
| 108 |
+
</div>
|
| 109 |
|
| 110 |
+
<div>
|
| 111 |
+
<button onclick="startNoise()">Start Noise</button>
|
| 112 |
+
<button onclick="stopNoise()">Stop Noise</button>
|
| 113 |
+
<button onclick="changeColor()">Change Color</button>
|
| 114 |
+
</div>
|
| 115 |
|
| 116 |
+
<script>
|
| 117 |
+
const canvas = document.getElementById('canvas');
|
| 118 |
+
const ctx = canvas.getContext('2d');
|
| 119 |
+
let noiseInterval = null;
|
| 120 |
+
let frames = 0;
|
| 121 |
+
let lastTime = Date.now();
|
| 122 |
+
let fps = 0;
|
| 123 |
+
let colorMode = 0; // 0: grayscale, 1: color, 2: sepia
|
| 124 |
+
|
| 125 |
+
function loadNoiseFrame() {
|
| 126 |
+
const startTime = Date.now();
|
| 127 |
+
|
| 128 |
+
fetch('/noise')
|
| 129 |
+
.then(response => {
|
| 130 |
+
if (!response.ok) throw new Error('HTTP ' + response.status);
|
| 131 |
+
return response.arrayBuffer();
|
| 132 |
+
})
|
| 133 |
+
.then(buffer => {
|
| 134 |
+
// Create ImageData
|
| 135 |
+
const imageData = new ImageData(400, 300);
|
| 136 |
+
const rgba = imageData.data;
|
| 137 |
+
const rgb = new Uint8Array(buffer);
|
| 138 |
+
|
| 139 |
+
// Verify size
|
| 140 |
+
if (rgb.length !== 400*300*3) {
|
| 141 |
+
console.error('Wrong size:', rgb.length);
|
| 142 |
+
return;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
// Convert RGB to RGBA
|
| 146 |
+
for (let i = 0, j = 0; i < rgba.length; i += 4, j += 3) {
|
| 147 |
+
[i] = rgb[j];
|
| 148 |
+
rgba[i + 1] = rgb[j + 1];
|
| 149 |
+
rgba[i + 2] = rgb[j + 2];
|
| 150 |
+
rgba[i + 3] = 255;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
// Apply color mode
|
| 154 |
+
if (colorMode === 1) {
|
| 155 |
+
// Color noise - already colored
|
| 156 |
+
} else if (colorMode === 2) {
|
| 157 |
+
// Sepia tone
|
| 158 |
+
for (let i = 0; i < rgba.length; i += 4) {
|
| 159 |
+
const r = rgba[i];
|
| 160 |
+
const g = rgba[i + 1];
|
| 161 |
+
const b = rgba[i + 2];
|
| 162 |
+
|
| 163 |
+
rgba[i] = Math.min(255, (r * 0.393) + (g * 0.769) + (b * 0.189));
|
| 164 |
+
rgba[i + 1] = Math.min(255, (r * 0.349) + (g * 0.686) + (b * 0.168));
|
| 165 |
+
rgba[i + 2] = Math.min(255, (r * 0.272) + (g * 0.534) + (b * 0.131));
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
// else: Grayscale - already from server
|
| 169 |
+
|
| 170 |
+
// Draw to canvas
|
| 171 |
+
ctx.putImageData(imageData, 0, 0);
|
| 172 |
+
|
| 173 |
+
// Update stats
|
| 174 |
+
frames++;
|
| 175 |
+
const now = Date.now();
|
| 176 |
+
if (now - lastTime >= 1000) {
|
| 177 |
+
fps = frames;
|
| 178 |
+
frames = 0;
|
| 179 |
+
lastTime = now;
|
| 180 |
+
document.getElementById('fps').textContent = fps;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
document.getElementById('frameCount').textContent = parseInt(document.getElementById('frameCount').textContent) + 1;
|
| 184 |
+
document.getElementById('status').textContent = 'Streaming β';
|
| 185 |
+
|
| 186 |
+
const latency = Date.now() - startTime;
|
| 187 |
+
if (latency > 100) {
|
| 188 |
+
console.log(`Frame latency: ${latency}ms`);
|
| 189 |
+
}
|
| 190 |
+
})
|
| 191 |
+
.catch(error => {
|
| 192 |
+
console.error('Error:', error);
|
| 193 |
+
document.getElementById('status').textContent = 'Error: ' + error.message;
|
| 194 |
+
document.getElementById('status').style.color = 'red';
|
| 195 |
+
});
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
function startNoise() {
|
| 199 |
+
if (!noiseInterval) {
|
| 200 |
+
noiseInterval = setInterval(loadNoiseFrame, 33); // ~30 FPS
|
| 201 |
+
document.getElementById('status').textContent = 'Noise started β';
|
| 202 |
+
console.log('Noise animation started');
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
function stopNoise() {
|
| 207 |
+
if (noiseInterval) {
|
| 208 |
+
clearInterval(noiseInterval);
|
| 209 |
+
noiseInterval = null;
|
| 210 |
+
document.getElementById('status').textContent = 'Stopped';
|
| 211 |
+
console.log('Noise animation stopped');
|
| 212 |
+
}
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
function changeColor() {
|
| 216 |
+
colorMode = (colorMode + 1) % 3;
|
| 217 |
+
const modes = ['Grayscale', 'Color', 'Sepia'];
|
| 218 |
+
console.log('Color mode:', modes[colorMode]);
|
| 219 |
+
document.getElementById('status').textContent = 'Mode: ' + modes[colorMode];
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
// Start automatically after 1 second
|
| 223 |
+
setTimeout(startNoise, 1000);
|
| 224 |
+
console.log('Static noise page loaded');
|
| 225 |
+
</script>
|
| 226 |
+
</body>
|
| 227 |
+
</html>
|
| 228 |
+
'''
|
| 229 |
+
|
| 230 |
+
@app.route('/noise')
|
| 231 |
+
def get_noise():
|
| 232 |
+
"""Generate random static noise"""
|
| 233 |
+
global frame_count
|
| 234 |
|
| 235 |
+
# Create random noise
|
| 236 |
+
if frame_count % 3 == 0:
|
| 237 |
+
# Grayscale noise
|
| 238 |
+
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 239 |
+
# Make it grayscale by setting all channels to same value
|
| 240 |
+
gray = np.random.randint(0, 256, (HEIGHT, WIDTH, 1), dtype=np.uint8)
|
| 241 |
+
noise[:, :, :] = gray
|
| 242 |
+
elif frame_count % 3 == 1:
|
| 243 |
+
# Color noise
|
| 244 |
+
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 245 |
+
else:
|
| 246 |
+
# TV static with scanlines
|
| 247 |
+
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 248 |
+
# Add scanlines every 4th row
|
| 249 |
+
noise[::4, :, :] = noise[::4, :, :] // 2
|
| 250 |
|
| 251 |
+
# Occasionally add a "glitch" - solid color flash
|
| 252 |
+
if np.random.random() < 0.02: # 2% chance
|
| 253 |
+
glitch_color = np.random.randint(0, 256, 3)
|
| 254 |
+
glitch_height = np.random.randint(20, 100)
|
| 255 |
+
glitch_y = np.random.randint(0, HEIGHT - glitch_height)
|
| 256 |
+
noise[glitch_y:glitch_y + glitch_height, :, :] = glitch_color
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
# Convert to bytes
|
| 259 |
+
raw_bytes = noise.tobytes()
|
| 260 |
|
|
|
|
| 261 |
frame_count += 1
|
| 262 |
+
if frame_count % 30 == 0:
|
| 263 |
+
print(f"π‘ Generated noise frame {frame_count}")
|
| 264 |
|
| 265 |
return Response(
|
| 266 |
raw_bytes,
|
| 267 |
mimetype='application/octet-stream',
|
| 268 |
headers={
|
| 269 |
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
| 270 |
+
'Pragma': 'no-cache',
|
| 271 |
+
'Expires': '0',
|
| 272 |
'X-Frame-Count': str(frame_count)
|
| 273 |
}
|
| 274 |
)
|
| 275 |
|
| 276 |
@app.route('/test')
|
| 277 |
def test():
|
| 278 |
+
"""Test endpoint - returns simple pattern"""
|
| 279 |
+
# Create a simple test pattern
|
| 280 |
+
pixels = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 281 |
+
|
| 282 |
+
# Red top half
|
| 283 |
+
pixels[:HEIGHT//2, :, 0] = 255
|
| 284 |
+
|
| 285 |
+
# Green bottom half
|
| 286 |
+
pixels[HEIGHT//2:, :, 1] = 255
|
| 287 |
+
|
| 288 |
+
# Blue diagonal line
|
| 289 |
+
for i in range(min(WIDTH, HEIGHT)):
|
| 290 |
+
if i < HEIGHT and i < WIDTH:
|
| 291 |
+
pixels[i, i, 2] = 255
|
| 292 |
+
|
| 293 |
+
return Response(
|
| 294 |
+
pixels.tobytes(),
|
| 295 |
+
mimetype='application/octet-stream',
|
| 296 |
+
headers={'Cache-Control': 'no-cache'}
|
| 297 |
+
)
|
| 298 |
|
| 299 |
if __name__ == "__main__":
|
| 300 |
print("="*60)
|
| 301 |
+
print("πΊ STATIC NOISE ANIMATION SERVER")
|
| 302 |
+
print("="*60)
|
| 303 |
+
print(f"π‘ Port: 7860")
|
| 304 |
+
print(f"π¨ Resolution: {WIDTH}x{HEIGHT}")
|
| 305 |
+
print(f"π Data per frame: {WIDTH * HEIGHT * 3} bytes")
|
| 306 |
print("="*60)
|
| 307 |
+
print("β
Endpoints:")
|
| 308 |
+
print(" / - Noise animation page")
|
| 309 |
+
print(" /noise - Raw noise data")
|
| 310 |
+
print(" /test - Test pattern")
|
| 311 |
print("="*60)
|
| 312 |
|
| 313 |
+
app.run(host='0.0.0.0', port=7860, debug=False, threaded=True)
|