Skip to content

Commit

Permalink
Use Twilio STUN/TURN servers
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx committed Apr 25, 2023
1 parent 996f73d commit e0bbcdf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 30 additions & 0 deletions app_deepspeech.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e0bbcdf

Please sign in to comment.