File size: 3,913 Bytes
39f538b
 
 
 
 
 
 
 
 
 
 
bd4c054
 
39f538b
bd4c054
 
 
 
 
39f538b
 
 
 
 
 
 
 
 
 
 
 
 
09d469f
39f538b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09d469f
39f538b
 
09d469f
ff9ef40
f77e6d0
39f538b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2ffef
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr
from langchain_community.document_loaders import YoutubeLoader


from langchain_cohere import ChatCohere
from langchain import hub
from langchain_chroma import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_cohere import CohereEmbeddings
import os
import os


COHERE_API_KEY = os.environ.get("COHERE_API_KEY")


llm = ChatCohere(model="command-r",cohere_api_key=COHERE_API_KEY)
prompt = hub.pull("rlm/rag-prompt")

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)


# Function to load YouTube details
def get_youtube_details(video_url):
    print(video_url)
    loader = YoutubeLoader.from_youtube_url(str(video_url), add_video_info=False)
    docs = loader.load()
    print(docs)
    print("Video transcripts loaded in DB")
    return docs, loader

# Function to handle user messages and update the history
def user_message(message, history):
    return "", history + [[message, None]]

# Function to clear the vector store (optional, not used in this example)
def clear_vectorstore(vectorstore):
    vectorstore.delete_all()
    return "Vector store cleared."

# Function to clear the text box and reset the state
def clear_textbox():
    return "", None, None

# Function to handle bot responses
def bot_message(history, docs):
    if docs is None:
        return history
    
    print(docs,"instide bot_message")
    user_question = history[-1][0]
    splits = text_splitter.split_documents(docs)
    print(splits,"splits are also here")
    vectorstore = Chroma.from_documents(documents=splits, embedding=CohereEmbeddings(model="embed-english-light-v3.0",
                                                                                     cohere_api_key=COHERE_API_KEY))
    retriever = vectorstore.as_retriever()

    rag_chain = (
        {"context": retriever | format_docs, "question": RunnablePassthrough()}
        | prompt
        | llm
        | StrOutputParser()
    )

    response = rag_chain.invoke(user_question)
    history[-1][1] = response
    return history

title=(
"""
<center> 

<h1> VideoQ: Quick Answers, Skip Clickbait </h1>
<b> text  📧<b>

</center>
"""
)

with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
    # gr.Markdown("#              VideoQ: Quick Answers, Skip Clickbait")
    with gr.Row():
          gr.HTML(title,label=" ")  

    gr.Markdown("""
    ### Skip the endless scrolling. VideoQ provides instant video insights.
    ### Ask Questions to YouTube video and Save Time 
    """,label="Description")

    text_box = gr.Textbox(lines=2, placeholder="Enter link of the YouTube video",label="Youtube valid link")
    with gr.Row():
        load_button = gr.Button("Load Document")
        clear_button = gr.Button("Clear Document")

    docs_box = gr.State()
    loader_box = gr.State()

    load_button.click(fn=get_youtube_details, inputs=[text_box], outputs=[docs_box, loader_box])
    clear_button.click(fn=clear_textbox, inputs=[], outputs=[text_box, docs_box, loader_box])

    chatbot_interface = gr.Chatbot(show_copy_button=True,label=" ")
    msg = gr.Textbox(label="Message")

    with gr.Row():
        submit_btn = gr.Button("Submit")
        clear_btn = gr.Button("Clear")

    submit_btn.click(user_message, [msg, chatbot_interface], [msg, chatbot_interface], queue=False).then(
        bot_message, [chatbot_interface, docs_box], chatbot_interface)
    
    msg.submit(user_message, [msg, chatbot_interface], [msg, chatbot_interface], queue=False).then(
        bot_message, [chatbot_interface, docs_box], chatbot_interface)

    clear_btn.click(lambda: None, None, chatbot_interface, queue=False)

demo.launch(server_name="0.0.0.0", server_port=7860)