Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_community.vectorstores import Chroma
|
| 7 |
from langchain.chains import ConversationalRetrievalChain
|
| 8 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 10 |
from langchain.chains import ConversationChain
|
| 11 |
from langchain.memory import ConversationBufferMemory
|
| 12 |
from langchain_community.llms import HuggingFaceEndpoint
|
|
@@ -14,14 +11,11 @@ from langchain_community.llms import HuggingFaceEndpoint
|
|
| 14 |
from pathlib import Path
|
| 15 |
import chromadb
|
| 16 |
from unidecode import unidecode
|
| 17 |
-
|
| 18 |
-
from transformers import AutoTokenizer, AutoModel
|
| 19 |
-
import torch
|
| 20 |
import re
|
| 21 |
|
| 22 |
-
#
|
| 23 |
LLM_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 24 |
-
LLM_MAX_TOKEN = 512
|
| 25 |
DB_CHUNK_SIZE = 512
|
| 26 |
CHUNK_OVERLAP = 24
|
| 27 |
TEMPERATURE = 0.1
|
|
@@ -50,13 +44,13 @@ def create_db(splits, collection_name):
|
|
| 50 |
return vectordb
|
| 51 |
|
| 52 |
# Initialize langchain LLM chain
|
| 53 |
-
def initialize_llmchain(llm_model,
|
| 54 |
progress(0.5, desc="Initializing HF Hub...")
|
| 55 |
|
| 56 |
-
#
|
| 57 |
tokenizer = AutoTokenizer.from_pretrained(llm_model)
|
| 58 |
-
model =
|
| 59 |
-
pipe =
|
| 60 |
|
| 61 |
progress(0.75, desc="Defining buffer memory...")
|
| 62 |
memory = ConversationBufferMemory(
|
|
@@ -102,8 +96,8 @@ def initialize_database(pdf_url, chunk_size, chunk_overlap, progress=gr.Progress
|
|
| 102 |
progress(0.9, desc="Done!")
|
| 103 |
return vector_db, collection_name, "Complete!"
|
| 104 |
|
| 105 |
-
def initialize_LLM(
|
| 106 |
-
qa_chain = initialize_llmchain(LLM_MODEL,
|
| 107 |
return qa_chain, "Complete!"
|
| 108 |
|
| 109 |
def format_chat_history(message, chat_history):
|
|
@@ -172,7 +166,7 @@ def demo():
|
|
| 172 |
|
| 173 |
def auto_initialize():
|
| 174 |
vector_db, collection_name, db_status = initialize_database(pdf_url, DB_CHUNK_SIZE, CHUNK_OVERLAP)
|
| 175 |
-
qa_chain, llm_status = initialize_LLM(
|
| 176 |
return vector_db, collection_name, db_status, qa_chain, llm_status, "Initialization complete."
|
| 177 |
|
| 178 |
demo.load(auto_initialize, [], [vector_db, collection_name, db_progress, qa_chain, llm_progress])
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
from langchain_community.vectorstores import Chroma
|
| 5 |
from langchain.chains import ConversationalRetrievalChain
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 7 |
from langchain.chains import ConversationChain
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
| 9 |
from langchain_community.llms import HuggingFaceEndpoint
|
|
|
|
| 11 |
from pathlib import Path
|
| 12 |
import chromadb
|
| 13 |
from unidecode import unidecode
|
| 14 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
|
|
|
|
| 15 |
import re
|
| 16 |
|
| 17 |
+
# Constants
|
| 18 |
LLM_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
| 19 |
DB_CHUNK_SIZE = 512
|
| 20 |
CHUNK_OVERLAP = 24
|
| 21 |
TEMPERATURE = 0.1
|
|
|
|
| 44 |
return vectordb
|
| 45 |
|
| 46 |
# Initialize langchain LLM chain
|
| 47 |
+
def initialize_llmchain(llm_model, vector_db, progress=gr.Progress()):
|
| 48 |
progress(0.5, desc="Initializing HF Hub...")
|
| 49 |
|
| 50 |
+
# Create the HuggingFacePipeline for the model
|
| 51 |
tokenizer = AutoTokenizer.from_pretrained(llm_model)
|
| 52 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(llm_model)
|
| 53 |
+
pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 54 |
|
| 55 |
progress(0.75, desc="Defining buffer memory...")
|
| 56 |
memory = ConversationBufferMemory(
|
|
|
|
| 96 |
progress(0.9, desc="Done!")
|
| 97 |
return vector_db, collection_name, "Complete!"
|
| 98 |
|
| 99 |
+
def initialize_LLM(vector_db, progress=gr.Progress()):
|
| 100 |
+
qa_chain = initialize_llmchain(LLM_MODEL, vector_db, progress)
|
| 101 |
return qa_chain, "Complete!"
|
| 102 |
|
| 103 |
def format_chat_history(message, chat_history):
|
|
|
|
| 166 |
|
| 167 |
def auto_initialize():
|
| 168 |
vector_db, collection_name, db_status = initialize_database(pdf_url, DB_CHUNK_SIZE, CHUNK_OVERLAP)
|
| 169 |
+
qa_chain, llm_status = initialize_LLM(vector_db)
|
| 170 |
return vector_db, collection_name, db_status, qa_chain, llm_status, "Initialization complete."
|
| 171 |
|
| 172 |
demo.load(auto_initialize, [], [vector_db, collection_name, db_progress, qa_chain, llm_progress])
|