Update app.py
Browse filesCorrect error message from "Pipeline" to "pipeline" in get_llm()
app.py
CHANGED
|
@@ -1,155 +1,159 @@
|
|
| 1 |
-
# from ibm_watsonx_ai.foundation_models import ModelInference
|
| 2 |
-
# from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
| 3 |
-
# from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames
|
| 4 |
-
# from ibm_watsonx_ai import Credentials
|
| 5 |
-
# from langchain_ibm import WatsonxLLM, WatsonxEmbeddings
|
| 6 |
-
# from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
-
# from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 8 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
-
from langchain_community.vectorstores import Chroma
|
| 10 |
-
from langchain_community.document_loaders import PyPDFLoader
|
| 11 |
-
# from langchain.chains import RetrievalQA
|
| 12 |
-
from langchain_classic.chains import RetrievalQA
|
| 13 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 14 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 15 |
-
from langchain_huggingface import HuggingFacePipeline
|
| 16 |
-
import torch
|
| 17 |
-
|
| 18 |
-
from huggingface_hub import HfFolder
|
| 19 |
-
import gradio as gr
|
| 20 |
-
|
| 21 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, Pipeline, BitsAndBytesConfig
|
| 22 |
-
|
| 23 |
-
## You can use this section to suppress warnings generated by your code:
|
| 24 |
-
def warn(*args, **kwargs):
|
| 25 |
-
pass
|
| 26 |
-
import warnings
|
| 27 |
-
warnings.warn = warn
|
| 28 |
-
warnings.filterwarnings('ignore')
|
| 29 |
-
|
| 30 |
-
## LLM
|
| 31 |
-
# def get_llm():
|
| 32 |
-
# model_id = 'ibm/granite-3-2-8b-instruct'
|
| 33 |
-
# parameters = {
|
| 34 |
-
# GenParams.MAX_NEW_TOKENS: 256, # This controls the maximum number of tokens in the generated output
|
| 35 |
-
# GenParams.TEMPERATURE: 0.5, # This controls randomness or creativity of the model's responses
|
| 36 |
-
# }
|
| 37 |
-
# project_id = 'skills-network'
|
| 38 |
-
# watsonx_llm = WatsonxLLM(
|
| 39 |
-
# model_id = model_id,
|
| 40 |
-
# url="https://us-south.ml.cloud.ibm.com",
|
| 41 |
-
# project_id = project_id,
|
| 42 |
-
# params = parameters,
|
| 43 |
-
# )
|
| 44 |
-
# return watsonx_llm
|
| 45 |
-
|
| 46 |
-
def get_llm():
|
| 47 |
-
model_id = 'ibm-granite/granite-3.0-8b-instruct'
|
| 48 |
-
|
| 49 |
-
# Configuration for 4-bit compression
|
| 50 |
-
quantization_config = BitsAndBytesConfig(
|
| 51 |
-
load_in_4bit=True,
|
| 52 |
-
bnb_4bit_compute_dtype=torch.float16,
|
| 53 |
-
bnb_4bit_quant_type="nf4",
|
| 54 |
-
bnb_4bit_use_double_quant=True,
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 58 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 59 |
-
model_id,
|
| 60 |
-
quantization_config=quantization_config,
|
| 61 |
-
device_map='cpu',
|
| 62 |
-
torch_dtype='float32',
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
pipeline =
|
| 66 |
-
model=model,
|
| 67 |
-
tokenizer=tokenizer,
|
| 68 |
-
max_new_tokens=256,
|
| 69 |
-
temperature= 0.3,
|
| 70 |
-
do_sample= True
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
return HuggingFacePipeline(pipeline=pipeline)
|
| 74 |
-
|
| 75 |
-
## Document loader
|
| 76 |
-
def document_loader(file):
|
| 77 |
-
loader = PyPDFLoader(file)
|
| 78 |
-
loaded_document = loader.load()
|
| 79 |
-
return loaded_document
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#
|
| 94 |
-
#
|
| 95 |
-
# EmbedTextParamsMetaNames.
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
#
|
| 100 |
-
#
|
| 101 |
-
#
|
| 102 |
-
#
|
| 103 |
-
#
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from ibm_watsonx_ai.foundation_models import ModelInference
|
| 2 |
+
# from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
| 3 |
+
# from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames
|
| 4 |
+
# from ibm_watsonx_ai import Credentials
|
| 5 |
+
# from langchain_ibm import WatsonxLLM, WatsonxEmbeddings
|
| 6 |
+
# from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
# from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 8 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
+
from langchain_community.vectorstores import Chroma
|
| 10 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 11 |
+
# from langchain.chains import RetrievalQA
|
| 12 |
+
from langchain_classic.chains import RetrievalQA
|
| 13 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 14 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 15 |
+
from langchain_huggingface import HuggingFacePipeline
|
| 16 |
+
import torch
|
| 17 |
+
|
| 18 |
+
from huggingface_hub import HfFolder
|
| 19 |
+
import gradio as gr
|
| 20 |
+
|
| 21 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, Pipeline, BitsAndBytesConfig
|
| 22 |
+
|
| 23 |
+
## You can use this section to suppress warnings generated by your code:
|
| 24 |
+
def warn(*args, **kwargs):
|
| 25 |
+
pass
|
| 26 |
+
import warnings
|
| 27 |
+
warnings.warn = warn
|
| 28 |
+
warnings.filterwarnings('ignore')
|
| 29 |
+
|
| 30 |
+
## LLM
|
| 31 |
+
# def get_llm():
|
| 32 |
+
# model_id = 'ibm/granite-3-2-8b-instruct'
|
| 33 |
+
# parameters = {
|
| 34 |
+
# GenParams.MAX_NEW_TOKENS: 256, # This controls the maximum number of tokens in the generated output
|
| 35 |
+
# GenParams.TEMPERATURE: 0.5, # This controls randomness or creativity of the model's responses
|
| 36 |
+
# }
|
| 37 |
+
# project_id = 'skills-network'
|
| 38 |
+
# watsonx_llm = WatsonxLLM(
|
| 39 |
+
# model_id = model_id,
|
| 40 |
+
# url="https://us-south.ml.cloud.ibm.com",
|
| 41 |
+
# project_id = project_id,
|
| 42 |
+
# params = parameters,
|
| 43 |
+
# )
|
| 44 |
+
# return watsonx_llm
|
| 45 |
+
|
| 46 |
+
def get_llm():
|
| 47 |
+
model_id = 'ibm-granite/granite-3.0-8b-instruct'
|
| 48 |
+
|
| 49 |
+
# Configuration for 4-bit compression
|
| 50 |
+
quantization_config = BitsAndBytesConfig(
|
| 51 |
+
load_in_4bit=True,
|
| 52 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 53 |
+
bnb_4bit_quant_type="nf4",
|
| 54 |
+
bnb_4bit_use_double_quant=True,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 58 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 59 |
+
model_id,
|
| 60 |
+
quantization_config=quantization_config,
|
| 61 |
+
device_map='cpu',
|
| 62 |
+
torch_dtype='float32',
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
pipeline = pipeline(
|
| 66 |
+
model=model,
|
| 67 |
+
tokenizer=tokenizer,
|
| 68 |
+
max_new_tokens=256,
|
| 69 |
+
temperature= 0.3,
|
| 70 |
+
do_sample= True
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return HuggingFacePipeline(pipeline=pipeline)
|
| 74 |
+
|
| 75 |
+
## Document loader
|
| 76 |
+
def document_loader(file):
|
| 77 |
+
loader = PyPDFLoader(file)
|
| 78 |
+
loaded_document = loader.load()
|
| 79 |
+
return loaded_document
|
| 80 |
+
|
| 81 |
+
## Text splitter
|
| 82 |
+
def text_splitter(data):
|
| 83 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 84 |
+
chunk_size = 1000,
|
| 85 |
+
chunk_overlap = 50,
|
| 86 |
+
length_function = len,
|
| 87 |
+
)
|
| 88 |
+
chuncks = text_splitter.split_documents(data)
|
| 89 |
+
return chuncks
|
| 90 |
+
## Embedding model
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# def watsonx_embedding():
|
| 94 |
+
# embed_params = {
|
| 95 |
+
# EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS: 256,
|
| 96 |
+
# EmbedTextParamsMetaNames.RETURN_OPTIONS: {'input_text': True},
|
| 97 |
+
# }
|
| 98 |
+
|
| 99 |
+
# watsonx_embedding = WatsonxEmbeddings(
|
| 100 |
+
# model_id = "ibm/slate-125m-english-rtrvr-v2",
|
| 101 |
+
# url="https://us-south.ml.cloud.ibm.com",
|
| 102 |
+
# project_id = "skills-network",
|
| 103 |
+
# params = embed_params,
|
| 104 |
+
# )
|
| 105 |
+
# return watsonx_embedding
|
| 106 |
+
|
| 107 |
+
def watsonx_embedding():
|
| 108 |
+
model_id = 'sentence-transformers/all-MiniLM-L6-v2'
|
| 109 |
+
embedding_model = HuggingFaceEmbeddings(model_name=model_id)
|
| 110 |
+
return embedding_model
|
| 111 |
+
|
| 112 |
+
## Vector db
|
| 113 |
+
def vector_database(chunks):
|
| 114 |
+
|
| 115 |
+
chunks = [
|
| 116 |
+
c for c in chunks
|
| 117 |
+
if c.page_content and c.page_content.strip() and len(c.page_content.strip()) > 10
|
| 118 |
+
]
|
| 119 |
+
embedding_model = watsonx_embedding()
|
| 120 |
+
vectordb = Chroma.from_documents(chunks, embedding_model)
|
| 121 |
+
return vectordb
|
| 122 |
+
|
| 123 |
+
## Retriever
|
| 124 |
+
def retriever(file):
|
| 125 |
+
splits = document_loader(file)
|
| 126 |
+
chunks = text_splitter(splits)
|
| 127 |
+
vectordb = vector_database(chunks)
|
| 128 |
+
retriever = vectordb.as_retriever()
|
| 129 |
+
|
| 130 |
+
return retriever
|
| 131 |
+
|
| 132 |
+
## QA Chain
|
| 133 |
+
def retriever_qa(file, query):
|
| 134 |
+
llm = get_llm()
|
| 135 |
+
retriever_obj = retriever(file)
|
| 136 |
+
qa = RetrievalQA.from_chain_type(
|
| 137 |
+
llm = llm,
|
| 138 |
+
chain_type = 'stuff',
|
| 139 |
+
retriever = retriever_obj,
|
| 140 |
+
return_source_documents = False
|
| 141 |
+
)
|
| 142 |
+
response = qa.invoke(query)
|
| 143 |
+
return response['result']
|
| 144 |
+
|
| 145 |
+
## Create Gradio interface
|
| 146 |
+
rag_application = gr.Interface(
|
| 147 |
+
fn = retriever_qa,
|
| 148 |
+
# allow_flagging = 'never',
|
| 149 |
+
flagging_mode = 'never',
|
| 150 |
+
inputs = [
|
| 151 |
+
gr.File(label = 'Upload PDF File', file_count = 'single', file_types = ['.pdf'], type ='filepath'), # Drag and drop file upload
|
| 152 |
+
gr.Textbox(label= 'Input Query', lines = 2, placeholder = 'Type your question here...')
|
| 153 |
+
],
|
| 154 |
+
outputs = gr.Textbox(label = 'Ouput'),
|
| 155 |
+
title= 'PDF QA Chatbot',
|
| 156 |
+
description = 'Upload a PDF document and ask any question. The chatbot will try to answer using the provided document.'
|
| 157 |
+
)
|
| 158 |
+
# Launch the app
|
| 159 |
+
rag_application.launch(server_name= '127.0.0.1', server_port= 7860)
|