| import gradio as gr |
| import openai |
| import os |
| openai.api_key= os.environ.get("openai.api_key") |
|
|
| messages = [{"role": "system", "content": 'You are helpful assistant'}] |
|
|
| def transcribe(audio): |
| global message |
| audio_filename_with_extension = audio + '.wav' |
| os.rename(audio, audio_filename_with_extension) |
| |
| audio_file = open(audio_filename_with_extension, "rb") |
| transcript = openai.Audio.transcriptions("whisper-1", audio_file) |
|
|
| messages.append({"role": "user", "content": transcript["text"]}) |
|
|
| response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) |
|
|
| system_message = response["choices"][0]["message"] |
| messages.append(system_message) |
|
|
| |
| |
|
|
| chat_transcript = "" |
| for message in messages: |
| if message['role'] != 'system': |
| chat_transcript += message['role'] + ": " + message['content'] + "\n\n" |
|
|
| return chat_transcript |
|
|
| ui = gr.Interface(fn=transcribe, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text").launch() |
| ui.launch() |