Spaces:
Runtime error
Runtime error
Commit
·
1522590
1
Parent(s):
73b5a0d
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,60 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
9 |
if response.status_code == 200:
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return image
|
15 |
else:
|
16 |
-
|
|
|
17 |
else:
|
|
|
18 |
return f"Failed to get image: {response.status_code}"
|
19 |
|
|
|
20 |
iface = gr.Interface(
|
21 |
-
fn=get_code_screenshot,
|
22 |
-
inputs=
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
)
|
25 |
|
|
|
26 |
if __name__ == "__main__":
|
27 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
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
|