Spaces:
Runtime error
Runtime error
Download app.py from metonymize-kripa/animate-chart: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/spaces/metonymize-kripa/animate-chart/resolve/main/app.py
- Command line
-
hf download hf://spaces/metonymize-kripa/animate-chart/app.py
-
curl -L -o app.py https://huggingface.co/spaces/metonymize-kripa/animate-chart/resolve/main/app.py
2.01 kB
| import gradio as gr | |
| import numpy as np | |
| from matplotlib import pyplot as plt | |
| from celluloid import Camera | |
| def animate_bars_from_text(numbers, labels, duration=5): | |
| # Parse the input strings into lists | |
| final_heights = [float(num) for num in numbers.split(',')] | |
| x_labels = labels.split(',') # Split label string into a list | |
| # Ensure the number of labels matches the number of bars | |
| if len(x_labels) != len(final_heights): | |
| return "The number of labels and bar heights must match." | |
| num_bars = len(final_heights) | |
| fps = 20 | |
| total_frames = int(duration) * fps | |
| # Initialize figure and camera | |
| fig, ax = plt.subplots() | |
| camera = Camera(fig) | |
| ax.set_ylim(0, max(final_heights) + 10) | |
| # Set x-axis labels | |
| ax.set_xticks(range(num_bars)) | |
| ax.set_xticklabels(x_labels, rotation=45, ha="right") | |
| # Generate a list of colors for each bar | |
| colors = plt.cm.viridis(np.linspace(0, 1, num_bars)) | |
| # Animate | |
| for frame in range(total_frames): | |
| current_heights = [(height * frame) / total_frames for height in final_heights] | |
| ax.bar(range(num_bars), current_heights, color=colors) | |
| camera.snap() | |
| animation = camera.animate(interval=1000/fps, repeat=False) | |
| plt.close() | |
| # Save animation | |
| animation_file = 'bar_animation_with_labels.mp4' | |
| animation.save(animation_file) | |
| return animation_file | |
| iface = gr.Interface( | |
| fn=animate_bars_from_text, | |
| inputs=[ | |
| gr.Text(label="Enter bar heights separated by commas (e.g., 20,40,60,80,100)"), | |
| gr.Text(label="Enter x-axis labels separated by commas (e.g., Label1,Label2,Label3,Label4,Label5)"), | |
| gr.Number(label="Duration (seconds)", value=5) | |
| ], | |
| outputs=gr.Video(label="Generated Animation"), | |
| title="Bar Chart Animation Generator", | |
| description="Enter bar chart values and x-axis labels separated by commas, and set the animation duration to generate an animated bar chart." | |
| ) | |
| iface.launch() | |