Spaces:
Runtime error
Runtime error
File size: 2,537 Bytes
f626979 71518c0 f626979 b508f76 f626979 71518c0 f626979 71518c0 f626979 71518c0 f626979 71518c0 f626979 71518c0 f626979 71518c0 f626979 b508f76 f626979 71518c0 f626979 71518c0 f626979 b508f76 71518c0 b508f76 f626979 b508f76 f626979 245110e |
1 2 3 4 5 6 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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() |