Spaces:
Sleeping
Sleeping
import pathlib | |
import modal | |
# Define container dependencies | |
image = ( | |
modal.Image.debian_slim() | |
.apt_install("git") | |
.pip_install("streamlit", "numpy", "pandas", "tensorflow", "transformers", "spotipy", "sentence_transformers") | |
) | |
stub = modal.Stub(name="streamlit_app.py", image=image) | |
# Define the run_streamlit function | |
def run_streamlit(publish_url: bool = False): | |
import streamlit.web.bootstrap | |
streamlit_script_remote_path = pathlib.Path("streamlit_app.py") | |
# Run the server. This function will not return until the server is shut down. | |
with modal.forward(8501) as tunnel: | |
# Reload Streamlit config with information about Modal tunnel address. | |
if publish_url: | |
stub.q.put(tunnel.url) | |
streamlit.web.bootstrap.run( | |
main_script_path=str(streamlit_script_remote_path), | |
command_line=None, | |
args=["--timeout", str(15 * 60)], # Adjust as needed | |
flag_options={}, | |
) | |
# Additional function for creating a web endpoint | |
def share(): | |
from fastapi.responses import RedirectResponse | |
run_streamlit.spawn(publish_url=True) | |
url = stub.q.get() | |
return RedirectResponse(url, status_code=303) | |
# Deploy the app | |
if __name__ == "__main__": | |
stub.deploy() | |