Whisper_AI / run.py
TANMAY-555's picture
Upload folder using huggingface_hub
c85835e verified
Raw
History Blame Contribute Delete
2.01 kB
"""Launches the Whisper Transcribe app behind an ngrok tunnel."""
import os
import sys
import threading
os.environ.setdefault("WHISPER_MODEL_SIZE", "large-v3")
os.environ.setdefault("HOST", "0.0.0.0")
os.environ.setdefault("PORT", "5000")
from backend import config # noqa: E402
from backend.app import app, logger # noqa: E402
import backend.transcriber as T # noqa: E402
from pyngrok import ngrok # noqa: E402
def _preload() -> None:
T.cleanup_outputs()
print(f"Pre-loading '{config.MODEL_SIZE}' model (first run downloads weights ~1-3 GB)…")
try:
T.get_model(config.MODEL_SIZE)
print("Model ready.\n")
except Exception as exc:
logger.warning("Model pre-load failed: %s — will retry on first request.", exc)
def main() -> None:
flask_thread = threading.Thread(
target=lambda: app.run(host=config.HOST, port=config.PORT, threaded=True),
daemon=True,
)
flask_thread.start()
token = os.environ.get("NGROK_AUTHTOKEN", "").strip()
if token:
ngrok.set_auth_token(token)
else:
print(
"WARNING: NGROK_AUTHTOKEN is not set — falling back to ngrok anonymous tier, "
"which may be rate-limited."
)
try:
public_url = ngrok.connect(config.PORT).public_url
except Exception as exc:
print("Could not open an ngrok tunnel:", exc)
print("Set NGROK_AUTHTOKEN in .env and run again.")
raise
print("\n" + "=" * 64)
print(f" Whisper Transcribe is live → {public_url}")
print("=" * 64 + "\n")
# Pre-load in background so the URL is immediately usable
threading.Thread(target=_preload, daemon=True).start()
flask_thread.join()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nShutting down.")
except Exception:
logger.exception("Fatal error while starting the app")
sys.exit(1)