Chatbot / app.py
NaimaAqeel's picture
Update app.py
ec0cc7d verified
raw
history blame
5.36 kB
import os
import gradio as gr
from docx import Document
import fitz # PyMuPDF for PDF text extraction
from sentence_transformers import SentenceTransformer
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from nltk.tokenize import sent_tokenize
import torch
import pickle
import nltk
import faiss
import numpy as np
# Ensure NLTK resources are downloaded
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
# 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)
# Initialize FAISS index using LangChain
hf_embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
# Load or create FAISS index
index_path = "faiss_index.index"
if os.path.exists(index_path):
faiss_index = faiss.read_index(index_path)
print("Loaded FAISS index from faiss_index.index")
else:
# Create a new FAISS index
d = embedding_model.get_sentence_embedding_dimension() # Dimension of the embeddings
faiss_index = faiss.IndexFlatL2(d) # Using IndexFlatL2 for simplicity
def extract_text_from_pdf(pdf_path):
text = ""
try:
doc = fitz.open(pdf_path)
for page_num in range(len(doc)):
page = doc.load_page(page_num)
text += page.get_text()
except Exception as e:
raise RuntimeError(f"Error extracting text from PDF '{pdf_path}': {e}")
return text
def extract_text_from_docx(docx_path):
text = ""
try:
doc = Document(docx_path)
text = "\n".join([para.text for para in doc.paragraphs])
except Exception as e:
raise RuntimeError(f"Error extracting text from DOCX '{docx_path}': {e}")
return text
def preprocess_text(text):
sentences = sent_tokenize(text)
return sentences
def upload_files(files):
try:
global faiss_index
for file in files:
try:
file_path = file.name
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": f"Unsupported file format: {file_path}"}
sentences = preprocess_text(text)
embeddings = embedding_model.encode(sentences)
faiss_index.add(np.array(embeddings).astype(np.float32)) # Add embeddings
except Exception as e:
print(f"Error processing file '{file.name}': {e}")
return {"error": str(e)}
# Save the updated index
faiss.write_index(faiss_index, index_path)
return {"message": "Files processed successfully"}
except Exception as e:
print(f"General error processing files: {e}")
return {"error": str(e)}
def process_and_query(state, files, question):
if files:
upload_result = upload_files(files)
if "error" in upload_result:
return upload_result
if question:
question_embedding = embedding_model.encode([question])
# Perform FAISS search
D, I = faiss_index.search(np.array(question_embedding).astype(np.float32), k=5)
retrieved_results = [state["sentences"][i] for i in I[0]]
# Generate response based on retrieved results
combined_input = question + " ".join(retrieved_results)
inputs = generator_tokenizer(combined_input, return_tensors="pt")
with torch.no_grad():
generator_outputs = generator.generate(**inputs)
generated_text = generator_tokenizer.decode(generator_outputs[0], skip_special_tokens=True)
# 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()