|
from tkinter.filedialog import Open |
|
from openai import OpenAI |
|
|
|
import os |
|
|
|
import pydub |
|
import numpy as np |
|
|
|
|
|
OPENAI_CLIENT = OpenAI() |
|
|
|
|
|
|
|
sys_msg = [{ |
|
"role": "system", |
|
"content": "Hi, I'm a chatbot! Ask me a question." |
|
}] |
|
|
|
App_state = { |
|
"messages":[], |
|
"sys_msg":[{ |
|
"role": "system", |
|
"content": "Hi, I'm a chatbot! Ask me a question." |
|
}], |
|
"language":"en", |
|
|
|
"voice":"Joanna", |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_chat(text,messages=[]): |
|
''' |
|
send_chat function takes two arguments: text and messages |
|
|
|
text: the text that the user inputs |
|
messages: a list of tuples, each tuple contains two strings, the first string is the user input, the second string is the assistant output |
|
|
|
return: a tuple, the first element is an empty string, the second element is the updated messages list |
|
|
|
''' |
|
openai_messages = App_state["sys_msg"] |
|
|
|
for m in messages: |
|
openai_messages.extend([ |
|
{ |
|
"role": "user", |
|
"content": m[0] |
|
}, |
|
{ |
|
"role": "assistant", |
|
"content": m[1] |
|
} |
|
]) |
|
|
|
openai_messages.extend([ |
|
{ |
|
"role": "user", |
|
"content": text |
|
} |
|
]) |
|
|
|
res = OPENAI_CLIENT.chat.completions.create( |
|
model="gpt-4-turbo", |
|
messages=openai_messages |
|
) |
|
|
|
assistant_text = res.choices[0].message.content |
|
|
|
messages.append((text,assistant_text)) |
|
App_state.update({"messages":messages}) |
|
|
|
return "",messages |
|
|
|
def translate(file_path): |
|
if file_path: |
|
f = open(file_path,"rb") |
|
res = OPENAI_CLIENT.audio.translations.create( |
|
file=f, |
|
model="whisper-1") |
|
return res.text |
|
else: |
|
return "" |
|
|
|
def text_to_audio(chat_messages): |
|
text = chat_messages[0][-1] |
|
print("content:",text) |
|
print("type:",type(text)) |
|
response = OPENAI_CLIENT.audio.speech.create( |
|
model="tts-1", |
|
voice="nova", |
|
input=text, |
|
|
|
) |
|
|
|
response.stream_to_file("last_audio.mp3") |
|
print(response) |
|
|