File size: 2,390 Bytes
79c3503
 
 
 
 
 
 
067f5df
79c3503
f18e20f
79c3503
00dc332
 
79c3503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396d793
 
79c3503
 
 
00dc332
 
 
 
067f5df
00dc332
 
 
 
 
 
 
 
 
 
067f5df
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
68
69
70
71
72
73
74
75
76
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
from langchain.llms import OpenAI

OPENAI_API_KEY = os.environ['OPENAI_API_KEY']

previous_youtube_url = None

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.
    global previous_youtube_url
    if previous_youtube_url == youtube_url:
        index = VectorstoreIndexCreator().from_loaders([loader])
        query = user_question
        answer = index.query(llm=OpenAI(model="text-davinci-003"), question = query)
    else:
        f= open("temp.txt","w+")
        f.write(get_captions(youtube_url))
        f.close() 
        loader = TextLoader("temp.txt")
    
        index = VectorstoreIndexCreator().from_loaders([loader])
        os.remove("temp.txt")

        query = user_question
        answer = index.query(llm=OpenAI(model="text-davinci-003"), question = 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()