Illia56 commited on
Commit
3b93f52
1 Parent(s): 365894c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -40
app.py CHANGED
@@ -13,6 +13,9 @@ from langchain.chains import RetrievalQA
13
  from langchain.prompts import PromptTemplate
14
  import streamlit as st
15
  from pytube import YouTube
 
 
 
16
 
17
 
18
 
@@ -24,10 +27,8 @@ models = '''| Model | Llama2 | Llama2-hf | Llama2-chat | Llama2-chat-hf |
24
 
25
  DESCRIPTION = """
26
  Welcome to the **YouTube Video Chatbot** powered by the state-of-the-art Llama-2-70b model. Here's what you can do:
27
-
28
  - **Transcribe & Understand**: Provide any YouTube video URL, and our system will transcribe it. Our advanced NLP model will then understand the content, ready to answer your questions.
29
  - **Ask Anything**: Based on the video's content, ask any question, and get instant, context-aware answers.
30
-
31
  To get started, simply paste a YouTube video URL in the sidebar and start chatting with the model about the video's content. Enjoy the experience!
32
  """
33
  st.title("YouTube Video Chatbot")
@@ -51,27 +52,6 @@ def transcribe_video(youtube_url: str, path: str) -> List[Document]:
51
 
52
 
53
 
54
-
55
-
56
- class LlamaLLM(LLM):
57
- """
58
- Custom LLM class.
59
- """
60
-
61
- @property
62
- def _llm_type(self) -> str:
63
- return "custom"
64
-
65
- def _call(self, prompt: str, stop: Optional[List[str]] = None,
66
- run_manager: Optional[CallbackManagerForLLMRun] = None) -> str:
67
- response = predict(prompt)
68
- return response
69
-
70
- @property
71
- def _identifying_params(self) -> Mapping[str, Any]:
72
- """Get the identifying parameters."""
73
- return {}
74
-
75
  PATH = os.path.join(os.path.expanduser("~"), "Data")
76
 
77
  def initialize_session_state():
@@ -86,6 +66,10 @@ def sidebar():
86
  with st.sidebar:
87
  st.markdown("Enter the YouTube Video URL below🔗\n")
88
  st.session_state.youtube_url = st.text_input("YouTube Video URL:")
 
 
 
 
89
 
90
  if st.session_state.youtube_url:
91
  # Get the video title
@@ -105,21 +89,6 @@ def sidebar():
105
  # RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)
106
 
107
 
108
- def predict(message: str) -> Any:
109
- """
110
- Predict a response using a client.
111
- """
112
- client = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/")
113
- response = client.predict(
114
- message,
115
- '',
116
- 0.9,
117
- 4096,
118
- 0.6,
119
- 1.2,
120
- api_name="/chat_1"
121
- )
122
- return response
123
 
124
  sidebar()
125
  initialize_session_state()
@@ -139,7 +108,7 @@ prompt = PromptTemplate(
139
  if st.session_state.youtube_url != st.session_state.doneYoutubeurl:
140
  st.session_state.setup_done = False
141
 
142
- if st.session_state.youtube_url and not st.session_state.setup_done:
143
  with st.status("Transcribing video..."):
144
  data = transcribe_video(st.session_state.youtube_url, PATH)
145
 
@@ -151,7 +120,10 @@ if st.session_state.youtube_url and not st.session_state.setup_done:
151
  retriever.search_kwargs['distance_metric'] = 'cos'
152
  retriever.search_kwargs['k'] = 4
153
  with st.status("Running RetrievalQA..."):
154
- llama_instance = LlamaLLM()
 
 
 
155
  st.session_state.qa = RetrievalQA.from_chain_type(llm=llama_instance, chain_type="stuff", retriever=retriever,chain_type_kwargs={"prompt": prompt})
156
 
157
  st.session_state.doneYoutubeurl = st.session_state.youtube_url
 
13
  from langchain.prompts import PromptTemplate
14
  import streamlit as st
15
  from pytube import YouTube
16
+ import replicate
17
+
18
+
19
 
20
 
21
 
 
27
 
28
  DESCRIPTION = """
29
  Welcome to the **YouTube Video Chatbot** powered by the state-of-the-art Llama-2-70b model. Here's what you can do:
 
30
  - **Transcribe & Understand**: Provide any YouTube video URL, and our system will transcribe it. Our advanced NLP model will then understand the content, ready to answer your questions.
31
  - **Ask Anything**: Based on the video's content, ask any question, and get instant, context-aware answers.
 
32
  To get started, simply paste a YouTube video URL in the sidebar and start chatting with the model about the video's content. Enjoy the experience!
33
  """
34
  st.title("YouTube Video Chatbot")
 
52
 
53
 
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  PATH = os.path.join(os.path.expanduser("~"), "Data")
56
 
57
  def initialize_session_state():
 
66
  with st.sidebar:
67
  st.markdown("Enter the YouTube Video URL below🔗\n")
68
  st.session_state.youtube_url = st.text_input("YouTube Video URL:")
69
+
70
+ REPLICATE_API_TOKEN = st.text_input("REPLICATE API TOKEN:", type="password") # Using type="password" to mask the input
71
+ if REPLICATE_API_TOKEN:
72
+ os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
73
 
74
  if st.session_state.youtube_url:
75
  # Get the video title
 
89
  # RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)
90
 
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  sidebar()
94
  initialize_session_state()
 
108
  if st.session_state.youtube_url != st.session_state.doneYoutubeurl:
109
  st.session_state.setup_done = False
110
 
111
+ if st.session_state.youtube_url and not st.session_state.setup_done and "REPLICATE_API_TOKEN" in os.environ:
112
  with st.status("Transcribing video..."):
113
  data = transcribe_video(st.session_state.youtube_url, PATH)
114
 
 
120
  retriever.search_kwargs['distance_metric'] = 'cos'
121
  retriever.search_kwargs['k'] = 4
122
  with st.status("Running RetrievalQA..."):
123
+ llama_instance = replicate.load(
124
+ model="meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
125
+ model_kwargs={"temperature": 0.75, "max_length": 4096, "top_p": 1},
126
+ )
127
  st.session_state.qa = RetrievalQA.from_chain_type(llm=llama_instance, chain_type="stuff", retriever=retriever,chain_type_kwargs={"prompt": prompt})
128
 
129
  st.session_state.doneYoutubeurl = st.session_state.youtube_url