rayochoajr commited on
Commit
ae5d47a
·
1 Parent(s): 1522590

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -53
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 requests
6
- from PIL import Image
7
- from io import BytesIO
8
-
9
- # Function to interact with the Rayso API
10
- def get_code_screenshot(**kwargs):
11
- # Sending a POST request to the Rayso API with the parameters received from the Gradio interface
12
- response = requests.post("https://rayso.herokuapp.com/api", json=kwargs)
13
-
14
- # Checking the response status code
15
- if response.status_code == 200:
16
- # Parsing the JSON response to obtain the image URL
17
- data = response.json()
18
- image_url = data.get('url')
19
-
20
- # Sending a GET request to fetch the image from the obtained URL
21
- response = requests.get(image_url)
22
-
23
- # Checking the response status code
24
- if response.status_code == 200:
25
- # Converting the image bytes to a PIL Image object using BytesIO
26
- image = Image.open(BytesIO(response.content))
27
- # Returning the PIL Image object to be displayed by Gradio
28
- return image
29
- else:
30
- # Returning an error message if failed to fetch the image
31
- return f"Failed to fetch image: {response.status_code}"
32
- else:
33
- # Returning an error message if failed to get the image URL
34
- return f"Failed to get image: {response.status_code}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Gradio Interface
37
  iface = gr.Interface(
38
- fn=get_code_screenshot, # Function to be called on user input
39
- inputs={
40
- "code": gr.Textbox(lines=10, placeholder="Enter your code here...", default="console.log('Hello World');"),
41
- "title": gr.Textbox(default="Untitled-1", placeholder="Enter title..."),
42
- "theme": gr.Dropdown(choices=["breeze", "candy", "crimson", "falcon", "meadow", "midnight", "raindrop", "sunset"], default="breeze"),
43
- "background": gr.Checkbox(default=True),
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
- # Launch the Gradio interface
55
- if __name__ == "__main__":
56
- iface.launch()
57
- ```
58
 
59
- Key Adjustments:
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()