Spaces:
Sleeping
Sleeping
import subprocess | |
import sys | |
try: | |
import openai | |
except ImportError: | |
subprocess.check_call([sys.executable, "-m", "pip", "install", "openai"]) | |
import openai # Import the library after installing it | |
def transcribe(filename,key): | |
client = openai.OpenAI(api_key = key) | |
audio_file = open(filename, "rb") | |
transcript_srt = client.audio.transcriptions.create( | |
model="whisper-1", | |
file=audio_file, | |
response_format="srt" | |
) | |
transcript_txt = client.audio.transcriptions.create( | |
model="whisper-1", | |
file=audio_file, | |
response_format="text" | |
) | |
return transcript_srt, transcript_txt | |
def setup_gradio_interface(): | |
with gr.Blocks() as demo: | |
gr.Markdown("## 音頻轉文字") | |
gr.Markdown("上傳您的 MP3 音頻文件,我們將返回 SRT 和 TXT 格式的字幕。") | |
with gr.Row(): | |
file_input = gr.File(label="Upload MP3 file") | |
api_key_input = gr.Textbox(label="Enter OpenAI API Key", placeholder="OpenAI API Key") | |
submit_button = gr.Button("Transcribe") | |
file_output_srt = gr.File(label="Download SRT") | |
file_output_txt = gr.File(label="Download TXT") | |
def transcribe_and_download(file,key): | |
if file is not None: | |
# Assuming `transcribe` function returns a tuple of (srt_content, txt_content) | |
srt_content, txt_content = transcribe(file,key) | |
srt_path = "output.srt" | |
txt_path = "output.txt" | |
with open(srt_path, "w") as srt_file: | |
srt_file.write(srt_content) | |
with open(txt_path, "w") as txt_file: | |
txt_file.write(txt_content) | |
return srt_path, txt_path | |
submit_button.click( | |
transcribe_and_download, | |
inputs=[file_input, api_key_input], | |
outputs=[file_output_srt, file_output_txt] | |
) | |
return demo | |
# First, try importing gradio. If it fails, attempt to install it. | |
try: | |
import gradio as gr | |
except ImportError: | |
import sys | |
import gradio as gr | |
# Run the interface | |
if __name__ == "__main__": | |
demo = setup_gradio_interface() | |
demo.launch() |