EC2 Default User commited on
Commit
21d2d39
1 Parent(s): 1f81e28
Files changed (1) hide show
  1. app.py +69 -34
app.py CHANGED
@@ -42,39 +42,54 @@ conversation = ConversationChain(
42
  memory=memory,
43
  )
44
 
45
- def play_mp3(file_path):
46
- pygame.mixer.init()
47
- pygame.mixer.music.load(file_path)
48
- pygame.mixer.music.play()
49
-
50
- def play_mp3_audio(path):
51
- with open(path, 'rb') as f:
52
- audio_data = f.read()
53
- gr.Audio(audio_data)
54
-
55
- def play_wav_audio(wav_file):
56
- # open the wave file
57
- wf = wave.open(wav_file, 'rb')
58
-
59
- # instantiate PyAudio
60
- p = pyaudio.PyAudio()
61
-
62
- # open a stream
63
- stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
64
- channels=wf.getnchannels(),
65
- rate=wf.getframerate(),
66
- output=True)
67
-
68
- # read data from the wave file and play it
69
- data = wf.readframes(1024)
70
- while data:
71
- stream.write(data)
72
- data = wf.readframes(1024)
73
-
74
- # close the stream and terminate PyAudio
75
- stream.stop_stream()
76
- stream.close()
77
- p.terminate()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  def download_file(bucket_name, object_key, file_path):
80
  try:
@@ -151,7 +166,27 @@ def predict(input, history=[]):
151
  print("all historical responses: "+str(responses))
152
  return responses, audio_file, history
153
 
154
- def transcribe_func(audio):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  audio_file = open(audio, "rb")
156
  file_name = audio_file.name
157
  #file_directory = os.path.dirname(audio_file.name)
 
42
  memory=memory,
43
  )
44
 
45
+ AUDIO_PATH = ''
46
+ CHUNK_SIZE = 1024 * 8
47
+ REGION = "us-west-2"
48
+ transcript_text = ''
49
+ transcriptions = []
50
+
51
+ class MyEventHandler(TranscriptResultStreamHandler):
52
+ def __init__(self, transcript_result_stream):
53
+ super().__init__(transcript_result_stream)
54
+ self.transcriptions = []
55
+ async def handle_transcript_event(self, transcript_event: TranscriptEvent):
56
+ # This handler can be implemented to handle transcriptions as needed.
57
+ # Here's an example to get started.
58
+ results = transcript_event.transcript.results
59
+ for result in results:
60
+ for alt in result.alternatives:
61
+ print(alt.transcript)
62
+ transcriptions.append(alt.transcript)
63
+
64
+
65
+ async def basic_transcribe():
66
+ # Setup up our client with our chosen AWS region
67
+ client = TranscribeStreamingClient(region=REGION)
68
+
69
+ # Start transcription to generate our async stream
70
+ stream = await client.start_stream_transcription(
71
+ language_code="zh-CN",
72
+ media_sample_rate_hz=SAMPLE_RATE,
73
+ media_encoding="pcm",
74
+ )
75
+
76
+ async def write_chunks():
77
+ # NOTE: For pre-recorded files longer than 5 minutes, the sent audio
78
+ # chunks should be rate limited to match the realtime bitrate of the
79
+ # audio stream to avoid signing issues.
80
+ async with aiofile.AIOFile(AUDIO_PATH, "rb") as afp:
81
+ reader = aiofile.Reader(afp, chunk_size=CHUNK_SIZE)
82
+ await apply_realtime_delay(
83
+ stream, reader, BYTES_PER_SAMPLE, SAMPLE_RATE, CHANNEL_NUMS
84
+ )
85
+ await stream.input_stream.end_stream()
86
+
87
+ # Instantiate our handler and start processing events
88
+ handler = MyEventHandler(stream.output_stream)
89
+ await asyncio.gather(write_chunks(), handler.handle_events())
90
+ # Retrieve the transcriptions from the handler
91
+ #transcriptions = handler.transcriptions
92
+
93
 
94
  def download_file(bucket_name, object_key, file_path):
95
  try:
 
166
  print("all historical responses: "+str(responses))
167
  return responses, audio_file, history
168
 
169
+ def transcribe_func_new(audio):
170
+ audio_file = open(audio, "rb")
171
+ wav_file = audio_file.name
172
+ print("audio_file: "+wav_file)
173
+ #transcript = openai.Audio.transcribe("whisper-1", audio_file)
174
+ #return transcript['text']
175
+
176
+ pcm_file = os.path.splitext(wav_file)[0] + ".pcm"
177
+
178
+ wav_to_pcm(wav_file, pcm_file)
179
+ AUDIO_PATH=pcm_file
180
+
181
+ loop = asyncio.get_event_loop()
182
+ loop.run_until_complete(basic_transcribe())
183
+ loop.close()
184
+
185
+ transcript_text = transcriptions[-1]
186
+ print("final transcribe script: "+transcript_text)
187
+ return transcript_text
188
+
189
+ def transcribe_func_old(audio):
190
  audio_file = open(audio, "rb")
191
  file_name = audio_file.name
192
  #file_directory = os.path.dirname(audio_file.name)