Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines45678914 unmodified lines2425262728293031323334353637383 unmodified linesfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.chains import RetrievalQAfrom langchain_community.llms import HuggingFaceHubfrom langchain_community.embeddings import HuggingFaceEmbeddings# You can use this section to suppress warnings generated by your code:14 unmodified linesInitializes and returns a Hugging Face Hub LLM model.Using a conversational model suitable for legal advice."""repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"llm = HuggingFaceHub(repo_id=repo_id,# The 'task' parameter is required to specify the model's function.# This fixes the 'Value error, Got invalid task None' error.task="text-generation",model_kwargs={"temperature": 0.1, "max_length": 512})return llm## Document loaderdef document_loader(file_path):3 unmodified lines45678910111213141516171819202122232425262728293031323314 unmodified lines484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061073 unmodified linesfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.chains import RetrievalQAfrom langchain_community.llms import HuggingFaceHubtry:from langchain_huggingface import HuggingFaceEndpointHUGGINGFACE_ENDPOINT_AVAILABLE = Trueexcept ImportError:HUGGINGFACE_ENDPOINT_AVAILABLE = Falseprint("langchain-huggingface not available, using fallback")from langchain.schema import BaseLanguageModelfrom langchain.callbacks.manager import CallbackManagerForLLMRunfrom typing import Any, List, Optional# Simple mock LLM for testingclass MockLLM(BaseLanguageModel):def _llm_type(self) -> str:return "mock"def _call(self,prompt: str,stop: Optional[List[str]] = None,run_manager: Optional[CallbackManagerForLLMRun] = None,**kwargs: Any,) -> str: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.]"from langchain_community.embeddings import HuggingFaceEmbeddings# You can use this section to suppress warnings generated by your code:14 unmodified linesInitializes and returns a Hugging Face Hub LLM model.Using a conversational model suitable for legal advice."""# Check if API token is properly setapi_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")if not api_token or api_token == "hf_YOUR_HUGGINGFACE_TOKEN":print("Warning: No valid API token found. Using mock LLM for testing.")return MockLLM()if HUGGINGFACE_ENDPOINT_AVAILABLE:try:# Try the newer HuggingFaceEndpoint firstllm = HuggingFaceEndpoint(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",max_length=512,temperature=0.1,huggingfacehub_api_token=api_token)print("Successfully initialized HuggingFaceEndpoint")return llmexcept Exception as e:print(f"HuggingFaceEndpoint failed: {e}")try:# Fallback to traditional HuggingFaceHubllm = HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",task="text-generation",model_kwargs={"temperature": 0.1,"max_length": 512,"do_sample": True,"top_p": 0.9},huggingfacehub_api_token=api_token)print("Successfully initialized HuggingFaceHub")return llmexcept Exception as e:print(f"HuggingFaceHub with Mixtral failed: {e}")# Try with a smaller, more reliable modeltry:llm = HuggingFaceHub(repo_id="google/flan-t5-base",task="text2text-generation",model_kwargs={"temperature": 0.1,"max_length": 512},huggingfacehub_api_token=api_token)print("Successfully initialized HuggingFaceHub with FLAN-T5")return llmexcept Exception as e2:print(f"All HuggingFace models failed. Using mock LLM. Errors: {e}, {e2}")return MockLLM()## Document loaderdef document_loader(file_path):
app.py+78-9