import os import subprocess import sys import logging import shutil import tempfile logging.basicConfig(level=logging.INFO) HF_TOKEN = os.environ.get("HF_TOKEN") REPO_URL = "https://huggingface.co/spaces/gthai/gthUTranslate" CLONE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "gthUTranslate") def setup_git_credentials(git_config_file, git_credentials_file): subprocess.run(["git", "config", "--file", git_config_file, "credential.helper", f"store --file={git_credentials_file}"], check=True) with open(git_credentials_file, "w") as f: f.write(f"https://api:{HF_TOKEN}@huggingface.co\n") def cleanup_git_credentials(git_config_file, git_credentials_file): if os.path.exists(git_credentials_file): os.remove(git_credentials_file) if os.path.exists(git_config_file): os.remove(git_config_file) def clone_repository(): logging.info(f"Token presence: {'Yes' if HF_TOKEN else 'No'}") if not HF_TOKEN: raise ValueError("Hugging Face token is not set. Please provide a valid token.") if os.path.exists(CLONE_DIR): logging.info(f"Directory {CLONE_DIR} already exists. Removing it.") shutil.rmtree(CLONE_DIR) with tempfile.TemporaryDirectory() as tmp_dir: tmp_config_path = os.path.join(tmp_dir, 'gitconfig') tmp_credentials_path = os.path.join(tmp_dir, 'git-credentials') try: # Format selon la documentation Hugging Face repo_url = f"https://huggingface.co/spaces/gthai/gthUTranslate" auth_repo_url = f"https://_:{HF_TOKEN}@huggingface.co/spaces/gthai/gthUTranslate" logging.info(f"Cloning repository...") result = subprocess.run( ["git", "clone", "-v", auth_repo_url, CLONE_DIR], check=True, capture_output=True, text=True ) logging.info("Clone successful") except subprocess.CalledProcessError as e: logging.error(f"Clone error: {e}") logging.error(f"Command error output: {e.stderr if e.stderr else 'No stderr'}") raise except Exception as e: logging.error(f"Unexpected error: {e}") raise finally: cleanup_git_credentials(tmp_config_path, tmp_credentials_path) def main(): clone_repository() # Ajouter le répertoire au chemin Python sys.path.append(CLONE_DIR) # Importer le demo après avoir configuré le chemin from gthUTranslate.app import main # Lancer l'application #main(server_name="0.0.0.0", server_port=7861) main() if __name__ == "__main__": main()