如何将上传的文件从 Flask 获取到我的 AI
从 Deepgram 导入 Deepgram 导入 asyncio、json、操作系统 从烧瓶导入烧瓶,渲染模板,请求,重定向
dg_key = '6a5db8f6e43c35769ddce7a3523c792179b69f5c' dg = Deepgram(dg_key)
MIMETYPE = 'mp3, wav'
目录='上传_文件夹'
选项={ “标点符号”:正确, “日记”:确实, “型号”:“一般”, “层”:“新星” }
定义主函数(): audio_folder = os.listdir(目录) 对于audio_folder中的audio_file: 如果audio_file.endswith(MIMETYPE): 将 open(f"{DIRECTORY}/{audio_file}", "rb") 作为 f: source = {"buffer": f, "mimetype":'audio/'+MIMETYPE} res = dg.transcription.sync_prerecorded(源,选项) 以 open(f"./{audio_file[:-4]}.json", "w") 作为记录: json.dump(res, 成绩单, 缩进=4) 返回
主要()
我的 flack 代码上的文件变量并设置为我的 ai 代码上的根
(披露:我在 Deepgram 工作,负责这些 SDK)
有几件事对我来说很突出。
这是直接从我们的 Python SDK 上的示例中提取的 - 我希望它有所帮助。注意事项;
nova-general
升级为 nova-2-general
punctuate
升级为 smart_format
,这是我们最新的格式化功能。import os
from dotenv import load_dotenv
import logging
from deepgram.utils import verboselogs
from datetime import datetime
import httpx
from deepgram import (
DeepgramClient,
DeepgramClientOptions,
PrerecordedOptions,
FileSource,
)
load_dotenv()
AUDIO_FILE = "/path/to/your/file/file.wav"
def main():
try:
deepgram: DeepgramClient = DeepgramClient()
with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()
payload: FileSource = {
"buffer": buffer_data,
}
options: PrerecordedOptions = PrerecordedOptions(
model="nova-2",
smart_format=True,
diarize=True,
)
response = deepgram.listen.prerecorded.v("1").transcribe_file(
payload, options, timeout=httpx.Timeout(300.0, connect=10.0)
)
print(response.to_json(indent=4))
except Exception as e:
print(f"Exception: {e}")
if __name__ == "__main__":
main()