Spaces:
Sleeping
Sleeping
Commit
·
46a768d
1
Parent(s):
bc0dc94
adding chatbot
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
|
4 |
from langchain.document_loaders import PDFMinerLoader,CSVLoader ,UnstructuredWordDocumentLoader,TextLoader,OnlinePDFLoader
|
5 |
from langchain.text_splitter import CharacterTextSplitter
|
6 |
from langchain.embeddings import SentenceTransformerEmbeddings
|
@@ -98,6 +98,30 @@ def process_pdf_document(document_file):
|
|
98 |
document = loader.load()
|
99 |
return document
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
|
103 |
css="""
|
@@ -134,7 +158,7 @@ with gr.Blocks(css=css) as demo:
|
|
134 |
# chatbot = gr.Chatbot()
|
135 |
# question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
136 |
# submit_button = gr.Button("Send Message")
|
137 |
-
|
138 |
load_pdf.click(loading_file, None, langchain_status, queue=False)
|
139 |
load_pdf.click(document_loader, inputs=[pdf_doc,API_key,file_extension,LLM_option], outputs=[langchain_status], queue=False)
|
140 |
|
@@ -144,7 +168,9 @@ with gr.Blocks(css=css) as demo:
|
|
144 |
sources = gr.HTML(value = "Source paragraphs where I looked for answers will appear here", height=300)
|
145 |
|
146 |
with gr.Row():
|
147 |
-
|
148 |
-
|
|
|
|
|
149 |
|
150 |
demo.launch()
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import time
|
4 |
from langchain.document_loaders import PDFMinerLoader,CSVLoader ,UnstructuredWordDocumentLoader,TextLoader,OnlinePDFLoader
|
5 |
from langchain.text_splitter import CharacterTextSplitter
|
6 |
from langchain.embeddings import SentenceTransformerEmbeddings
|
|
|
98 |
document = loader.load()
|
99 |
return document
|
100 |
|
101 |
+
def infer(question, history):
|
102 |
+
|
103 |
+
res = []
|
104 |
+
for human, ai in history[:-1]:
|
105 |
+
pair = (human, ai)
|
106 |
+
res.append(pair)
|
107 |
+
|
108 |
+
chat_history = res
|
109 |
+
query = question
|
110 |
+
result = qa({"question": query, "chat_history": chat_history})
|
111 |
+
return result["answer"]
|
112 |
+
|
113 |
+
def bot(history):
|
114 |
+
response = infer(history[-1][0], history)
|
115 |
+
history[-1][1] = ""
|
116 |
+
|
117 |
+
for character in response:
|
118 |
+
history[-1][1] += character
|
119 |
+
time.sleep(0.05)
|
120 |
+
yield history
|
121 |
+
|
122 |
+
def add_text(history, text):
|
123 |
+
history = history + [(text, None)]
|
124 |
+
return history, ""
|
125 |
|
126 |
|
127 |
css="""
|
|
|
158 |
# chatbot = gr.Chatbot()
|
159 |
# question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
160 |
# submit_button = gr.Button("Send Message")
|
161 |
+
|
162 |
load_pdf.click(loading_file, None, langchain_status, queue=False)
|
163 |
load_pdf.click(document_loader, inputs=[pdf_doc,API_key,file_extension,LLM_option], outputs=[langchain_status], queue=False)
|
164 |
|
|
|
168 |
sources = gr.HTML(value = "Source paragraphs where I looked for answers will appear here", height=300)
|
169 |
|
170 |
with gr.Row():
|
171 |
+
question = gr.Textbox(label="Type your question?",lines=1).style(full_width=False)
|
172 |
+
submit_btn = gr.Button(value="Send message", variant="secondary", scale = 1)
|
173 |
+
question.submit(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
|
174 |
+
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
|
175 |
|
176 |
demo.launch()
|