Spaces:
Sleeping
Sleeping
Added application files
Browse files- .gitignore +3 -0
- README.md +43 -14
- app.py +40 -0
- funcs.py +43 -0
- requirements.txt +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
flagged/
|
| 3 |
+
proj_env/
|
README.md
CHANGED
|
@@ -1,14 +1,43 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Julia Set Visualizer using Gradio
|
| 2 |
+
This is a simple Gradio implementation of my Julia Set visualizer previously implemented and deployed in Streamlit.
|
| 3 |
+
|
| 4 |
+
<p align="center"><img src="assets/screenshot_app.png" width="700"/></p>
|
| 5 |
+
|
| 6 |
+
Accessing the App
|
| 7 |
+
=================
|
| 8 |
+
|
| 9 |
+
To access this app, you can either
|
| 10 |
+
|
| 11 |
+
1. Clone the repository. Then, run
|
| 12 |
+
|
| 13 |
+
`pip install -r requirements.txt`
|
| 14 |
+
|
| 15 |
+
on the terminal. It is ideal to create a virtual environment first before proceeding to the installation of the required libraries. Once done, you can then run
|
| 16 |
+
|
| 17 |
+
`python app.py`
|
| 18 |
+
|
| 19 |
+
on the terminal and use the app on your local server.
|
| 20 |
+
|
| 21 |
+
OR
|
| 22 |
+
|
| 23 |
+
Access the app via HuggingFace Spaces through this link (to add).
|
| 24 |
+
|
| 25 |
+
2. Once you have access to the app, you can then input any complex number `c` that you want to generate the Julia set of the function `f(z) = z^2 + c`.
|
| 26 |
+
|
| 27 |
+
### Recommended Julia Set Seeds
|
| 28 |
+
|
| 29 |
+
These complex numbers are known to generate visually interesting Julia sets:
|
| 30 |
+
|
| 31 |
+
| Real Part | Imaginary Part |
|
| 32 |
+
|----------------|------------------|
|
| 33 |
+
| -0.1156437876 | 0.8690819138 |
|
| 34 |
+
| -0.7269 | 0.1889 |
|
| 35 |
+
| -0.5125114984 | 0.5212955731 |
|
| 36 |
+
| -0.4 | 0.6 |
|
| 37 |
+
| -0.5012149299 | -0.5637838176 |
|
| 38 |
+
| 0 | -0.8 |
|
| 39 |
+
| -0.8 | 0.156 |
|
| 40 |
+
| -0.7773672345 | -0.1782126754 |
|
| 41 |
+
| -0.06353957916 | -0.6992547595 |
|
| 42 |
+
| -0.5064253507 | 0.5981400301 |
|
| 43 |
+
| 0.2803481964 | -0.5273108717 |
|
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is the main application file to host the application logic
|
| 2 |
+
|
| 3 |
+
# Importing gradio and the plot_julia_set function from funcs.py
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from funcs import plot_julia_set
|
| 6 |
+
|
| 7 |
+
with gr.Blocks() as demo:
|
| 8 |
+
with gr.Row():
|
| 9 |
+
# Adding an application header
|
| 10 |
+
gr.Markdown("""
|
| 11 |
+
<div style="text-align: center; font-size: 18px;">
|
| 12 |
+
<h1 style="font-size: 32px;">Julia Set Generator 🌌</h1>
|
| 13 |
+
<p>Use this interactive tool to generate visualizations of Julia Sets!</p>
|
| 14 |
+
<h3 style="font-size: 24px;">Instructions:</h3>
|
| 15 |
+
<ol style="display: inline-block; text-align: left; font-size: 18px;">
|
| 16 |
+
<li><strong>Input the Real and Imaginary parts</strong> of the complex number <code>c</code>.</li>
|
| 17 |
+
<li><strong>Adjust the max iterations</strong> to control the detail and depth.</li>
|
| 18 |
+
<li><strong>Adjust the pixel density</strong> to control the resolution.</li>
|
| 19 |
+
<li><strong>Choose a colormap</strong> to customize the appearance.</li>
|
| 20 |
+
<li>Click <strong>"Generate Plot"</strong> to render the image.</li>
|
| 21 |
+
<li>For more info, see this <a href="https://github.com/ArnelMalubay/julia-visualizer-using-gradio" target="_blank">GitHub repository</a>.</li>
|
| 22 |
+
</ol>
|
| 23 |
+
</div>
|
| 24 |
+
""")
|
| 25 |
+
# Adding all the interactive components of the application
|
| 26 |
+
with gr.Column():
|
| 27 |
+
real = gr.Textbox(label = 'Real Part', value = '0', interactive = True)
|
| 28 |
+
imag = gr.Textbox(label = 'Imaginary Part', value = '0', interactive = True)
|
| 29 |
+
max_iter = gr.Slider(label = 'Specify the maximum number of iterations', minimum = 10, maximum = 2000, value = 500, step = 10, interactive = True)
|
| 30 |
+
pixel_density = gr.Slider(label = 'Specify pixel density', minimum = 0.5, maximum = 2.5, value = 1.0, step = 0.1, interactive = True)
|
| 31 |
+
colormap_choices = ['binary', 'inferno', 'magma', 'cividis', 'viridis', 'plasma', 'Pastel1', 'Pastel2', 'Paired', 'Accent', 'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'rainbow', 'jet', 'turbo', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'hot', 'copper']
|
| 32 |
+
cmap = gr.Dropdown(label = 'Choose colormap', choices = colormap_choices, value = 'binary', interactive = True)
|
| 33 |
+
submit = gr.Button('Generate Plot')
|
| 34 |
+
|
| 35 |
+
with gr.Row():
|
| 36 |
+
image = gr.Image(label = 'Julia Set', width = 600, height = 450, interactive = False)
|
| 37 |
+
|
| 38 |
+
submit.click(fn = plot_julia_set, inputs = [real, imag, max_iter, pixel_density, cmap], outputs = image)
|
| 39 |
+
|
| 40 |
+
demo.launch()
|
funcs.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file contains all the functions needed for plotting the Julia set.
|
| 2 |
+
|
| 3 |
+
# Importing necessary libraries
|
| 4 |
+
import numpy as np
|
| 5 |
+
from numba import vectorize
|
| 6 |
+
from matplotlib.colors import LogNorm
|
| 7 |
+
from matplotlib import cm
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# This is a vectorized implementation (via numba) of the escape-time algorithm (with threshold = 2).
|
| 11 |
+
@vectorize
|
| 12 |
+
def stability(z, c, max_iter):
|
| 13 |
+
z_i = z
|
| 14 |
+
for i in range(max_iter):
|
| 15 |
+
z_i = z_i**2 + c
|
| 16 |
+
if abs(z_i) >= 2:
|
| 17 |
+
return (i+1)/max_iter
|
| 18 |
+
else:
|
| 19 |
+
i += 1
|
| 20 |
+
return 1.0
|
| 21 |
+
|
| 22 |
+
# This computes for the normalized escape counts for a grid of complex numbers.
|
| 23 |
+
def get_stability_map(c, max_iter = 100, pixel_density = 1):
|
| 24 |
+
x = np.linspace(-1.5, 1.5, int(1000 * pixel_density))
|
| 25 |
+
y = np.linspace(-1.25, 1.25, int(750 * pixel_density))
|
| 26 |
+
z = x[np.newaxis, :] + y[:, np.newaxis] * 1j
|
| 27 |
+
return np.flipud(stability(z, c, max_iter))
|
| 28 |
+
|
| 29 |
+
# This plots the Julia set of a given complex number c, returning a Numpy array that will be used in a Gradio image component
|
| 30 |
+
def plot_julia_set(real, imag, max_iter = 500, pixel_density = 1.0, cmap = 'magma'):
|
| 31 |
+
try:
|
| 32 |
+
c = complex(float(real), float(imag))
|
| 33 |
+
stabilities = get_stability_map(c = c, max_iter = max_iter, pixel_density = pixel_density)
|
| 34 |
+
# Normalize values for log scaling; induces image banding
|
| 35 |
+
norm = LogNorm(vmin = 1 / max_iter, vmax = 1.0)
|
| 36 |
+
normalized = norm(stabilities) # Now between 0 and 1, log-scaled
|
| 37 |
+
# Apply colormap
|
| 38 |
+
rgba_img = cm.get_cmap(cmap)(normalized) # shape (H, W, 4), values in [0, 1]
|
| 39 |
+
# Drop alpha channel and convert to uint8
|
| 40 |
+
rgb_img = (rgba_img[:, :, :3] * 255).astype("uint8")
|
| 41 |
+
return rgb_img # NumPy array
|
| 42 |
+
except Exception as e:
|
| 43 |
+
raise gr.Error(f"Error generating image: {e}")
|
requirements.txt
ADDED
|
Binary file (3.18 kB). View file
|
|
|