File size: 1,185 Bytes
bf5e43e
 
 
 
 
 
724fe2b
 
 
 
bf5e43e
724fe2b
 
 
 
 
 
 
 
bf5e43e
 
724fe2b
bf5e43e
 
 
724fe2b
 
 
bf5e43e
 
 
 
 
724fe2b
bf5e43e
724fe2b
 
 
 
 
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
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()