import gradio as gr
import os
import random
import json
import requests
import time
from openai import AzureOpenAI
# audio to text here
def audio_to_text(audio_path):
"""
audio to text here,目前是openai whisper
Parameters:
audio_path: str, 音频文件路径
Returns:
transcription.text: str, 音频转换的文本
"""
if audio_path == None or "":
return None
print(f"正在处理audio_path:{
audio_path}")
client = AzureOpenAI(
api_key='',
api_version = "",
azure_endpoint="https://speech-01.openai.azure.com/"
)
audio_file= open(audio_path, "rb")
transcription = client.audio.transcriptions.create(
model="whisper",
file=audio_file
)
print(transcription.text)
return transcription.text
def chat_completions(messages, gr_states, history):
"""
chat completion here,目前是kimi free api
Parameters:
messages: openai 格式 messages
Returns:
response: dict, openai chat api返回的结果
"""
if not messages:
return gr_states, history
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + "{your refresh token here}"
}
max_retry = 5
retry = 0
while retry < max_retry:
try:
retry += 1
response = requests.post(
url="{your free kimi api deploy url here}",
headers=headers,
data=json.dumps({
"model": "kimi",
"messages": messages,
"stream": False,
# "temperature": 0.8,
}),
)
print(response.json())
content = response.json(
Gradio——语音对话demo
最新推荐文章于 2025-04-06 10:06:49 发布