Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 4 |
+
from langchain.vectorstores import FAISS
|
| 5 |
+
from langchain.document_loaders import PyPDFLoader
|
| 6 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 7 |
+
from langchain.llms import HuggingFaceHub
|
| 8 |
+
|
| 9 |
+
# Function to load and process the document (PDF)
|
| 10 |
+
def load_document(file):
|
| 11 |
+
loader = PyPDFLoader(file.name)
|
| 12 |
+
documents = loader.load()
|
| 13 |
+
return documents
|
| 14 |
+
|
| 15 |
+
# Function to embed the documents using sentence-transformers and store them in FAISS
|
| 16 |
+
def embed_documents(documents):
|
| 17 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
| 18 |
+
vector_store = FAISS.from_documents(documents, embeddings)
|
| 19 |
+
return vector_store
|
| 20 |
+
|
| 21 |
+
# Function to handle the chatbot's conversation by querying the document embeddings
|
| 22 |
+
def chat_with_document(query, vector_store):
|
| 23 |
+
retriever = vector_store.as_retriever()
|
| 24 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-large", model_kwargs={"temperature":0.2})
|
| 25 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
| 26 |
+
results = retriever.get_relevant_documents(query)
|
| 27 |
+
answer = chain.run(input_documents=results, question=query)
|
| 28 |
+
return answer
|
| 29 |
+
|
| 30 |
+
# Function to build the Gradio interface
|
| 31 |
+
def chatbot_interface():
|
| 32 |
+
vector_store = None
|
| 33 |
+
|
| 34 |
+
# Function to handle file upload and document embedding
|
| 35 |
+
def upload_file(file):
|
| 36 |
+
nonlocal vector_store
|
| 37 |
+
documents = load_document(file)
|
| 38 |
+
vector_store = embed_documents(documents)
|
| 39 |
+
return "Document uploaded and processed. You can now ask questions."
|
| 40 |
+
|
| 41 |
+
# Function to handle user queries
|
| 42 |
+
def ask_question(query):
|
| 43 |
+
if vector_store:
|
| 44 |
+
return chat_with_document(query, vector_store)
|
| 45 |
+
return "Please upload a document first."
|
| 46 |
+
|
| 47 |
+
# Gradio interface components
|
| 48 |
+
upload = gr.File(label="Upload a PDF document")
|
| 49 |
+
question = gr.Textbox(label="Ask a question about the document")
|
| 50 |
+
answer = gr.Textbox(label="Answer", readonly=True)
|
| 51 |
+
|
| 52 |
+
# Linking the functions to Gradio interface
|
| 53 |
+
upload_button = gr.Interface(fn=upload_file, inputs=upload, outputs="text")
|
| 54 |
+
chat_box = gr.Interface(fn=ask_question, inputs=question, outputs=answer)
|
| 55 |
+
|
| 56 |
+
# Gradio app layout
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("# Document Chatbot")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
upload_button.render()
|
| 61 |
+
with gr.Row():
|
| 62 |
+
question.render()
|
| 63 |
+
answer.render()
|
| 64 |
+
|
| 65 |
+
# Launch the Gradio app
|
| 66 |
+
demo.launch()
|
| 67 |
+
|
| 68 |
+
# Start the chatbot interface
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
chatbot_interface()
|