#!/usr/bin/env python3 """Gradio UI for the offline mini-RAG example. This Space imports mini_rag.py and uses its index and answer functions as they are. It does not change the retrieval or answering logic. """ from __future__ import annotations import sys from pathlib import Path import gradio as gr import mini_rag DATA_DIR = Path(__file__).resolve().parent / "data" INDEX = mini_rag.build_index(DATA_DIR) EXAMPLES = [ "What does MCP stand for?", "What are the four steps of a RAG pipeline?", "What does a forward deployed engineer do?", "Who won the 1974 FIFA World Cup?", ] EXAMPLE_LABELS = [ "MCP", "RAG pipeline", "FDE", "Out of scope (should refuse)", ] def answer(question: str) -> str: """Return the extractive, cited answer from mini_rag, or a refusal.""" question = (question or "").strip() if not question: return "Please enter a question." try: result = mini_rag.answer_question(INDEX, question) return mini_rag.format_answer(result) except Exception as exc: return f"Something went wrong while answering: {exc}" DESCRIPTION = """ Ask a question about the three short Datamarcos explainer notes in this Space (FDE, MCP, and RAG). The app retrieves matching passages with citations. If nothing in the corpus is similar enough, it says it does not know instead of guessing. The last example is out of scope and should take that path. Source code: [datamarcos-official/agentic-ai-projects](https://github.com/datamarcos-official/agentic-ai-projects). Guides: [datamarcos.com/guides](https://www.datamarcos.com/guides). """ with gr.Blocks(title="Mini RAG with Citations") as demo: gr.Markdown("# Mini RAG with Citations") gr.Markdown(DESCRIPTION) question = gr.Textbox( label="Question", lines=2, placeholder="Ask a question about FDE, MCP, or RAG...", ) output = gr.Textbox(label="Answer with cited sources", lines=14) ask = gr.Button("Ask", variant="primary") ask.click(fn=answer, inputs=question, outputs=output) question.submit(fn=answer, inputs=question, outputs=output) gr.Examples( examples=[[example] for example in EXAMPLES], example_labels=EXAMPLE_LABELS, inputs=question, label="Example questions", ) def _in_browser() -> bool: return sys.platform == "emscripten" or "pyodide" in sys.modules if _in_browser() or __name__ == "__main__": demo.launch(share=False)