FranklinWillemen commited on
Commit
10e7b48
1 Parent(s): 586b46c

refactor branch initial commit

Browse files
Files changed (1) hide show
  1. app.py +22 -32
app.py CHANGED
@@ -11,49 +11,39 @@ speech_config = speechsdk.SpeechConfig(subscription=os.environ['AZURE_SPEECH_KEY
11
  speech_config.speech_synthesis_voice_name = "nl-NL-ColetteNeural"
12
  speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
13
 
14
- messages = [{"role": "system", "content": 'You are a clever and helpfull conversationalist. Respond to all input in 25 words or less. Do not mention that you are an AI or service of any kind.'}]
15
 
16
- def transcribe(audio: str):
17
- global messages
18
-
19
- #audio_filename_with_extension = audio + '.wav'
20
- #os.rename(audio, audio_filename_with_extension)
21
-
22
  audio_file = open(audio, "rb")
23
- transcript = openai.Audio.transcribe("whisper-1", audio_file)
24
-
25
- messages.append({"role": "user", "content": transcript["text"]})
26
-
27
- response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
28
 
29
- system_message = response["choices"][0]["message"]
30
- messages.append(system_message)
 
31
 
32
- # engine = pyttsx3.init()
33
- # engine.say(system_message['content'])
34
- # engine.runAndWait()
 
35
 
36
- voice_reponse = speech_synthesizer.speak_text_async(system_message['content']).get()
37
- stream = speechsdk.AudioDataStream(voice_reponse)
38
- stream.save_to_wav_file("file.wav")
 
39
 
40
- # Checks result.
41
- # if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
42
- # print("Speech synthesized to speaker for text [{}]".format(text))
43
- # elif result.reason == speechsdk.ResultReason.Canceled:
44
- # cancellation_details = result.cancellation_details
45
- # print("Speech synthesis canceled: {}".format(cancellation_details.reason))
46
- # if cancellation_details.reason == speechsdk.CancellationReason.Error:
47
- # if cancellation_details.error_details:
48
- # print("Error details: {}".format(cancellation_details.error_details))
49
- # print("Did you update the subscription info?")
50
 
51
  chat_transcript = ""
52
- for message in messages:
53
  if message['role'] != 'system':
54
  chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
55
 
56
- return chat_transcript, "file.wav"
57
 
58
  # set a custom theme
59
  theme = gr.themes.Default().set(
 
11
  speech_config.speech_synthesis_voice_name = "nl-NL-ColetteNeural"
12
  speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
13
 
14
+ context = [{"role": "system", "content": 'You are a clever and helpfull conversationalist. Respond to all input in 25 words or less. Do not mention that you are an AI or service of any kind.'}]
15
 
16
+ def transcribe(model: str, audio: str):
 
 
 
 
 
17
  audio_file = open(audio, "rb")
18
+ transcript = openai.Audio.transcribe(model, audio_file)
19
+ return transcript
 
 
 
20
 
21
+ def gen_response(model: str, context: list):
22
+ response = openai.ChatCompletion.create(model=model, messages=context)
23
+ return response["choices"][0]["message"]
24
 
25
+ def gen_voice(response, response_filename):
26
+ reponse_audio = speech_synthesizer.speak_text_async(response['content']).get()
27
+ stream = speechsdk.AudioDataStream(reponse_audio)
28
+ stream.save_to_wav_file(response_filename)
29
 
30
+ def conversation(audio:str):
31
+ audio_file = open(audio, "rb")
32
+ transcript = transcribe("whisper-1", audio_file)
33
+ context.append({"role": "user", "content": transcript['text']})
34
 
35
+ response = gen_response("gpt-3.5-turbo", context)
36
+ system_message = response["choices"][0]["message"]
37
+ context.append(system_message)
38
+
39
+ gen_voice(system_message, "voice.wav")
 
 
 
 
 
40
 
41
  chat_transcript = ""
42
+ for message in context:
43
  if message['role'] != 'system':
44
  chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
45
 
46
+ return chat_transcript, "voice.wav"
47
 
48
  # set a custom theme
49
  theme = gr.themes.Default().set(