File size: 1,943 Bytes
aa9cce2
07d3e9f
 
cd864c2
5ee7f09
aa9cce2
5ee7f09
 
 
 
aa9cce2
07d3e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd864c2
 
 
 
 
 
c408f46
b3c270b
 
5ee7f09
 
c408f46
5ee7f09
67a77d9
5ee7f09
 
 
 
 
 
 
 
 
 
07d3e9f
 
 
 
5ee7f09
07d3e9f
5ee7f09
 
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
import gradio as gr
import yt_dlp
import os
from openai import OpenAI
import anthropic

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

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',
}

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"
        )

    print(transcription)
    return transcription

def summarize_text():
    text = output.value
    prompt = f"Please summarize the following article in 5 sentences or less: [{text}]"
    print(prompt)
    message = claude_client.messages.create(
        model="claude-3-haiku-20240307",
        max_tokens=1000,
        temperature=0.0,
        # system="Respond only in mandarin",
        messages=[
            {"role": "user", "content": prompt}
        ]
    )
    return message.content[0].text

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()