Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -30,9 +30,9 @@ corpus = [] # List of paragraphs from URLs
|
|
30 |
embeddings = None # Precomputed embeddings for retrieval
|
31 |
sources_list = [] # Source URLs for each paragraph
|
32 |
|
33 |
-
# Load models at startup (memory: ~
|
34 |
-
# Retrieval model: all-
|
35 |
-
retriever = SentenceTransformer('all-
|
36 |
|
37 |
# Load PyTorch model for QA
|
38 |
# Model: distilbert-base-uncased-distilled-squad (~260MB)
|
@@ -98,7 +98,7 @@ def ingest_urls(urls):
|
|
98 |
|
99 |
# Compute embeddings if content was ingested
|
100 |
if corpus:
|
101 |
-
# Embeddings: ~
|
102 |
embeddings = retriever.encode(corpus, convert_to_tensor=True, show_progress_bar=False)
|
103 |
return f"Success: Ingested {len(corpus)} paragraphs from {len(set(url_list))} URLs."
|
104 |
return "Error: No valid content ingested."
|
@@ -106,7 +106,7 @@ def ingest_urls(urls):
|
|
106 |
def answer_question(question):
|
107 |
"""
|
108 |
Answer a question using retrieved context and DistilBERT QA (PyTorch).
|
109 |
-
Retrieves top
|
110 |
If total context exceeds 512 tokens (DistilBERT's max length), it will be truncated automatically.
|
111 |
"""
|
112 |
global corpus, embeddings, sources_list
|
@@ -118,10 +118,10 @@ def answer_question(question):
|
|
118 |
|
119 |
# Compute cosine similarity with stored embeddings
|
120 |
cos_scores = util.cos_sim(question_embedding, embeddings)[0]
|
121 |
-
top_k = min(
|
122 |
top_indices = np.argsort(-cos_scores)[:top_k]
|
123 |
|
124 |
-
# Retrieve context (top
|
125 |
contexts = [corpus[i] for i in top_indices]
|
126 |
context = " ".join(contexts) # Concatenate with space
|
127 |
sources = [sources_list[i] for i in top_indices]
|
|
|
30 |
embeddings = None # Precomputed embeddings for retrieval
|
31 |
sources_list = [] # Source URLs for each paragraph
|
32 |
|
33 |
+
# Load models at startup (memory: ~370MB total)
|
34 |
+
# Retrieval model: all-mpnet-base-v2 (~110MB, 768-dim embeddings)
|
35 |
+
retriever = SentenceTransformer('all-mpnet-base-v2')
|
36 |
|
37 |
# Load PyTorch model for QA
|
38 |
# Model: distilbert-base-uncased-distilled-squad (~260MB)
|
|
|
98 |
|
99 |
# Compute embeddings if content was ingested
|
100 |
if corpus:
|
101 |
+
# Embeddings: ~3KB per paragraph, ~900KB for 300 paragraphs (768-dim)
|
102 |
embeddings = retriever.encode(corpus, convert_to_tensor=True, show_progress_bar=False)
|
103 |
return f"Success: Ingested {len(corpus)} paragraphs from {len(set(url_list))} URLs."
|
104 |
return "Error: No valid content ingested."
|
|
|
106 |
def answer_question(question):
|
107 |
"""
|
108 |
Answer a question using retrieved context and DistilBERT QA (PyTorch).
|
109 |
+
Retrieves top 3 paragraphs to improve answer accuracy.
|
110 |
If total context exceeds 512 tokens (DistilBERT's max length), it will be truncated automatically.
|
111 |
"""
|
112 |
global corpus, embeddings, sources_list
|
|
|
118 |
|
119 |
# Compute cosine similarity with stored embeddings
|
120 |
cos_scores = util.cos_sim(question_embedding, embeddings)[0]
|
121 |
+
top_k = min(2, len(corpus)) # Get top 3 paragraphs to improve accuracy
|
122 |
top_indices = np.argsort(-cos_scores)[:top_k]
|
123 |
|
124 |
+
# Retrieve context (top 3 paragraphs)
|
125 |
contexts = [corpus[i] for i in top_indices]
|
126 |
context = " ".join(contexts) # Concatenate with space
|
127 |
sources = [sources_list[i] for i in top_indices]
|