File size: 4,551 Bytes
89b078e
138ee9c
9d2b048
89b078e
 
 
9d2b048
89b078e
9d2b048
89b078e
3f75f59
9d2b048
 
 
 
89b078e
 
 
 
 
 
 
 
 
 
 
 
 
 
ade57f6
89b078e
 
 
 
 
 
 
aea7840
89b078e
 
138ee9c
89b078e
 
 
 
 
 
 
 
 
a01fcf4
d4a3868
 
 
df0a789
138ee9c
a01fcf4
df0a789
138ee9c
 
 
89b078e
9d2b048
 
 
 
0798764
 
89b078e
 
9d2b048
 
 
0798764
9d2b048
 
a01fcf4
dcad378
 
 
 
 
138ee9c
df0a789
138ee9c
dcad378
 
138ee9c
 
dcad378
 
 
a01fcf4
dcad378
77e2bcc
89b078e
 
ac669c3
89b078e
 
ab5d546
9e12eb2
 
 
89b078e
ab5d546
 
 
9e12eb2
df0a789
138ee9c
ab5d546
 
138ee9c
 
ab5d546
 
 
138ee9c
ab5d546
cf919a1
10ca324
a01fcf4
89b078e
 
 
 
 
dcad378
89b078e
 
138ee9c
89b078e
 
138ee9c
9d2b048
 
47cbb99
9d2b048
 
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
138
139
140
141
import streamlit as st
import time
from transformers import pipeline
from pytube import YouTube
from pydub import AudioSegment
from audio_extract import extract_audio
import google.generativeai as google_genai
import os
from dotenv import load_dotenv


load_dotenv()

GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
google_genai.configure(api_key=GOOGLE_API_KEY)

st.set_page_config(
    page_title="VidText"
)

def youtube_video_downloader(url):
    yt_vid = YouTube(url)
    title = yt_vid.title
    vid_dld = (
        yt_vid.streams.filter(progressive=True, file_extension="mp4")
        .order_by("resolution")
        .desc()
        .first()    
    )
    vid_dld = vid_dld.download()
    return vid_dld, title


def audio_extraction(video_file, output_format):
    # temp_filename = video_file.name
    # video_path = f"{temp_filename}"
    audio = extract_audio(
        input_path=os.fspath(video_file), output_path=f"{str(video_file)[:-4]}.mp3", output_format=f"{output_format}"
    )
    return audio
  

def audio_processing(mp3_audio):
    audio = AudioSegment.from_file(mp3_audio, format="mp3")
    wav_file = "audio_file.wav"
    audio = audio.export(wav_file, format="wav")
    return wav_file


@st.cache_resource
def load_asr_model():
    asr_model = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3")
    return asr_model
    
def transcribe_video(processed_audio):
    st = time.now()
    transcriber_model = load_asr_model()
    text_extract = transcriber_model(processed_audio)
    et = time.now()
    run_time = et - st 
    return text_extract['text'], run_time

def generate_ai_summary(transcript):
    model = google_genai.GenerativeModel('gemini-pro')
    model_response = model.generate_content([f"Give a summary of the text {transcript}"], stream=True)
    return model_response.text

    
# Streamlit UI

youtube_url_tab, file_select_tab, audio_file_tab = st.tabs(
    ["Youtube url", "Video file", "Audio file"]
)

with youtube_url_tab:
    url = st.text_input("Enter the Youtube url")
    
    try:
        yt_video, title = youtube_video_downloader(url)
        if url:
           if st.button("Transcribe", key="yturl"):
               with st.spinner("Transcribing..."):
                   audio = audio_extraction(os.fspath(yt_video), "mp3")
                   audio = audio_processing(audio)
                   ytvideo_transcript, run_time = transcribe_video(audio)
               st.success(f"Transcription successful")
               st.write(ytvideo_transcript)
               st.write(f'Completed in {run_time}')
               
               if st.button("Generate Summary"):
                   summary = generate_ai_summary(ytvideo_transcript)
                   st.write(summary)
                   
    except Exception as e:
        st.error(f"Enter a valid url: {e}")

# Video file transcription

with file_select_tab:
    video_file = st.file_uploader("Upload video file", type="mp4")
    try:
        #video_file = video_file.getvalue()
        with open(video_file, "wb") as f:
            f.write(uploaded_file.getvalue())

        if video_file:
           if st.button("Transcribe", key="vidfile"):
               with st.spinner("Transcribing..."):
                   audio = audio_extraction(video_file, "mp3")
                   audio = audio_processing(audio)
                   video_transcript, run_time = transcribe_video(audio)
                   st.success(f"Transcription successful")
                   st.write(video_transcript)
                   st.write(f'Completed in {run_time}')
                   
                   if st.button("Generate Summary", key="ti2"):
                       summary = generate_ai_summary(video_transcript)
                       st.write(summary)
                       

    except Exception as e: 
        st.error(e)
        
# Audio transcription
with audio_file_tab:
    audio_file = st.file_uploader("Upload audio file", type="mp3")  

    if audio_file:
        if st.button("Transcribe", key="audiofile"):
            with st.spinner("Transcribing..."):
                processed_audio = audio_processing(audio_file)
                audio_transcript, run_time = transcribe_video(processed_audio)
                st.success(f"Transcription successful")
                st.write(audio_transcript)
                st.write(f'Completed in {run_time}')


                if st.button("Generate Summary", key="ti1"):
                    summary = generate_ai_summary(audio_transcript)
                    st.write(summary)