File size: 3,830 Bytes
857dbaf
 
6a4ed54
857dbaf
 
 
 
5002c95
6a4ed54
857dbaf
 
 
 
 
 
c0e82c0
 
 
 
857dbaf
 
c0e82c0
857dbaf
c0e82c0
857dbaf
c0e82c0
 
 
 
857dbaf
 
 
 
 
 
 
 
 
 
c0e82c0
857dbaf
c0e82c0
857dbaf
 
 
 
 
 
 
 
 
 
 
c0e82c0
 
 
857dbaf
 
 
c0e82c0
 
857dbaf
c0e82c0
 
 
 
 
 
dd41bc7
c0e82c0
 
 
 
 
857dbaf
c0e82c0
857dbaf
c0e82c0
857dbaf
 
 
 
 
 
 
 
 
 
c0e82c0
857dbaf
 
 
c0e82c0
857dbaf
 
 
 
 
 
6a4ed54
 
857dbaf
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 FC_tool_main 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 formatted as youtube comment with clickable timestamps
    - Question answering based on video content

    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()