vikrambhat2/YoutubeRAG
0
1import logging2import gradio as gr3from langchain_community.document_loaders import YoutubeLoader4 5# Set up logging6logging.basicConfig(level=logging.INFO)7logger = logging.getLogger(__name__)8 9def fetch_youtube_captions(video_url):10 try:11 # Initialize the YouTube loader12 loader = YoutubeLoader.from_youtube_url(video_url)13 14 # Load the video data and captions15 documents = loader.load()16 17 # Extract and return the captions18 captions = []19 for doc in documents:20 captions.append(f"Document Metadata: {doc.metadata}\nDocument Text: {doc.page_content}\n")21 22 # Calculate the length of the documents23 documents_length = len(documents)24 captions_text = "\n".join(captions)25 26 if documents_length==0:27 captions_text+="No documents fetched"28 29 30 return documents_length, captions_text31 32 except Exception as e:33 logger.error(f"An error occurred: {e}")34 return None, f"Failed to fetch captions: {str(e)}"35 36# Create a Gradio interface37demo = gr.Interface(38 fn=fetch_youtube_captions,39 inputs=gr.Textbox(label="YouTube Video URL"),40 outputs=[41 gr.Number(label="Number of Documents"),42 gr.Textbox(label="Captions")43 ],44 title="YouTube Captions Fetcher",45 description="Enter a YouTube video URL to fetch its captions.",46)47 48if __name__ == "__main__":49 demo.launch()