File size: 2,790 Bytes
3e992b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import re
import openai
from pytube import YouTube
import os

# Remove previous audio file
audio_filename = "temp.mp3"
if os.path.exists(audio_filename):
    os.remove(audio_filename)

# Function to check if the provided OpenAI API key is valid
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

# Function to check if a provided link is a valid YouTube video link
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

# Define the application layout
st.set_page_config(page_title="YouTube Video to Text", layout="wide")

# Add the application header
st.title("YouTube Video to Text")

# Add the side panel
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.')

# In the main section of the app, add the following code:
if openai_api_key and youtube_video_link and is_valid_youtube_link(youtube_video_link):
    if is_valid_openai_key(openai_api_key):
        # Your code to process the YouTube video using the OpenAI API goes here
        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.")

# Add the footer with the creator's name and GitHub link
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
)