Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -227,59 +227,6 @@ def get_truncated_prompt(prompt: str, max_tokens: int = 3072) -> str:
|
|
| 227 |
truncated_text = lfm_tokenizer.decode(truncated_tokens, skip_special_tokens=True)
|
| 228 |
return truncated_text + " [Content truncated...]"
|
| 229 |
|
| 230 |
-
def summarize_tool_output(tool_output: str, max_length: int = 500) -> str:
|
| 231 |
-
"""
|
| 232 |
-
Summarize web search results to reduce token count dramatically.
|
| 233 |
-
Extracts only essential information.
|
| 234 |
-
"""
|
| 235 |
-
import re
|
| 236 |
-
|
| 237 |
-
# For price queries, extract key data
|
| 238 |
-
lower_output = tool_output.lower()
|
| 239 |
-
|
| 240 |
-
# Extract prices (format: $XX,XXX.XX)
|
| 241 |
-
prices = re.findall(r'\$\d{1,3}(?:,\d{3})*\.\d{2}', tool_output)
|
| 242 |
-
|
| 243 |
-
# Extract market cap (format: $X.XXT or $X.XXB)
|
| 244 |
-
market_caps = re.findall(r'\$[\d\.]+[TBM]', tool_output)
|
| 245 |
-
|
| 246 |
-
# Extract volume
|
| 247 |
-
volume_matches = re.findall(r'volume.*?(\$[\d\.]+[TBM])', lower_output)
|
| 248 |
-
|
| 249 |
-
# Extract change percentages
|
| 250 |
-
changes = re.findall(r'[-+]?\d+\.?\d*%', tool_output)
|
| 251 |
-
|
| 252 |
-
# Build concise summary
|
| 253 |
-
summary_parts = []
|
| 254 |
-
|
| 255 |
-
if prices:
|
| 256 |
-
# Take the first 2 unique prices
|
| 257 |
-
unique_prices = []
|
| 258 |
-
for price in prices[:5]:
|
| 259 |
-
if price not in unique_prices:
|
| 260 |
-
unique_prices.append(price)
|
| 261 |
-
if unique_prices:
|
| 262 |
-
summary_parts.append(f"Prices found: {', '.join(unique_prices[:2])}")
|
| 263 |
-
|
| 264 |
-
if market_caps:
|
| 265 |
-
summary_parts.append(f"Market cap: {market_caps[0]}")
|
| 266 |
-
|
| 267 |
-
if volume_matches:
|
| 268 |
-
summary_parts.append(f"24h volume: {volume_matches[0]}")
|
| 269 |
-
|
| 270 |
-
if changes:
|
| 271 |
-
# Look for the change that's near "24h" or "change"
|
| 272 |
-
for i, change in enumerate(changes[:3]):
|
| 273 |
-
if "24h" in lower_output or "change" in lower_output:
|
| 274 |
-
summary_parts.append(f"24h change: {change}")
|
| 275 |
-
break
|
| 276 |
-
|
| 277 |
-
if summary_parts:
|
| 278 |
-
return " | ".join(summary_parts)
|
| 279 |
-
else:
|
| 280 |
-
# Fallback: first 200 chars
|
| 281 |
-
return tool_output[:200] + "..."
|
| 282 |
-
|
| 283 |
# ================== OPTIMIZED STREAMING GENERATION =================
|
| 284 |
async def generate_reply_stream(
|
| 285 |
prompt: str,
|
|
|
|
| 227 |
truncated_text = lfm_tokenizer.decode(truncated_tokens, skip_special_tokens=True)
|
| 228 |
return truncated_text + " [Content truncated...]"
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
# ================== OPTIMIZED STREAMING GENERATION =================
|
| 231 |
async def generate_reply_stream(
|
| 232 |
prompt: str,
|