Sugamdeol's picture
Update app.py
724fe2b verified
raw
history blame
1.19 kB
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import io
from PIL import Image
def generate_graph_from_code(code):
# Create a buffer to capture output
buf = io.BytesIO()
try:
# Define a dummy multiplier if not present in the code
exec(code)
if 'multiplier' not in locals():
multiplier = 1
except Exception as e:
return str(e)
# Generate the graph
x = np.linspace(0, 10, 100)
y = np.sin(x) * multiplier
plt.figure()
plt.plot(x, y)
plt.title("Generated Graph")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.savefig(buf, format='png')
buf.seek(0)
img = Image.open(buf)
return img
# Define the Gradio interface
interface = gr.Interface(
fn=generate_graph_from_code,
inputs=gr.inputs.Textbox(lines=10, placeholder="Paste your Python code here", label="Python Code"),
outputs=gr.outputs.Image(type="pil", label="Generated Graph"),
title="Python Code to Graph Generator",
description="Paste Python code that defines a variable `multiplier` to generate a graph."
)
if __name__ == "__main__":
interface.launch()