text2audio / src /app.py
myyim's picture
Update src/app.py
ba50aa9 verified
raw
history blame
1.51 kB
import streamlit as st
import os,types,requests
from huggingface_hub import login
# Retrieve your hidden script from the environment variable
app_url = os.environ.get("APP_URL", "")
token = os.environ.get("HF_TOKEN", "")
# Pass the token as a string
login(token=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
# st.write(code_string)
# Create a fresh module namespace to execute the code in
dynamic_module = types.ModuleType("dynamic_module")
st.write('Here:')
st.write(dynamic_module)
# Execute the code string in the new namespace
exec(code_string, dynamic_module.__dict__)
st.write('DONE')
if hasattr(module,"main"):
module.main()
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")