Spaces:
Sleeping
Sleeping
Added application files
Browse filesFiles changed (5)
- .gitignoreAdded+3
- README.md+43-14
- app.pyAdded+40
- funcs.pyAdded+43
- requirements.txtAdded
123__pycache__/
flagged/
proj_env/1234567891011121314---title: Julia Set Visualizeremoji: 🏆colorFrom: yellowcolorTo: greensdk: gradiosdk_version: 5.30.0app_file: app.pypinned: falselicense: mitshort_description: This is a simple Julia set visualizer implemented in Gradio---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference12345678910111213141516171819202122232425262728293031323334353637383940414243# Julia Set Visualizer using GradioThis is a simple Gradio implementation of my Julia Set visualizer previously implemented and deployed in Streamlit.<p align="center"><img src="assets/screenshot_app.png" width="700"/></p>Accessing the App=================To access this app, you can either1. Clone the repository. Then, run`pip install -r requirements.txt`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`python app.py`on the terminal and use the app on your local server.ORAccess the app via HuggingFace Spaces through this link (to add).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`.### Recommended Julia Set SeedsThese complex numbers are known to generate visually interesting Julia sets:| Real Part | Imaginary Part ||----------------|------------------|| -0.1156437876 | 0.8690819138 || -0.7269 | 0.1889 || -0.5125114984 | 0.5212955731 || -0.4 | 0.6 || -0.5012149299 | -0.5637838176 || 0 | -0.8 || -0.8 | 0.156 || -0.7773672345 | -0.1782126754 || -0.06353957916 | -0.6992547595 || -0.5064253507 | 0.5981400301 || 0.2803481964 | -0.5273108717 |
12345678910111213141516171819202122232425262728293031323334353637383940# This file is the main application file to host the application logic
# Importing gradio and the plot_julia_set function from funcs.py
import gradio as gr
from funcs import plot_julia_set
with gr.Blocks() as demo:
with gr.Row():
# Adding an application header
gr.Markdown("""
<div style="text-align: center; font-size: 18px;">
<h1 style="font-size: 32px;">Julia Set Generator 🌌</h1>
<p>Use this interactive tool to generate visualizations of Julia Sets!</p>
<h3 style="font-size: 24px;">Instructions:</h3>
<ol style="display: inline-block; text-align: left; font-size: 18px;">
<li><strong>Input the Real and Imaginary parts</strong> of the complex number <code>c</code>.</li>
<li><strong>Adjust the max iterations</strong> to control the detail and depth.</li>
<li><strong>Adjust the pixel density</strong> to control the resolution.</li>
<li><strong>Choose a colormap</strong> to customize the appearance.</li>
<li>Click <strong>"Generate Plot"</strong> to render the image.</li>
<li>For more info, see this <a href="https://github.com/ArnelMalubay/julia-visualizer-using-gradio" target="_blank">GitHub repository</a>.</li>
</ol>
</div>
""")
# Adding all the interactive components of the application
with gr.Column():
real = gr.Textbox(label = 'Real Part', value = '0', interactive = True)
imag = gr.Textbox(label = 'Imaginary Part', value = '0', interactive = True)
max_iter = gr.Slider(label = 'Specify the maximum number of iterations', minimum = 10, maximum = 2000, value = 500, step = 10, interactive = True)
pixel_density = gr.Slider(label = 'Specify pixel density', minimum = 0.5, maximum = 2.5, value = 1.0, step = 0.1, interactive = True)
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']
cmap = gr.Dropdown(label = 'Choose colormap', choices = colormap_choices, value = 'binary', interactive = True)
submit = gr.Button('Generate Plot')
with gr.Row():
image = gr.Image(label = 'Julia Set', width = 600, height = 450, interactive = False)
submit.click(fn = plot_julia_set, inputs = [real, imag, max_iter, pixel_density, cmap], outputs = image)
demo.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243# This file contains all the functions needed for plotting the Julia set.
# Importing necessary libraries
import numpy as np
from numba import vectorize
from matplotlib.colors import LogNorm
from matplotlib import cm
import gradio as gr
# This is a vectorized implementation (via numba) of the escape-time algorithm (with threshold = 2).
@vectorize
def stability(z, c, max_iter):
z_i = z
for i in range(max_iter):
z_i = z_i**2 + c
if abs(z_i) >= 2:
return (i+1)/max_iter
else:
i += 1
return 1.0
# This computes for the normalized escape counts for a grid of complex numbers.
def get_stability_map(c, max_iter = 100, pixel_density = 1):
x = np.linspace(-1.5, 1.5, int(1000 * pixel_density))
y = np.linspace(-1.25, 1.25, int(750 * pixel_density))
z = x[np.newaxis, :] + y[:, np.newaxis] * 1j
return np.flipud(stability(z, c, max_iter))
# This plots the Julia set of a given complex number c, returning a Numpy array that will be used in a Gradio image component
def plot_julia_set(real, imag, max_iter = 500, pixel_density = 1.0, cmap = 'magma'):
try:
c = complex(float(real), float(imag))
stabilities = get_stability_map(c = c, max_iter = max_iter, pixel_density = pixel_density)
# Normalize values for log scaling; induces image banding
norm = LogNorm(vmin = 1 / max_iter, vmax = 1.0)
normalized = norm(stabilities) # Now between 0 and 1, log-scaled
# Apply colormap
rgba_img = cm.get_cmap(cmap)(normalized) # shape (H, W, 4), values in [0, 1]
# Drop alpha channel and convert to uint8
rgb_img = (rgba_img[:, :, :3] * 255).astype("uint8")
return rgb_img # NumPy array
except Exception as e:
raise gr.Error(f"Error generating image: {e}").gitignoreAdded+3
README.md+43-14
app.pyAdded+40
funcs.pyAdded+43
requirements.txtAdded