Shriharsh commited on
Commit
5148cc2
·
verified ·
1 Parent(s): 1544b6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -22
app.py CHANGED
@@ -1,6 +1,8 @@
 
 
1
  # Web Content Q&A Tool for Hugging Face Spaces
2
  # Optimized for memory constraints (2GB RAM) and 24-hour timeline
3
- # Features: Ingest up to 3 URLs, ask questions, get concise one-line answers using DistilBERT with PyTorch
4
 
5
  import gradio as gr
6
  from bs4 import BeautifulSoup
@@ -12,7 +14,6 @@ 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
- import re
16
 
17
  # Configure Hugging Face Hub to use a custom session with increased timeout and retries
18
  def create_custom_session():
@@ -57,16 +58,6 @@ model = torch.quantization.quantize_dynamic(
57
  # Create the QA pipeline with PyTorch
58
  qa_model = pipeline("question-answering", model=model, tokenizer=tokenizer, framework="pt", device=-1) # device=-1 for CPU
59
 
60
- # Utility function to truncate text to one line
61
- def truncate_to_one_line(text):
62
- # Split by sentence-ending punctuation and take the first sentence
63
- sentences = re.split(r'[.!?]+', text.strip())
64
- first_sentence = sentences[0].strip() if sentences else text.strip()
65
- # If the sentence is too long, truncate to 100 characters
66
- if len(first_sentence) > 100:
67
- first_sentence = first_sentence[:100].rsplit(' ', 1)[0] + "..."
68
- return first_sentence if first_sentence else "No answer available."
69
-
70
  def ingest_urls(urls):
71
  """
72
  Ingest up to 3 URLs, scrape content, and compute embeddings.
@@ -117,9 +108,8 @@ def ingest_urls(urls):
117
  def answer_question(question):
118
  """
119
  Answer a question using retrieved context and DistilBERT QA (PyTorch).
120
- Retrieves top 2 paragraphs to improve answer accuracy.
121
  If total context exceeds 512 tokens (DistilBERT's max length), it will be truncated automatically.
122
- Ensures answers are one line (max 100 chars).
123
  """
124
  global corpus, embeddings, sources_list
125
  if not corpus or embeddings is None:
@@ -130,10 +120,10 @@ def answer_question(question):
130
 
131
  # Compute cosine similarity with stored embeddings
132
  cos_scores = util.cos_sim(question_embedding, embeddings)[0]
133
- top_k = min(2, len(corpus)) # Get top 2 paragraphs to improve accuracy
134
  top_indices = np.argsort(-cos_scores)[:top_k]
135
 
136
- # Retrieve context (top 2 paragraphs)
137
  contexts = [corpus[i] for i in top_indices]
138
  context = " ".join(contexts) # Concatenate with space
139
  sources = [sources_list[i] for i in top_indices]
@@ -144,12 +134,6 @@ def answer_question(question):
144
  answer = result['answer']
145
  confidence = result['score']
146
 
147
- # Truncate answer to one line
148
- answer = truncate_to_one_line(answer)
149
- # Ensure at least one line
150
- if not answer:
151
- answer = "No answer available."
152
-
153
  # Format response with answer, confidence, and sources
154
  sources_str = "\n".join(set(sources)) # Unique sources
155
  return f"Answer: {answer}\nConfidence: {confidence:.2f}\nSources:\n{sources_str}"
 
1
+ this is the app.py code without keyword searching
2
+
3
  # Web Content Q&A Tool for Hugging Face Spaces
4
  # Optimized for memory constraints (2GB RAM) and 24-hour timeline
5
+ # Features: Ingest up to 3 URLs, ask questions, get concise answers using DistilBERT with PyTorch
6
 
7
  import gradio as gr
8
  from bs4 import BeautifulSoup
 
14
  from huggingface_hub import hf_hub_download, HfFolder
15
  from huggingface_hub.utils import configure_http_backend
16
  import requests as hf_requests
 
17
 
18
  # Configure Hugging Face Hub to use a custom session with increased timeout and retries
19
  def create_custom_session():
 
58
  # Create the QA pipeline with PyTorch
59
  qa_model = pipeline("question-answering", model=model, tokenizer=tokenizer, framework="pt", device=-1) # device=-1 for CPU
60
 
 
 
 
 
 
 
 
 
 
 
61
  def ingest_urls(urls):
62
  """
63
  Ingest up to 3 URLs, scrape content, and compute embeddings.
 
108
  def answer_question(question):
109
  """
110
  Answer a question using retrieved context and DistilBERT QA (PyTorch).
111
+ Retrieves top 3 paragraphs to improve answer accuracy.
112
  If total context exceeds 512 tokens (DistilBERT's max length), it will be truncated automatically.
 
113
  """
114
  global corpus, embeddings, sources_list
115
  if not corpus or embeddings is None:
 
120
 
121
  # Compute cosine similarity with stored embeddings
122
  cos_scores = util.cos_sim(question_embedding, embeddings)[0]
123
+ top_k = min(1, len(corpus)) # Get top 3 paragraphs to improve accuracy
124
  top_indices = np.argsort(-cos_scores)[:top_k]
125
 
126
+ # Retrieve context (top 3 paragraphs)
127
  contexts = [corpus[i] for i in top_indices]
128
  context = " ".join(contexts) # Concatenate with space
129
  sources = [sources_list[i] for i in top_indices]
 
134
  answer = result['answer']
135
  confidence = result['score']
136
 
 
 
 
 
 
 
137
  # Format response with answer, confidence, and sources
138
  sources_str = "\n".join(set(sources)) # Unique sources
139
  return f"Answer: {answer}\nConfidence: {confidence:.2f}\nSources:\n{sources_str}"