Spaces:
Runtime error
Runtime error
| """ | |
| utils.py -- Helper functions used across the RAG-lite pipeline. | |
| """ | |
| import re | |
| from datetime import datetime | |
| def validate_inputs(text: str, question: str) -> tuple[bool, str]: | |
| """Basic input validation. Returns (is_valid, error_message).""" | |
| if not text or not text.strip(): | |
| return False, "Please provide a paragraph or text to analyze." | |
| if len(text.strip().split()) < 10: | |
| return False, "Text is too short. Please provide at least a few sentences." | |
| if not question or not question.strip(): | |
| return False, "Please enter a question." | |
| if len(question.strip()) < 3: | |
| return False, "Question seems too short. Try asking something more specific." | |
| return True, "" | |
| def format_debug_info(chunks: list[str], scores: list[float]) -> str: | |
| """Readable debug string showing retrieved chunks and similarity scores.""" | |
| if not chunks: | |
| return "No chunks retrieved." | |
| lines = ["Debug Info -- Top Retrieved Chunks\n"] | |
| for i, (chunk, score) in enumerate(zip(chunks, scores), start=1): | |
| preview = chunk.strip()[:200] + ("..." if len(chunk) > 200 else "") | |
| lines.append(f"[Chunk {i}] Similarity Score: {score:.4f}") | |
| lines.append(preview) | |
| lines.append("") | |
| return "\n".join(lines) | |
| def format_context_display(chunks: list[str], metadata: list[dict]) -> str: | |
| """Show which parts of the text were used to form the answer.""" | |
| if not chunks: | |
| return "" | |
| parts = ["Relevant context used:\n"] | |
| for i, (chunk, meta) in enumerate(zip(chunks, metadata), start=1): | |
| start = meta.get("start_word", "?") | |
| end = meta.get("end_word", "?") | |
| preview = chunk.strip()[:300] + ("..." if len(chunk) > 300 else "") | |
| parts.append(f"[Segment {i} -- words {start} to {end}]\n{preview}\n") | |
| return "\n".join(parts) | |
| def clean_text(text: str) -> str: | |
| """Normalize whitespace and collapse blank lines.""" | |
| text = re.sub(r"\n{3,}", "\n\n", text) | |
| text = re.sub(r"[ \t]+", " ", text) | |
| return text.strip() | |
| def format_multi_answer_output(qa_pairs: list[dict]) -> str: | |
| """ | |
| Format multiple Q/A pairs into clean professional output. | |
| No separator lines. Each pair separated by a blank line. | |
| Example output: | |
| Question 1: What is machine learning? | |
| Machine learning is a subset of artificial intelligence that enables | |
| systems to learn patterns from data and improve performance over time. | |
| Question 2: What are its types? | |
| The types include supervised learning and unsupervised learning. | |
| Question 3: What are its applications? | |
| Machine learning is used in recommendation systems, fraud detection, | |
| healthcare diagnostics, and autonomous vehicles. | |
| """ | |
| if not qa_pairs: | |
| return "" | |
| lines = [] | |
| for i, pair in enumerate(qa_pairs, start=1): | |
| q = pair.get("question", "").strip() | |
| a = pair.get("answer", "").strip() | |
| lines.append(f"Question {i}: {q}") | |
| lines.append(a) | |
| if i < len(qa_pairs): | |
| lines.append("") | |
| return "\n".join(lines) |