Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,21 @@ from sentence_transformers import SentenceTransformer, util
|
|
9 |
import numpy as np
|
10 |
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
|
11 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Global variables for in-memory storage (reset on app restart)
|
14 |
corpus = [] # List of paragraphs from URLs
|
@@ -21,8 +36,14 @@ retriever = SentenceTransformer('all-MiniLM-L6-v2')
|
|
21 |
|
22 |
# Load PyTorch model for QA
|
23 |
# Model: distilbert-base-uncased-distilled-squad (~260MB)
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Set model to evaluation mode
|
28 |
model.eval()
|
|
|
9 |
import numpy as np
|
10 |
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
|
11 |
import torch
|
12 |
+
from huggingface_hub import hf_hub_download, HfFolder
|
13 |
+
from huggingface_hub.utils import configure_http_backend
|
14 |
+
import requests as hf_requests
|
15 |
+
|
16 |
+
# Configure Hugging Face Hub to use a custom session with increased timeout and retries
|
17 |
+
def create_custom_session():
|
18 |
+
session = hf_requests.Session()
|
19 |
+
# Increase timeout to 30 seconds (default is 10 seconds)
|
20 |
+
adapter = hf_requests.adapters.HTTPAdapter(max_retries=3) # Retry 3 times on failure
|
21 |
+
session.mount("https://", adapter)
|
22 |
+
session.timeout = 30 # Set timeout to 30 seconds
|
23 |
+
return session
|
24 |
+
|
25 |
+
# Set the custom session for Hugging Face Hub
|
26 |
+
configure_http_backend(backend_factory=create_custom_session)
|
27 |
|
28 |
# Global variables for in-memory storage (reset on app restart)
|
29 |
corpus = [] # List of paragraphs from URLs
|
|
|
36 |
|
37 |
# Load PyTorch model for QA
|
38 |
# Model: distilbert-base-uncased-distilled-squad (~260MB)
|
39 |
+
try:
|
40 |
+
model = AutoModelForQuestionAnswering.from_pretrained("distilbert-base-uncased-distilled-squad")
|
41 |
+
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-distilled-squad")
|
42 |
+
except Exception as e:
|
43 |
+
print(f"Error loading model: {str(e)}. Retrying with force_download=True...")
|
44 |
+
# Force re-download in case of corrupted cache
|
45 |
+
model = AutoModelForQuestionAnswering.from_pretrained("distilbert-base-uncased-distilled-squad", force_download=True)
|
46 |
+
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-distilled-squad", force_download=True)
|
47 |
|
48 |
# Set model to evaluation mode
|
49 |
model.eval()
|