bstraehle commited on
Commit
dc12c17
·
1 Parent(s): ccdcea2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -25,21 +25,23 @@ QA_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"], temp
25
  CHROMA_DIR = "docs/chroma"
26
  YOUTUBE_DIR = "docs/youtube"
27
 
 
 
28
  MODEL_NAME = "gpt-4"
29
 
30
- def invoke(openai_api_key, youtube_url, process_video, prompt):
31
- if (process_video):
32
- if (os.path.isdir(CHROMA_DIR)):
33
- shutil.rmtree(CHROMA_DIR)
34
- if (os.path.isdir(YOUTUBE_DIR)):
35
- shutil.rmtree(YOUTUBE_DIR)
36
- loader = GenericLoader(YoutubeAudioLoader([youtube_url], YOUTUBE_DIR), OpenAIWhisperParser())
37
  docs = loader.load()
38
  text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1500, chunk_overlap = 150)
39
  splits = text_splitter.split_documents(docs)
40
  vector_db = Chroma.from_documents(documents = splits, embedding = OpenAIEmbeddings(), persist_directory = CHROMA_DIR)
41
- else:
42
- vector_db = Chroma(persist_directory = CHROMA_DIR, embedding_function = OpenAIEmbeddings())
43
  llm = ChatOpenAI(model_name = MODEL_NAME, openai_api_key = openai_api_key, temperature = 0)
44
  qa_chain = RetrievalQA.from_chain_type(llm, retriever = vector_db.as_retriever(search_kwargs = {"k": 3}), return_source_documents = True, chain_type_kwargs = {"prompt": QA_CHAIN_PROMPT})
45
  result = qa_chain({"query": prompt})
@@ -50,14 +52,14 @@ description = """<strong>Overview:</strong> The app demonstrates how to use a La
50
  (in this case YouTube videos, but it could be PDFs, URLs, or other structured/unstructured private/public
51
  <a href='https://raw.githubusercontent.com/bstraehle/ai-ml-dl/c38b224c196fc984aab6b6cc6bdc666f8f4fbcff/langchain/document-loaders.png'>data sources</a>).\n\n
52
  <strong>Instructions:</strong> Enter an OpenAI API key and perform LLM use cases on a YouTube video (semantic search, sentiment analysis, summarization,
53
- translation, etc.) The example is a short video about GPT-4.
54
  <ul style="list-style-type:square;">
55
  <li>Set "Process Video" to "False" and submit prompt "what is gpt-4". The LLM <strong>without</strong> RAG does not know the answer.</li>
56
  <li>Set "Process Video" to "True" and submit prompt "what is gpt-4". The LLM <strong>with</strong> RAG knows the answer.</li>
57
  <li>Set "Process Video" to "False" and experiment with different prompts, for example "what is gpt-4, answer in german" or "write a haiku about gpt-4".</li>
58
  </ul>
59
  In a production system processing external data would be done in a batch process. An idea for a production system would be to perform LLM use cases on the
60
- <a href='https://www.youtube.com/playlist?list=PL2yQDdvlhXf_hIzmfHCdbcXj2hS52oP9r'>AWS re:Invent</a> playlist.\n\n
61
  <strong>Technology:</strong> <a href='https://www.gradio.app/'>Gradio</a> UI using <a href='https://platform.openai.com/'>OpenAI</a> API via AI-first
62
  <a href='https://www.langchain.com/'>LangChain</a> toolkit with <a href='https://openai.com/research/whisper'>Whisper</a> (speech-to-text) and
63
  <a href='https://openai.com/research/gpt-4'>GPT-4</a> (LLM) foundation models as well as AI-native <a href='https://www.trychroma.com/'>Chroma</a>
@@ -65,7 +67,7 @@ description = """<strong>Overview:</strong> The app demonstrates how to use a La
65
 
66
  gr.close_all()
67
  demo = gr.Interface(fn=invoke,
68
- inputs = [gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1), gr.Textbox(label = "YouTube URL", value = "https://www.youtube.com/watch?v=--khbXchTeE", lines = 1), gr.Radio([True, False], label="Process Video", value = False), gr.Textbox(label = "Prompt", value = "what is gpt-4", lines = 1)],
69
  outputs = [gr.Textbox(label = "Completion", lines = 1)],
70
  title = "Generative AI - LLM & RAG",
71
  description = description)
 
25
  CHROMA_DIR = "docs/chroma"
26
  YOUTUBE_DIR = "docs/youtube"
27
 
28
+ YOUTUBE_URL = " playlist"
29
+
30
  MODEL_NAME = "gpt-4"
31
 
32
+ def invoke(openai_api_key, use_rag, prompt):
33
+ if (os.path.isdir(CHROMA_DIR)):
34
+ shutil.rmtree(CHROMA_DIR)
35
+ if (os.path.isdir(YOUTUBE_DIR)):
36
+ shutil.rmtree(YOUTUBE_DIR)
37
+ if (use_rag):
38
+ loader = GenericLoader(YoutubeAudioLoader([YOUTUBE_URL], YOUTUBE_DIR), OpenAIWhisperParser())
39
  docs = loader.load()
40
  text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1500, chunk_overlap = 150)
41
  splits = text_splitter.split_documents(docs)
42
  vector_db = Chroma.from_documents(documents = splits, embedding = OpenAIEmbeddings(), persist_directory = CHROMA_DIR)
43
+ #else:
44
+ # vector_db = Chroma(persist_directory = CHROMA_DIR, embedding_function = OpenAIEmbeddings())
45
  llm = ChatOpenAI(model_name = MODEL_NAME, openai_api_key = openai_api_key, temperature = 0)
46
  qa_chain = RetrievalQA.from_chain_type(llm, retriever = vector_db.as_retriever(search_kwargs = {"k": 3}), return_source_documents = True, chain_type_kwargs = {"prompt": QA_CHAIN_PROMPT})
47
  result = qa_chain({"query": prompt})
 
52
  (in this case YouTube videos, but it could be PDFs, URLs, or other structured/unstructured private/public
53
  <a href='https://raw.githubusercontent.com/bstraehle/ai-ml-dl/c38b224c196fc984aab6b6cc6bdc666f8f4fbcff/langchain/document-loaders.png'>data sources</a>).\n\n
54
  <strong>Instructions:</strong> Enter an OpenAI API key and perform LLM use cases on a YouTube video (semantic search, sentiment analysis, summarization,
55
+ translation, etc.) The example is a <a href='c'>short video about GPT-4</a>.
56
  <ul style="list-style-type:square;">
57
  <li>Set "Process Video" to "False" and submit prompt "what is gpt-4". The LLM <strong>without</strong> RAG does not know the answer.</li>
58
  <li>Set "Process Video" to "True" and submit prompt "what is gpt-4". The LLM <strong>with</strong> RAG knows the answer.</li>
59
  <li>Set "Process Video" to "False" and experiment with different prompts, for example "what is gpt-4, answer in german" or "write a haiku about gpt-4".</li>
60
  </ul>
61
  In a production system processing external data would be done in a batch process. An idea for a production system would be to perform LLM use cases on the
62
+ <a href='https://www.youtube.com/playlist?list=PL2yQDdvlhXf_hIzmfHCdbcXj2hS52oP9r'>AWS re:Invent playlist</a>.\n\n
63
  <strong>Technology:</strong> <a href='https://www.gradio.app/'>Gradio</a> UI using <a href='https://platform.openai.com/'>OpenAI</a> API via AI-first
64
  <a href='https://www.langchain.com/'>LangChain</a> toolkit with <a href='https://openai.com/research/whisper'>Whisper</a> (speech-to-text) and
65
  <a href='https://openai.com/research/gpt-4'>GPT-4</a> (LLM) foundation models as well as AI-native <a href='https://www.trychroma.com/'>Chroma</a>
 
67
 
68
  gr.close_all()
69
  demo = gr.Interface(fn=invoke,
70
+ inputs = [gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1), gr.Radio([True, False], label="Use RAG", value = False), gr.Textbox(label = "Prompt", value = "what is gpt-4", lines = 1)],
71
  outputs = [gr.Textbox(label = "Completion", lines = 1)],
72
  title = "Generative AI - LLM & RAG",
73
  description = description)