Spaces:
Sleeping
Sleeping
Samarth991
commited on
Commit
•
e9840df
1
Parent(s):
278cbaa
adding my LLM-chatbot model
Browse files- app.py +105 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Chroma, FAISS
|
8 |
+
from langchain import HuggingFaceHub
|
9 |
+
|
10 |
+
|
11 |
+
DEVICE = 'cpu '
|
12 |
+
FILE_EXT = ['pdf','text','csv','word','wav']
|
13 |
+
|
14 |
+
|
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])
|
22 |
+
return texts
|
23 |
+
|
24 |
+
def get_hugging_face_model(model_id,API_key,temperature=0.1):
|
25 |
+
chat_llm = HuggingFaceHub(huggingfacehub_api_token=API_key,
|
26 |
+
repo_id=model_id,
|
27 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": 2048})
|
28 |
+
return chat_llm
|
29 |
+
|
30 |
+
def document_loading(file_data,doc_type='pdf',key=None):
|
31 |
+
|
32 |
+
embedding_model = SentenceTransformerEmbeddings(model_name='all-mpnet-base-v2',model_kwargs={"device": DEVICE})
|
33 |
+
|
34 |
+
document = None
|
35 |
+
if doc_type == 'pdf':
|
36 |
+
document = process_pdf_document(document_file_name=file_data)
|
37 |
+
elif doc_type == 'text':
|
38 |
+
document = process_text_document(document_file_name=file_data)
|
39 |
+
elif doc_type == 'csv':
|
40 |
+
document = process_csv_document(document_file_name=file_data)
|
41 |
+
elif doc_type == 'word':
|
42 |
+
document = process_word_document(document_file_name=file_data)
|
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):
|
49 |
+
loader = TextLoader(document_file_name)
|
50 |
+
document = loader.load()
|
51 |
+
return document
|
52 |
+
|
53 |
+
|
54 |
+
def process_csv_document(document_file_name):
|
55 |
+
loader = CSVLoader(file_path=document_file_name)
|
56 |
+
document = loader.load()
|
57 |
+
return document
|
58 |
+
|
59 |
+
|
60 |
+
def process_word_document(document_file_name):
|
61 |
+
loader = UnstructuredWordDocumentLoader(file_path=document_file_name)
|
62 |
+
document = loader.load()
|
63 |
+
return document
|
64 |
+
|
65 |
+
|
66 |
+
def process_pdf_document(document_file_name):
|
67 |
+
loader = PDFMinerLoader(document_file_name)
|
68 |
+
document = loader.load()[0]
|
69 |
+
return document
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
css="""
|
76 |
+
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
77 |
+
"""
|
78 |
+
|
79 |
+
title = """
|
80 |
+
<div style="text-align: center;max-width: 700px;">
|
81 |
+
<h1>Chat with Data • OpenAI/HuggingFace</h1>
|
82 |
+
<p style="text-align: center;">Upload a file from your computer, click the "Load data to LangChain" button, <br />
|
83 |
+
when everything is ready, you can start asking questions about the data you uploaded ;) <br />
|
84 |
+
This version is just for QA retrival so it will not use chat history, and uses Hugging face as LLM,
|
85 |
+
so you don't need any key</p>
|
86 |
+
</div>
|
87 |
+
"""
|
88 |
+
|
89 |
+
with gr.Blocks(css=css) as demo:
|
90 |
+
with gr.Column(elem_id="col-container"):
|
91 |
+
gr.HTML(title)
|
92 |
+
|
93 |
+
with gr.Column():
|
94 |
+
with gr.Box():
|
95 |
+
LLM_option = gr.Dropdown(['HuggingFace','OpenAI'],label='LLM',info='select the LLM to be used')
|
96 |
+
API_key = gr.Textbox(label="You OpenAI/Huggingface API key", type="password")
|
97 |
+
with gr.Column():
|
98 |
+
file_extension = gr.Dropdown(FILE_EXT, label="File Extensions", info="Select your files extensions!")
|
99 |
+
pdf_doc = gr.File(label="Load a File", file_types=FILE_EXT, type="file")
|
100 |
+
with gr.Row():
|
101 |
+
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
|
102 |
+
load_pdf = gr.Button("Load file to langchain")
|
103 |
+
|
104 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
|
105 |
+
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
tiktoken
|
3 |
+
chromadb
|
4 |
+
langchain
|
5 |
+
unstructured
|
6 |
+
unstructured[local-inference]
|
7 |
+
transformers
|
8 |
+
|