-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
223 lines (198 loc) · 6.65 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from flask import Flask, render_template, request, redirect, url_for, jsonify, send_file
import google.generativeai as genai
from google.cloud import translate_v2 as translate
import torch
import sounddevice as sd
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from scipy.io.wavfile import write
from google.cloud import texttospeech
import vertexai
from vertexai.generative_models import GenerativeModel, ChatSession
from dotenv import load_dotenv
import os
import time
import pygame
import base64
import json
load_dotenv()
GOOGLE_API_KEY=os.getenv("GOOGLE_API_KEY")
# model=genai.GenerativeModel('gemini-pro')
# genai.configure(api_key=GOOGLE_API_KEY)
# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.getenv("TRANSLATION_CREDENTIALS_PATH")
credpath=os.getenv("TRANSLATION_CREDENTIALS_PATH")
decode=base64.b64decode(os.getenv("TRANSLATION_CREDENTIALS_ENCODED"))
jsondecode=json.loads(decode)
with open(credpath,'wb') as file:
file.write(decode)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=credpath
project_id=os.getenv("CLOUD_PROJECT_ID")
location=os.getenv("CLOUD_PROJECT_LOCATION")
vertexai.init(project=project_id, location=location)
model=GenerativeModel(
model_name="gemini-1.0-pro-002",
system_instruction=[
"You are a human in a voice conversation with someone, keep the conversation going. Give a brief response of about 1-2 sentences in the same language that the input is given in. No emojis since this is verbal",
],
)
chat=model.start_chat(response_validation=False)
device="cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
model_id="openai/whisper-base"
openaimodel=AutoModelForSpeechSeq2Seq.from_pretrained(
model_id,torch_dtype=torch_dtype,
use_safetensors=True
)
openaimodel.to(device)
processor=AutoProcessor.from_pretrained(model_id)
pipe=pipeline(
"automatic-speech-recognition",
model=openaimodel,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
max_new_tokens=128,
chunk_length_s=30,
batch_size=16,
return_timestamps=True,
torch_dtype=torch_dtype,
device=device,
)
app = Flask(__name__)
app.config['chosenlang']='en' #this will change during execution
app.config['temp_msg']='Hello, how are you today?' #this too
app.config['transpath']='session/trans.txt'
@app.route('/')
def index():
return render_template('lang_select.html')
@app.route('/process_language',methods=['POST'])
def process_language():
lang=request.form['language']
app.config['chosenlang']=lang
return redirect(url_for('conversation',language=lang))
@app.route('/conversation/<language>',methods=['GET'])
def conversation(language):
cleartranscript()
pygame.init()
return render_template('microphone.html',language=displaylang(language))
@app.route('/process_human',methods=['POST'])
def process_intermediate():
#human_response=request.form['human_input']
human_response=human_turn()
app.config['temp_msg']=human_response
return jsonify(human_response=human_response)
@app.route('/process_machine',methods=['POST'])
def process_machine():
machinput=app.config['temp_msg']
machine_response=machine_turn(machinput)
print('machine response: ',machine_response)
return jsonify(machine_response=machine_response)
@app.route('/machine_speak')
def machine_speak():
mf='recordings/machine.wav'
play_audio(mf)
return 'Audio playing'
@app.route('/session/trans.txt')
def download_transcript():
tpath=app.config['transpath']
if os.path.exists(tpath):
return send_file(tpath,attachment_filename='transcript.txt',as_attachment=True)
@app.route('/stop_session')
def stop_session():
pygame.quit()
return 'Pygame session stopped'
def get_chat_response(chat: ChatSession,prompt: str) -> str:
text_response=[]
print('prompt: '+prompt)
responses=chat.send_message(prompt,stream=True)
print('responses: ',responses,type(responses))
for chunk in responses:
text_response.append(chunk.text)
return "".join(text_response)
def generate_response(input_text):
print('chat: ',chat,type(chat))
generation=get_chat_response(chat,input_text)
#print(generation)
return generation
def machine_turn(text):
machine_file='recordings/machine.wav'
talk=generate_response(text)
updatetrans(talk,False)
make_speech_file(machine_file,talk)
#eng=translate_to(english,machine_file)
#play_audio(machine_file)
# print('Computer: '+talk+' ('+eng+')')
return talk
def make_speech_file(speech_file_path,text):
googclient=texttospeech.TextToSpeechClient()
synthesis_input=texttospeech.SynthesisInput(text=text)
voice=texttospeech.VoiceSelectionParams(
language_code=app.config['chosenlang'],ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
audio_config=texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response=googclient.synthesize_speech(
input=synthesis_input,voice=voice,audio_config=audio_config
)
with open(speech_file_path,"wb") as out:
out.write(response.audio_content)
print('Audio content written to '+speech_file_path)
def play_audio(file):
sound=pygame.mixer.Sound(file)
recordlength=int(sound.get_length()*1000)
sound.play()
pygame.time.wait(recordlength)
def human_turn():
file='recordings/human.wav'
talk=record_audio(file)
updatetrans(talk,True)
print('human response: ',talk)
# eng=translate_to(english,file)
# print('Me: '+talk+' ('+eng+')')
return talk
def record_audio(filename,duration=5,fs=44100):
print('recording...')
recording=sd.rec(int(duration*fs),samplerate=fs,channels=1)
sd.wait()
write(filename,fs,recording)
result=pipe(filename,generate_kwargs={'language':app.config['chosenlang']})
return result['text']
def updatetrans(line,isme):
pre=''
if isme:
pre='Me: '
else:
pre='Machine: '
with open(app.config['transpath'],'a') as file:
file.write(pre+line+'\n'+'\n')
print('Updated transcript file')
def cleartranscript():
open(app.config['transpath'],'w').close()
def displaylang(l):
c=0
if l=='en':
c='English'
elif l=='es':
c='Spanish'
elif l=='fr':
c='French'
elif l=='zh':
c='Chinese (Mandarin)'
elif l=='ru':
c='Russian'
elif l=='ar':
c='Arabic'
elif l=='pt':
c='Portuguese'
elif l=='ja':
c='Japanese'
elif l=='de':
c='German'
elif l=='ko':
c='Korean'
elif l=='th':
c='Thai'
elif l=='hi':
c='Hindi'
return c
if __name__ == '__main__':
app.run(debug=True)