import os import openai import gradio as gr from youtube_transcript_api import YouTubeTranscriptApi from langchain_openai import ChatOpenAI from langchain.agents import AgentExecutor from langchain.memory import ConversationBufferWindowMemory from FCnew18thJul import YouTubeAgent, set_temperature import logging logging.getLogger().setLevel(logging.ERROR) import warnings warnings.filterwarnings("ignore") from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] class ChatBot: def __init__(self): self.youtube_agent = YouTubeAgent() def chat(self, message, history, temperature): try: # Set the temperature using the function from FCnew18thJul.py set_temperature(temperature) # Reinitialize the agent to use the new temperature self.youtube_agent = YouTubeAgent() response = self.youtube_agent.invoke(message) return response except Exception as e: return f"An error occurred: {str(e)}" chatbot = ChatBot() # Create an instance of ChatBot def user_message(message, history): return "", history + [[message, None]] def bot_message(history, temperature): user_message = history[-1][0] bot_response = chatbot.chat(user_message, history, temperature) history[-1][1] = bot_response return history def use_example(example, text_input): return example # Example messages example_messages = [ "What tools are available for use?", "What is the following video about? https://www.youtube.com/watch?v=dZxbVGhpEkI", "Can you summarize this video? https://www.youtube.com/watch?v=hM8unyUM6KA", "Extract the main points from this video: https://www.youtube.com/watch?v=UF8uR6Z6KLc", "What are the main challenges discussed in the video? https://www.youtube.com/watch?v=-OSxeoIAs2w&t=4262s", "What is the speakers name in this video? dZxbVGhpEkI" ] with gr.Blocks() as demo: gr.Markdown(""" # Chat with YouTube Videos This application provides a comprehensive set of tools for analyzing YouTube videos, extracting information, and answering questions based on video content. It leverages the LangChain library for natural language processing tasks and the YouTube Transcript API for fetching video transcripts. Key Features: - Main points summarization in multiple formats - Video content summarization - Question answering based on video content - Flexible AI agent for handling various YouTube video-related tasks Simply enter your question or request along with a YouTube video link, and the AI will process and respond accordingly. Adjust the temperature slider to control the creativity of the AI's responses. """) temperature_slider = gr.Slider(minimum=0, maximum=1, step=0.1, label="Temperature", value=0) chatbot_interface = gr.Chatbot(show_copy_button=True) msg = gr.Textbox(label="Message") with gr.Row(): submit_btn = gr.Button("Submit") clear_btn = gr.Button("Clear") gr.Markdown("## Example Messages") example_btns = [gr.Button(i) for i in example_messages] submit_btn.click(user_message, [msg, chatbot_interface], [msg, chatbot_interface], queue=False).then( bot_message, [chatbot_interface, temperature_slider], chatbot_interface ) msg.submit(user_message, [msg, chatbot_interface], [msg, chatbot_interface], queue=False).then( bot_message, [chatbot_interface, temperature_slider], chatbot_interface ) clear_btn.click(lambda: None, None, chatbot_interface, queue=False) for btn, example in zip(example_btns, example_messages): btn.click(use_example, inputs=[gr.Textbox(value=example, visible=False)], outputs=msg) if __name__ == "__main__": demo.launch()