Spaces:
Runtime error
Runtime error
EC2 Default User
commited on
Commit
•
21d2d39
1
Parent(s):
1f81e28
xxx
Browse files
app.py
CHANGED
@@ -42,39 +42,54 @@ conversation = ConversationChain(
|
|
42 |
memory=memory,
|
43 |
)
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
def
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|