Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# Alternative: if you want to use PyPDF2 instead
|
| 2 |
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
from langchain_community.vectorstores import Chroma
|
|
@@ -16,20 +15,19 @@ def initialize_llm():
|
|
| 16 |
"""Initialize LLM using HuggingFace API"""
|
| 17 |
try:
|
| 18 |
# Get HF token from environment
|
| 19 |
-
hf_token = os.getenv("
|
| 20 |
|
| 21 |
if not hf_token:
|
| 22 |
print("Warning: No HuggingFace token found")
|
| 23 |
return None
|
| 24 |
|
| 25 |
-
#
|
| 26 |
llm = HuggingFaceHub(
|
| 27 |
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 28 |
model_kwargs={
|
| 29 |
"temperature": 0.1,
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"repetition_penalty": 1.1
|
| 33 |
},
|
| 34 |
huggingfacehub_api_token=hf_token
|
| 35 |
)
|
|
@@ -39,7 +37,21 @@ def initialize_llm():
|
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
print(f"Error initializing LLM: {e}")
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Initialize the LLM globally
|
| 45 |
llm = initialize_llm()
|
|
@@ -100,17 +112,26 @@ def process_pdf(pdf_file_path):
|
|
| 100 |
|
| 101 |
# 4️⃣ Store embeddings in ChromaDB (temp folder)
|
| 102 |
# Clear existing chroma cache first
|
| 103 |
-
import shutil
|
| 104 |
if os.path.exists("./chroma_cache"):
|
| 105 |
shutil.rmtree("./chroma_cache")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# 5️⃣ Setup RetrievalQA with Mistral model via API
|
| 116 |
qa = RetrievalQA.from_chain_type(
|
|
|
|
|
|
|
| 1 |
from langchain_community.document_loaders import PyPDFLoader
|
| 2 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 3 |
from langchain_community.vectorstores import Chroma
|
|
|
|
| 15 |
"""Initialize LLM using HuggingFace API"""
|
| 16 |
try:
|
| 17 |
# Get HF token from environment
|
| 18 |
+
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 19 |
|
| 20 |
if not hf_token:
|
| 21 |
print("Warning: No HuggingFace token found")
|
| 22 |
return None
|
| 23 |
|
| 24 |
+
# Use HuggingFaceHub with proper configuration
|
| 25 |
llm = HuggingFaceHub(
|
| 26 |
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 27 |
model_kwargs={
|
| 28 |
"temperature": 0.1,
|
| 29 |
+
"max_length": 512,
|
| 30 |
+
"max_new_tokens": 200,
|
|
|
|
| 31 |
},
|
| 32 |
huggingfacehub_api_token=hf_token
|
| 33 |
)
|
|
|
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
print(f"Error initializing LLM: {e}")
|
| 40 |
+
# Fallback to a smaller, more reliable model
|
| 41 |
+
try:
|
| 42 |
+
llm = HuggingFaceHub(
|
| 43 |
+
repo_id="google/flan-t5-base",
|
| 44 |
+
model_kwargs={
|
| 45 |
+
"temperature": 0.1,
|
| 46 |
+
"max_length": 512,
|
| 47 |
+
},
|
| 48 |
+
huggingfacehub_api_token=hf_token
|
| 49 |
+
)
|
| 50 |
+
print("Fallback to FLAN-T5 model")
|
| 51 |
+
return llm
|
| 52 |
+
except Exception as fallback_error:
|
| 53 |
+
print(f"Fallback also failed: {fallback_error}")
|
| 54 |
+
return None
|
| 55 |
|
| 56 |
# Initialize the LLM globally
|
| 57 |
llm = initialize_llm()
|
|
|
|
| 112 |
|
| 113 |
# 4️⃣ Store embeddings in ChromaDB (temp folder)
|
| 114 |
# Clear existing chroma cache first
|
|
|
|
| 115 |
if os.path.exists("./chroma_cache"):
|
| 116 |
shutil.rmtree("./chroma_cache")
|
| 117 |
|
| 118 |
+
try:
|
| 119 |
+
vectorstore = Chroma.from_documents(
|
| 120 |
+
docs,
|
| 121 |
+
embeddings,
|
| 122 |
+
persist_directory="./chroma_cache"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
print("Vector store created")
|
| 126 |
+
|
| 127 |
+
except Exception as vector_error:
|
| 128 |
+
print(f"Vector store creation failed: {vector_error}")
|
| 129 |
+
# Try without persistence directory
|
| 130 |
+
try:
|
| 131 |
+
vectorstore = Chroma.from_documents(docs, embeddings)
|
| 132 |
+
print("Vector store created without persistence")
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return f"Error creating vector database: {str(e)}"
|
| 135 |
|
| 136 |
# 5️⃣ Setup RetrievalQA with Mistral model via API
|
| 137 |
qa = RetrievalQA.from_chain_type(
|