Spaces:
Running
Running
Run pysr in secondary instance
Browse files- gui/app.py +5 -75
- gui/install_pysr.sh +12 -0
- gui/run_pysr_and_save.py +68 -0
gui/app.py
CHANGED
|
@@ -1,11 +1,7 @@
|
|
| 1 |
-
import io
|
| 2 |
import gradio as gr
|
| 3 |
-
import sys
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
-
import numpy as np
|
| 7 |
import pandas as pd
|
| 8 |
-
import traceback as tb
|
| 9 |
|
| 10 |
empty_df = pd.DataFrame(
|
| 11 |
{
|
|
@@ -14,7 +10,6 @@ empty_df = pd.DataFrame(
|
|
| 14 |
"complexity": [],
|
| 15 |
}
|
| 16 |
)
|
| 17 |
-
Main = None
|
| 18 |
|
| 19 |
def greet(
|
| 20 |
file_obj: tempfile._TemporaryFileWrapper,
|
|
@@ -23,12 +18,6 @@ def greet(
|
|
| 23 |
binary_operators: list,
|
| 24 |
unary_operators: list,
|
| 25 |
):
|
| 26 |
-
global Main
|
| 27 |
-
if Main is not None:
|
| 28 |
-
return (
|
| 29 |
-
empty_df,
|
| 30 |
-
"Refresh the page to run with a different configuration."
|
| 31 |
-
)
|
| 32 |
if col_to_fit == "":
|
| 33 |
return (
|
| 34 |
empty_df,
|
|
@@ -44,71 +33,12 @@ def greet(
|
|
| 44 |
empty_df,
|
| 45 |
"Please upload a CSV file!",
|
| 46 |
)
|
| 47 |
-
niterations = int(niterations)
|
| 48 |
-
|
| 49 |
-
# Install Julia:
|
| 50 |
-
os.system(
|
| 51 |
-
"""if [ ! -d "/home/user/julia" ]; then
|
| 52 |
-
wget https://julialang-s3.julialang.org/bin/linux/x64/1.7/julia-1.7.3-linux-x86_64.tar.gz
|
| 53 |
-
tar zxvf julia-1.7.3-linux-x86_64.tar.gz
|
| 54 |
-
mkdir /home/user/julia
|
| 55 |
-
mv julia-1.7.3/* /home/user/julia/
|
| 56 |
-
fi""")
|
| 57 |
-
os.environ["PATH"] += ":/home/user/julia/bin/"
|
| 58 |
-
# Need to install PySR in separate python instance:
|
| 59 |
-
os.system(
|
| 60 |
-
"""if [ ! -d "/home/user/.julia/environments/pysr-0.9.3" ]; then
|
| 61 |
-
export PATH="$PATH:/home/user/julia/bin/"
|
| 62 |
-
python -c 'import pysr; pysr.install()'
|
| 63 |
-
fi"""
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
import pysr
|
| 67 |
-
try:
|
| 68 |
-
from julia.api import JuliaInfo
|
| 69 |
-
info = JuliaInfo.load(julia="/home/user/julia/bin/julia")
|
| 70 |
-
from julia import Main as _Main
|
| 71 |
-
pysr.sr.Main = _Main
|
| 72 |
-
except Exception as e:
|
| 73 |
-
error_message = tb.format_exc()
|
| 74 |
-
return (
|
| 75 |
-
empty_df,
|
| 76 |
-
error_message,
|
| 77 |
-
)
|
| 78 |
-
from pysr import PySRRegressor
|
| 79 |
-
|
| 80 |
-
df = pd.read_csv(file_obj.name)
|
| 81 |
-
y = np.array(df[col_to_fit])
|
| 82 |
-
X = df.drop([col_to_fit], axis=1)
|
| 83 |
-
|
| 84 |
-
model = PySRRegressor(
|
| 85 |
-
update=False,
|
| 86 |
-
temp_equation_file=True,
|
| 87 |
-
niterations=niterations,
|
| 88 |
-
binary_operators=binary_operators,
|
| 89 |
-
unary_operators=unary_operators,
|
| 90 |
-
)
|
| 91 |
-
try:
|
| 92 |
-
model.fit(X, y)
|
| 93 |
-
# Catch all error:
|
| 94 |
-
except Exception as e:
|
| 95 |
-
error_traceback = tb.format_exc()
|
| 96 |
-
if "CalledProcessError" in error_traceback:
|
| 97 |
-
return (
|
| 98 |
-
empty_df,
|
| 99 |
-
"Could not initialize Julia. Error message:\n"
|
| 100 |
-
+ error_traceback,
|
| 101 |
-
)
|
| 102 |
-
else:
|
| 103 |
-
return (
|
| 104 |
-
empty_df,
|
| 105 |
-
"Failed due to error:\n" + error_traceback,
|
| 106 |
-
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
df =
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
def main():
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import tempfile
|
|
|
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
empty_df = pd.DataFrame(
|
| 7 |
{
|
|
|
|
| 10 |
"complexity": [],
|
| 11 |
}
|
| 12 |
)
|
|
|
|
| 13 |
|
| 14 |
def greet(
|
| 15 |
file_obj: tempfile._TemporaryFileWrapper,
|
|
|
|
| 18 |
binary_operators: list,
|
| 19 |
unary_operators: list,
|
| 20 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if col_to_fit == "":
|
| 22 |
return (
|
| 23 |
empty_df,
|
|
|
|
| 33 |
empty_df,
|
| 34 |
"Please upload a CSV file!",
|
| 35 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
os.system("bash install_pysr.sh")
|
| 38 |
+
os.system(f"python run_pysr_and_save.py --niterations {niterations} --binary_operators '{binary_operators}' --unary_operators '{unary_operators}' --col_to_fit {col_to_fit} --filename {file_obj.name}")
|
| 39 |
+
df = pd.read_csv("pysr_output.csv")
|
| 40 |
+
error_log = open("error.log", "r").read()
|
| 41 |
+
return df, error_log
|
| 42 |
|
| 43 |
|
| 44 |
def main():
|
gui/install_pysr.sh
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# Install Julia:
|
| 4 |
+
if [ ! -f "/home/user/.local/bin/julia" ]; then
|
| 5 |
+
bash -ci "$(curl -fsSL https://raw.githubusercontent.com/abelsiqueira/jill/main/jill.sh)"
|
| 6 |
+
fi
|
| 7 |
+
|
| 8 |
+
# Need to install PySR in separate python instance:
|
| 9 |
+
if [ ! -d "/home/user/.julia/environments/pysr-0.9.3" ]; then
|
| 10 |
+
export PATH="$PATH:/home/user/julia/bin/"
|
| 11 |
+
python -c 'import pysr; pysr.install()'
|
| 12 |
+
fi
|
gui/run_pysr_and_save.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import traceback as tb
|
| 4 |
+
import numpy as np
|
| 5 |
+
from argparse import ArgumentParser
|
| 6 |
+
|
| 7 |
+
# Args:
|
| 8 |
+
# niterations
|
| 9 |
+
# binary_operators
|
| 10 |
+
# unary_operators
|
| 11 |
+
# col_to_fit
|
| 12 |
+
|
| 13 |
+
empty_df = pd.DataFrame(
|
| 14 |
+
{
|
| 15 |
+
"equation": [],
|
| 16 |
+
"loss": [],
|
| 17 |
+
"complexity": [],
|
| 18 |
+
}
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
if __name__ == "__main__":
|
| 22 |
+
parser = ArgumentParser()
|
| 23 |
+
parser.add_argument("niterations", type=int)
|
| 24 |
+
parser.add_argument("binary_operators", type=str)
|
| 25 |
+
parser.add_argument("unary_operators", type=str)
|
| 26 |
+
parser.add_argument("col_to_fit", type=str)
|
| 27 |
+
parser.add_argument("filename", type=str)
|
| 28 |
+
args = parser.parse_args()
|
| 29 |
+
niterations = args.niterations
|
| 30 |
+
binary_operators = eval(args.binary_operators)
|
| 31 |
+
unary_operators = eval(args.unary_operators)
|
| 32 |
+
col_to_fit = args.col_to_fit
|
| 33 |
+
filename = args.filename
|
| 34 |
+
|
| 35 |
+
os.environ["PATH"] += ":/home/user/.local/bin/"
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
import pysr
|
| 39 |
+
from julia.api import JuliaInfo
|
| 40 |
+
info = JuliaInfo.load(julia="/home/user/.local/bin/julia")
|
| 41 |
+
from julia import Main as _Main
|
| 42 |
+
pysr.sr.Main = _Main
|
| 43 |
+
|
| 44 |
+
from pysr import PySRRegressor
|
| 45 |
+
|
| 46 |
+
df = pd.read_csv(filename)
|
| 47 |
+
y = np.array(df[col_to_fit])
|
| 48 |
+
X = df.drop([col_to_fit], axis=1)
|
| 49 |
+
|
| 50 |
+
model = PySRRegressor(
|
| 51 |
+
update=False,
|
| 52 |
+
niterations=niterations,
|
| 53 |
+
binary_operators=binary_operators,
|
| 54 |
+
unary_operators=unary_operators,
|
| 55 |
+
)
|
| 56 |
+
model.fit(X, y)
|
| 57 |
+
|
| 58 |
+
df = model.equations_[["equation", "loss", "complexity"]]
|
| 59 |
+
# Convert all columns to string type:
|
| 60 |
+
df = df.astype(str)
|
| 61 |
+
df.to_csv("pysr_output.csv", index=False)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
error_message = tb.format_exc()
|
| 64 |
+
# Dump to file:
|
| 65 |
+
empty_df.to_csv("pysr_output.csv", index=False)
|
| 66 |
+
with open("error.log", "w") as f:
|
| 67 |
+
f.write(error_message)
|
| 68 |
+
|