Spaces:
Runtime error
Runtime error
Commit
·
71518c0
1
Parent(s):
b508f76
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
3 |
|
4 |
# Function to interact with the Rayso API
|
5 |
def get_code_screenshot(code, title, theme, background, darkMode, padding, language):
|
|
|
6 |
payload = {
|
7 |
"code": code,
|
8 |
"title": title,
|
@@ -12,28 +15,42 @@ def get_code_screenshot(code, title, theme, background, darkMode, padding, langu
|
|
12 |
"padding": padding,
|
13 |
"language": language
|
14 |
}
|
|
|
15 |
response = requests.post("https://rayso.herokuapp.com/api", json=payload)
|
|
|
16 |
if response.status_code == 200:
|
|
|
17 |
data = response.json()
|
18 |
image_url = data.get('url')
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
else:
|
|
|
21 |
return f"Failed to get image: {response.status_code}"
|
22 |
|
23 |
# Gradio Interface
|
24 |
iface = gr.Interface(
|
25 |
-
fn=get_code_screenshot,
|
26 |
inputs=[
|
27 |
-
gr.
|
28 |
-
gr.
|
29 |
-
gr.
|
30 |
-
gr.
|
31 |
-
gr.
|
32 |
-
gr.
|
33 |
-
gr.
|
34 |
],
|
35 |
outputs=[
|
36 |
-
gr.
|
37 |
],
|
38 |
live=False # Set to False to only call the function when the submit button is pressed
|
39 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
|
6 |
# Function to interact with the Rayso API
|
7 |
def get_code_screenshot(code, title, theme, background, darkMode, padding, language):
|
8 |
+
# Constructing the payload to be sent to the Rayso API
|
9 |
payload = {
|
10 |
"code": code,
|
11 |
"title": title,
|
|
|
15 |
"padding": padding,
|
16 |
"language": language
|
17 |
}
|
18 |
+
# Sending a POST request to the Rayso API
|
19 |
response = requests.post("https://rayso.herokuapp.com/api", json=payload)
|
20 |
+
# Checking the response status code
|
21 |
if response.status_code == 200:
|
22 |
+
# Parsing the JSON response to obtain the image URL
|
23 |
data = response.json()
|
24 |
image_url = data.get('url')
|
25 |
+
# Sending a GET request to fetch the image from the obtained URL
|
26 |
+
response = requests.get(image_url)
|
27 |
+
# Checking the response status code
|
28 |
+
if response.status_code == 200:
|
29 |
+
# Converting the image bytes to a PIL Image object using BytesIO
|
30 |
+
image = Image.open(BytesIO(response.content))
|
31 |
+
# Returning the PIL Image object to be displayed by Gradio
|
32 |
+
return image
|
33 |
+
else:
|
34 |
+
# Returning an error message if failed to fetch the image
|
35 |
+
return f"Failed to fetch image: {response.status_code}"
|
36 |
else:
|
37 |
+
# Returning an error message if failed to get the image URL
|
38 |
return f"Failed to get image: {response.status_code}"
|
39 |
|
40 |
# Gradio Interface
|
41 |
iface = gr.Interface(
|
42 |
+
fn=get_code_screenshot, # Function to be called on user input
|
43 |
inputs=[
|
44 |
+
gr.Textbox(lines=10, placeholder="Enter your code here...", label="Code"),
|
45 |
+
gr.Textbox(default="Untitled-1", placeholder="Enter title...", label="Title"),
|
46 |
+
gr.Dropdown(choices=["breeze", "candy", "crimson", "falcon", "meadow", "midnight", "raindrop", "sunset"], label="Theme"),
|
47 |
+
gr.Checkbox(default=True, label="Background"),
|
48 |
+
gr.Checkbox(default=True, label="Dark Mode"),
|
49 |
+
gr.Dropdown(choices=["16", "32", "64", "128"], label="Padding"),
|
50 |
+
gr.Textbox(default="auto", placeholder="Enter language...", label="Language"),
|
51 |
],
|
52 |
outputs=[
|
53 |
+
gr.Image(label="Generated Image"), # Updated to handle the PIL Image object returned by get_code_screenshot
|
54 |
],
|
55 |
live=False # Set to False to only call the function when the submit button is pressed
|
56 |
)
|