|
import os |
|
import requests |
|
import streamlit as st |
|
from io import BytesIO |
|
from PyPDF2 import PdfReader |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.vectorstores import FAISS |
|
from transformers import pipeline |
|
import torch |
|
|
|
|
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
from langchain_community.vectorstores import FAISS |
|
|
|
|
|
st.set_page_config(page_title="RAG-based PDF Chat", layout="centered", page_icon="π") |
|
|
|
|
|
embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") |
|
|
|
|
|
@st.cache_resource |
|
def load_qa_pipeline(): |
|
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") |
|
return qa_pipeline |
|
|
|
qa_pipeline = load_qa_pipeline() |
|
|
|
|
|
def generate_summary_with_qa_pipeline(query, retrieved_text): |
|
|
|
summary = qa_pipeline(question=query, context=retrieved_text) |
|
return summary["answer"] |
|
|
|
|
|
PDF_URLS = [ |
|
"https://huggingface.co/spaces/tahirsher/GenAI_Lawyers_Guide/blob/main/administrator92ada0936848e501425591b4ad0cd417.pdf", |
|
"https://huggingface.co/spaces/tahirsher/GenAI_Lawyers_Guide/blob/main/Pakistan%20Penal%20Code.pdf", |
|
] |
|
|
|
|
|
def get_huggingface_raw_url(url): |
|
if "huggingface.co" in url and "/blob/" in url: |
|
return url.replace("/blob/", "/resolve/") |
|
return url |
|
|
|
|
|
def fetch_pdf_text_from_huggingface(urls): |
|
text = "" |
|
for url in urls: |
|
raw_url = get_huggingface_raw_url(url) |
|
response = requests.get(raw_url) |
|
if response.status_code == 200: |
|
pdf_file = BytesIO(response.content) |
|
try: |
|
pdf_reader = PdfReader(pdf_file) |
|
for page in pdf_reader.pages: |
|
page_text = page.extract_text() |
|
if page_text: |
|
text += page_text |
|
except Exception as e: |
|
st.error(f"Failed to read PDF from URL {url}: {e}") |
|
else: |
|
st.error(f"Failed to fetch PDF from URL: {url}") |
|
return text |
|
|
|
|
|
@st.cache_data |
|
def get_text_chunks(text): |
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000) |
|
chunks = text_splitter.split_text(text) |
|
return chunks |
|
|
|
|
|
embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") |
|
|
|
|
|
@st.cache_resource |
|
def load_or_create_vector_store(text_chunks): |
|
vector_store = FAISS.from_texts(text_chunks, embedding=embedding_function) |
|
return vector_store |
|
|
|
|
|
def generate_summary_with_qa_pipeline(query, retrieved_text): |
|
|
|
summary = qa_pipeline(question=query, context=retrieved_text) |
|
return summary["answer"] |
|
|
|
|
|
def user_input(user_question, vector_store): |
|
docs = vector_store.similarity_search(user_question) |
|
context_text = " ".join([doc.page_content for doc in docs]) |
|
return generate_summary_with_qa_pipeline(user_question, context_text) |
|
|
|
|
|
def main(): |
|
st.title("π Gen AI Lawyers Guide") |
|
|
|
|
|
raw_text = fetch_pdf_text_from_huggingface(PDF_URLS) |
|
text_chunks = get_text_chunks(raw_text) |
|
vector_store = load_or_create_vector_store(text_chunks) |
|
|
|
|
|
user_question = st.text_input("Ask a Question:", placeholder="Type your question here...") |
|
|
|
if st.button("Get Response"): |
|
if not user_question: |
|
st.warning("Please enter a question before submitting.") |
|
else: |
|
with st.spinner("Generating response..."): |
|
answer = user_input(user_question, vector_store) |
|
st.markdown(f"**π€ AI:** {answer}") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|