问题描述
我正在尝试制作一个可以使用GCP和Twilio实时转录电话的API。我购买了twilio数字,还使用Flask,Ngrok和Websockets编译服务器代码,公开本地端口并传输数据,TwiML动词“ Stream”用于将音频数据流式传输到Google Cloud Speech-Text API 。我正在使用github上可用的Twilio的python演示 链接:https://github.com/twilio/media-streams/tree/master/python/realtime-transcriptions
在twilio号码和个人号码之间建立了联系,但未发生抄录。建立连接后,我将得到输出“ POST TwiML”,此后无响应。
服务器代码:
from flask import Flask,render_template
from flask_sockets import Sockets
from SpeechClientBridge import SpeechClientBridge
from google.cloud.speech import enums
from google.cloud.speech import types
import os
import json
import base64
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] ="<KEY>.json"
HTTP_SERVER_PORT = 8080
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.MULAW,sample_rate_hertz=8000,model="phone_call",language_code='en-US')
streaming_config = types.StreamingRecognitionConfig(
config=config,interim_results=True)
app = Flask(__name__)
sockets = Sockets(app)
@app.route('/twiml',methods=['POST'])
def return_twiml():
print("POST TwiML")
return render_template('streams.xml')
def on_transcription_response(response):
if not response.results:
return
result = response.results[0]
if not result.alternatives:
return
transcription = result.alternatives[0].transcript
print("Transcription: " + transcription)
@sockets.route('/')
def transcript(ws):
print("WS connection opened")
bridge = SpeechClientBridge(
streaming_config,on_transcription_response
)
while not ws.closed:
message = ws.receive()
if message is None:
bridge.terminate()
break
data = json.loads(message)
if data["event"] in ("connected","start"):
print(f"Media WS: Received event '{data['event']}': {message}")
continue
if data["event"] == "media":
media = data["media"]
chunk = base64.b64decode(media["payload"])
bridge.add_request(chunk)
if data["event"] == "stop":
print(f"Media WS: Received event 'stop': {message}")
print("Stopping...")
break
bridge.terminate()
print("WS connection closed")
if __name__ == '__main__':
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WsgiServer(('',HTTP_SERVER_PORT),app,handler_class=WebSocketHandler)
print("Server listening on: http://localhost:" + str(HTTP_SERVER_PORT))
server.serve_forever()
streams.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say> I will stream for 60 seconds!</Say>
<Start>
<Stream url="wss://https://<ngrok-URL>.ngrok.io/"/>
</Start>
<Pause length="60"/>
</Response>
Twilio WebHook:
http://<ngrok-URL>.ngrok.io/twiml
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)