fix again; redid;
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import subprocess
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import traceback
|
| 5 |
import sys
|
| 6 |
import io
|
| 7 |
import numpy as np
|
|
@@ -9,32 +7,33 @@ from PIL import Image
|
|
| 9 |
|
| 10 |
def run_code(code):
|
| 11 |
try:
|
| 12 |
-
if any(bad_cmd in code for bad_cmd in ['rm', 'os', 'shutil']):
|
| 13 |
-
return create_image("")
|
| 14 |
-
|
| 15 |
old_stdout = sys.stdout
|
| 16 |
sys.stdout = io.StringIO()
|
| 17 |
|
| 18 |
-
local_env = {'plt': plt, '
|
| 19 |
exec(code, {}, local_env)
|
| 20 |
|
| 21 |
output = sys.stdout.getvalue()
|
| 22 |
sys.stdout = old_stdout
|
| 23 |
|
| 24 |
-
if
|
| 25 |
buf = io.BytesIO()
|
|
|
|
| 26 |
plt.savefig(buf, format='png')
|
| 27 |
buf.seek(0)
|
| 28 |
img = Image.open(buf)
|
| 29 |
img_array = np.array(img)
|
| 30 |
plt.close()
|
| 31 |
-
|
|
|
|
| 32 |
return img_array
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
return create_image("")
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
-
return create_image("")
|
| 38 |
|
| 39 |
def create_image(message):
|
| 40 |
fig, ax = plt.subplots()
|
|
@@ -51,8 +50,8 @@ interface = gr.Interface(
|
|
| 51 |
fn=run_code,
|
| 52 |
inputs=gr.Code(language="python", label="Enter Python Code"),
|
| 53 |
outputs=gr.Image(type="numpy", label="Output"),
|
| 54 |
-
title="
|
| 55 |
-
description="
|
| 56 |
)
|
| 57 |
|
| 58 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
|
|
|
| 3 |
import sys
|
| 4 |
import io
|
| 5 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
def run_code(code):
|
| 9 |
try:
|
|
|
|
|
|
|
|
|
|
| 10 |
old_stdout = sys.stdout
|
| 11 |
sys.stdout = io.StringIO()
|
| 12 |
|
| 13 |
+
local_env = {'plt': plt, 'np': np}
|
| 14 |
exec(code, {}, local_env)
|
| 15 |
|
| 16 |
output = sys.stdout.getvalue()
|
| 17 |
sys.stdout = old_stdout
|
| 18 |
|
| 19 |
+
if 'plt' in code:
|
| 20 |
buf = io.BytesIO()
|
| 21 |
+
plt.draw()
|
| 22 |
plt.savefig(buf, format='png')
|
| 23 |
buf.seek(0)
|
| 24 |
img = Image.open(buf)
|
| 25 |
img_array = np.array(img)
|
| 26 |
plt.close()
|
| 27 |
+
|
| 28 |
+
if img_array.sum() > 0:
|
| 29 |
return img_array
|
| 30 |
+
else:
|
| 31 |
+
return create_image("Empty plot")
|
| 32 |
|
| 33 |
+
return create_image("No valid matplotlib plot found")
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
+
return create_image(f"Error: {str(e)}")
|
| 37 |
|
| 38 |
def create_image(message):
|
| 39 |
fig, ax = plt.subplots()
|
|
|
|
| 50 |
fn=run_code,
|
| 51 |
inputs=gr.Code(language="python", label="Enter Python Code"),
|
| 52 |
outputs=gr.Image(type="numpy", label="Output"),
|
| 53 |
+
title="Matplotlib Plot Generator",
|
| 54 |
+
description="Enter Python code that uses matplotlib to generate plots."
|
| 55 |
)
|
| 56 |
|
| 57 |
+
interface.launch()
|