Spaces:
Sleeping
Sleeping
Commit
·
3dff4cb
1
Parent(s):
be312e0
Updating Chat bot
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch as th
|
| 3 |
|
| 4 |
from langchain.document_loaders import PDFMinerLoader,CSVLoader ,UnstructuredWordDocumentLoader,TextLoader
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 7 |
-
from langchain.vectorstores import
|
| 8 |
from langchain import HuggingFaceHub
|
| 9 |
|
| 10 |
|
|
@@ -16,6 +16,15 @@ def loading_pdf():
|
|
| 16 |
return "Loading..."
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def process_documents(documents,data_chunk=1000,chunk_overlap=50):
|
| 20 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=data_chunk, chunk_overlap=chunk_overlap)
|
| 21 |
texts = text_splitter.split_documents(documents[0])
|
|
@@ -27,8 +36,7 @@ def get_hugging_face_model(model_id,API_key,temperature=0.1):
|
|
| 27 |
model_kwargs={"temperature": temperature, "max_new_tokens": 2048})
|
| 28 |
return chat_llm
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
embedding_model = SentenceTransformerEmbeddings(model_name='all-mpnet-base-v2',model_kwargs={"device": DEVICE})
|
| 33 |
|
| 34 |
document = None
|
|
@@ -43,6 +51,12 @@ def document_loading(file_data,doc_type='pdf',key=None):
|
|
| 43 |
|
| 44 |
texts = process_documents(documents=document)
|
| 45 |
vectordb = FAISS.from_documents(documents=texts, embedding= embedding_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
def process_text_document(document_file_name):
|
|
@@ -104,3 +118,5 @@ with gr.Blocks(css=css) as demo:
|
|
| 104 |
chatbot = gr.Chatbot()
|
| 105 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
| 106 |
submit_button = gr.Button("Send Message")
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
from langchain.document_loaders import PDFMinerLoader,CSVLoader ,UnstructuredWordDocumentLoader,TextLoader
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
from langchain import HuggingFaceHub
|
| 9 |
|
| 10 |
|
|
|
|
| 16 |
return "Loading..."
|
| 17 |
|
| 18 |
|
| 19 |
+
def get_openai_chat_model(API_key):
|
| 20 |
+
try:
|
| 21 |
+
from langchain.llms import OpenAI
|
| 22 |
+
except ImportError as err:
|
| 23 |
+
raise "{}, unable to load openAI. Please install openai and add OPENAIAPI_KEY"
|
| 24 |
+
os.environ["OPENAI_API_KEY"] = API_key
|
| 25 |
+
llm = OpenAI()
|
| 26 |
+
return llm
|
| 27 |
+
|
| 28 |
def process_documents(documents,data_chunk=1000,chunk_overlap=50):
|
| 29 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=data_chunk, chunk_overlap=chunk_overlap)
|
| 30 |
texts = text_splitter.split_documents(documents[0])
|
|
|
|
| 36 |
model_kwargs={"temperature": temperature, "max_new_tokens": 2048})
|
| 37 |
return chat_llm
|
| 38 |
|
| 39 |
+
def chat_api(file_data,doc_type='pdf',key=None,llm_model='HuggingFace'):
|
|
|
|
| 40 |
embedding_model = SentenceTransformerEmbeddings(model_name='all-mpnet-base-v2',model_kwargs={"device": DEVICE})
|
| 41 |
|
| 42 |
document = None
|
|
|
|
| 51 |
|
| 52 |
texts = process_documents(documents=document)
|
| 53 |
vectordb = FAISS.from_documents(documents=texts, embedding= embedding_model)
|
| 54 |
+
if llm_model == 'HuggingFace':
|
| 55 |
+
llm = get_hugging_face_model(model_id='tiiuae/falcon-7b-instruct',API_key=key)
|
| 56 |
+
else:
|
| 57 |
+
llm_model = get_openai_chat_model(API_key=key)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
|
| 61 |
|
| 62 |
def process_text_document(document_file_name):
|
|
|
|
| 118 |
chatbot = gr.Chatbot()
|
| 119 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
| 120 |
submit_button = gr.Button("Send Message")
|
| 121 |
+
load_pdf.click(loading_pdf, None, langchain_status, queue=False)
|
| 122 |
+
load_pdf.click(chat_api, inputs=[pdf_doc,file_extension,API_key,LLM_option], outputs=[langchain_status], queue=False)
|