Delete app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165import numpy as npimport torchimport torch.nn as nnfrom PIL import Imageimport randomimport gradio as grimport imageiofrom io import BytesIO
torch.set_default_dtype(torch.float64)
# 1. Quantum Pixel Fieldclass QuantumPixelField: def __init__(self, image, hbar=1.0, mass=0.5): arr = np.array(image) h, w, _ = arr.shape gray = (arr.mean(axis=-1) - arr.min()) / (arr.ptp() + 1e-8) fft_gray = np.fft.fft2(gray) self.psi = torch.tensor(fft_gray / (np.abs(fft_gray).max() + 1e-8), dtype=torch.complex128) self.V = torch.tensor(gray, dtype=torch.float64) kx, ky = np.meshgrid(2 * np.pi * np.fft.fftfreq(w), 2 * np.pi * np.fft.fftfreq(h)) self.K = torch.tensor(kx**2 + ky**2, dtype=torch.float64)
def evolve(self, dt): kinetic = torch.exp(-0.5j * dt * self.K) psi_k = torch.fft.fft2(self.psi) * kinetic psi = torch.fft.ifft2(psi_k) * torch.exp(-1j * dt * self.V) self.psi = torch.fft.ifft2(torch.fft.fft2(psi) * kinetic)
def measure(self, ratio=0.4): prob = torch.abs(self.psi)**2 prob /= prob.sum() n = int(prob.numel() * ratio) idx = torch.multinomial(prob.flatten(), n, replacement=False) mask = torch.zeros_like(prob.flatten()) mask[idx] = 1 return mask.reshape(prob.shape) * torch.exp(1j * torch.angle(self.psi))
# 2. Fractal Generatorclass InfiniteFractal: def __init__(self, size): self.size = size
def generate(self, chaos=1.0): a, b, c, d = (random.uniform(-k*chaos, k*chaos) for k in [2.5, 2.5, 1.0, 1.0]) x, y = 0.0, 0.0 fractal = np.zeros((self.size, self.size), dtype=np.float32) for _ in range(20000): x, y = np.sin(a*y) + c*np.cos(a*x), np.sin(b*x) + d*np.cos(b*y) px, py = int(self.size/2 + self.size/3*x), int(self.size/2 + self.size/3*y) if 0 <= px < self.size and 0 <= py < self.size: fractal[py, px] += 0.1 fractal = np.log1p(fractal) return (fractal - fractal.min()) / (fractal.ptp() + 1e-8)
# 3. Chaotic Neural Netclass ChaoticNet(nn.Module): def __init__(self, hidden=128): super().__init__() self.fc = nn.Sequential( nn.Linear(7 + hidden, hidden), nn.Tanh(), nn.Linear(hidden, 3), nn.Sigmoid() ) self.chaos = nn.Parameter(torch.tensor([0.5]))
def forward(self, x, h): z = torch.cat((x, h), 1) h = self.fc[:2](z) h += 0.45 * torch.sin(2 * np.pi * self.chaos * h) return self.fc[2:](h), h
# 4. Visual Effectsdef swirl(img, strength=1.2): h, w = img.shape[:2] y, x = np.ogrid[:h, :w] cy, cx = h/2, w/2 r = np.sqrt((x-cx)**2 + (y-cy)**2) angle = strength * r / (r.max() + 1e-8) cos_a, sin_a = np.cos(angle), np.sin(angle) x_new = (x-cx)*cos_a - (y-cy)*sin_a + cx y_new = (x-cx)*sin_a + (y-cy)*cos_a + cy coords = [np.clip(y_new, 0, h-1).astype(int), np.clip(x_new, 0, w-1).astype(int)] return img[coords[0], coords[1]]
def relativistic(img, v=0.8): gamma = 1/np.sqrt(1-v**2) img = img.astype(float) / 255 r = gamma * (img[..., 0] - v * img[..., 1]) g = gamma * (img[..., 1] - v * img[..., 0]) b = img[..., 2] out = np.stack([r, g, b], -1) return (np.clip(out, 0, 1) ** (1/gamma) * 255).astype(np.uint8)
# 5. Quantum Art Enginedef quantum_art(image, q_time=3.0, chaos=1.8, n_steps=10, velocity=0.87, collapse=0.42, alpha=0.66): img = image.convert("RGB").resize((512, 512)) arr = np.array(img) h, w = arr.shape[:2]
qfield = QuantumPixelField(img) for _ in range(6): qfield.evolve(q_time/6) qmap = qfield.measure(collapse).real.numpy() qmap = (qmap - qmap.min()) / qmap.ptp() qimg = (np.abs(arr/255 - np.stack([qmap]*3, -1))**0.5 * 255).astype(np.uint8)
fractal = InfiniteFractal(h).generate(chaos) fractal = np.pad(fractal, ((0, h-fractal.shape[0]), (0, w-fractal.shape[1])), 'constant')[:h, :w] fimg = (np.stack([fractal]*3, -1) * 255).astype(np.uint8)
xv, yv = np.meshgrid(np.linspace(0, 1, w), np.linspace(0, 1, h)) inputs = np.column_stack([arr.reshape(-1, 3)/255, qmap.flatten(), fractal.flatten(), xv.flatten(), yv.flatten()]) inputs = torch.tensor(inputs, dtype=torch.float64) net = ChaoticNet() h_state = torch.zeros(inputs.shape[0], 128, dtype=torch.float64)
for _ in range(n_steps): out, h_state = net(inputs, h_state)
nimg = (out.reshape(h, w, 3).detach().numpy() * 255).astype(np.uint8) nimg = swirl(nimg, strength=chaos*1.6)
blend = (alpha * nimg + (1 - alpha) * qimg).astype(np.uint8) final = relativistic(blend, velocity) final = final[..., [1, 0, 2]] final += np.random.normal(0, chaos*25, final.shape) return np.clip(final, 0, 255).astype(np.uint8)
# 6. Gradio UIdef interface(img, qt, ch, ns, vel, cr, alpha): if img is None: return None, None with torch.no_grad(): result = quantum_art(img, qt, ch, ns, vel, cr, alpha) gif = BytesIO() imageio.mimsave(gif, [np.array(img), result], format='GIF', duration=500) return Image.fromarray(result), gif.getvalue()
with gr.Blocks(title="⚛️ Quantum Visual Revolution", theme="gradio/dark") as app: gr.Markdown("# ⚛️ Quantum Visual Revolution\nConvert your images into AI-powered fractal-quantum art")
with gr.Row(): with gr.Column(): img_in = gr.Image(label="Upload Image", type="pil") with gr.Accordion("⚙️ Quantum Parameters", open=False): qt = gr.Slider(1.0, 8.0, 3.0, label="Quantum Time") ch = gr.Slider(0.3, 2.5, 1.8, label="Chaos Level") ns = gr.Slider(3, 20, 10, step=1, label="Neural Iterations") vel = gr.Slider(0.1, 0.99, 0.87, label="Relativistic Speed") cr = gr.Slider(0.2, 0.7, 0.42, label="Collapse Ratio") alpha = gr.Slider(0.3, 0.95, 0.66, label="Final Blend") btn = gr.Button("🔥 Generate Quantum Art")
with gr.Column(): img_out = gr.Image(label="Quantum Masterpiece", interactive=False) gif_out = gr.File(label="Creative Process (GIF)", type="binary")
btn.click(interface, [img_in, qt, ch, ns, vel, cr, alpha], [img_out, gif_out])
app.launch()app.pyDeleted-165