Update main.py
Browse files
main.py
CHANGED
|
@@ -1,62 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_community.document_loaders import PyPDFLoader
|
| 2 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 3 |
from langchain_community.vectorstores import Chroma
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
from langchain.chains import RetrievalQA
|
| 6 |
-
from langchain_community.llms import HuggingFaceEndpoint
|
| 7 |
-
import os
|
| 8 |
-
import shutil
|
| 9 |
|
| 10 |
-
# ----------------
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
print("β HuggingFace token not found. Set HF_API_TOKEN in environment variables.")
|
| 24 |
-
return None
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
-
|
| 40 |
-
return None
|
| 41 |
-
|
| 42 |
-
# Initialize global LLM
|
| 43 |
-
llm = initialize_llm()
|
| 44 |
|
| 45 |
# ---------------- PDF Processing ----------------
|
|
|
|
|
|
|
| 46 |
def process_pdf(pdf_file_path):
|
| 47 |
"""Load PDF, create embeddings, and initialize QA system"""
|
| 48 |
global qa
|
| 49 |
|
| 50 |
-
if llm is None:
|
| 51 |
-
return "β Error: Failed to initialize LLM. Check your HF token."
|
| 52 |
-
|
| 53 |
try:
|
| 54 |
# Load PDF
|
| 55 |
loader = PyPDFLoader(pdf_file_path)
|
| 56 |
documents = loader.load()
|
| 57 |
if not documents:
|
| 58 |
-
return "
|
| 59 |
-
print(f"Loaded {len(documents)} pages from PDF")
|
| 60 |
|
| 61 |
# Split text into chunks
|
| 62 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
@@ -67,15 +62,13 @@ def process_pdf(pdf_file_path):
|
|
| 67 |
docs = text_splitter.split_documents(documents)
|
| 68 |
docs = [doc for doc in docs if doc.page_content.strip()]
|
| 69 |
if not docs:
|
| 70 |
-
return "
|
| 71 |
-
print(f"Created {len(docs)} text chunks")
|
| 72 |
|
| 73 |
# Create embeddings
|
| 74 |
embeddings = HuggingFaceEmbeddings(
|
| 75 |
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 76 |
model_kwargs={"device": "cpu"}
|
| 77 |
)
|
| 78 |
-
print("β
Embeddings model loaded")
|
| 79 |
|
| 80 |
# Clear previous Chroma cache
|
| 81 |
if os.path.exists("./chroma_cache"):
|
|
@@ -85,32 +78,29 @@ def process_pdf(pdf_file_path):
|
|
| 85 |
vectorstore = Chroma.from_documents(
|
| 86 |
docs, embeddings, persist_directory="./chroma_cache"
|
| 87 |
)
|
| 88 |
-
print("β
Vector store created")
|
| 89 |
|
| 90 |
# Initialize QA system
|
| 91 |
qa = RetrievalQA.from_chain_type(
|
| 92 |
-
llm=
|
| 93 |
chain_type="stuff",
|
| 94 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 95 |
return_source_documents=False
|
| 96 |
)
|
| 97 |
-
print("β
QA system ready")
|
| 98 |
return f"β
PDF processed successfully! {len(docs)} text chunks ready."
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
return f"β Error processing PDF: {str(e)}"
|
| 102 |
|
| 103 |
-
# ---------------- Query PDF ----------------
|
| 104 |
def query_pdf(question):
|
| 105 |
"""Query the processed PDF"""
|
| 106 |
global qa
|
| 107 |
if qa is None:
|
| 108 |
-
return "
|
| 109 |
if not question.strip():
|
| 110 |
-
return "
|
| 111 |
|
| 112 |
try:
|
| 113 |
result = qa.invoke({"query": question})
|
| 114 |
return result.get("result", "No answer found")
|
| 115 |
except Exception as e:
|
| 116 |
-
return f"
|
|
|
|
| 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 |
+
# ---------------- Hugging Face API Setup ----------------
|
| 11 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 12 |
+
HF_API_TOKEN = "your_huggingface_token_here" # Replace with your actual Hugging Face token
|
| 13 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 14 |
|
| 15 |
+
def query_hf_model(prompt):
|
| 16 |
+
"""Query Hugging Face Inference API"""
|
| 17 |
+
data = {
|
| 18 |
+
"inputs": prompt,
|
| 19 |
+
"parameters": {
|
| 20 |
+
"max_length": 100,
|
| 21 |
+
"temperature": 0.7,
|
| 22 |
+
"return_full_text": False
|
| 23 |
+
}
|
| 24 |
+
}
|
|
|
|
|
|
|
| 25 |
|
| 26 |
try:
|
| 27 |
+
response = requests.post(HF_API_URL, headers=headers, json=data)
|
| 28 |
+
if response.status_code == 200:
|
| 29 |
+
result = response.json()
|
| 30 |
+
if isinstance(result, list) and len(result) > 0:
|
| 31 |
+
if "generated_text" in result[0]:
|
| 32 |
+
return result[0]["generated_text"]
|
| 33 |
+
else:
|
| 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 |
# ---------------- PDF Processing ----------------
|
| 43 |
+
qa = None
|
| 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 |
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(
|
| 69 |
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 70 |
model_kwargs={"device": "cpu"}
|
| 71 |
)
|
|
|
|
| 72 |
|
| 73 |
# Clear previous Chroma cache
|
| 74 |
if os.path.exists("./chroma_cache"):
|
|
|
|
| 78 |
vectorstore = Chroma.from_documents(
|
| 79 |
docs, embeddings, persist_directory="./chroma_cache"
|
| 80 |
)
|
|
|
|
| 81 |
|
| 82 |
# Initialize QA system
|
| 83 |
qa = RetrievalQA.from_chain_type(
|
| 84 |
+
llm=query_hf_model,
|
| 85 |
chain_type="stuff",
|
| 86 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 87 |
return_source_documents=False
|
| 88 |
)
|
|
|
|
| 89 |
return f"β
PDF processed successfully! {len(docs)} text chunks ready."
|
| 90 |
|
| 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)}"
|