initial app.
Browse files- app.py +72 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import io
|
| 4 |
+
import sys
|
| 5 |
+
from PIL import Image
|
| 6 |
+
sys.path.insert(0, 'src')
|
| 7 |
+
|
| 8 |
+
from rayt.camera import Camera
|
| 9 |
+
from rayt.cli import random_scene
|
| 10 |
+
from rayt.cuda_renderer import render_with_cuda
|
| 11 |
+
from rayt.vec3 import Point3, Vec3
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@spaces.GPU
|
| 15 |
+
def render_image(image_width, samples_per_pixel, aspect_ratio_str, max_depth):
|
| 16 |
+
"""
|
| 17 |
+
Renders an image using the rayt library and returns it.
|
| 18 |
+
"""
|
| 19 |
+
# --- Capture stdout ---
|
| 20 |
+
old_stdout = sys.stdout
|
| 21 |
+
sys.stdout = captured_output = io.StringIO()
|
| 22 |
+
|
| 23 |
+
# --- Run the rendering logic ---
|
| 24 |
+
aspect_ratio = eval(aspect_ratio_str)
|
| 25 |
+
image_height = int(image_width / aspect_ratio)
|
| 26 |
+
world = random_scene()
|
| 27 |
+
camera = Camera(
|
| 28 |
+
lookfrom=Point3(13, 2, 3),
|
| 29 |
+
lookat=Point3(0, 0, 0),
|
| 30 |
+
vup=Vec3(0, 1, 0),
|
| 31 |
+
vfov=20.0,
|
| 32 |
+
aspect_ratio=aspect_ratio,
|
| 33 |
+
aperture=0.1,
|
| 34 |
+
focus_dist=10.0,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
render_with_cuda(world, camera, image_width, image_height, samples_per_pixel, max_depth)
|
| 38 |
+
|
| 39 |
+
# --- Restore stdout and get the PPM data ---
|
| 40 |
+
sys.stdout = old_stdout
|
| 41 |
+
ppm_data = captured_output.getvalue()
|
| 42 |
+
|
| 43 |
+
# --- Convert PPM to Image ---
|
| 44 |
+
# The PPM data is in a string, so we need to wrap it in a file-like object
|
| 45 |
+
ppm_file = io.BytesIO(ppm_data.encode())
|
| 46 |
+
image = Image.open(ppm_file)
|
| 47 |
+
|
| 48 |
+
return image
|
| 49 |
+
|
| 50 |
+
# --- Create the Gradio Interface ---
|
| 51 |
+
with gr.Blocks() as iface:
|
| 52 |
+
gr.Markdown("# Ray Tracing in One Weekend")
|
| 53 |
+
gr.Markdown("A Python implementation of the book by Peter Shirley, rendered with Numba.")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
image_width = gr.Slider(label="Image Width", minimum=100, maximum=1200, value=300, step=10)
|
| 58 |
+
samples_per_pixel = gr.Slider(label="Samples Per Pixel", minimum=1, maximum=200, value=20, step=1)
|
| 59 |
+
max_depth = gr.Slider(label="Max Ray Depth", minimum=1, maximum=100, value=50, step=1)
|
| 60 |
+
aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16/9", "3/2", "1/1"], value="16/9")
|
| 61 |
+
render_button = gr.Button("Render Image")
|
| 62 |
+
with gr.Column():
|
| 63 |
+
output_image = gr.Image(label="Rendered Image", type="pil")
|
| 64 |
+
|
| 65 |
+
render_button.click(
|
| 66 |
+
fn=render_image,
|
| 67 |
+
inputs=[image_width, samples_per_pixel, aspect_ratio, max_depth],
|
| 68 |
+
outputs=output_image
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
spaces
|
| 3 |
+
rayt[cuda] @ git+https://github.com/gkmngrgn/rayt.git
|