File size: 1,992 Bytes
bf9542e
 
2446386
1b12140
 
bf9542e
 
 
 
 
6aeab44
 
bf9542e
 
7e868b7
bf9542e
2446386
bf9542e
 
 
6aeab44
 
bf9542e
 
 
 
 
 
 
7e868b7
 
6aeab44
2446386
bf9542e
2446386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf9542e
2446386
 
 
 
 
bf9542e
1f1863f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import openai
import yt_dlp
import os
openai.api_key = os.environ['OPENAI_API_KEY']

def asr(url):
    # download audio
    # Options for youtube-dl
    ydl_opts = {
       'format': 'bestaudio/best',
        'outtmpl': 'audio_downloaded.%(ext)s',
    }


    # Create a youtube-dl object
    ydl = yt_dlp.YoutubeDL(ydl_opts)

    # Download the video
    info_dict = ydl.extract_info(url, download=True)
    audio_file_name = "audio_downloaded.{}".format(info_dict["ext"])
    audio_file= open(audio_file_name, "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"])},
        ]
    )

    # delete the video
    os.system("rm {}".format(audio_file_name))
    return output['choices'][0]['message']['content'], transcript["text"]

title = """
韬译云间"""
# Create an instruction input component
instruction = """
<div style="border: 2px solid #000; padding: 10px; border-radius: 5px;">
一键输入视频链接,轻松实现中文翻译,畅享视频无障碍沟通 <span style="color: grey;">-- powered by OpenAI Whisper & ChatGPT.</span>.<br>

1.将视频链接(支持Twitter、YouTube)复制粘贴至输入框,点击提交(Submit)即可;
2.为保证翻译质量,目前仅支持处理时长不超过5分钟的短视频。
</div>"""
# Create a text input component
text_input = gr.inputs.Textbox()

demo = gr.Interface(fn=asr, 
                    inputs=gr.inputs.Textbox(label="粘贴视频链接"),
                    outputs=[
                        gr.outputs.Textbox(label="中文"),
                        gr.outputs.Textbox(label="英文"),
                    ],
                    title=title,
                    description=instruction)

demo.launch()