Dy commited on
Commit
0ff7ecf
1 Parent(s): e5af086

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -1
app.py CHANGED
@@ -13,6 +13,12 @@ from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePr
13
  from langchain.llms import OpenAI
14
  from collections import Counter
15
  import pandas as pd
 
 
 
 
 
 
16
 
17
  OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
18
  GOOGLE_MAPS_API = os.environ['GOOGLE_MAPS_API']
@@ -223,9 +229,84 @@ iface2 = gr.Interface(
223
  )
224
 
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  #tab1 = gr.Tab("AI Market Research", inputs=iface1.inputs, outputs=iface1.outputs)
228
  #tab2 = gr.Tab("SEO Keyword Extractor", inputs=iface2.inputs, outputs=iface2.outputs)
229
 
230
- demo = gr.TabbedInterface([iface2, iface1], ["SEO Keyword Extractor", "AI Market Researcher"])
231
  demo.launch()
 
13
  from langchain.llms import OpenAI
14
  from collections import Counter
15
  import pandas as pd
16
+ from langchain.document_loaders import TextLoader, YoutubeLoader
17
+ from youtube_transcript_api import YouTubeTranscriptApi
18
+ from langchain.indexes import VectorstoreIndexCreator
19
+
20
+
21
+ OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
22
 
23
  OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
24
  GOOGLE_MAPS_API = os.environ['GOOGLE_MAPS_API']
 
229
  )
230
 
231
 
232
+ #### TAB 3 ####
233
+
234
+
235
+
236
+ previous_youtube_url = None
237
+ index = None
238
+
239
+ def get_video_id(url):
240
+ video_id = None
241
+ if 'youtu.be' in url:
242
+ video_id = url.split('/')[-1]
243
+ else:
244
+ video_id = url.split('watch?v=')[-1]
245
+ return video_id
246
+
247
+ def get_captions(url):
248
+ try:
249
+ video_id = get_video_id(url)
250
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
251
+ transcript = transcript_list.find_transcript(['en'])
252
+ captions = transcript.fetch()
253
+
254
+ formatted_captions = ''
255
+ for caption in captions:
256
+ formatted_captions += caption['text'] + ' '
257
+
258
+ return formatted_captions
259
+
260
+ except Exception as e:
261
+ print(e)
262
+ return "Error. Could not fetch captions."
263
+
264
+
265
+
266
+ def answer_question(youtube_url, user_question):
267
+ # You can implement your logic here to process the video, transcribe it, and answer the user question.
268
+ # For now, let's return the user question as output.
269
+ global previous_youtube_url
270
+ global index
271
+
272
+ query = '''
273
+ You are an expert researcher that can answer any questions from a given text. Here is the question:
274
+ {}
275
+ '''.format(str(user_question))
276
+
277
+ if previous_youtube_url == youtube_url:
278
+ #index = VectorstoreIndexCreator().from_loaders([loader])
279
+ #query = user_question
280
+ answer = index.query(llm=OpenAI(model="text-davinci-003"), question = query)
281
+ else:
282
+ f= open("temp.txt","w+")
283
+ f.write(get_captions(youtube_url))
284
+ f.close()
285
+ loader = TextLoader("temp.txt")
286
+
287
+ index = VectorstoreIndexCreator().from_loaders([loader])
288
+ os.remove("temp.txt")
289
+
290
+ #query = user_question
291
+ answer = index.query(llm=OpenAI(model="text-davinci-003"), question = query)
292
+
293
+ return answer
294
+
295
+ iface3 = gr.Interface(
296
+ fn=answer_question,
297
+ inputs=[
298
+ gr.Textbox(lines=1, placeholder="Enter YouTube URL here..."),
299
+ gr.Textbox(lines=1, placeholder="Enter your question here...")
300
+ ],
301
+ outputs=gr.Textbox(),
302
+ title="YouTube Smart Q & A",
303
+ description="Enter a YouTube URL & a question and the app will find the answer from the video captions."
304
+ )
305
+
306
+
307
 
308
  #tab1 = gr.Tab("AI Market Research", inputs=iface1.inputs, outputs=iface1.outputs)
309
  #tab2 = gr.Tab("SEO Keyword Extractor", inputs=iface2.inputs, outputs=iface2.outputs)
310
 
311
+ demo = gr.TabbedInterface([iface2, iface1, iface3], ["SEO Keyword Extractor", "AI Market Researcher","YouTube Smart Q & A"])
312
  demo.launch()