|
import streamlit as st |
|
import requests |
|
import re |
|
import openai |
|
from pytube import YouTube |
|
import os |
|
|
|
|
|
audio_filename = "temp.mp3" |
|
if os.path.exists(audio_filename): |
|
os.remove(audio_filename) |
|
|
|
|
|
def is_valid_openai_key(api_key): |
|
try: |
|
openai.api_key = api_key |
|
openai.Engine.list() |
|
return True |
|
except Exception as e: |
|
st.write(f"Error: {e}") |
|
return False |
|
|
|
|
|
def is_valid_youtube_link(url): |
|
youtube_regex = re.compile( |
|
r'(https?://)?(www\.)?' |
|
r'(youtube|youtu|youtube-nocookie)\.(com|be)/' |
|
r'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})' |
|
) |
|
return youtube_regex.match(url) is not None |
|
|
|
|
|
st.set_page_config(page_title="YouTube Video to Text", layout="wide") |
|
|
|
|
|
st.title("YouTube Video to Text") |
|
|
|
|
|
with st.sidebar: |
|
st.header("Settings") |
|
openai_api_key = st.text_input("OpenAI API Key", type="password") |
|
youtube_video_link = st.text_input("YouTube Video Link") |
|
st.caption('Note: Only videos in english language are currently supported.') |
|
|
|
|
|
if openai_api_key and youtube_video_link and is_valid_youtube_link(youtube_video_link): |
|
if is_valid_openai_key(openai_api_key): |
|
|
|
print(youtube_video_link) |
|
youtube_video = YouTube(youtube_video_link, use_oauth=True, allow_oauth_cache=True) |
|
st.markdown(f"Title: **{youtube_video.title}**") |
|
streams=youtube_video.streams.filter(only_audio=True) |
|
stream=streams.last() |
|
with st.spinner('Downloading Audio'): |
|
stream.download(filename='temp.mp3') |
|
audio_file= open("temp.mp3", "rb") |
|
with st.spinner('Transcribing Text'): |
|
transcript_json = openai.Audio.transcribe("whisper-1", audio_file) |
|
transcript = transcript_json["text"] |
|
st.markdown("**Text:**") |
|
st.markdown(transcript) |
|
else: |
|
st.warning("Invalid OpenAI API Key. Please enter a valid API key.") |
|
else: |
|
st.warning("Please enter a valid YouTube video link and OpenAI API key.") |
|
|
|
|
|
st.markdown( |
|
""" |
|
<div style="position: fixed; bottom: 10px; right: 10px; width: auto; height: auto; background-color: white; border: 1px solid #f0f2f6; border-radius: 5px; padding: 1rem;"> |
|
Created by Taaha Bajwa - <a href="https://github.com/yourusername/yourrepository" target="_blank">Source Code</a> |
|
</div> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|