binaryfarmer/Chatbot
0
1import openai2import pyttsx33 4openai.api_key = "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"5 6# Global variable to hold the chat history, initialise with system role7conversation = [8 {"role": "system", "content": "You are an intelligent professor."}9 ]10 11# transcribe function to record the audio input12 13def transcribe(audio):14 print(audio)15 16# Whisper API17 18 audio_file = open(audio, "rb")19 transcript = openai.Audio.transcribe("whisper-1", audio_file)20 21 22 print(transcript)23 24# ChatGPT API25 26# append user's inut to conversation27 conversation.append({"role": "user", "content": transcript["text"]})28 29 response = openai.ChatCompletion.create(30 model="gpt-3.5-turbo",31 messages=conversation32 )33 34 print(response)35 36# system_message is the response from ChatGPT API37 system_message = response["choices"][0]["message"]["content"]38 39# append ChatGPT response (assistant role) back to conversation40 conversation.append({"role": "assistant", "content": system_message})41 42 43# Text to speech44 engine = pyttsx3.init()45 engine.setProperty("rate", 150)46 engine.setProperty("voice", "english-us")47 engine.save_to_file(system_message, "response.mp3")48 engine.runAndWait()49 50 return "response.mp3"