File size: 1,252 Bytes
296130e
b096ccc
61201bb
a912b78
 
b096ccc
 
a912b78
296130e
a912b78
296130e
 
a912b78
 
a8c19e8
09b86d5
f05c097
a912b78
 
 
 
 
 
 
 
 
 
 
 
 
 
61201bb
296130e
a912b78
 
61201bb
28fd9d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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")