Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
from PIL import Image | |
from io import BytesIO | |
# Function to interact with the Rayso API | |
def get_code_screenshot(code, title, theme, background, darkMode, padding, language): | |
# Constructing the payload to be sent to the Rayso API | |
payload = { | |
"code": code, | |
"title": title, | |
"theme": theme, | |
"background": background, | |
"darkMode": darkMode, | |
"padding": padding, | |
"language": language | |
} | |
# Sending a POST request to the Rayso API | |
response = requests.post("https://rayso.herokuapp.com/api", json=payload) | |
# Checking the response status code | |
if response.status_code == 200: | |
# Parsing the JSON response to obtain the image URL | |
data = response.json() | |
image_url = data.get('url') | |
# Sending a GET request to fetch the image from the obtained URL | |
response = requests.get(image_url) | |
# Checking the response status code | |
if response.status_code == 200: | |
# Converting the image bytes to a PIL Image object using BytesIO | |
image = Image.open(BytesIO(response.content)) | |
# Returning the PIL Image object to be displayed by Gradio | |
return image | |
else: | |
# Returning an error message if failed to fetch the image | |
return f"Failed to fetch image: {response.status_code}" | |
else: | |
# Returning an error message if failed to get the image URL | |
return f"Failed to get image: {response.status_code}" | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=get_code_screenshot, # Function to be called on user input | |
inputs=[ | |
gr.Textbox(lines=10, placeholder="Enter your code here...", label="Code"), | |
gr.Textbox(default="Untitled-1", placeholder="Enter title...", label="Title"), | |
gr.Dropdown(choices=["breeze", "candy", "crimson", "falcon", "meadow", "midnight", "raindrop", "sunset"], label="Theme"), | |
gr.Checkbox(default=True, label="Background"), | |
gr.Checkbox(default=True, label="Dark Mode"), | |
gr.Dropdown(choices=["16", "32", "64", "128"], label="Padding"), | |
gr.Textbox(default="auto", placeholder="Enter language...", label="Language"), | |
], | |
outputs=[ | |
gr.Image(label="Generated Image"), # Updated to handle the PIL Image object returned by get_code_screenshot | |
], | |
live=False # Set to False to only call the function when the submit button is pressed | |
) | |
# Launch the Gradio interface | |
if __name__ == "__main__": | |
iface.launch() |