import gradio as gr import shutil, openai, os 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 dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) #openai.api_key = os.environ["OPENAI_API_KEY"] ### from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(model_name = "gpt-4", temperature = 0) from langchain.prompts import PromptTemplate template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "Author: Bernd Straehle 🚀" at the end of the answer. {context} Question: {question} Helpful Answer:""" QA_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"], template = template, ) ### def invoke(openai_api_key, youtube_url, prompt): openai.api_key = openai_api_key url = youtube_url save_dir = "docs/youtube/" loader = GenericLoader( YoutubeAudioLoader([url], save_dir), OpenAIWhisperParser() ) docs = loader.load() shutil.rmtree(save_dir) content = docs[0].page_content ##### #TODO ##### return content description = """The app demonstrates how to use a Large Language Model (LLM) with Retrieval Augmented Generation (RAG) on external data. Enter an OpenAI API key, YouTube URL (external data), and prompt to search the video, analyse its sentiment, summarize it, and/or translate it, etc.\n\n Implementation: Gradio UI using OpenAI API via AI-first toolkit LangChain with foundation models Whisper (speech to text) and GPT-4 (LLM use cases).""" gr.close_all() demo = gr.Interface(fn=invoke, inputs = [gr.Textbox(label = "OpenAI API Key", lines = 1), gr.Textbox(label = "YouTube URL", lines = 1), gr.Textbox(label = "Prompt", lines = 1)], outputs = [gr.Textbox(label = "Completion", lines = 1)], title = "Generative AI - LLM & RAG", description = description) demo.launch()