Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +123 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
from io import StringIO
|
7 |
+
import sys
|
8 |
+
import contextlib
|
9 |
+
from sklearn import datasets, metrics, model_selection, preprocessing
|
10 |
+
import warnings
|
11 |
+
warnings.filterwarnings('ignore')
|
12 |
+
|
13 |
+
# Capture output helper
|
14 |
+
@contextlib.contextmanager
|
15 |
+
def capture_output():
|
16 |
+
new_out, new_err = StringIO(), StringIO()
|
17 |
+
old_out, old_err = sys.stdout, sys.stderr
|
18 |
+
try:
|
19 |
+
sys.stdout, sys.stderr = new_out, new_err
|
20 |
+
yield sys.stdout, sys.stderr
|
21 |
+
finally:
|
22 |
+
sys.stdout, sys.stderr = old_out, old_err
|
23 |
+
|
24 |
+
def execute_code(code: str):
|
25 |
+
"""
|
26 |
+
Execute the provided Python code and return the output
|
27 |
+
"""
|
28 |
+
# Initialize output components
|
29 |
+
output_text = ""
|
30 |
+
output_plot = None
|
31 |
+
output_df = None
|
32 |
+
|
33 |
+
# Create a namespace for code execution
|
34 |
+
namespace = {
|
35 |
+
'np': np,
|
36 |
+
'pd': pd,
|
37 |
+
'plt': plt,
|
38 |
+
'sns': sns,
|
39 |
+
'datasets': datasets,
|
40 |
+
'metrics': metrics,
|
41 |
+
'model_selection': model_selection,
|
42 |
+
'preprocessing': preprocessing
|
43 |
+
}
|
44 |
+
|
45 |
+
try:
|
46 |
+
# Capture print outputs
|
47 |
+
with capture_output() as (out, err):
|
48 |
+
# Execute the code
|
49 |
+
exec(code, namespace)
|
50 |
+
|
51 |
+
# Capture print statements
|
52 |
+
output_text = out.getvalue()
|
53 |
+
if err.getvalue():
|
54 |
+
output_text += "\nErrors:\n" + err.getvalue()
|
55 |
+
|
56 |
+
# Check if there's a plot
|
57 |
+
if plt.gcf().axes:
|
58 |
+
output_plot = plt.gcf()
|
59 |
+
plt.close()
|
60 |
+
|
61 |
+
# Check for DataFrame output
|
62 |
+
for var in namespace:
|
63 |
+
if isinstance(namespace[var], pd.DataFrame):
|
64 |
+
output_df = namespace[var]
|
65 |
+
break
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
output_text = f"Error: {str(e)}"
|
69 |
+
|
70 |
+
return output_text, output_plot, output_df
|
71 |
+
|
72 |
+
# Create the Gradio interface
|
73 |
+
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("""
|
75 |
+
# Python Data Science Code Executor
|
76 |
+
Execute Python code with access to common data science libraries:
|
77 |
+
- NumPy (np)
|
78 |
+
- Pandas (pd)
|
79 |
+
- Matplotlib (plt)
|
80 |
+
- Seaborn (sns)
|
81 |
+
- Scikit-learn (datasets, metrics, model_selection, preprocessing)
|
82 |
+
""")
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
with gr.Column():
|
86 |
+
code_input = gr.Code(
|
87 |
+
label="Python Code",
|
88 |
+
language="python",
|
89 |
+
value="""# Example: Load and visualize iris dataset
|
90 |
+
from sklearn.datasets import load_iris
|
91 |
+
iris = load_iris()
|
92 |
+
df = pd.DataFrame(iris.data, columns=iris.feature_names)
|
93 |
+
df['target'] = iris.target
|
94 |
+
|
95 |
+
# Create a scatter plot
|
96 |
+
plt.figure(figsize=(10, 6))
|
97 |
+
plt.scatter(df['sepal length (cm)'], df['sepal width (cm)'], c=df['target'])
|
98 |
+
plt.xlabel('Sepal Length (cm)')
|
99 |
+
plt.ylabel('Sepal Width (cm)')
|
100 |
+
plt.title('Iris Dataset - Sepal Length vs Width')
|
101 |
+
|
102 |
+
# Print first few rows
|
103 |
+
print("First 5 rows of the dataset:")
|
104 |
+
print(df.head())
|
105 |
+
"""
|
106 |
+
)
|
107 |
+
run_button = gr.Button("Execute Code", variant="primary")
|
108 |
+
|
109 |
+
with gr.Column():
|
110 |
+
output_text = gr.Textbox(label="Output", lines=5)
|
111 |
+
output_plot = gr.Plot(label="Plot Output")
|
112 |
+
output_df = gr.Dataframe(label="DataFrame Output")
|
113 |
+
|
114 |
+
# Handle code execution
|
115 |
+
run_button.click(
|
116 |
+
fn=execute_code,
|
117 |
+
inputs=[code_input],
|
118 |
+
outputs=[output_text, output_plot, output_df]
|
119 |
+
)
|
120 |
+
|
121 |
+
# Launch the app
|
122 |
+
if __name__ == "__main__":
|
123 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=4.0.0
|
2 |
+
numpy>=1.24.0
|
3 |
+
pandas>=2.0.0
|
4 |
+
matplotlib>=3.7.0
|
5 |
+
seaborn>=0.12.0
|
6 |
+
scikit-learn>=1.2.0
|