phenixrhyder commited on
Commit
3b54482
·
unverified ·
1 Parent(s): 8f6f3a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -36
app.py CHANGED
@@ -1,47 +1,73 @@
1
  # app.py
2
 
3
  import gradio as gr
 
4
 
5
- # --- 1. DEFINE YOUR CORE FUNCTION ---
6
- # This is a simple placeholder function. It takes some text as input
7
- # and returns a slightly modified version of it.
8
- def process_text(input_text):
9
- """
10
- This function takes a string and returns a new string.
11
- """
12
- # for debugging, this will show up in the logs of your Hugging Face Space
13
- print(f"Processing input: {input_text}")
14
-
15
- # The actual logic of our simple app
16
- output_text = f"The app received your message: '{input_text}'"
17
- return output_text
18
-
19
-
20
- # --- 2. CREATE THE GRADIO INTERFACE ---
21
- # This creates the web UI that users will see and interact with.
22
- with gr.Blocks() as demo:
23
- gr.Markdown("# My First Hugging Face App")
24
- gr.Markdown("This is a simple demo. Type something in the box and click the button.")
 
25
 
26
- # Define the input component (a textbox)
27
- input_component = gr.Textbox(label="Your Message")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # Define the output component
30
- output_component = gr.Textbox(label="App's Response")
 
 
31
 
32
- # Define the button that will trigger the function
33
- process_button = gr.Button("Submit")
34
 
35
- # Link the button to your function.
36
- # When the button is clicked, it will run the `process_text` function.
37
- process_button.click(
38
- fn=process_text,
39
- inputs=input_component,
40
- outputs=output_component
41
- )
42
 
 
 
 
 
 
 
43
 
44
- # --- 3. LAUNCH THE APP ---
45
- # This command starts the web server when Hugging Face runs the script.
46
  if __name__ == "__main__":
47
- demo.launch()
 
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()