Spaces:
Runtime error
Runtime error
File size: 1,459 Bytes
bf5e43e 69583a0 724fe2b bf5e43e 69583a0 724fe2b 69583a0 bf5e43e 69583a0 bf5e43e 69583a0 bf5e43e |
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 61 62 |
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import io
from PIL import Image
def execute_code_and_get_plot(code):
# Clear any existing plots
plt.clf()
# Create a BytesIO object to store the plot
buf = io.BytesIO()
try:
# Execute the code
exec(code, globals())
# Save the current plot to the BytesIO object
plt.savefig(buf, format='png')
buf.seek(0)
# Convert to PIL Image
image = Image.open(buf)
return image, "Code executed successfully!"
except Exception as e:
return None, f"Error: {str(e)}"
# Define the Gradio interface
interface = gr.Interface(
fn=execute_code_and_get_plot,
inputs=[
gr.Code(
label="Python Code",
language="python",
lines=20,
placeholder="""
# Example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(8, 6))
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid(True)
"""
)
],
outputs=[
gr.Image(type="pil", label="Generated Plot"),
gr.Textbox(label="Output Message")
],
title="Python Code to Matplotlib Plot Generator",
description="Enter Python code that generates a matplotlib plot. The plot will be displayed as an image."
)
if __name__ == "__main__":
interface.launch() |