Spaces:
Runtime error
Runtime error
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 trhacknon - <a href="https://github.com/tucommenceapousser/youtube-video-to-text-generation" target="_blank">Source Code</a> | |
</div> | |
""", | |
unsafe_allow_html=True | |
) | |