Update main.py
Browse files
main.py
CHANGED
|
@@ -1,57 +1,49 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
import shutil
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
from langchain_community.vectorstores import Chroma
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langchain.chains import RetrievalQA
|
|
|
|
| 9 |
|
| 10 |
-
# ----------------
|
| 11 |
-
|
| 12 |
-
HF_API_TOKEN = "token2" # Replace with your actual Hugging Face token
|
| 13 |
-
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"return_full_text": False
|
| 23 |
-
}
|
| 24 |
-
}
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return str(result[0])
|
| 35 |
-
else:
|
| 36 |
-
return str(result)
|
| 37 |
-
else:
|
| 38 |
-
return f"HF API Error: {response.status_code} - {response.text}"
|
| 39 |
-
except Exception as e:
|
| 40 |
-
return f"API request failed: {str(e)}"
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
|
|
|
|
| 45 |
def process_pdf(pdf_file_path):
|
| 46 |
"""Load PDF, create embeddings, and initialize QA system"""
|
| 47 |
global qa
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
# Load PDF
|
| 51 |
loader = PyPDFLoader(pdf_file_path)
|
| 52 |
documents = loader.load()
|
| 53 |
if not documents:
|
| 54 |
-
return "Error: No content found in PDF"
|
| 55 |
|
| 56 |
# Split text into chunks
|
| 57 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
@@ -62,7 +54,7 @@ def process_pdf(pdf_file_path):
|
|
| 62 |
docs = text_splitter.split_documents(documents)
|
| 63 |
docs = [doc for doc in docs if doc.page_content.strip()]
|
| 64 |
if not docs:
|
| 65 |
-
return "Error: PDF has no extractable text chunks"
|
| 66 |
|
| 67 |
# Create embeddings
|
| 68 |
embeddings = HuggingFaceEmbeddings(
|
|
@@ -81,7 +73,7 @@ def process_pdf(pdf_file_path):
|
|
| 81 |
|
| 82 |
# Initialize QA system
|
| 83 |
qa = RetrievalQA.from_chain_type(
|
| 84 |
-
llm=
|
| 85 |
chain_type="stuff",
|
| 86 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 87 |
return_source_documents=False
|
|
@@ -91,16 +83,17 @@ def process_pdf(pdf_file_path):
|
|
| 91 |
except Exception as e:
|
| 92 |
return f"β Error processing PDF: {str(e)}"
|
| 93 |
|
|
|
|
| 94 |
def query_pdf(question):
|
| 95 |
"""Query the processed PDF"""
|
| 96 |
global qa
|
| 97 |
if qa is None:
|
| 98 |
-
return "No PDF processed yet. Please upload and process a PDF first."
|
| 99 |
if not question.strip():
|
| 100 |
-
return "Please ask a valid question."
|
| 101 |
|
| 102 |
try:
|
| 103 |
result = qa.invoke({"query": question})
|
| 104 |
return result.get("result", "No answer found")
|
| 105 |
except Exception as e:
|
| 106 |
-
return f"Error answering question: {str(e)}"
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import shutil
|
| 3 |
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 9 |
|
| 10 |
+
# ---------------- Global QA object ----------------
|
| 11 |
+
qa = None
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# ---------------- Initialize LLM ----------------
|
| 14 |
+
def initialize_llm():
|
| 15 |
+
"""
|
| 16 |
+
Initialize HuggingFaceEndpoint LLM for Mistral-7B
|
| 17 |
+
β οΈ Replace 'hf_your_actual_token_here' with your actual Hugging Face token
|
| 18 |
+
"""
|
| 19 |
+
hf_token = "token2" # <-- Your Hugging Face token
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
llm = HuggingFaceEndpoint(
|
| 22 |
+
endpoint_url="https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
|
| 23 |
+
huggingfacehub_api_token=hf_token,
|
| 24 |
+
task="text2text-generation",
|
| 25 |
+
model_kwargs={"temperature": 0.1, "max_length": 512}
|
| 26 |
+
)
|
| 27 |
+
print("β
Mistral-7B LLM initialized")
|
| 28 |
+
return llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Initialize global LLM
|
| 31 |
+
llm = initialize_llm()
|
| 32 |
|
| 33 |
+
# ---------------- PDF Processing ----------------
|
| 34 |
def process_pdf(pdf_file_path):
|
| 35 |
"""Load PDF, create embeddings, and initialize QA system"""
|
| 36 |
global qa
|
| 37 |
|
| 38 |
+
if llm is None:
|
| 39 |
+
return "β Error: Failed to initialize LLM. Check your HF token."
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
# Load PDF
|
| 43 |
loader = PyPDFLoader(pdf_file_path)
|
| 44 |
documents = loader.load()
|
| 45 |
if not documents:
|
| 46 |
+
return "β Error: No content found in PDF"
|
| 47 |
|
| 48 |
# Split text into chunks
|
| 49 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
|
|
| 54 |
docs = text_splitter.split_documents(documents)
|
| 55 |
docs = [doc for doc in docs if doc.page_content.strip()]
|
| 56 |
if not docs:
|
| 57 |
+
return "β Error: PDF has no extractable text chunks"
|
| 58 |
|
| 59 |
# Create embeddings
|
| 60 |
embeddings = HuggingFaceEmbeddings(
|
|
|
|
| 73 |
|
| 74 |
# Initialize QA system
|
| 75 |
qa = RetrievalQA.from_chain_type(
|
| 76 |
+
llm=llm, # β
Pass HuggingFaceEndpoint instance here
|
| 77 |
chain_type="stuff",
|
| 78 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 79 |
return_source_documents=False
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"β Error processing PDF: {str(e)}"
|
| 85 |
|
| 86 |
+
# ---------------- Query PDF ----------------
|
| 87 |
def query_pdf(question):
|
| 88 |
"""Query the processed PDF"""
|
| 89 |
global qa
|
| 90 |
if qa is None:
|
| 91 |
+
return "β No PDF processed yet. Please upload and process a PDF first."
|
| 92 |
if not question.strip():
|
| 93 |
+
return "β οΈ Please ask a valid question."
|
| 94 |
|
| 95 |
try:
|
| 96 |
result = qa.invoke({"query": question})
|
| 97 |
return result.get("result", "No answer found")
|
| 98 |
except Exception as e:
|
| 99 |
+
return f"β Error answering question: {str(e)}"
|