File size: 2,017 Bytes
79c3503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f3dcc0
79c3503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f3dcc0
79c3503
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import openai
import os
from langchain.document_loaders import TextLoader, YoutubeLoader
#pytube, gradio, langchain, openai
import gradio as gr
from youtube_transcript_api import YouTubeTranscriptApi
from langchain.indexes import VectorstoreIndexCreator

os.environ["OPENAI_API_KEY"] = "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"

def get_video_id(url):
    video_id = None
    if 'youtu.be' in url:
        video_id = url.split('/')[-1]
    else:
        video_id = url.split('watch?v=')[-1]
    return video_id

def get_captions(url):
    try:
        video_id = get_video_id(url)
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        transcript = transcript_list.find_transcript(['en'])
        captions = transcript.fetch()

        formatted_captions = ''
        for caption in captions:
            formatted_captions += caption['text'] + ' '

        return formatted_captions

    except Exception as e:
        print(e)
        return "Error. Could not fetch captions."

def answer_question(youtube_url, user_question):
    # You can implement your logic here to process the video, transcribe it, and answer the user question.
    # For now, let's return the user question as output.

    f= open("temp.txt","w+")
    f.write(get_captions(user_question))
    f.close() 

    loader = TextLoader("temp.txt")

    index = VectorstoreIndexCreator().from_loaders([loader])
    os.remove("temp.txt")

    query = user_question
    answer = index.query(query)

    return answer

iface = gr.Interface(
    fn=answer_question,
    inputs=[
        gr.inputs.Textbox(lines=1, placeholder="Enter YouTube URL here..."),
        gr.inputs.Textbox(lines=1, placeholder="Enter your question here...")
    ],
    outputs=gr.outputs.Textbox(),
    title="YouTube Video Question Answering",
    description="Enter a YouTube URL and a question related to the video content. The app will return the answer if answer exists in the video."
)
if __name__ == "__main__":
    iface.launch()