Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,30 @@ from langchain_community.vectorstores import FAISS
|
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
from langchain.chains import RetrievalQA
|
| 6 |
from langchain_community.llms import HuggingFaceHub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
|
| 9 |
# You can use this section to suppress warnings generated by your code:
|
|
@@ -24,15 +48,60 @@ def get_llm():
|
|
| 24 |
Initializes and returns a Hugging Face Hub LLM model.
|
| 25 |
Using a conversational model suitable for legal advice.
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
## Document loader
|
| 38 |
def document_loader(file_path):
|
|
|
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
from langchain.chains import RetrievalQA
|
| 6 |
from langchain_community.llms import HuggingFaceHub
|
| 7 |
+
try:
|
| 8 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
+
HUGGINGFACE_ENDPOINT_AVAILABLE = True
|
| 10 |
+
except ImportError:
|
| 11 |
+
HUGGINGFACE_ENDPOINT_AVAILABLE = False
|
| 12 |
+
print("langchain-huggingface not available, using fallback")
|
| 13 |
+
|
| 14 |
+
from langchain.schema import BaseLanguageModel
|
| 15 |
+
from langchain.callbacks.manager import CallbackManagerForLLMRun
|
| 16 |
+
from typing import Any, List, Optional
|
| 17 |
+
|
| 18 |
+
# Simple mock LLM for testing
|
| 19 |
+
class MockLLM(BaseLanguageModel):
|
| 20 |
+
def _llm_type(self) -> str:
|
| 21 |
+
return "mock"
|
| 22 |
+
|
| 23 |
+
def _call(
|
| 24 |
+
self,
|
| 25 |
+
prompt: str,
|
| 26 |
+
stop: Optional[List[str]] = None,
|
| 27 |
+
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
| 28 |
+
**kwargs: Any,
|
| 29 |
+
) -> str:
|
| 30 |
+
return f"Based on the document context, here's a response to your query: {prompt[:100]}... [This is a mock response for testing. Please configure a proper LLM.]"
|
| 31 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 32 |
|
| 33 |
# You can use this section to suppress warnings generated by your code:
|
|
|
|
| 48 |
Initializes and returns a Hugging Face Hub LLM model.
|
| 49 |
Using a conversational model suitable for legal advice.
|
| 50 |
"""
|
| 51 |
+
# Check if API token is properly set
|
| 52 |
+
api_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 53 |
+
if not api_token or api_token == "hf_YOUR_HUGGINGFACE_TOKEN":
|
| 54 |
+
print("Warning: No valid API token found. Using mock LLM for testing.")
|
| 55 |
+
return MockLLM()
|
| 56 |
+
|
| 57 |
+
if HUGGINGFACE_ENDPOINT_AVAILABLE:
|
| 58 |
+
try:
|
| 59 |
+
# Try the newer HuggingFaceEndpoint first
|
| 60 |
+
llm = HuggingFaceEndpoint(
|
| 61 |
+
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 62 |
+
max_length=512,
|
| 63 |
+
temperature=0.1,
|
| 64 |
+
huggingfacehub_api_token=api_token
|
| 65 |
+
)
|
| 66 |
+
print("Successfully initialized HuggingFaceEndpoint")
|
| 67 |
+
return llm
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"HuggingFaceEndpoint failed: {e}")
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Fallback to traditional HuggingFaceHub
|
| 73 |
+
llm = HuggingFaceHub(
|
| 74 |
+
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 75 |
+
task="text-generation",
|
| 76 |
+
model_kwargs={
|
| 77 |
+
"temperature": 0.1,
|
| 78 |
+
"max_length": 512,
|
| 79 |
+
"do_sample": True,
|
| 80 |
+
"top_p": 0.9
|
| 81 |
+
},
|
| 82 |
+
huggingfacehub_api_token=api_token
|
| 83 |
+
)
|
| 84 |
+
print("Successfully initialized HuggingFaceHub")
|
| 85 |
+
return llm
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"HuggingFaceHub with Mixtral failed: {e}")
|
| 88 |
+
|
| 89 |
+
# Try with a smaller, more reliable model
|
| 90 |
+
try:
|
| 91 |
+
llm = HuggingFaceHub(
|
| 92 |
+
repo_id="google/flan-t5-base",
|
| 93 |
+
task="text2text-generation",
|
| 94 |
+
model_kwargs={
|
| 95 |
+
"temperature": 0.1,
|
| 96 |
+
"max_length": 512
|
| 97 |
+
},
|
| 98 |
+
huggingfacehub_api_token=api_token
|
| 99 |
+
)
|
| 100 |
+
print("Successfully initialized HuggingFaceHub with FLAN-T5")
|
| 101 |
+
return llm
|
| 102 |
+
except Exception as e2:
|
| 103 |
+
print(f"All HuggingFace models failed. Using mock LLM. Errors: {e}, {e2}")
|
| 104 |
+
return MockLLM()
|
| 105 |
|
| 106 |
## Document loader
|
| 107 |
def document_loader(file_path):
|