File size: 2,540 Bytes
fec3704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
083e518
fec3704
60e53d4
4eab181
 
 
 
 
 
 
7c04ec9
efba8ea
fec3704
 
60e53d4
 
fec3704
 
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
#from langchain.document_loaders.generic import GenericLoader
#from langchain.document_loaders.parsers import OpenAIWhisperParser
#from langchain.document_loaders.blob_loaders.youtube_audio import YoutubeAudioLoader
from langchain.document_loaders import YoutubeLoader
from langchain.chains import RetrievalQA
from langchain.vectorstores import FAISS
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
import gradio as gr
import os
import time
import shutil


def process_and_transcribe(url, openai_api_key, model):
    """Process and transcribe the video at a given url"""
    # Setting qa_chain as a global variable
    global qa_chain
    #save_dir = some_title
    #loader = GenericLoader(YoutubeAudioLoader(url, save_dir), OpenAIWhisperParser(api_key = openai_api_key))
    loader = YoutubeLoader.from_youtube_url(url)
    docs = loader.load()
    combined_docs = [doc.page_content for doc in docs]
    text = " ".join(combined_docs)
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=150)
    splits = text_splitter.split_text(text)
    embeddings = OpenAIEmbeddings(openai_api_key = openai_api_key)
    vectordb = FAISS.from_texts(splits, embeddings)
    qa_chain = RetrievalQA.from_chain_type(
        llm=ChatOpenAI(model_name=model, temperature=0, openai_api_key=openai_api_key),
        chain_type="stuff",
        retriever=vectordb.as_retriever(),
    )

    return text

def response(message, history):
    return qa_chain.run(message)

transcribe_interface = gr.Interface(
    fn=process_and_transcribe, 
    inputs=['text', 'text', gr.components.Radio(['gpt-3.5-turbo', 'gpt-3.5-turbo-16k','gpt-4'])],
    outputs=['text'],
    title="Transcribe Video",
    description=""" Summarize a YouTube Video (Supports Multiple Language Transcription)\n
    \n1. Paste the YouTube Video Link.
    \n2. Provide the OpenAI API Key (For chatting with GPT Models).\n
    \n3. Choose the Model, GPT-3.5-turbo for smaller summaries, GPT-3.5-turbo-16k for larger context, GPT-4 for advanced reasoning\n
    \n4. Go to the Chat interface\n
    \n5. Type 'Summarize'\n
    \n6. Done!\n
    
    Clear everything with the Clear button. """
)

chat_interface = gr.ChatInterface(fn=response, title="Chat", description="Chat with the AI about the video you just summarized.")
demo = gr.TabbedInterface([transcribe_interface, chat_interface], ["Transcribe", "Summarize & Chat"])
demo.queue()
demo.launch()