Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import trafilatura
|
| 5 |
from ddgs import DDGS
|
| 6 |
import uvicorn
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
from sentence_transformers import SentenceTransformer
|
|
@@ -60,6 +61,19 @@ def clean_text(text: str):
|
|
| 60 |
|
| 61 |
return text.strip()
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# --------------------------------------------------
|
| 64 |
# CHUNKING
|
| 65 |
# --------------------------------------------------
|
|
@@ -167,34 +181,67 @@ def rank_chunks(query, chunks):
|
|
| 167 |
# COMPRESS TEXT
|
| 168 |
# --------------------------------------------------
|
| 169 |
|
| 170 |
-
def compress_chunks(chunks):
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
|
| 173 |
|
| 174 |
-
|
| 175 |
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
|
| 180 |
-
if not
|
| 181 |
-
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
- total_chars
|
| 186 |
)
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
|
|
|
| 190 |
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# --------------------------------------------------
|
| 200 |
# RESEARCH PIPELINE
|
|
@@ -245,6 +292,7 @@ def perform_research(query):
|
|
| 245 |
)
|
| 246 |
|
| 247 |
compressed_content = compress_chunks(
|
|
|
|
| 248 |
ranked_chunks
|
| 249 |
)
|
| 250 |
|
|
|
|
| 4 |
import trafilatura
|
| 5 |
from ddgs import DDGS
|
| 6 |
import uvicorn
|
| 7 |
+
import re
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 61 |
|
| 62 |
return text.strip()
|
| 63 |
|
| 64 |
+
def split_sentences(text):
|
| 65 |
+
|
| 66 |
+
sentences = re.split(
|
| 67 |
+
r'(?<=[.!?])\s+',
|
| 68 |
+
text
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return [
|
| 72 |
+
s.strip()
|
| 73 |
+
for s in sentences
|
| 74 |
+
if len(s.strip()) > 40
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
# --------------------------------------------------
|
| 78 |
# CHUNKING
|
| 79 |
# --------------------------------------------------
|
|
|
|
| 181 |
# COMPRESS TEXT
|
| 182 |
# --------------------------------------------------
|
| 183 |
|
| 184 |
+
def compress_chunks(query, chunks):
|
| 185 |
+
|
| 186 |
+
try:
|
| 187 |
|
| 188 |
+
all_sentences = []
|
| 189 |
|
| 190 |
+
for chunk in chunks:
|
| 191 |
|
| 192 |
+
sentences = split_sentences(chunk)
|
| 193 |
|
| 194 |
+
all_sentences.extend(sentences)
|
| 195 |
|
| 196 |
+
if not all_sentences:
|
| 197 |
+
return ""
|
| 198 |
|
| 199 |
+
query_embedding = embedding_model.encode(
|
| 200 |
+
[query]
|
|
|
|
| 201 |
)
|
| 202 |
|
| 203 |
+
sentence_embeddings = embedding_model.encode(
|
| 204 |
+
all_sentences
|
| 205 |
+
)
|
| 206 |
|
| 207 |
+
similarities = cosine_similarity(
|
| 208 |
+
query_embedding,
|
| 209 |
+
sentence_embeddings
|
| 210 |
+
)[0]
|
| 211 |
|
| 212 |
+
ranked_sentences = sorted(
|
| 213 |
+
zip(all_sentences, similarities),
|
| 214 |
+
key=lambda x: x[1],
|
| 215 |
+
reverse=True
|
| 216 |
+
)
|
| 217 |
|
| 218 |
+
final_sentences = []
|
| 219 |
|
| 220 |
+
total_chars = 0
|
| 221 |
+
|
| 222 |
+
used = set()
|
| 223 |
+
|
| 224 |
+
for sentence, score in ranked_sentences:
|
| 225 |
+
|
| 226 |
+
normalized = sentence.lower().strip()
|
| 227 |
+
|
| 228 |
+
if normalized in used:
|
| 229 |
+
continue
|
| 230 |
+
|
| 231 |
+
used.add(normalized)
|
| 232 |
+
|
| 233 |
+
if total_chars + len(sentence) > MAX_FINAL_CONTEXT_CHARS:
|
| 234 |
+
break
|
| 235 |
+
|
| 236 |
+
final_sentences.append(sentence)
|
| 237 |
+
|
| 238 |
+
total_chars += len(sentence)
|
| 239 |
+
|
| 240 |
+
return "\n\n".join(final_sentences)
|
| 241 |
+
|
| 242 |
+
except Exception:
|
| 243 |
+
|
| 244 |
+
return "\n\n".join(chunks[:2])
|
| 245 |
|
| 246 |
# --------------------------------------------------
|
| 247 |
# RESEARCH PIPELINE
|
|
|
|
| 292 |
)
|
| 293 |
|
| 294 |
compressed_content = compress_chunks(
|
| 295 |
+
query,
|
| 296 |
ranked_chunks
|
| 297 |
)
|
| 298 |
|