karthikeyan-r commited on
Commit
13e00d0
·
0 Parent(s):

Duplicate from ADOPLE/Mult-URL-Doc-Chatbot

Browse files
Files changed (5) hide show
  1. .gitattributes +35 -0
  2. README.md +13 -0
  3. app.py +247 -0
  4. requirements.txt +16 -0
  5. style.css +39 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Mult URL Doc Chatbot
3
+ emoji: 🏃
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.38.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: ADOPLE/Mult-URL-Doc-Chatbot
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import NoneStr
2
+ import os
3
+ from langchain.chains.question_answering import load_qa_chain
4
+ from langchain.document_loaders import UnstructuredFileLoader
5
+ from langchain.embeddings.openai import OpenAIEmbeddings
6
+ from langchain.llms import OpenAI
7
+ from langchain.text_splitter import CharacterTextSplitter
8
+ from langchain.vectorstores import FAISS
9
+ from langchain.vectorstores import Chroma
10
+ from langchain.chains import ConversationalRetrievalChain
11
+ import gradio as gr
12
+ import openai
13
+ from langchain import PromptTemplate, OpenAI, LLMChain
14
+ import validators
15
+ import requests
16
+ import mimetypes
17
+ import tempfile
18
+
19
+ class Chatbot:
20
+ def __init__(self):
21
+ openai.api_key = os.getenv("OPENAI_API_KEY")
22
+ def get_empty_state(self):
23
+
24
+ """ Create empty Knowledge base"""
25
+
26
+ return {"knowledge_base": None}
27
+
28
+ def create_knowledge_base(self,docs):
29
+
30
+ """Create a knowledge base from the given documents.
31
+ Args:
32
+ docs (List[str]): List of documents.
33
+ Returns:
34
+ FAISS: Knowledge base built from the documents.
35
+ """
36
+
37
+ # Initialize a CharacterTextSplitter to split the documents into chunks
38
+ # Each chunk has a maximum length of 500 characters
39
+ # There is no overlap between the chunks
40
+ text_splitter = CharacterTextSplitter(
41
+ separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len
42
+ )
43
+
44
+ # Split the documents into chunks using the text_splitter
45
+ chunks = text_splitter.split_documents(docs)
46
+
47
+ # Initialize an OpenAIEmbeddings model to compute embeddings of the chunks
48
+ embeddings = OpenAIEmbeddings()
49
+
50
+ # Build a knowledge base using FAISS from the chunks and their embeddings
51
+ knowledge_base = Chroma.from_documents(chunks, embeddings)
52
+
53
+ # Return the resulting knowledge base
54
+ return knowledge_base
55
+
56
+
57
+ def upload_file(self,file_paths):
58
+ """Upload a file and create a knowledge base from its contents.
59
+ Args:
60
+ file_paths : The files to uploaded.
61
+ Returns:
62
+ tuple: A tuple containing the file name and the knowledge base.
63
+ """
64
+
65
+ file_paths = [i.name for i in file_paths]
66
+ print(file_paths)
67
+
68
+
69
+ loaders = [UnstructuredFileLoader(file_obj, strategy="fast") for file_obj in file_paths]
70
+
71
+ # Load the contents of the file using the loader
72
+ docs = []
73
+ for loader in loaders:
74
+ docs.extend(loader.load())
75
+
76
+ # Create a knowledge base from the loaded documents using the create_knowledge_base() method
77
+ knowledge_base = self.create_knowledge_base(docs)
78
+
79
+
80
+ # Return a tuple containing the file name and the knowledge base
81
+ return file_paths, {"knowledge_base": knowledge_base}
82
+
83
+ def add_text(self,history, text):
84
+ history = history + [(text, None)]
85
+ return history, gr.update(value="", interactive=False)
86
+
87
+
88
+
89
+ def upload_multiple_urls(self,urls):
90
+ urlss = [url.strip() for url in urls.split(',')]
91
+ all_docs = []
92
+ file_paths = []
93
+ for url in urlss:
94
+ if validators.url(url):
95
+ headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',}
96
+ r = requests.get(url,headers=headers)
97
+ if r.status_code != 200:
98
+ raise ValueError(
99
+ "Check the url of your file; returned status code %s" % r.status_code
100
+ )
101
+ content_type = r.headers.get("content-type")
102
+ file_extension = mimetypes.guess_extension(content_type)
103
+ temp_file = tempfile.NamedTemporaryFile(suffix=file_extension, delete=False)
104
+ temp_file.write(r.content)
105
+ file_path = temp_file.name
106
+ file_paths.append(file_path)
107
+
108
+ loaders = [UnstructuredFileLoader(file_obj, strategy="fast") for file_obj in file_paths]
109
+
110
+ # Load the contents of the file using the loader
111
+ docs = []
112
+ for loader in loaders:
113
+ docs.extend(loader.load())
114
+
115
+ # Create a knowledge base from the loaded documents using the create_knowledge_base() method
116
+ knowledge_base = self.create_knowledge_base(docs)
117
+
118
+ return file_paths,{"knowledge_base":knowledge_base}
119
+
120
+ def answer_question(self, question,history,state):
121
+ """Answer a question based on the current knowledge base.
122
+ Args:
123
+ state (dict): The current state containing the knowledge base.
124
+ Returns:
125
+ str: The answer to the question.
126
+ """
127
+
128
+
129
+ # Retrieve the knowledge base from the state dictionary
130
+ knowledge_base = state["knowledge_base"]
131
+ retriever = knowledge_base.as_retriever()
132
+ qa = ConversationalRetrievalChain.from_llm(
133
+ llm=OpenAI(temperature=0.5),
134
+ retriever=retriever,
135
+ return_source_documents=False)
136
+ # Set the question for which we want to find the answer
137
+ res = []
138
+ question = history[-1][0]
139
+ for human, ai in history[:-1]:
140
+ pair = (human, ai)
141
+ res.append(pair)
142
+
143
+ chat_history = res
144
+ #print(chat_history)
145
+ query = question
146
+ result = qa({"question": query, "chat_history": chat_history})
147
+ # Perform a similarity search on the knowledge base to retrieve relevant documents
148
+ response = result["answer"]
149
+ # Return the response as the answer to the question
150
+ history[-1][1] = response
151
+ return history
152
+
153
+
154
+ def extract_excel_data(self,file_path):
155
+ # Read the Excel file
156
+ df = pd.read_excel(file_path)
157
+
158
+ # Flatten the data to a single list
159
+ data_list = []
160
+ for _, row in df.iterrows():
161
+ data_list.extend(row.tolist())
162
+
163
+ return data_list
164
+
165
+ def comparing_chemicals(self,excel_file_path,chemicals):
166
+ chemistry_capability = self.extract_excel_data(excel_file_path.name)
167
+ response = openai.Completion.create(
168
+ engine="text-davinci-003",
169
+ prompt= f"""Analyse the following text delimited by triple backticks to return the comman chemicals.
170
+ text : ```{chemicals} {chemistry_capability}```.
171
+ result should be in bullet points format.
172
+ """,
173
+ max_tokens=100,
174
+ n=1,
175
+ stop=None,
176
+ temperature=0,
177
+ top_p=1.0,
178
+ frequency_penalty=0.0,
179
+ presence_penalty=0.0
180
+ )
181
+
182
+ result = response.choices[0].text.strip()
183
+ return result
184
+
185
+ def clear_function(self,state):
186
+ state.clear()
187
+ # state = gr.State(self.get_empty_state())
188
+
189
+ def gradio_interface(self):
190
+
191
+ """Create the Gradio interface for the Chemical Identifier."""
192
+
193
+ with gr.Blocks(css="style.css",theme=gr.themes.Soft()) as demo:
194
+ state = gr.State(self.get_empty_state())
195
+ with gr.Column(elem_id="col-container"):
196
+ gr.HTML(
197
+ """<hr style="border-top: 5px solid white;">"""
198
+ )
199
+ gr.HTML(
200
+ """<br>
201
+ <h1 style="text-align:center;">
202
+ Multi URL and Doc Chatbot
203
+ </h1> """
204
+ )
205
+ gr.HTML(
206
+ """<hr style="border-top: 5px solid white;">"""
207
+ )
208
+
209
+ gr.Markdown("**Upload your URL,Documents**")
210
+ with gr.Accordion("Upload Files", open = False):
211
+ with gr.Row(elem_id="row-flex"):
212
+ with gr.Row(elem_id="row-flex"):
213
+ with gr.Column(scale=1,):
214
+ file_url = gr.Textbox(label='file url :',show_label=True,lines=10, placeholder="")
215
+ with gr.Row(elem_id="row-flex"):
216
+ with gr.Column(scale=1):
217
+ file_output = gr.File()
218
+ with gr.Column(scale=1):
219
+ upload_button = gr.UploadButton(
220
+ "Browse File", file_types=[".txt", ".pdf", ".doc", ".docx"],
221
+ file_count = "multiple")
222
+ with gr.Row():
223
+ chatbot = gr.Chatbot([], elem_id="chatbot")
224
+ with gr.Row():
225
+ txt = gr.Textbox(
226
+ label = "Question",
227
+ show_label=True,
228
+ lines=2,
229
+ placeholder="Enter text and press shift+enter",
230
+ )
231
+ with gr.Row():
232
+ clear_btn = gr.Button(value="Clear")
233
+
234
+ txt_msg = txt.submit(self.add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
235
+ self.answer_question, [txt,chatbot,state], chatbot
236
+ )
237
+ txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
238
+
239
+ file_url.submit(self.upload_multiple_urls, file_url, [file_output, state])
240
+ clear_btn.click(self.clear_function,[state],[])
241
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
242
+ upload_button.upload(self.upload_file, upload_button, [file_output,state])
243
+ demo.queue().launch(debug=True)
244
+
245
+ if __name__=="__main__":
246
+ chatbot = Chatbot()
247
+ chatbot.gradio_interface()
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ tiktoken
3
+ chromadb
4
+ langchain
5
+ gradio
6
+ pypdf
7
+ requests
8
+ unstructured
9
+ validators
10
+ pytesseract
11
+ pdf2image
12
+ tabulate
13
+ nltk
14
+ python-dotenv
15
+ faiss-cpu
16
+ requests
style.css ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #col-container {
2
+ max-width: 800px;
3
+ margin-left: auto;
4
+ margin-right: auto;
5
+ }
6
+ .heightfit{
7
+ height:120px;
8
+ }
9
+
10
+
11
+ #row-flex {
12
+ display: flex;
13
+ align-items: center;
14
+ justify-content: center;
15
+ }
16
+ .leftimage .rightimage{
17
+ float:left;
18
+ }
19
+ .leftimage{
20
+ padding-top:27px;
21
+ margin-left:210px;
22
+ }
23
+ .rightimage{
24
+ margin-right:210px;
25
+ margin-top:15px;
26
+ }
27
+ a,
28
+ a:hover,
29
+ a:visited {
30
+ text-decoration-line: underline;
31
+ font-weight: 600;
32
+ color: #1f2937 !important;
33
+ }
34
+
35
+ .dark a,
36
+ .dark a:hover,
37
+ .dark a:visited {
38
+ color: #f3f4f6 !important;
39
+ }