import gradio as gr from openai import OpenAI import os client = OpenAI(api_key=os.environ["API_KEY"]) def play_tts(x: gr.LikeData): response = client.audio.speech.create( model="tts-1", voice="alloy", input=x.value ) response.stream_to_file('temp.mp3') f = gr.Audio('temp.mp3', streaming=True, elem_id='audio') print(f'"{x.value}" saved temp.mp3 file') return f def answer(state, state_chatbot, text): messages = state + [{ 'role': 'user', 'content': text }] res = client.chat.completions.create( model='gpt-4', messages=messages ) msg = res.choices[0].message.content new_state = [{ 'role': 'user', 'content': text }, { 'role': 'assistant', 'content': msg }] state = state + new_state state_chatbot = state_chatbot + [(text, msg)] print(state) return state, state_chatbot, state_chatbot with gr.Blocks() as demo: with gr.Row(): gr.HTML("""