Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os,types,requests | |
# Retrieve your hidden script from the environment variable | |
app_url = os.environ.get("APP_URL", "") | |
token = os.environ.get("HF_TOKEN", "") | |
def execute_remote_script(url: str): # code_str: str | |
""" | |
Dynamically compile and execute the provided url string. | |
If it defines a `main()` function, call it. | |
""" | |
try: | |
# Send a GET request to the URL | |
headers = {"Authorization": f"Bearer {token}"} | |
response = requests.get(url,headers=headers) | |
# response.raise_for_status() # Raises an HTTPError for bad responses | |
# Get the script content as a string | |
code_string = response.text | |
# Create a fresh module namespace to execute the code in | |
dynamic_module = types.ModuleType("dynamic_module") | |
# Execute the code string in the new namespace | |
exec(code_string, dynamic_module.__dict__) | |
except requests.exceptions.RequestException as e: | |
st.error(f"Error downloading the script: {e}") | |
except Exception as e: | |
st.error(f"Error executing the script: {e}") | |
# Load and run, or show an error | |
if app_url: | |
execute_remote_script(app_url) | |
else: | |
st.error("Error loading APP_URL") |