diff --git a/Solar-Fullstack-LLM-101/82_gradio_chatpdf.ipynb b/Solar-Fullstack-LLM-101/82_gradio_chatpdf.ipynb
index d3c0138..b02c8b9 100644
--- a/Solar-Fullstack-LLM-101/82_gradio_chatpdf.ipynb
+++ b/Solar-Fullstack-LLM-101/82_gradio_chatpdf.ipynb
@@ -1,231 +1,299 @@
{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 82. Gradio_chatpdf\n",
- "\n",
- "## Overview \n",
- "In this exercise, we will utilize all the RAG (Retrieval-Augmented Generation) techniques we have previously learned. We will begin by uploading a document, analyzing it, splitting it into smaller chunks, and vectorizing these chunks to store them in a vector database. When a user submits a query, the system will search the vector database for relevant information and generate real-time responses. This process will handle real-time interactions with users and create an interactive chatbot interface with search functionality.\n",
- " \n",
- "## Purpose of the Exercise\n",
- "The purpose of this exercise is to utilize all the previously learned techniques to build a comprehensive RAG system capable of processing user queries in real-time. By the end of this tutorial, users will be able to create a robust application that efficiently retrieves and responds to user queries using document-based data, enhancing the user experience with accurate and relevant information.\n",
- "\n"
- ]
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyMgmvPljJ91rsE+UlswPqjw",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
},
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "!pip install -qU gradio python-dotenv langchain-upstage python-dotenv"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# @title set API key\n",
- "import os\n",
- "import getpass\n",
- "from pprint import pprint\n",
- "import warnings\n",
- "\n",
- "warnings.filterwarnings(\"ignore\")\n",
- "\n",
- "from IPython import get_ipython\n",
- "\n",
- "if \"google.colab\" in str(get_ipython()):\n",
- " # Running in Google Colab. Please set the UPSTAGE_API_KEY in the Colab Secrets\n",
- " from google.colab import userdata\n",
- " os.environ[\"UPSTAGE_API_KEY\"] = userdata.get(\"UPSTAGE_API_KEY\")\n",
- "else:\n",
- " # Running locally. Please set the UPSTAGE_API_KEY in the .env file\n",
- " from dotenv import load_dotenv\n",
- "\n",
- " load_dotenv()\n",
- "\n",
- "if \"UPSTAGE_API_KEY\" not in os.environ:\n",
- " os.environ[\"UPSTAGE_API_KEY\"] = getpass.getpass(\"Enter your Upstage API key: \")\n"
- ]
-
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "import gradio as gr\n",
- "\n",
- "from langchain_upstage import (\n",
- " ChatUpstage,\n",
- " UpstageEmbeddings,\n",
- " UpstageLayoutAnalysisLoader,\n",
- ")\n",
- "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
- "from langchain_core.output_parsers import StrOutputParser\n",
- "from langchain.schema import AIMessage, HumanMessage\n",
- "from langchain_text_splitters import (\n",
- " Language,\n",
- " RecursiveCharacterTextSplitter,\n",
- ")\n",
- "from langchain_chroma import Chroma\n",
- "\n",
- "llm = ChatUpstage(streaming=True)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# More general chat\n",
- "chat_with_history_prompt = ChatPromptTemplate.from_messages(\n",
- " [\n",
- " (\"system\", \"You are a helpful assistant.\\nContext: {context}\"),\n",
- " MessagesPlaceholder(variable_name=\"history\"),\n",
- " (\"human\", \"{message}\"),\n",
- " ]\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "chain = chat_with_history_prompt | llm | StrOutputParser()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "text_splitter = RecursiveCharacterTextSplitter.from_language(\n",
- " chunk_size=1000, chunk_overlap=100, language=Language.HTML\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "def chat(message, history, retriever):\n",
- " result_docs = \"\"\n",
- " if retriever:\n",
- " result_docs = retriever.invoke(message)\n",
- "\n",
- " history_langchain_format = []\n",
- " for human, ai in history:\n",
- " history_langchain_format.append(HumanMessage(content=human))\n",
- " history_langchain_format.append(AIMessage(content=ai))\n",
- "\n",
- " generator = chain.stream(\n",
- " {\n",
- " \"context\": result_docs,\n",
- " \"message\": message,\n",
- " \"history\": history_langchain_format,\n",
- " }\n",
- " )\n",
- "\n",
- " assistant = \"\"\n",
- " for gen in generator:\n",
- " assistant += gen\n",
- " yield assistant"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "def file_upload(file):\n",
- " layzer = UpstageLayoutAnalysisLoader(file, output_type=\"html\", use_ocr=False)\n",
- " docs = layzer.load()\n",
- " splits = text_splitter.split_documents(docs)\n",
- " print(len(splits))\n",
- " vectorstore = Chroma.from_documents(\n",
- " documents=splits,\n",
- " embedding=UpstageEmbeddings(\n",
- " model=\"solar-embedding-1-large\", embed_batch_size=100\n",
- " ),\n",
- " )\n",
- " retriever = vectorstore.as_retriever()\n",
- "\n",
- " return file, retriever"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "with gr.Blocks() as demo:\n",
- " gr.Markdown(\"# Solar Chatbot\")\n",
- " gr.Markdown(\n",
- " \"Upstage Solar Chatbot\",\n",
- " )\n",
- " with gr.Row():\n",
- " with gr.Column():\n",
- " file = gr.File()\n",
- " retreiver = gr.State()\n",
- " with gr.Column():\n",
- " chatbot = gr.ChatInterface(\n",
- " chat,\n",
- " examples=[\n",
- " [\"How to eat healthy?\"],\n",
- " [\"Best Places in Korea\"],\n",
- " [\"How to make a chatbot?\"],\n",
- " ],\n",
- " additional_inputs=retreiver,\n",
- " )\n",
- " chatbot.chatbot.height = 300\n",
- " file.upload(file_upload, file, [file, retreiver])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "if __name__ == \"__main__\":\n",
- " demo.launch()"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3 (ipykernel)",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.10.10"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# 82. Gradio_chatpdf\n",
+ "## Overview\n",
+ "In this exercise, we will utilize all the RAG (Retrieval-Augmented Generation) techniques we have previously learned. We will begin by uploading a document, analyzing it, splitting it into smaller chunks, and vectorizing these chunks to store them in a vector database. When a user submits a query, the system will search the vector database for relevant information and generate real-time responses. This process will handle real-time interactions with users and create an interactive chatbot interface with search functionality.\n",
+ "\n",
+ "\n",
+ "\n",
+ "## Purpose of the Exercise\n",
+ "\n",
+ "The purpose of this exercise is to utilize all the previously learned techniques to build a comprehensive RAG system capable of processing user queries in real-time. By the end of this tutorial, users will be able to create a robust application that efficiently retrieves and responds to user queries using document-based data, enhancing the user experience with accurate and relevant information.\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "zI1N80v6TcRf"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "!pip install -qU gradio python-dotenv langchain-upstage python-dotenv langchain langchain-chroma"
+ ],
+ "metadata": {
+ "id": "xYzD-_KLTcEw"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "id": "jF72fUedTWgg"
+ },
+ "outputs": [],
+ "source": [
+ "# @title set API key\n",
+ "from pprint import pprint\n",
+ "import os\n",
+ "\n",
+ "import warnings\n",
+ "\n",
+ "warnings.filterwarnings(\"ignore\")\n",
+ "\n",
+ "if \"google.colab\" in str(get_ipython()):\n",
+ " # Running in Google Colab. Please set the UPSTAGE_API_KEY in the Colab Secrets\n",
+ " from google.colab import userdata\n",
+ "\n",
+ " os.environ[\"UPSTAGE_API_KEY\"] = userdata.get(\"UPSTAGE_API_KEY\")\n",
+ "else:\n",
+ " # Running locally. Please set the UPSTAGE_API_KEY in the .env file\n",
+ " from dotenv import load_dotenv\n",
+ "\n",
+ " load_dotenv()\n",
+ "\n",
+ "assert (\n",
+ " \"UPSTAGE_API_KEY\" in os.environ\n",
+ "), \"Please set the UPSTAGE_API_KEY environment variable\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from langchain_upstage import (\n",
+ " ChatUpstage,\n",
+ " UpstageEmbeddings,\n",
+ " UpstageLayoutAnalysisLoader,\n",
+ ")\n",
+ "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
+ "from langchain_core.output_parsers import StrOutputParser\n",
+ "from langchain.schema import AIMessage, HumanMessage\n",
+ "from langchain_text_splitters import (\n",
+ " Language,\n",
+ " RecursiveCharacterTextSplitter,\n",
+ ")\n",
+ "from langchain_chroma import Chroma\n",
+ "\n",
+ "llm = ChatUpstage(model=\"solar-pro\", streaming=True)"
+ ],
+ "metadata": {
+ "id": "5CEG-_rcUD6x"
+ },
+ "execution_count": 10,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# More general chat\n",
+ "chat_with_history_prompt = ChatPromptTemplate.from_messages(\n",
+ " [\n",
+ " (\"system\", \"You are a helpful assistant.\\nContext: {context}\"),\n",
+ " MessagesPlaceholder(variable_name=\"history\"),\n",
+ " (\"human\", \"{message}\"),\n",
+ " ]\n",
+ ")"
+ ],
+ "metadata": {
+ "id": "QOdLNIegUFb2"
+ },
+ "execution_count": 11,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "chain = chat_with_history_prompt | llm | StrOutputParser()"
+ ],
+ "metadata": {
+ "id": "QZjsCZumUHbr"
+ },
+ "execution_count": 12,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "text_splitter = RecursiveCharacterTextSplitter.from_language(\n",
+ " chunk_size=1000, chunk_overlap=100, language=Language.HTML\n",
+ ")"
+ ],
+ "metadata": {
+ "id": "jF3r0rhOUJGz"
+ },
+ "execution_count": 13,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def chat(message, history, retriever):\n",
+ " result_docs = \"\"\n",
+ " if retriever:\n",
+ " result_docs = retriever.invoke(message)\n",
+ "\n",
+ " history_langchain_format = []\n",
+ " for human, ai in history:\n",
+ " history_langchain_format.append(HumanMessage(content=human))\n",
+ " history_langchain_format.append(AIMessage(content=ai))\n",
+ "\n",
+ " generator = chain.stream(\n",
+ " {\n",
+ " \"context\": result_docs,\n",
+ " \"message\": message,\n",
+ " \"history\": history_langchain_format,\n",
+ " }\n",
+ " )\n",
+ "\n",
+ " assistant = \"\"\n",
+ " for gen in generator:\n",
+ " assistant += gen\n",
+ " yield assistant"
+ ],
+ "metadata": {
+ "id": "ElY_a4wpUKiK"
+ },
+ "execution_count": 14,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def file_upload(file):\n",
+ " layzer = UpstageLayoutAnalysisLoader(file, output_type=\"html\", use_ocr=False)\n",
+ " docs = layzer.load()\n",
+ " splits = text_splitter.split_documents(docs)\n",
+ " print(len(splits))\n",
+ " vectorstore = Chroma.from_documents(\n",
+ " documents=splits,\n",
+ " embedding=UpstageEmbeddings(\n",
+ " model=\"solar-embedding-1-large\", embed_batch_size=100\n",
+ " ),\n",
+ " )\n",
+ " retriever = vectorstore.as_retriever()\n",
+ "\n",
+ " return file, retriever"
+ ],
+ "metadata": {
+ "id": "rVjsbzURUMGt"
+ },
+ "execution_count": 15,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import gradio as gr\n",
+ "\n",
+ "with gr.Blocks() as demo:\n",
+ " gr.Markdown(\"# Solar Chatbot\")\n",
+ " gr.Markdown(\n",
+ " \"Upstage Solar Chatbot\",\n",
+ " )\n",
+ " with gr.Row():\n",
+ " with gr.Column():\n",
+ " file = gr.File()\n",
+ " retreiver = gr.State()\n",
+ " with gr.Column():\n",
+ " chatbot = gr.ChatInterface(\n",
+ " chat,\n",
+ " examples=[\n",
+ " [\"How to eat healthy?\"],\n",
+ " [\"Best Places in Korea\"],\n",
+ " [\"How to make a chatbot?\"],\n",
+ " ],\n",
+ " additional_inputs=retreiver,\n",
+ " )\n",
+ " chatbot.chatbot.height = 300\n",
+ " file.upload(file_upload, file, [file, retreiver])"
+ ],
+ "metadata": {
+ "id": "14BHwVqnUNat"
+ },
+ "execution_count": 16,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "\n",
+ "if __name__ == \"__main__\":\n",
+ " demo.launch()"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 625
+ },
+ "id": "A4hN3whzUOp-",
+ "outputId": "f35f196d-c168-43aa-c84f-3ab1dd8809dc"
+ },
+ "execution_count": 17,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
+ "\n",
+ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
+ "Running on public URL: https://e0236f979583f8e11a.gradio.live\n",
+ "\n",
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
+ ]
+ },
+ {
+ "output_type": "display_data",
+ "data": {
+ "text/plain": [
+ ""
+ ],
+ "text/html": [
+ ""
+ ]
+ },
+ "metadata": {}
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Example"
+ ],
+ "metadata": {
+ "id": "HXRIpmF3VX8C"
+ }
+ }
+ ]
}