-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.py
197 lines (161 loc) · 5.64 KB
/
voice.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
import os
import pyaudio
import wave
import time
from datetime import datetime,timedelta
import whisper
import torch
from threading import Thread
from flask import Flask
app = Flask(__name__)
# Global variable to store the recording state
recording = False
waves = ""
status = False
start_time = 0
def listen():
# Initialize PyAudio
audio = pyaudio.PyAudio()
# Audio recording parameters
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
global recording
recording = True
if recording:
# Open an audio stream
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True, frames_per_buffer=CHUNK)
print('recording')
frames = []
while recording:
audio_data = stream.read(CHUNK)
frames.append(audio_data)
stream.stop_stream()
stream.close()
audio.terminate()
print('stopped Streaming')
t1 = time.time()
global waves
t = time.localtime()
current_time = time.strftime("%H_%M_%S", t)
today = str(datetime.now().date())
path = os.path.join(os.getcwd(), today)
print(path)
if os.path.exists(path):
print("Directory Available")
else:
os.mkdir(path)
print("Directory Created")
print("Saved Audio Path", path)
waves = str(current_time)+".wav"
audiosave = os.path.join(path,waves)
wave_file = wave.open(audiosave, 'wb')
# wave_file = wave.open(waves, 'wb')
wave_file.setnchannels(CHANNELS)
wave_file.setsampwidth(audio.get_sample_size(FORMAT))
wave_file.setframerate(RATE)
wave_file.writeframes(b''.join(frames))
t2 = time.time()
print("Time to Save : ", t2 - t1)
wave_file.close()
# Route to the index page
@app.route('/')
def index():
today = datetime.now().date()
print("Today",today)
return "Hello!!!!"
# Route to stop recording
@app.route('/stop')
def stop_recording():
global recording
recording = False
global status
status = False
time.sleep(0.1)
global waves
print("name of the File",waves)
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
t3 = time.time()
# Whisper Params
model = whisper.load_model("base",device=DEVICE)
try:
# SAVING RESULTS
today = str(datetime.now().date())
path = os.path.join(os.getcwd(), today)
print(path)
if os.path.exists(path):
print("Directory Available")
else:
os.mkdir(path)
print("Directory Created")
print("Saved Translation Path", path)
wav_file = os.path.join(path,waves)
result = model.transcribe('jfk.wav')
#result = model.transcribe(wav_file,word_timestamps=True)
t4 = time.time()
print("Time to Decode : ", t4 - t3)
file = waves+"_output.txt"
savin = os.path.join(path,file)
# with open(savin, 'w') as file:
# file.write(result["text"])
print(result["text"])
#Extracting Segment level TimeStamps
# Comment these 2 lines if mic is working
global start_time
start_time = datetime.now()
content = ''
contentlist = []
for i in range(len(result["segments"])):
localdict = {}
# print(result["segments"][i]["text"],start_time+timedelta(seconds=result["segments"][i]['start']),
# start_time+timedelta(seconds=result["segments"][i]['end']))
localdict["sentence"] = result["segments"][i]["text"]
sys_start = start_time+timedelta(seconds=result["segments"][i]['start'])
sys_end = start_time+timedelta(seconds=result["segments"][i]['end'])
localdict["system_start_time"] = str(sys_start)
localdict["system_end_time"] = str(sys_end)
localdict["UNIX_start_time"] = str(time.mktime(sys_start.timetuple()))
localdict["UNIX_end_time"] = str(time.mktime(sys_end.timetuple()))
content += "sentence: "+result["segments"][i]["text"] + " system_start_time: " + str(sys_start) + " system_end_time: " + \
str(sys_end) + " UNIX_start_time: " + str(time.mktime(sys_start.timetuple()))+" UNIX_start_time: "+str(time.mktime(sys_end.timetuple()))+"\n"
print(localdict)
contentlist.append(localdict)
with open(savin, 'w') as file:
file.write(content)
t5 = time.time()
print("Time to Save : ", t5 - t3)
except:
print("No Audio File Recorded")
return {"transcription":"---"}
return {"transcription":contentlist} #Returns content with time stamps
#return {"transcription":result["text"]} #Returns only content
# Route to handle the audio recording
@app.route('/record')
def record():
global status
if status:
print("Already Recording")
return {"status":"Already Recording"}
else:
thread = Thread(target=listen)
thread.daemon = True
thread.start()
status = True
global start_time
start_time = datetime.now()
print("Thread Started")
return {"status":"Recording"}
@app.route('/record_test')
def record_test():
try:
audioT = pyaudio.PyAudio()
# Open an audio stream
stream = audioT.open(format=pyaudio.paInt16, channels=1,
rate=44100, input=True, frames_per_buffer=1024)
return "Test!!! Audio Device Found"
except:
return "No Audio Device Found"
if __name__ == '__main__':
app.run(debug=True)