CHATGPT文字转语音回答


```python
import openai
import time
import pyttsx3

# 初始化语音合成引擎
engine = pyttsx3.init()

# 设置要使用的声音
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)  # 这里使用第二个声音
openai.api_key = ""
model = "text-davinci-003"


def generate_text(prompt, model_engine=model, max_tokens=1024, n=1, stop=None, temperature=0.1):
    completions = openai.Completion.create(engine=model_engine, prompt=prompt, max_tokens=max_tokens, n=n, stop=stop,
                                           temperature=temperature, )

    message = completions.choices[0].text.strip()
    return message


a = time.time()
message = generate_text("说出著名的医学期刊3个")
b = time.time()

print(message, )
engine.say(message)
engine.runAndWait()
print(f"Time taken: {b - a:.2f} seconds")

换自己的APi就行

你可能感兴趣的:(web,chatgpt,python,语音识别)