Spaces:
Build error
Build error
Initialize Streamlit transcription app.
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSpeechSeq2Seq
|
| 3 |
+
from pytube import YouTube
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
from audio_extract import extract_audio
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="VidText"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def youtube_video_downloader(url):
|
| 15 |
+
yt_vid = YouTube(url)
|
| 16 |
+
title = yt_vid.title
|
| 17 |
+
vid_dld = (
|
| 18 |
+
yt_vid.streams.filter(progressive=True, file_extension="mp4")
|
| 19 |
+
.order_by("resolution")
|
| 20 |
+
.desc()
|
| 21 |
+
.first()
|
| 22 |
+
)
|
| 23 |
+
# vid_dld = vid_dld.download()
|
| 24 |
+
return vid_dld, title
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def audio_extraction(video_file, output_format):
|
| 28 |
+
# temp_filename = video_file.name
|
| 29 |
+
# video_path = f"{temp_filename}"
|
| 30 |
+
audio = extract_audio(
|
| 31 |
+
input_path=video_file, output_path=f"{video_file[:-4]}.mp3", output_format=f"{output_format}"
|
| 32 |
+
)
|
| 33 |
+
return audio
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def audio_processing(mp3_audio):
|
| 37 |
+
audio = AudioSegment.from_file(mp3_audio, format="mp3")
|
| 38 |
+
wav_file = "audio_file.wav"
|
| 39 |
+
audio = audio.export(wav_file, format="wav")
|
| 40 |
+
return wav_file
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@st.cache_resource
|
| 44 |
+
def transcribe_video(processed_audio):
|
| 45 |
+
transcriber_model = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3")
|
| 46 |
+
text_extract = transcriber_model(processed_audio)
|
| 47 |
+
return text_extract['text']
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Streamlit UI
|
| 51 |
+
|
| 52 |
+
url_input_tab, file_select_tab, audio_file_tab = st.tabs(["Youtube url", "Video file", "Audio file"])
|
| 53 |
+
|
| 54 |
+
# with url_input_tab:video_path
|
| 55 |
+
# url = st.text_input("Enter the Youtube url")
|
| 56 |
+
# yt_video, title = youtube_video_downloader(url)
|
| 57 |
+
# if yt_video:
|
| 58 |
+
# if st.button("Transcribe"):
|
| 59 |
+
# with st.spinner("Transcribing..."):
|
| 60 |
+
# ytvideo_transcript = transcribe(yt_video)
|
| 61 |
+
# st.success(f"Transcription successful")
|
| 62 |
+
# st.write(ytvideo_transcript)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Video file transcription
|
| 66 |
+
with file_select_tab:
|
| 67 |
+
video_file = st.file_uploader("Upload video file", type="mp4")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if video_file:
|
| 71 |
+
if st.button("Transcribe"):
|
| 72 |
+
with st.spinner("Transcribing..."):
|
| 73 |
+
audio = audio_extraction(video_file, "mp3")
|
| 74 |
+
video_transcript = transcribe_video(audio)
|
| 75 |
+
st.success(f"Transcription successful")
|
| 76 |
+
st.write(video_transcript)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Audio transcription
|
| 80 |
+
with audio_file_tab:
|
| 81 |
+
audio_file = st.file_uploader("Upload audio file", type="mp3")
|
| 82 |
+
|
| 83 |
+
if audio_file:
|
| 84 |
+
if st.button("Transcribe"):
|
| 85 |
+
with st.spinner("Transcribing..."):
|
| 86 |
+
processed_audio = audio_processing(audio_file)
|
| 87 |
+
audio_transcript = transcribe_video(processed_audio)
|
| 88 |
+
st.success(f"Transcription successful")
|
| 89 |
+
st.write(audio_transcript)
|