Spaces:
Runtime error
Runtime error
Juan Ignacio Cuiule commited on
Commit ·
09efc0b
1
Parent(s): d9328cd
Update audio transcription code
Browse files- app.py +10 -10
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import numpy as np
|
| 4 |
from pydub import AudioSegment
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
audio = AudioSegment.from_mp3(audio_file)
|
| 9 |
segment_duration = 10 * 60 * 1000
|
| 10 |
-
num_segments = (len(audio) // segment_duration) + 1
|
| 11 |
|
| 12 |
segments = []
|
| 13 |
|
|
@@ -18,13 +19,11 @@ def transcribe_audio(audio_file):
|
|
| 18 |
segments.append(segment)
|
| 19 |
print(f"Segment {i}: {start_time} - {end_time} ({len(segment)} ms)")
|
| 20 |
|
| 21 |
-
model = whisper.load_model("base")
|
| 22 |
-
|
| 23 |
text = ""
|
| 24 |
|
| 25 |
for i, segment in enumerate(segments):
|
| 26 |
-
|
| 27 |
-
transcription =
|
| 28 |
text += transcription["text"] + " "
|
| 29 |
|
| 30 |
return text
|
|
@@ -32,9 +31,10 @@ def transcribe_audio(audio_file):
|
|
| 32 |
|
| 33 |
def main():
|
| 34 |
audio_input = gr.Audio(sources="upload", type="filepath")
|
|
|
|
| 35 |
output_text = gr.Textbox()
|
| 36 |
|
| 37 |
-
iface = gr.Interface(fn=transcribe_audio, inputs=audio_input,
|
| 38 |
outputs=output_text, title="Audio Transcription App",
|
| 39 |
description="Upload an audio file and hit the 'Submit'\
|
| 40 |
button")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
|
|
|
| 3 |
from pydub import AudioSegment
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
def transcribe_audio(audio_file, api_key):
|
| 7 |
+
client = OpenAI(api_key=api_key)
|
| 8 |
+
|
| 9 |
audio = AudioSegment.from_mp3(audio_file)
|
| 10 |
segment_duration = 10 * 60 * 1000
|
| 11 |
+
num_segments = 1 # (len(audio) // segment_duration) + 1
|
| 12 |
|
| 13 |
segments = []
|
| 14 |
|
|
|
|
| 19 |
segments.append(segment)
|
| 20 |
print(f"Segment {i}: {start_time} - {end_time} ({len(segment)} ms)")
|
| 21 |
|
|
|
|
|
|
|
| 22 |
text = ""
|
| 23 |
|
| 24 |
for i, segment in enumerate(segments):
|
| 25 |
+
audio_file = np.frombuffer(segment.raw_data, np.int16).flatten().astype(np.float32) / 32768.0
|
| 26 |
+
transcription = client.audio.transcriptions.create(file=audio_file, model="whisper-1")
|
| 27 |
text += transcription["text"] + " "
|
| 28 |
|
| 29 |
return text
|
|
|
|
| 31 |
|
| 32 |
def main():
|
| 33 |
audio_input = gr.Audio(sources="upload", type="filepath")
|
| 34 |
+
api_key = gr.Textbox(lines=1, label="OpenAI API Key")
|
| 35 |
output_text = gr.Textbox()
|
| 36 |
|
| 37 |
+
iface = gr.Interface(fn=transcribe_audio, inputs=[audio_input, api_key],
|
| 38 |
outputs=output_text, title="Audio Transcription App",
|
| 39 |
description="Upload an audio file and hit the 'Submit'\
|
| 40 |
button")
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
openai
|
| 2 |
pydub==0.25.1
|
| 3 |
numpy==1.23.5
|
| 4 |
jiwer==3.0.3
|
|
|
|
| 1 |
+
openai==1.3.9
|
| 2 |
pydub==0.25.1
|
| 3 |
numpy==1.23.5
|
| 4 |
jiwer==3.0.3
|