|
import gradio as gr |
|
import openai |
|
import youtube_dl |
|
import os |
|
openai.api_key = os.environ['OPENAI_API_KEY'] |
|
|
|
def asr(url): |
|
|
|
|
|
ydl_opts = { |
|
'outtmpl': 'my_video.mp4' |
|
} |
|
|
|
|
|
ydl = youtube_dl.YoutubeDL(ydl_opts) |
|
|
|
|
|
info_dict = ydl.extract_info(url, download=True) |
|
|
|
audio_file= open("my_video.mp4", "rb") |
|
transcript = openai.Audio.transcribe("whisper-1", audio_file) |
|
output = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=[ |
|
{"role": "user", "content": "Transcript: {transcript}. \n Translate the video conversation transcript into fluent Chinese. Chinese: ".format(transcript=transcript["text"])}, |
|
] |
|
) |
|
return transcript["text"], output['choices'][0]['message']['content'] |
|
|
|
demo = gr.Interface(fn=asr, inputs="text", |
|
outputs=[ |
|
gr.outputs.Textbox(label="English"), |
|
gr.outputs.Textbox(label="Chinese"), |
|
]) |
|
|
|
demo.launch() |