MilesCranmer/PySR

[BUG]: Freezes after launching pysr in streamlit app

bahonya opened this issue · 3 comments

What happened?

I am currently trying to launch pysr from streamlit app and after running model.fit(x_train, y_train)
I see only this and nothing else:
Compiling Julia backend...
[ Info: Started!

running it just as a python script or in jupiter notebook works
I also trying calling pysr using ThreadPoolExecutor and same happens

Would appreciate any suggestions

Version

0.19.0

Operating System

macOS

Package Manager

Conda

Interface

Other (specify below)

Relevant log output

Compiling Julia backend...
[ Info: Started!

Extra Info

No response

Hi @bahonya,
Can you give a full code example so I can reproduce it? I am not familiar with streamlit.
Thanks,
Miles

Hi @MilesCranmer , thanks for the quick response

I got it, it happens because I change directory during the run, if I remove os.chdir in example below it works. I have to change the directory durung the run. I will look for a workaround. I think I will close this ticket then

`
import streamlit as st
import numpy as np
import pandas as pd
import os
from pysr import PySRRegressor

def generate_data(n_samples):
X = np.random.rand(n_samples, 1) * 10
y = 2 * X.squeeze() + np.random.randn(n_samples) * 2
return X, y
st.title("example app")
st.sidebar.header("parameters to generate data")
n_samples = st.sidebar.slider("number of samples", min_value=10, max_value=1000, value=100)
X, y = generate_data(n_samples)
st.subheader("generated data")
data = pd.DataFrame(data={'X': X.squeeze(), 'y': y})
st.write(data)
model = PySRRegressor(
populations=5,
niterations=100,
binary_operators=["+", "*"],
unary_operators=["sin", "cos"],
)
if st.button("Fit the model"):
os.chdir('subfolder')
model.fit(X, y)
os.chdir('..')
st.subheader("found equations")
st.write(model.equations_)
st.subheader("predictions")
y_pred = model.predict(X)
st.write("predicted values", y_pred)
import matplotlib.pyplot as plt
plt.scatter(X, y, label='ground truth', color='blue')
plt.scatter(X, y_pred, label='predicted values', color='red')
plt.legend()
st.pyplot(plt)
`

Cool!

If you are interested, maybe we could work together on a GUI? I have a start on this PR: #589. It's with gradio rather than streamlit but parts are similar. The goal is to have it be compatible with HuggingFace spaces so you can have web version of PySR.