semrush_scraper / app.py
limitedonly41's picture
Update app.py
16fb0f1 verified
import os
from huggingface_hub import hf_hub_download
import importlib.util
import sys
# Get HF token from environment (set in Space secrets)
HF_TOKEN = os.environ.get("HF_TOKEN")
# Your private model/repo details
REPO_ID = "limitedonly41/cv_all_src"
FILENAME = "semrush_scraper_code.py"
def load_and_run():
try:
# Download the code file from private repo
file_path = hf_hub_download(
repo_id=REPO_ID,
filename=FILENAME,
token=HF_TOKEN,
repo_type="model"
)
# Load the module dynamically
spec = importlib.util.spec_from_file_location("semrush_scraper_module", file_path)
module = importlib.util.module_from_spec(spec)
sys.modules["semrush_scraper_module"] = module
spec.loader.exec_module(module)
# Try to find and launch the interface
if hasattr(module, 'interface'):
print("Found 'interface' object, launching...")
module.interface.launch()
elif hasattr(module, 'demo'):
print("Found 'demo' object, launching...")
module.demo.launch()
elif hasattr(module, 'create_interface'):
print("Found 'create_interface' function, creating and launching...")
interface = module.create_interface()
interface.launch()
else:
print("Error: Could not find 'interface', 'demo', or 'create_interface' in the loaded module")
print("Available attributes:", dir(module))
except Exception as e:
print(f"Error loading code: {e}")
import traceback
traceback.print_exc()
print("\nMake sure:")
print("1. HF_TOKEN is set in Space secrets")
print("2. REPO_ID points to your private repository")
print("3. The code file exists in the repository")
if __name__ == "__main__":
load_and_run()