import gradio as gr import time import os, shutil, requests, zipfile, io, threading from huggingface_hub import HfApi SECRET_KEY = os.getenv('SECRET', None) def restart_space(): api = HfApi( token=os.getenv('HF_TOKEN') ) while True: time.sleep(60 * 60 * 3) print("Restarting space") api.restart_space(repo_id='mrfakename/sync_f5') restart_thread = threading.Thread(target=restart_space) restart_thread.daemon = True restart_thread.start() def refresh(token): if SECRET_KEY and token == SECRET_KEY: print("Clone repo") try: shutil.rmtree("repo") except: pass url = "https://github.com/SWivid/F5-TTS/archive/refs/heads/main.zip" response = requests.get(url) zip_file = io.BytesIO(response.content) repo_folder = "repo" os.makedirs(repo_folder) with zipfile.ZipFile(zip_file, 'r') as zip_ref: zip_ref.extractall(repo_folder) os.rename("repo/F5-TTS-main/src/f5_tts/infer/infer_gradio.py", "repo/F5-TTS-main/app.py") os.unlink("repo/F5-TTS-main/.gitignore") os.rename("repo/F5-TTS-main/README.md", "repo/F5-TTS-main/README_REPO.md") with open("repo/F5-TTS-main/README.md", "w") as f, open("README_TEMPLATE.md", "r") as template: f.write(template.read()) with open("repo/F5-TTS-main/requirements.txt", "w") as f, open("REQUIREMENTS_TEMPLATE.txt", "r") as template: f.write(template.read()) api = HfApi(token=os.getenv('HF_TOKEN')) print("Upload") api.upload_folder( folder_path="repo/F5-TTS-main", repo_id="mrfakename/E2-F5-TTS", repo_type="space", commit_message="Sync from GitHub repo", commit_description="This Space is synced from the GitHub repo: https://github.com/SWivid/F5-TTS. Please submit contributions to the Space there" ) try: shutil.rmtree("repo") except: pass return "Synced" else: raise gr.Error("Invalid token") with gr.Blocks() as demo: gr.Markdown("This Space is to sync the F5-TTS HF demo with the GitHub repo.") token = gr.Textbox(label="Password", info="Enter password to sync demo") btn = gr.Button("Refresh") out = gr.Markdown("Press Refresh to sync") btn.click(refresh, inputs=token, outputs=out) demo.queue().launch()