随着 AI 聊天的兴起,流式展现数据的需求变得更常见。前端 EventSource 的使用频率也会更高。接openai,文心一言,通义千问这些接口,并以流式的方式在页面展现出来。就得自己的接口服务端也以流式的方式返回给自己的前端,自己的服务器端接它们,让它们也要以流的方式返回。这个时候服务端不仅要做接口的对接和数据的转发,还得做数据的解析和存储。
这里前端以 vue3,后端使用 laravel 的方式,来简单介绍下怎么搞。
前端 API 选择
https://developer.mozilla.org/zh-CN/docs/Web/API/EventSource
前端选择 fetch,并没有选择 EventSource。
因为 fetch 本身就可以支持 EventSource 的方式接受数据,使用方式也会像使用 axios 类似。而单纯 EventSource 的使用会收到请求方式,不能自定义Header头,连接数目等方式的限制。fetch 就像在调用接口,EventSource 就像是 websocket。
EventSource 示例:
const sse = new EventSource("/api/v1/sse");
sse.addEventListener("notice", (e) => {
console.log(e.data);
});
sse.addEventListener("update", (e) => {
console.log(e.data);
});
sse.addEventListener("message", (e) => {
console.log(e.data);
});
文本生视频
https://github.com/harry0703/MoneyPrinterTurbo
部署比较简单,操作使用也很简单(网页操作)。 需要申请 pexels api keys
、通义千问 api keys
,当然也可以使用 openai 等。
效果不是很好,就是将 pexels 上的视频组合成一个视频。
训练一分钟内的的语音成为目标语言,文字转语音
https://www.bilibili.com/video/BV12g4y1m7Uw/?vd_source=6993ce0cd88c9947ac7681b6864a99d0
https://github.com/RVC-Boss/GPT-SoVITS?tab=readme-ov-file
这个还不错,更有用(达成目标)。
在自己的服务器环境使用语音转视频,需要配置 ice server。而这个玩意先得创建通信资源。创建通信资源,在后台就可以实现。ice server 可以使用编程语言去获取。
相关文档
代码
这里以 python 来获取。只需要准备好连接字符串(endpoint)就好,这个就是需要去后台创建通信资源。
# issue-relay-tokens.py
from azure.communication.networktraversal import CommunicationRelayClient
from azure.identity import DefaultAzureCredential
from azure.communication.identity import CommunicationIdentityClient
# You can find your endpoint and access token from your resource in the Azure Portal
connection_str = "endpoint=https://xxxx.communication.azure.com/;accesskey=xxxx"
endpoint = "https://xxxx.communication.azure.com/"
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
# We also need Identity client to get a User Identifier
# identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
# relay_client = CommunicationRelayClient(endpoint, DefaultAzureCredential())
#You can also authenticate using your connection string
identity_client = CommunicationIdentityClient.from_connection_string(connection_str)
relay_client = CommunicationRelayClient.from_connection_string(connection_str)
identity_client.create_user()
relay_configuration = relay_client.get_relay_configuration()
for iceServer in relay_configuration.ice_servers:
assert iceServer.username is not None
print('Username: ' + iceServer.username)
assert iceServer.credential is not None
print('Credential: ' + iceServer.credential)
assert iceServer.urls is not None
for url in iceServer.urls:
print('Url:' + url)
执行 python .\issue-relay-tokens.py
,如果配置都正确,就会得到 ice 相关信息。
Username: xxx1
Credential: cre1
Url:stun:relay.communication.microsoft.com:3478
Url:turn:relay.communication.microsoft.com:3478
Username: xxx2
Credential: cre2
Url:stun:20.202.255.225:3478
Url:turn:20.202.255.225:3478
转换截图
通过文字输入,输出视频+语音虚拟形象输出。可以尝试尝试。
使用微软 Speech Studio
样例
https://speech.microsoft.com/portal/talkingavatar
训练声音
https://speech.microsoft.com/portal/customvoice/overview
精简版训练
https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/custom-neural-voice-lite
申请授权
https://speech.microsoft.com/portal/customvoice/accessrequirement