Shriharsh commited on
Commit
f1dc219
·
verified ·
1 Parent(s): df6464c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
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: ~340MB total)
34
- # Retrieval model: all-MiniLM-L6-v2 (~80MB, 384-dim embeddings)
35
- retriever = SentenceTransformer('all-MiniLM-L6-v2')
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: ~1.5KB per paragraph, ~450KB for 300 paragraphs
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 1 paragraph to reduce inference time.
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(1, len(corpus)) # Get top 1 paragraph to speed up inference
122
  top_indices = np.argsort(-cos_scores)[:top_k]
123
 
124
- # Retrieve context (top 1 paragraph)
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]