File size: 3,590 Bytes
079312c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import streamlit as st
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
import glob
import os

def vid_to_audio(url=None):
    # importing packages 
    from pytube import YouTube 
    import os 

    # url input from user 
    yt = YouTube(url) 

    # extract only audio 
    video = yt.streams.filter(only_audio=True).first() 

    # check for destination to save file 
    destination = '.'

    # download the file 
    out_file = video.download(output_path=destination) 

    # save the file 
    base, ext = os.path.splitext(out_file) 
    new_file = base + '.mp3'
    os.rename(out_file, new_file) 

    # result of success 
    print(yt.title + " has been successfully downloaded.")

    return "OK"

#vid_to_text(url='https://youtu.be/FE5tva_o7ew?si=ztkKeO7qwcpC36AS')

def audio_to_text():
    import torch
    
    
    from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
    


    device = "cuda:0" if torch.cuda.is_available() else "cpu"
    torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

    model_id = "openai/whisper-tiny"

    model = AutoModelForSpeechSeq2Seq.from_pretrained(
        model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
    )
    model.to(device)

    processor = AutoProcessor.from_pretrained(model_id)
    #
    pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=30,
    batch_size=16,

    torch_dtype=torch_dtype,
    device=device,
    )
    
    #files = glob.glob('*.mp3')[0]
    files = os.listdir()
    # Get a list of all files in the current directory
    files = os.listdir()
    #st.write(files)

    # Create an empty list to collect results
    results = []
    
    # Iterate through the files
    for i in files:
        if ".mp3" in i:
            # Build the full path to the MP3 file
            file_path = os.path.join(os.getcwd(), i)
    
            # Display information (optional)
            st.write("Current Directory:", os.getcwd())
            st.write("File Path:", file_path)
            
            
            result = pipe(file_path)
            #print(result)
            return result['text']
    


def summarize():
    transcript = audio_to_text()
    len_trans = len(transcript)

    chunks = int(len_trans/512)
    from transformers import pipeline

    summarizer = pipeline("summarization", model="snrspeaks/t5-one-line-summary")

    
    #print(summarizer(transcript, do_sample=False))
    cutoff = 512
    final_output = ''
    """
    for i in range(chunks):
        print(i)
        if i == 0:
            tran_text = transcript[:512]
            inter_output = summarizer(tran_text, do_sample=False)[0]['summary_text']
            final_output += inter_output
            final_output += ' '

        else:
            #end_slice = cutoff + cutoff
            tran_text = transcript[cutoff:cutoff + 2]
            inter_output = summarizer(tran_text, do_sample=False)[0]['summary_text']
            final_output += inter_output
            final_output += ' '
            cutoff += cutoff
    """
    final_output = summarizer(tran_text, do_sample=False)
    return final_output

yt_link = st.text_input("Enter the YouTube URL: ")

if st.button("Start Summarization"):
    
    with st.status("Downloading the video..."):
        vid_to_audio(url=yt_link)
    with st.status("Summarizing..."):
        s = summarize()
        st.write(s)