Spaces:
Sleeping
Sleeping
eaglelandsonce
commited on
Commit
•
0f6852b
1
Parent(s):
483c51c
Delete pages/3_TalktoAVideo
Browse files- pages/3_TalktoAVideo +0 -83
pages/3_TalktoAVideo
DELETED
@@ -1,83 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
|
4 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
5 |
-
from langchain_community.document_loaders import YoutubeLoader
|
6 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
-
from langchain_community.vectorstores import FAISS
|
8 |
-
from langchain.chains import LLMChain
|
9 |
-
from langchain.prompts.chat import (
|
10 |
-
ChatPromptTemplate,
|
11 |
-
SystemMessagePromptTemplate,
|
12 |
-
HumanMessagePromptTemplate,
|
13 |
-
)
|
14 |
-
|
15 |
-
def create_db_from_video_url(video_url, api_key):
|
16 |
-
"""
|
17 |
-
Creates an Embedding of the Video and performs
|
18 |
-
"""
|
19 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
20 |
-
|
21 |
-
loader = YoutubeLoader.from_youtube_url(video_url)
|
22 |
-
transcripts = loader.load()
|
23 |
-
# cannot provide this directly to the model so we are splitting the transcripts into small chunks
|
24 |
-
|
25 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
26 |
-
docs = text_splitter.split_documents(transcripts)
|
27 |
-
|
28 |
-
db = FAISS.from_documents(docs, embedding=embeddings)
|
29 |
-
return db
|
30 |
-
|
31 |
-
def get_response(video, request):
|
32 |
-
"""
|
33 |
-
Usind Gemini Pro to get the response. It can handle upto 32k tokens.
|
34 |
-
"""
|
35 |
-
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
36 |
-
db = create_db_from_video_url(video, API_KEY)
|
37 |
-
docs = db.similarity_search(query=request, k=5)
|
38 |
-
docs_content = " ".join([doc.page_content for doc in docs])
|
39 |
-
|
40 |
-
chat = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=API_KEY, convert_system_message_to_human=True)
|
41 |
-
|
42 |
-
# creating a template for request
|
43 |
-
template = """
|
44 |
-
You are an assistant that can answer questions about youtube videos based on
|
45 |
-
video transcripts: {docs}
|
46 |
-
Only use factual information from the transcript to answer the question.
|
47 |
-
If you don't have enough information to answer the question, say "I don't know".
|
48 |
-
Your Answers should be detailed.
|
49 |
-
"""
|
50 |
-
|
51 |
-
system_msg_prompt = SystemMessagePromptTemplate.from_template(template)
|
52 |
-
|
53 |
-
# human prompt
|
54 |
-
human_template = "Answer the following questions: {question}"
|
55 |
-
human_msg_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
56 |
-
|
57 |
-
chat_prompt = ChatPromptTemplate.from_messages(
|
58 |
-
[system_msg_prompt, human_msg_prompt]
|
59 |
-
)
|
60 |
-
|
61 |
-
chain = LLMChain(llm=chat, prompt=chat_prompt)
|
62 |
-
|
63 |
-
response = chain.run(question=request, docs=docs_content)
|
64 |
-
|
65 |
-
return response
|
66 |
-
|
67 |
-
# creating title, description for the web app
|
68 |
-
title = "YouTube Video AI Assistant 🤖"
|
69 |
-
description = "Answers to the Questions asked by the user on the specified YouTube video."
|
70 |
-
|
71 |
-
|
72 |
-
# building the app
|
73 |
-
youtube_video_assistant = gr.Interface(
|
74 |
-
fn=get_response,
|
75 |
-
inputs=[gr.Text(label="Enter the Youtube Video URL:", placeholder="Example: https://www.youtube.com/watch?v=MnDudvCyWpc"),
|
76 |
-
gr.Text(label="Enter your Question", placeholder="Example: What's the video is about?")],
|
77 |
-
outputs=gr.TextArea(label="Answers using....some secret llm 🤫😉:"),
|
78 |
-
title=title,
|
79 |
-
description=description
|
80 |
-
)
|
81 |
-
|
82 |
-
# launching the web app
|
83 |
-
youtube_video_assistant.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|