eaglelandsonce commited on
Commit
327ed91
β€’
1 Parent(s): a8793bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import streamlit as st
2
  import os
3
 
4
  from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
@@ -14,12 +14,13 @@ from langchain.prompts.chat import (
14
 
15
  def create_db_from_video_url(video_url, api_key):
16
  """
17
- Creates an Embedding of the Video and performs similarity search
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
 
24
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
25
  docs = text_splitter.split_documents(transcripts)
@@ -28,6 +29,9 @@ def create_db_from_video_url(video_url, api_key):
28
  return db
29
 
30
  def get_response(video, request):
 
 
 
31
  API_KEY = os.environ.get("GOOGLE_API_KEY")
32
  db = create_db_from_video_url(video, API_KEY)
33
  docs = db.similarity_search(query=request, k=5)
@@ -35,6 +39,7 @@ def get_response(video, request):
35
 
36
  chat = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=API_KEY, convert_system_message_to_human=True)
37
 
 
38
  template = """
39
  You are an assistant that can answer questions about youtube videos based on
40
  video transcripts: {docs}
@@ -44,6 +49,8 @@ def get_response(video, request):
44
  """
45
 
46
  system_msg_prompt = SystemMessagePromptTemplate.from_template(template)
 
 
47
  human_template = "Answer the following questions: {question}"
48
  human_msg_prompt = HumanMessagePromptTemplate.from_template(human_template)
49
 
@@ -52,15 +59,25 @@ def get_response(video, request):
52
  )
53
 
54
  chain = LLMChain(llm=chat, prompt=chat_prompt)
 
55
  response = chain.run(question=request, docs=docs_content)
 
56
  return response
57
 
58
- # Streamlit interface
59
- st.title("YouTube Video AI Assistant")
60
- st.write("This application answers questions based on the specified YouTube video.")
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- video_url = st.text_input("Enter the YouTube Video URL:", placeholder="Example: https://www.youtube.com/watch?v=MnDudvCyWpc")
63
- question = st.text_input("Enter your Question", placeholder="What's the video about?")
64
- if st.button("Get Answer"):
65
- answer = get_response(video_url, question)
66
- st.text_area("Answer:", value=answer, height=300)
 
1
+ import gradio as gr
2
  import os
3
 
4
  from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
 
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)
 
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)
 
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}
 
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
 
 
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)