phenixrhyder
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,73 @@
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
process_button.click(
|
| 38 |
-
fn=process_text,
|
| 39 |
-
inputs=input_component,
|
| 40 |
-
outputs=output_component
|
| 41 |
-
)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# ---
|
| 45 |
-
# This command starts the web server when Hugging Face runs the script.
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
|
| 6 |
+
def create_checkerboard(board_size=8, square_size=50):
|
| 7 |
+
"""
|
| 8 |
+
Generates a checkerboard image using the Pillow library.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
board_size (int): The number of squares per side (e.g., 8 for an 8x8 board).
|
| 12 |
+
square_size (int): The size of each square in pixels.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
PIL.Image.Image: The generated checkerboard image.
|
| 16 |
+
"""
|
| 17 |
+
# Calculate the total size of the image
|
| 18 |
+
image_size = board_size * square_size
|
| 19 |
+
|
| 20 |
+
# Create a new blank image in RGB mode with a white background
|
| 21 |
+
image = Image.new("RGB", (image_size, image_size), "white")
|
| 22 |
+
draw = ImageDraw.Draw(image)
|
| 23 |
+
|
| 24 |
+
# Define the colors for the checkerboard
|
| 25 |
+
color1 = (255, 255, 255) # White
|
| 26 |
+
color2 = (0, 0, 0) # Black
|
| 27 |
|
| 28 |
+
# Loop through each square position
|
| 29 |
+
for row in range(board_size):
|
| 30 |
+
for col in range(board_size):
|
| 31 |
+
# Calculate the top-left and bottom-right coordinates of the square
|
| 32 |
+
x1 = col * square_size
|
| 33 |
+
y1 = row * square_size
|
| 34 |
+
x2 = x1 + square_size
|
| 35 |
+
y2 = y1 + square_size
|
| 36 |
+
|
| 37 |
+
# Determine the color of the square
|
| 38 |
+
if (row + col) % 2 == 0:
|
| 39 |
+
square_color = color1
|
| 40 |
+
else:
|
| 41 |
+
square_color = color2
|
| 42 |
+
|
| 43 |
+
# Draw the rectangle on the image
|
| 44 |
+
draw.rectangle([x1, y1, x2, y2], fill=square_color)
|
| 45 |
+
|
| 46 |
+
return image
|
| 47 |
+
|
| 48 |
+
# --- Create the Gradio Interface ---
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# Checkerboard Pattern Generator")
|
| 51 |
+
gr.Markdown("Use the sliders to change the size of the board and the squares, then click Generate.")
|
| 52 |
|
| 53 |
+
with gr.Row():
|
| 54 |
+
# Input sliders for customization
|
| 55 |
+
board_size_slider = gr.Slider(minimum=2, maximum=20, value=8, step=1, label="Board Size (e.g., 8x8)")
|
| 56 |
+
square_size_slider = gr.Slider(minimum=10, maximum=100, value=50, step=5, label="Square Size (pixels)")
|
| 57 |
|
| 58 |
+
# The button to trigger the image generation
|
| 59 |
+
generate_button = gr.Button("Generate Image")
|
| 60 |
|
| 61 |
+
# The output component to display the generated image
|
| 62 |
+
output_image = gr.Image(label="Generated Checkerboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# Link the button to the function
|
| 65 |
+
generate_button.click(
|
| 66 |
+
fn=create_checkerboard,
|
| 67 |
+
inputs=[board_size_slider, square_size_slider],
|
| 68 |
+
outputs=output_image
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# --- Launch the App ---
|
|
|
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
demo.launch()
|