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)}"

# Example code
example_code = """
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)
"""

# Define the Gradio interface
interface = gr.Interface(
    fn=execute_code_and_get_plot,
    inputs=[
        gr.Code(
            label="Python Code",
            language="python",
            lines=20,
        )
    ],
    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.",
    examples=[[example_code]],
)

if __name__ == "__main__":
    interface.launch()