Spaces:
Sleeping
Sleeping
File size: 2,566 Bytes
79c3503 067f5df 79c3503 f18e20f 79c3503 00dc332 60b0ed5 00dc332 79c3503 396d793 79c3503 00dc332 60b0ed5 bb1eb3a 4e729ea 84270c1 00dc332 60b0ed5 4e729ea 067f5df 00dc332 4e729ea 067f5df 79c3503 14e7a13 79c3503 fed2792 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 77 78 79 80 81 82 83 84 |
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
index = 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
global index
query = '''
You are an expert researcher that can answer any questions from a given text. Here is the question:
{}
'''.format(str(user_question))
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.Textbox(lines=1, placeholder="Enter YouTube URL here..."),
gr.Textbox(lines=1, placeholder="Enter your question here...")
],
outputs=gr.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()
|