File size: 1,958 Bytes
aa9cce2
07d3e9f
 
cd864c2
aa9cce2
5ee7f09
 
 
6ad5c5c
 
 
 
 
aa9cce2
07d3e9f
 
 
 
 
 
 
 
 
 
ffba281
 
07d3e9f
 
 
 
 
 
 
cd864c2
 
 
 
 
 
ffba281
 
b3c270b
5ee7f09
 
7e6eec7
67a77d9
6ad5c5c
 
5ee7f09
 
 
 
6ad5c5c
07d3e9f
 
 
 
5ee7f09
07d3e9f
5ee7f09
ffba281
07d3e9f
5ee7f09
 
 
 
 
c408f46
07d3e9f
 
 
 
5ee7f09
 
 
 
 
 
07d3e9f
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import yt_dlp
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
)

opnerouter_client =  OpenAI(
  base_url="https://openrouter.ai/api/v1",
  api_key=os.getenv("OPENROUTER_API_KEY"),
)

ydl_opts = {
    'outtmpl': 'demo.m4a',
    'format': 'm4a/bestaudio/best',
    'postprocessors': [{  # Extract audio using ffmpeg
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'm4a',
    }],
    # 'proxy': 'socks5://192.168.2.18:20170',
}

text = ''

def download_audio(url):
    if os.path.exists('demo.m4a'):
        os.remove('demo.m4a')
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        code = ydl.download([url])
    assert code == 0, "Failed to download audio"

    with open("demo.m4a", "rb") as f:
        transcription = client.audio.transcriptions.create(
            model="whisper-1", 
            file=f, 
            response_format="text"
        )
    global text
    text = transcription
    return transcription

def summarize_text():
    prompt = f"Please summarize the following article in 5 sentences or less use the language of the audio: [{text}]"
    print(prompt)
    completion = opnerouter_client.chat.completions.create(
        model="google/gemini-pro",
        messages=[
            {"role": "user", "content": prompt}
        ]
    )
    return completion.choices[0].message.content

with gr.Blocks() as demo:
    with gr.Column():
        name = gr.Textbox(label="Enter your youtube url")
        button_download = gr.Button("Download")

    with gr.Row():
        output = gr.TextArea(label="Output")

        with gr.Column():
            button_summary = gr.Button("Summary")
            summary = gr.TextArea(label="Summary")

    button_download.click(
        download_audio,
        inputs=[name],
        outputs=[output],
    )

    button_summary.click(
        summarize_text,
        inputs=[],
        outputs=[summary],
    )

demo.launch()