Spaces:
Sleeping
Sleeping
File size: 1,709 Bytes
e272f4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
from typing import Dict, List
import re
class ResponseFormatter:
@staticmethod
def clean_text(text: str) -> str:
"""Clean up raw text by removing excessive whitespace and common boilerplate"""
# Remove multiple newlines and spaces
text = re.sub(r'\n+', '\n', text)
text = re.sub(r'\s+', ' ', text)
# Remove common website elements
text = re.sub(r'SUBSCRIBE|RECENT|POPULAR|TRENDY', '', text, flags=re.I)
text = re.sub(r'Copyright © \d{4}.*', '', text)
text = re.sub(r'Privacy Policy|Terms of Service', '', text)
return text.strip()
@staticmethod
def format_sources(sources: List[Dict]) -> str:
"""Format source URLs into readable references"""
if not sources:
return ""
formatted_sources = "\n\nSources:\n"
for i, source in enumerate(sources, 1):
formatted_sources += f"{i}. {source['url']}\n"
return formatted_sources
@staticmethod
def format_response(api_response: Dict) -> str:
"""Convert API response to natural language"""
if "error" in api_response:
return f"Sorry, I encountered an error: {api_response['error']}"
if "response" not in api_response:
return "I couldn't find any relevant information."
# Clean and format the main response
clean_response = ResponseFormatter.clean_text(api_response["response"])
# Add sources if available
if "sources" in api_response:
clean_response += ResponseFormatter.format_sources(api_response["sources"])
return clean_response |