Spaces:
Runtime error
Runtime error
farhananis005
commited on
Commit
•
2bba890
1
Parent(s):
6271511
Update app.py
Browse files
app.py
CHANGED
@@ -10,15 +10,18 @@ def save_docs(docs):
|
|
10 |
import shutil
|
11 |
import os
|
12 |
|
13 |
-
|
14 |
-
os.makedirs(destination_dir, exist_ok=True)
|
15 |
|
16 |
-
output_dir
|
|
|
|
|
|
|
|
|
17 |
|
18 |
for doc in docs:
|
19 |
-
|
20 |
|
21 |
-
return "
|
22 |
|
23 |
def process_docs():
|
24 |
|
@@ -26,21 +29,31 @@ def process_docs():
|
|
26 |
from langchain.document_loaders import DirectoryLoader
|
27 |
from langchain.document_loaders import TextLoader
|
28 |
from langchain.document_loaders import Docx2txtLoader
|
|
|
|
|
29 |
from langchain.vectorstores import FAISS
|
30 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
31 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
32 |
|
33 |
-
loader1 = DirectoryLoader('/
|
34 |
document1 = loader1.load()
|
35 |
|
36 |
-
loader2 = DirectoryLoader('/
|
37 |
document2 = loader2.load()
|
38 |
|
39 |
-
loader3 = DirectoryLoader('/
|
40 |
document3 = loader3.load()
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
document1.extend(document2)
|
43 |
document1.extend(document3)
|
|
|
|
|
44 |
|
45 |
text_splitter = RecursiveCharacterTextSplitter(
|
46 |
chunk_size=1000,
|
@@ -52,11 +65,26 @@ def process_docs():
|
|
52 |
embeddings = OpenAIEmbeddings()
|
53 |
|
54 |
docs_db = FAISS.from_documents(docs, embeddings)
|
55 |
-
docs_db.save_local("/
|
56 |
|
57 |
-
return "
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
formatted_output = response + "\n\nSources"
|
62 |
|
@@ -71,49 +99,39 @@ def formatted_response(docs, response):
|
|
71 |
else:
|
72 |
formatted_output += f"\n{doc_name}"
|
73 |
|
74 |
-
|
|
|
75 |
|
76 |
-
def search_docs(question):
|
77 |
|
78 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
79 |
from langchain.vectorstores import FAISS
|
80 |
-
from langchain.chains.question_answering import load_qa_chain
|
81 |
from langchain.callbacks import get_openai_callback
|
82 |
-
|
|
|
|
|
|
|
83 |
|
84 |
embeddings = OpenAIEmbeddings()
|
85 |
-
docs_db = FAISS.load_local("/
|
86 |
docs = docs_db.similarity_search(question)
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
90 |
|
91 |
with get_openai_callback() as cb:
|
92 |
-
response =
|
93 |
print(cb)
|
94 |
|
95 |
-
return formatted_response(docs, response)
|
96 |
-
|
97 |
-
def delete_docs():
|
98 |
-
|
99 |
-
import shutil
|
100 |
-
|
101 |
-
path1 = "/home/user/app/docs/"
|
102 |
-
path2 = "/home/user/app/docs_db/"
|
103 |
-
|
104 |
-
try:
|
105 |
-
shutil.rmtree(path1)
|
106 |
-
shutil.rmtree(path2)
|
107 |
-
return "Deleted Successfully"
|
108 |
-
|
109 |
-
except:
|
110 |
-
return "Already Deleted"
|
111 |
|
112 |
import gradio as gr
|
113 |
|
114 |
css = """
|
115 |
.col{
|
116 |
-
max-width:
|
117 |
margin: 0 auto;
|
118 |
display: flex;
|
119 |
flex-direction: column;
|
@@ -123,9 +141,9 @@ css = """
|
|
123 |
"""
|
124 |
|
125 |
with gr.Blocks(css=css) as demo:
|
126 |
-
gr.Markdown("## <center>
|
127 |
|
128 |
-
with gr.Tab("
|
129 |
with gr.Column(elem_classes="col"):
|
130 |
|
131 |
with gr.Tab("Upload and Process Documents"):
|
@@ -138,28 +156,33 @@ with gr.Blocks(css=css) as demo:
|
|
138 |
docs_process_button = gr.Button("Process")
|
139 |
docs_process_output = gr.Textbox(label="Output")
|
140 |
|
141 |
-
gr.
|
|
|
|
|
|
|
142 |
|
143 |
with gr.Tab("Query Documents"):
|
144 |
with gr.Column():
|
145 |
|
146 |
-
|
147 |
-
docs_search_button = gr.Button("Search")
|
148 |
-
docs_search_output = gr.Textbox(label="Output")
|
149 |
|
150 |
-
|
151 |
-
|
152 |
|
153 |
-
gr.
|
|
|
|
|
|
|
154 |
|
155 |
#########################################################################################################
|
|
|
156 |
docs_upload_button.click(save_docs, inputs=docs_upload_input, outputs=docs_upload_output)
|
157 |
docs_process_button.click(process_docs, inputs=None, outputs=docs_process_output)
|
|
|
158 |
|
159 |
-
docs_search_button.click(search_docs, inputs=docs_search_input, outputs=
|
160 |
|
161 |
-
docs_delete_button.click(delete_docs, inputs=None, outputs=docs_delete_output)
|
162 |
#########################################################################################################
|
163 |
|
164 |
demo.queue()
|
165 |
-
demo.launch()
|
|
|
10 |
import shutil
|
11 |
import os
|
12 |
|
13 |
+
output_dir="/content/docs/"
|
|
|
14 |
|
15 |
+
if os.path.exists(output_dir):
|
16 |
+
shutil.rmtree(output_dir)
|
17 |
+
|
18 |
+
if not os.path.exists(output_dir):
|
19 |
+
os.makedirs(output_dir)
|
20 |
|
21 |
for doc in docs:
|
22 |
+
shutil.copy(doc.name, output_dir)
|
23 |
|
24 |
+
return "Successful!"
|
25 |
|
26 |
def process_docs():
|
27 |
|
|
|
29 |
from langchain.document_loaders import DirectoryLoader
|
30 |
from langchain.document_loaders import TextLoader
|
31 |
from langchain.document_loaders import Docx2txtLoader
|
32 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
33 |
+
from langchain.document_loaders import UnstructuredExcelLoader
|
34 |
from langchain.vectorstores import FAISS
|
35 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
36 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
37 |
|
38 |
+
loader1 = DirectoryLoader('/content/docs/', glob="./*.pdf", loader_cls=PyPDFLoader)
|
39 |
document1 = loader1.load()
|
40 |
|
41 |
+
loader2 = DirectoryLoader('/content/docs/', glob="./*.txt", loader_cls=TextLoader)
|
42 |
document2 = loader2.load()
|
43 |
|
44 |
+
loader3 = DirectoryLoader('/content/docs/', glob="./*.docx", loader_cls=Docx2txtLoader)
|
45 |
document3 = loader3.load()
|
46 |
|
47 |
+
loader4 = DirectoryLoader('/content/docs/', glob="./*.csv", loader_cls=CSVLoader)
|
48 |
+
document4 = loader4.load()
|
49 |
+
|
50 |
+
loader5 = DirectoryLoader('/content/docs/', glob="./*.xlsx", loader_cls=UnstructuredExcelLoader)
|
51 |
+
document5 = loader5.load()
|
52 |
+
|
53 |
document1.extend(document2)
|
54 |
document1.extend(document3)
|
55 |
+
document1.extend(document4)
|
56 |
+
document1.extend(document5)
|
57 |
|
58 |
text_splitter = RecursiveCharacterTextSplitter(
|
59 |
chunk_size=1000,
|
|
|
65 |
embeddings = OpenAIEmbeddings()
|
66 |
|
67 |
docs_db = FAISS.from_documents(docs, embeddings)
|
68 |
+
docs_db.save_local("/content/docs_db/")
|
69 |
|
70 |
+
return "Successful!"
|
71 |
|
72 |
+
global agent
|
73 |
+
|
74 |
+
def create_agent():
|
75 |
+
|
76 |
+
from langchain.chat_models import ChatOpenAI
|
77 |
+
from langchain.chains.conversation.memory import ConversationSummaryBufferMemory
|
78 |
+
from langchain.chains import ConversationChain
|
79 |
+
global agent
|
80 |
+
|
81 |
+
llm = ChatOpenAI(model_name='gpt-3.5-turbo-16k')
|
82 |
+
memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=1000)
|
83 |
+
agent = ConversationChain(llm=llm, memory=memory, verbose=True)
|
84 |
+
|
85 |
+
return "Successful!"
|
86 |
+
|
87 |
+
def formatted_response(docs, question, response, state):
|
88 |
|
89 |
formatted_output = response + "\n\nSources"
|
90 |
|
|
|
99 |
else:
|
100 |
formatted_output += f"\n{doc_name}"
|
101 |
|
102 |
+
state.append((question, formatted_output))
|
103 |
+
return state, state
|
104 |
|
105 |
+
def search_docs(prompt, question, state):
|
106 |
|
107 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
108 |
from langchain.vectorstores import FAISS
|
|
|
109 |
from langchain.callbacks import get_openai_callback
|
110 |
+
global agent
|
111 |
+
agent = agent
|
112 |
+
|
113 |
+
state = state or []
|
114 |
|
115 |
embeddings = OpenAIEmbeddings()
|
116 |
+
docs_db = FAISS.load_local("/content/docs_db/", embeddings)
|
117 |
docs = docs_db.similarity_search(question)
|
118 |
|
119 |
+
prompt += "\n\n"
|
120 |
+
prompt += question
|
121 |
+
prompt += "\n\n"
|
122 |
+
prompt += str(docs)
|
123 |
|
124 |
with get_openai_callback() as cb:
|
125 |
+
response = agent.predict(input=prompt)
|
126 |
print(cb)
|
127 |
|
128 |
+
return formatted_response(docs, question, response, state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
import gradio as gr
|
131 |
|
132 |
css = """
|
133 |
.col{
|
134 |
+
max-width: 75%;
|
135 |
margin: 0 auto;
|
136 |
display: flex;
|
137 |
flex-direction: column;
|
|
|
141 |
"""
|
142 |
|
143 |
with gr.Blocks(css=css) as demo:
|
144 |
+
gr.Markdown("## <center>All in One Document Chatting App</center>")
|
145 |
|
146 |
+
with gr.Tab("Chat With Your Documents"):
|
147 |
with gr.Column(elem_classes="col"):
|
148 |
|
149 |
with gr.Tab("Upload and Process Documents"):
|
|
|
156 |
docs_process_button = gr.Button("Process")
|
157 |
docs_process_output = gr.Textbox(label="Output")
|
158 |
|
159 |
+
create_agent_button = gr.Button("Create Agent")
|
160 |
+
create_agent_output = gr.Textbox(label="Output")
|
161 |
+
|
162 |
+
gr.ClearButton([docs_upload_input, docs_upload_output, docs_process_output, create_agent_output])
|
163 |
|
164 |
with gr.Tab("Query Documents"):
|
165 |
with gr.Column():
|
166 |
|
167 |
+
docs_prompt_input = gr.Textbox(label="Custom Prompt")
|
|
|
|
|
168 |
|
169 |
+
docs_chatbot = gr.Chatbot(label="Chats")
|
170 |
+
docs_state = gr.State()
|
171 |
|
172 |
+
docs_search_input = gr.Textbox(label="Question")
|
173 |
+
docs_search_button = gr.Button("Search")
|
174 |
+
|
175 |
+
gr.ClearButton([docs_prompt_input, docs_search_input])
|
176 |
|
177 |
#########################################################################################################
|
178 |
+
|
179 |
docs_upload_button.click(save_docs, inputs=docs_upload_input, outputs=docs_upload_output)
|
180 |
docs_process_button.click(process_docs, inputs=None, outputs=docs_process_output)
|
181 |
+
create_agent_button.click(create_agent, inputs=None, outputs=create_agent_output)
|
182 |
|
183 |
+
docs_search_button.click(search_docs, inputs=[docs_prompt_input, docs_search_input, docs_state], outputs=[docs_chatbot, docs_state])
|
184 |
|
|
|
185 |
#########################################################################################################
|
186 |
|
187 |
demo.queue()
|
188 |
+
demo.launch(debug=True, share=True)
|