Spaces:
Runtime error
Runtime error
Download app.py from juancuiule/podcast-transcript: direct link, hf CLI and curl.
- Browser
- Download file 1.43 kB
-
https://huggingface.co/spaces/juancuiule/podcast-transcript/resolve/main/app.py
- Command line
-
hf download hf://spaces/juancuiule/podcast-transcript/app.py
-
curl -L -o app.py https://huggingface.co/spaces/juancuiule/podcast-transcript/resolve/main/app.py
1.43 kB
| import gradio as gr | |
| from openai import OpenAI | |
| from pydub import AudioSegment | |
| import numpy as np | |
| def transcribe_audio(audio_file, api_key): | |
| client = OpenAI(api_key=api_key) | |
| audio = AudioSegment.from_mp3(audio_file) | |
| segment_duration = 10 * 60 * 1000 | |
| num_segments = (len(audio) // segment_duration) + 1 | |
| segments = [] | |
| for i in range(num_segments): | |
| start_time = i * segment_duration | |
| end_time = (i + 1) * segment_duration | |
| segment = audio[start_time:end_time] | |
| segments.append(segment) | |
| print(f"Segment {i}: {start_time} - {end_time} ({len(segment)} ms)") | |
| text = "" | |
| for i, segment in enumerate(segments): | |
| segment.export('./segment.mp3', format="mp3") | |
| audio = open("./segment.mp3", 'rb') | |
| transcription = client.audio.transcriptions.create(file=audio, language='es', model="whisper-1") | |
| text += transcription.text + " " | |
| return text | |
| def main(): | |
| audio_input = gr.Audio(sources="upload", type="filepath") | |
| api_key = gr.Textbox(lines=1, label="OpenAI API Key") | |
| output_text = gr.Textbox() | |
| iface = gr.Interface(fn=transcribe_audio, inputs=[audio_input, api_key], | |
| outputs=output_text, title="Audio Transcription App", | |
| description="Upload an audio file and hit the 'Submit'\ | |
| button") | |
| iface.launch() | |
| if __name__ == '__main__': | |
| main() |