Manav0603 commited on
Commit
61af5d1
Β·
verified Β·
1 Parent(s): 7d8afcb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +38 -48
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
- # ---------------- Global QA object ----------------
11
- qa = None
 
 
12
 
13
- # ---------------- Initialize LLM ----------------
14
- def initialize_llm():
15
- """
16
- Initialize LLM using HuggingFace API.
17
- ⚠️ Replace 'hf_your_actual_huggingface_token_here' with your actual Hugging Face token.
18
- """
19
- # Old: hf_token = os.getenv("HF_API_TOKEN")
20
- hf_token = "token2"
21
-
22
- if not hf_token:
23
- print("❌ HuggingFace token not found. Set HF_API_TOKEN in environment variables.")
24
- return None
25
 
26
  try:
27
- llm = HuggingFaceEndpoint(
28
- endpoint_url="https://api-inference.huggingface.co/models/google/flan-t5-base", # HF model endpoint
29
- huggingfacehub_api_token=hf_token,
30
- task="text2text-generation",
31
- model_kwargs={
32
- "temperature": 0.1,
33
- "max_length": 512,
34
- }
35
- )
36
- print("βœ… FLAN-T5 LLM initialized via HuggingFace Endpoint")
37
- return llm
 
38
  except Exception as e:
39
- print(f"❌ HuggingFaceEndpoint failed: {e}")
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 "❌ Error: No content found in PDF"
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 "❌ Error: PDF has no extractable text chunks"
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=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 "❌ No PDF processed yet. Please upload and process a PDF first."
109
  if not question.strip():
110
- return "⚠️ Please ask a valid question."
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"❌ Error answering question: {str(e)}"
 
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)}"