From e0bbcdfced2c4e4cd1f5cdf3cd3e60531282c5e7 Mon Sep 17 00:00:00 2001 From: "Yuichiro Tachibana (Tsuchiya)" Date: Tue, 25 Apr 2023 16:42:41 +0200 Subject: [PATCH] Use Twilio STUN/TURN servers --- app_deepspeech.py | 30 ++++++++++++++++++++++++++++++ requirements.txt | 5 +++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app_deepspeech.py b/app_deepspeech.py index 605f9f3..2543f94 100644 --- a/app_deepspeech.py +++ b/app_deepspeech.py @@ -4,6 +4,7 @@ import threading import time import urllib.request +import os from collections import deque from pathlib import Path from typing import List @@ -12,6 +13,7 @@ import numpy as np import pydub import streamlit as st +from twilio.rest import Client from streamlit_webrtc import WebRtcMode, webrtc_streamer @@ -66,6 +68,34 @@ def download_file(url, download_to: Path, expected_size=None): progress_bar.empty() +# This code is based on https://github.com/whitphx/streamlit-webrtc/blob/c1fe3c783c9e8042ce0c95d789e833233fd82e74/sample_utils/turn.py +@st.cache_data # type: ignore +def get_ice_servers(): + """Use Twilio's TURN server because Streamlit Community Cloud has changed + its infrastructure and WebRTC connection cannot be established without TURN server now. # noqa: E501 + We considered Open Relay Project (https://www.metered.ca/tools/openrelay/) too, + but it is not stable and hardly works as some people reported like https://github.com/aiortc/aiortc/issues/832#issuecomment-1482420656 # noqa: E501 + See https://github.com/whitphx/streamlit-webrtc/issues/1213 + """ + + # Ref: https://www.twilio.com/docs/stun-turn/api + try: + account_sid = os.environ["TWILIO_ACCOUNT_SID"] + auth_token = os.environ["TWILIO_AUTH_TOKEN"] + except KeyError: + logger.warning( + "Twilio credentials are not set. Fallback to a free STUN server from Google." # noqa: E501 + ) + return [{"urls": ["stun:stun.l.google.com:19302"]}] + + client = Client(account_sid, auth_token) + + token = client.tokens.create() + + return token.ice_servers + + + def main(): st.header("Real Time Speech-to-Text") st.markdown( diff --git a/requirements.txt b/requirements.txt index 994eaad..dbbcb4f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,6 @@ av==9.2.0 deepspeech==0.9.3 numpy==1.23.0 pydub==0.25.1 -streamlit==1.10.0 -streamlit_webrtc==0.43.0 +streamlit==1.20.0 +streamlit_webrtc==0.45.0 +twilio~=8.1.0