Spaces:
Runtime error
Runtime error
Commit
·
ae5d47a
1
Parent(s):
1522590
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,84 @@
|
|
1 |
-
Here's a revised version of your code with comments explaining the changes and clarifications. The values for the API parameters are now preloaded as default values in the Gradio interface, and the use of **kwargs** has been implemented to simplify the function signature and invocation.
|
2 |
-
|
3 |
-
```python
|
4 |
import gradio as gr
|
5 |
-
import
|
6 |
-
from PIL import Image
|
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 |
# Gradio Interface
|
37 |
iface = gr.Interface(
|
38 |
-
fn=
|
39 |
-
inputs=
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
"darkMode": gr.Checkbox(default=True),
|
45 |
-
"padding": gr.Dropdown(choices=["16", "32", "64", "128"], default="32"),
|
46 |
-
"language": gr.Textbox(default="auto", placeholder="Enter language..."),
|
47 |
-
},
|
48 |
-
outputs=[
|
49 |
-
gr.Image(label="Generated Image"), # Updated to handle the PIL Image object returned by get_code_screenshot
|
50 |
-
],
|
51 |
-
live=False # Set to False to only call the function when the submit button is pressed
|
52 |
)
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
```
|
58 |
|
59 |
-
|
60 |
-
1. The `get_code_screenshot` function now accepts **kwargs, which collects all the named arguments into a dictionary. This allows for a more flexible function signature and
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import random
|
3 |
+
from PIL import Image, ImageDraw
|
4 |
+
|
5 |
+
# Game Constants
|
6 |
+
bird_position = 250
|
7 |
+
gravity = 3
|
8 |
+
flap_strength = -30
|
9 |
+
ground_position = 300
|
10 |
+
pipe_position = 500
|
11 |
+
pipe_gap = 200
|
12 |
+
pipe_height = 100
|
13 |
+
score = 0
|
14 |
+
game_over = False
|
15 |
+
screen_width, screen_height = 500, 350
|
16 |
+
|
17 |
+
# Draw Function
|
18 |
+
def draw_game():
|
19 |
+
img = Image.new('RGB', (screen_width, screen_height), 'white')
|
20 |
+
draw = ImageDraw.Draw(img)
|
21 |
+
|
22 |
+
# Draw bird
|
23 |
+
draw.ellipse([150, bird_position, 180, bird_position + 30], fill='blue')
|
24 |
+
|
25 |
+
# Draw pipes
|
26 |
+
draw.rectangle([pipe_position, 0, pipe_position + 30, pipe_height], fill='green')
|
27 |
+
draw.rectangle([pipe_position, pipe_height + pipe_gap, pipe_position + 30, screen_height], fill='green')
|
28 |
+
|
29 |
+
# Draw ground
|
30 |
+
draw.rectangle([0, ground_position, screen_width, screen_height], fill='brown')
|
31 |
+
|
32 |
+
return img
|
33 |
+
|
34 |
+
# Game Functions
|
35 |
+
def flap():
|
36 |
+
global bird_position, game_over
|
37 |
+
if not game_over:
|
38 |
+
bird_position += flap_strength
|
39 |
+
|
40 |
+
def update_game():
|
41 |
+
global bird_position, pipe_position, score, ground_position, game_over, pipe_height
|
42 |
+
if not game_over:
|
43 |
+
bird_position += gravity
|
44 |
+
pipe_position -= 5
|
45 |
+
|
46 |
+
if bird_position > ground_position:
|
47 |
+
bird_position = ground_position
|
48 |
+
game_over = True
|
49 |
+
|
50 |
+
if (150 < pipe_position < 190) and not (pipe_height < bird_position < pipe_height + pipe_gap):
|
51 |
+
game_over = True
|
52 |
+
|
53 |
+
if pipe_position < 0:
|
54 |
+
score += 1
|
55 |
+
pipe_position = 500
|
56 |
+
pipe_height = random.randint(50, 250)
|
57 |
+
|
58 |
+
def reset_game():
|
59 |
+
global bird_position, pipe_position, score, game_over, pipe_height
|
60 |
+
bird_position = 250
|
61 |
+
pipe_position = 500
|
62 |
+
pipe_height = 100
|
63 |
+
score = 0
|
64 |
+
game_over = False
|
65 |
+
|
66 |
+
def game_output():
|
67 |
+
update_game()
|
68 |
+
return draw_game(), f"Score: {score}\n{'GAME OVER' if game_over else ''}"
|
69 |
|
70 |
# Gradio Interface
|
71 |
iface = gr.Interface(
|
72 |
+
fn=game_output,
|
73 |
+
inputs=gr.Button("Flap"),
|
74 |
+
outputs=["image", "text"],
|
75 |
+
live=True,
|
76 |
+
allow_flagging="never",
|
77 |
+
manual_run=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
)
|
79 |
|
80 |
+
reset_button = gr.Button("Reset")
|
81 |
+
reset_button.click(fn=reset_game, inputs=None, outputs=None)
|
82 |
+
iface.add_component(reset_button)
|
|
|
83 |
|
84 |
+
iface.launch()
|
|