Chatbot / app.py
NaimaAqeel's picture
Update app.py
c4f7f00 verified
raw
history blame
5.03 kB
import os
import fitz # PyMuPDF
from docx import Document
from sentence_transformers import SentenceTransformer
from langchain_community.vectorstores import FAISS
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from nltk.tokenize import sent_tokenize
import torch
import gradio as gr
# Function to extract text from a PDF file
def extract_text_from_pdf(pdf_path):
text = ""
doc = fitz.open(pdf_path)
for page in doc:
text += page.get_text()
return text
# Function to extract text from a Word document
def extract_text_from_docx(docx_path):
doc = Document(docx_path)
text = "\n".join([para.text for para in doc.paragraphs])
return text
# Initialize the embedding model
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
# Hugging Face API token
api_token = os.getenv('HUGGINGFACEHUB_API_TOKEN')
if not api_token:
raise ValueError("HUGGINGFACEHUB_API_TOKEN environment variable is not set")
# Define RAG models
generator_model_name = "facebook/bart-base"
retriever_model_name = "facebook/bart-base" # Can be the same as generator
generator = AutoModelForSeq2SeqLM.from_pretrained(generator_model_name)
generator_tokenizer = AutoTokenizer.from_pretrained(generator_model_name)
retriever = AutoModelForSeq2SeqLM.from_pretrained(retriever_model_name)
retriever_tokenizer = AutoTokenizer.from_pretrained(retriever_model_name)
# Load or create FAISS index
index_path = "faiss_index.pkl"
if os.path.exists(index_path):
with open(index_path, "rb") as f:
index = FAISS.load(f)
print("Loaded FAISS index from faiss_index.pkl")
else:
# Create a new FAISS index if it doesn't exist
index = FAISS(embedding_dimension=embedding_model.get_sentence_embedding_dimension())
with open(index_path, "wb") as f:
FAISS.save(index, f)
print("Created new FAISS index and saved to faiss_index.pkl")
def preprocess_text(text):
sentences = sent_tokenize(text)
return sentences
def upload_files(files):
global index
try:
for file_path in files:
if file_path.endswith('.pdf'):
text = extract_text_from_pdf(file_path)
elif file_path.endswith('.docx'):
text = extract_text_from_docx(file_path)
else:
return {"error": "Unsupported file format"}
# Preprocess text
sentences = preprocess_text(text)
# Encode sentences and add to FAISS index
embeddings = embedding_model.encode(sentences)
index.add(embeddings)
return {"message": "Files processed successfully"}
except Exception as e:
print(f"Error processing files: {e}")
return {"error": "Error processing files"}
def process_and_query(state, files, question):
if files:
upload_result = upload_files(files)
if "error" in upload_result:
return upload_result
if question:
# Preprocess the question
question_embedding = embedding_model.encode([question])
# Use retriever model to retrieve relevant passages
with torch.no_grad():
retriever_outputs = retriever(**retriever_tokenizer(question, return_tensors="pt"))
retriever_hidden_states = retriever_outputs.hidden_states[-1] # Last hidden state
# Search the FAISS index for similar passages based on retrieved hidden states
distances, retrieved_ids = index.search(retriever_hidden_states.cpu().numpy(), k=5) # Retrieve top 5 passages
# Get the retrieved passages from the document text
retrieved_passages = [state["processed_text"].split("\n")[i] for i in retrieved_ids.flatten()]
# Use generator model to generate response based on question and retrieved passages
combined_input = torch.cat([question_embedding, embedding_model.encode(retrieved_passages)], dim=0)
with torch.no_grad():
generator_outputs = generator(**generator_tokenizer(combined_input, return_tensors="pt"))
generated_text = generator_tokenizer.decode(generator_outputs.sequences.squeeze())
# Update conversation history
state["conversation"].append({"question": question, "answer": generated_text})
return {"message": generated_text, "conversation": state["conversation"]}
return {"error": "No question provided"}
# Create Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## Document Upload and Query System")
with gr.Tab("Upload Files"):
upload = gr.File(file_count="multiple", label="Upload PDF or DOCX files")
upload_button = gr.Button("Upload")
upload_output = gr.Textbox()
upload_button.click(fn=upload_files, inputs=upload, outputs=upload_output)
with gr.Tab("Query"):
query = gr.Textbox(label="Enter your query")
query_button = gr.Button("Search")
query_output = gr.Textbox()
query_button.click(fn=process_and_query, inputs=[query], outputs=query_output)
demo.launch()