Kim Adams commited on
Commit
35a9399
·
1 Parent(s): b53e4f2

voices added

Browse files
app.py CHANGED
@@ -3,19 +3,48 @@ import openai
3
  import pandas as pd
4
  import numpy as np
5
  import gradio as gr
6
- import openai, subprocess, os
7
  import requests, base64
8
  from io import BytesIO
9
  from huggingface_hub import Repository
10
  from utilities import api_keys, prompts
11
  from create_embedding import QueryEmbeddings
12
 
 
 
 
 
13
  openai.api_key = api_keys.APIKeys().get_key('OPENAI_API_KEY')
 
 
 
 
 
 
 
 
14
 
 
15
  messages = [ {"role": "system", "content": "**hello**." }]
16
 
17
- def transcribe(audio):
18
- global messages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  audio_filename_with_extension = audio + '.wav'
20
  os.rename(audio, audio_filename_with_extension)
21
 
@@ -30,18 +59,41 @@ def transcribe(audio):
30
 
31
  response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
32
  system_message = response["choices"][0]["message"]
33
- messages.append(system_message)
34
-
35
- subprocess.call(["say", system_message['content']])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- '''chat_transcript = ""
38
- for message in messages:
39
- if message['role'] != 'system':
40
- chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
41
- print(chat_transcript)'''
42
 
43
- # return chat_transcript
44
- return system_message['content']
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- ui = gr.Interface(title="USAA Benefit Advisor", fn=transcribe, inputs=gr.Audio(label="Question", source="microphone", type="filepath"), outputs=gr.Textbox(label='Response')).launch()
47
- ui.launch()
 
3
  import pandas as pd
4
  import numpy as np
5
  import gradio as gr
6
+ import openai, os, re
7
  import requests, base64
8
  from io import BytesIO
9
  from huggingface_hub import Repository
10
  from utilities import api_keys, prompts
11
  from create_embedding import QueryEmbeddings
12
 
13
+ import pkg_resources
14
+ pyttsx3_version = pkg_resources.get_distribution("pyttsx3").version
15
+ print(f"pyttsx3version: {pyttsx3_version}")
16
+
17
  openai.api_key = api_keys.APIKeys().get_key('OPENAI_API_KEY')
18
+ eleven_api_key = api_keys.APIKeys().get_key('ELEVEN_LABS_API_KEY')
19
+ voice_id1 = api_keys.APIKeys().get_key('VOICE_ID1')
20
+ voice_id2 = api_keys.APIKeys().get_key('VOICE_ID2')
21
+ voice_id3 = api_keys.APIKeys().get_key('VOICE_ID3')
22
+ voice_id4 = api_keys.APIKeys().get_key('VOICE_ID4')
23
+ voice_id5 = api_keys.APIKeys().get_key('VOICE_ID5')
24
+ voice_id6 = api_keys.APIKeys().get_key('VOICE_ID6')
25
+ voice_id7 = api_keys.APIKeys().get_key('VOICE_ID7')
26
 
27
+ voice_id = voice_id1
28
  messages = [ {"role": "system", "content": "**hello**." }]
29
 
30
+ def Transcribe(audio, voice):
31
+ global messages, voice_id
32
+ print ("voice: " + voice)
33
+ if voice=="Voice 1":
34
+ voice_id = voice_id1
35
+ elif voice=="Voice 2":
36
+ voice_id=voice_id2
37
+ elif voice=="Voice 3":
38
+ voice_id=voice_id3
39
+ elif voice=="Voice 4":
40
+ voice_id=voice_id4
41
+ elif voice=="Voice 5":
42
+ voice_id=voice_id5
43
+ elif voice=="Voice 6":
44
+ voice_id=voice_id6
45
+ else:
46
+ voice_id=voice_id7
47
+
48
  audio_filename_with_extension = audio + '.wav'
49
  os.rename(audio, audio_filename_with_extension)
50
 
 
59
 
60
  response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
61
  system_message = response["choices"][0]["message"]
62
+ # messages.append(system_message)
63
+
64
+ # text to speech request with eleven labs
65
+ url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream"
66
+ data = {
67
+ "text": system_message["content"].replace('"', '').replace('401k', '4 oh 1 k'),
68
+ "voice_settings": {
69
+ "stability": 0.95,
70
+ "similarity_boost": 0.93
71
+ }
72
+ }
73
+ r = requests.post(url, headers={'xi-api-key':eleven_api_key}, json=data)
74
+ r_audio =r.content
75
+ audio_io = BytesIO(r_audio)
76
+ audio_io.seek(0)
77
+ audio_base64 = base64.b64encode(audio_io.read()).decode("utf-8")
78
+ audio_html = f'<audio src="data:audio/aac;base64,{audio_base64}" controls autoplay playsinline></audio>'
79
+ return system_message['content'], audio_html
80
 
81
+ #ui = gr.Interface(title="USAA Benefit Advisor", fn=transcribe, inputs=gr.Audio(label="Question", source="microphone", type="filepath"), outputs=[gr.Textbox(label='Response'),gr.HTML()]).launch()
82
+ #ui.launch()
 
 
 
83
 
84
+ '''comp = gr.Interface(
85
+ Transcribe,
86
+ title="USAA Benefit Advisor",
87
+ inputs=[gr.Audio(label="Question", source="microphone", type="filepath")],
88
+ outputs=[gr.Textbox(label='Response'),gr.HTML()])
89
+ '''
90
+ comp = gr.Interface(
91
+ Transcribe,
92
+ title="USAA Benefit Advisor",
93
+ inputs=[gr.Audio(label="Question", source="microphone", type="filepath"),
94
+ gr.Radio(["Voice 1", "Voice 2", "Voice 3","Voice 4", "Voice 5", "Voice 6", "Voice 7"], value="Voice 1", label="Change voice", info="Which voice would you like to use?")],
95
+ outputs=[gr.Textbox(label='Response'),
96
+ gr.HTML()])
97
 
98
+ #demo = gr.Parallel(comp, ui1, title="USAA Benefit Advisor")
99
+ comp.launch()
text.mp3 ADDED
Binary file (245 kB). View file
 
utilities/__pycache__/prompts.cpython-311.pyc CHANGED
Binary files a/utilities/__pycache__/prompts.cpython-311.pyc and b/utilities/__pycache__/prompts.cpython-311.pyc differ
 
utilities/prompts.py CHANGED
@@ -1 +1 @@
1
- ADVISOR_CUSTOM_PROMPT= "You are an HR benefits advisor for USAA. Don't say your an AI language model. Provide answers with kindness and respect for military personnel and families. Be professional and warm."
 
1
+ ADVISOR_CUSTOM_PROMPT= "You are an HR benefits advisor for USAA. Don't say your an AI language model. Provide answers with kindness and respect for military personnel and families. Be professional and warm, keep answers short and sweet."