Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,24 +10,6 @@ try:
|
|
| 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:
|
|
@@ -51,57 +33,46 @@ def get_llm():
|
|
| 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 |
-
|
| 55 |
-
return MockLLM()
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"
|
| 81 |
-
|
| 82 |
-
|
| 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=
|
| 93 |
-
task=
|
| 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
|
| 101 |
return llm
|
| 102 |
-
except Exception as
|
| 103 |
-
print(f"
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
## Document loader
|
| 107 |
def document_loader(file_path):
|
|
@@ -266,12 +237,20 @@ def retriever_qa(file, query):
|
|
| 266 |
|
| 267 |
return result_text
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
except Exception as e:
|
| 270 |
error_msg = str(e)
|
| 271 |
if "API token" in error_msg or "authentication" in error_msg.lower():
|
| 272 |
return "Error: Please check your Hugging Face API token configuration."
|
| 273 |
elif "embedding" in error_msg.lower():
|
| 274 |
return "Error: Failed to create document embeddings. Please try uploading a different PDF file."
|
|
|
|
|
|
|
| 275 |
else:
|
| 276 |
return f"Error processing your request: {error_msg}"
|
| 277 |
|
|
|
|
| 10 |
except ImportError:
|
| 11 |
HUGGINGFACE_ENDPOINT_AVAILABLE = False
|
| 12 |
print("langchain-huggingface not available, using fallback")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 14 |
|
| 15 |
# You can use this section to suppress warnings generated by your code:
|
|
|
|
| 33 |
# Check if API token is properly set
|
| 34 |
api_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 35 |
if not api_token or api_token == "hf_YOUR_HUGGINGFACE_TOKEN":
|
| 36 |
+
raise ValueError("Please set a valid HUGGINGFACEHUB_API_TOKEN environment variable. You can get one from https://huggingface.co/settings/tokens")
|
|
|
|
| 37 |
|
| 38 |
+
# Try different models in order of preference
|
| 39 |
+
models_to_try = [
|
| 40 |
+
("mistralai/Mixtral-8x7B-Instruct-v0.1", "text-generation"),
|
| 41 |
+
("microsoft/DialoGPT-medium", "text-generation"),
|
| 42 |
+
("google/flan-t5-base", "text2text-generation"),
|
| 43 |
+
("huggingface/CodeBERTa-small-v1", "text-generation")
|
| 44 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
for repo_id, task in models_to_try:
|
| 47 |
+
if HUGGINGFACE_ENDPOINT_AVAILABLE:
|
| 48 |
+
try:
|
| 49 |
+
llm = HuggingFaceEndpoint(
|
| 50 |
+
repo_id=repo_id,
|
| 51 |
+
max_length=512,
|
| 52 |
+
temperature=0.1,
|
| 53 |
+
huggingfacehub_api_token=api_token
|
| 54 |
+
)
|
| 55 |
+
print(f"Successfully initialized HuggingFaceEndpoint with {repo_id}")
|
| 56 |
+
return llm
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"HuggingFaceEndpoint with {repo_id} failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 60 |
try:
|
| 61 |
llm = HuggingFaceHub(
|
| 62 |
+
repo_id=repo_id,
|
| 63 |
+
task=task,
|
| 64 |
model_kwargs={
|
| 65 |
+
"temperature": 0.1,
|
| 66 |
"max_length": 512
|
| 67 |
},
|
| 68 |
huggingfacehub_api_token=api_token
|
| 69 |
)
|
| 70 |
+
print(f"Successfully initialized HuggingFaceHub with {repo_id}")
|
| 71 |
return llm
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"HuggingFaceHub with {repo_id} failed: {e}")
|
| 74 |
+
|
| 75 |
+
raise ValueError("All LLM initialization attempts failed. Please check your API token and internet connection.")
|
| 76 |
|
| 77 |
## Document loader
|
| 78 |
def document_loader(file_path):
|
|
|
|
| 237 |
|
| 238 |
return result_text
|
| 239 |
|
| 240 |
+
except ValueError as ve:
|
| 241 |
+
# Handle specific ValueError (like API token issues)
|
| 242 |
+
if "API token" in str(ve):
|
| 243 |
+
return f"Configuration Error: {ve}\n\nPlease:\n1. Get a HuggingFace API token from https://huggingface.co/settings/tokens\n2. Set it as HUGGINGFACEHUB_API_TOKEN environment variable"
|
| 244 |
+
else:
|
| 245 |
+
return f"Error: {ve}"
|
| 246 |
except Exception as e:
|
| 247 |
error_msg = str(e)
|
| 248 |
if "API token" in error_msg or "authentication" in error_msg.lower():
|
| 249 |
return "Error: Please check your Hugging Face API token configuration."
|
| 250 |
elif "embedding" in error_msg.lower():
|
| 251 |
return "Error: Failed to create document embeddings. Please try uploading a different PDF file."
|
| 252 |
+
elif "InferenceClient" in error_msg:
|
| 253 |
+
return "Error: HuggingFace library compatibility issue. Please try updating your dependencies or contact support."
|
| 254 |
else:
|
| 255 |
return f"Error processing your request: {error_msg}"
|
| 256 |
|