Spaces:
Sleeping
Sleeping
Dy
commited on
Commit
•
79c3503
1
Parent(s):
7550ee2
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
from langchain.document_loaders import TextLoader, YoutubeLoader
|
4 |
+
#pytube, gradio, langchain, openai
|
5 |
+
import gradio as gr
|
6 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
7 |
+
from langchain.indexes import VectorstoreIndexCreator
|
8 |
+
|
9 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
10 |
+
|
11 |
+
def get_video_id(url):
|
12 |
+
video_id = None
|
13 |
+
if 'youtu.be' in url:
|
14 |
+
video_id = url.split('/')[-1]
|
15 |
+
else:
|
16 |
+
video_id = url.split('watch?v=')[-1]
|
17 |
+
return video_id
|
18 |
+
|
19 |
+
def get_captions(url):
|
20 |
+
try:
|
21 |
+
video_id = get_video_id(url)
|
22 |
+
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
23 |
+
transcript = transcript_list.find_transcript(['en'])
|
24 |
+
captions = transcript.fetch()
|
25 |
+
|
26 |
+
formatted_captions = ''
|
27 |
+
for caption in captions:
|
28 |
+
formatted_captions += caption['text'] + ' '
|
29 |
+
|
30 |
+
return formatted_captions
|
31 |
+
|
32 |
+
except Exception as e:
|
33 |
+
print(e)
|
34 |
+
return "Error. Could not fetch captions."
|
35 |
+
|
36 |
+
def answer_question(youtube_url, user_question):
|
37 |
+
# You can implement your logic here to process the video, transcribe it, and answer the user question.
|
38 |
+
# For now, let's return the user question as output.
|
39 |
+
|
40 |
+
f= open("temp.txt","w+")
|
41 |
+
f.write(get_captions("https://www.youtube.com/watch?v=mXjaob63K2w"))
|
42 |
+
f.close()
|
43 |
+
|
44 |
+
loader = TextLoader("temp.txt")
|
45 |
+
|
46 |
+
index = VectorstoreIndexCreator().from_loaders([loader])
|
47 |
+
os.remove("temp.txt")
|
48 |
+
|
49 |
+
query = user_question
|
50 |
+
answer = index.query(query)
|
51 |
+
|
52 |
+
return answer
|
53 |
+
|
54 |
+
iface = gr.Interface(
|
55 |
+
fn=answer_question,
|
56 |
+
inputs=[
|
57 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter YouTube URL here..."),
|
58 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter your question here...")
|
59 |
+
],
|
60 |
+
outputs=gr.outputs.Textbox(),
|
61 |
+
title="YouTube Video Question Answering",
|
62 |
+
description="Enter a YouTube URL and a question related to the video content. The app will return the answer if answer exist from the video."
|
63 |
+
)
|
64 |
+
if __name__ == "__main__":
|
65 |
+
iface.launch()
|
66 |
+
|