🚀 Launch the server locally and explore the interactive UI to query legal provisions and get evidence-backed results instantly!

1 Repository Clone and Setup

First, pull the project source code, initialize the execution environment, and run the data preprocessing.

!git clone https://github.com/Fan-Luo/Legal-RAG.git
%cd Legal-RAG
!python -m scripts.setup

2 Start the local LegalRAG API server

import os, time, subprocess, requests
from legalrag.config import AppConfig
cfg = AppConfig.load(None)
PORT = 7900

env = {
    **os.environ,
    "PORT": str(PORT),
    cfg.llm.qwen_model_env: "Qwen/Qwen2.5-3B-Instruct",  
}

uv = subprocess.Popen(
    ["python", "-m", "uvicorn", "legalrag.api.server:app",
     "--host", "0.0.0.0", "--port", str(PORT), "--log-level", "info"],
    env=env,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    text=True,
    bufsize=1
)

The server listens on a local port and handles incoming requests for retrieval and answer legal generation.

3 Expose the Service via a Public Tunnel

With a Cloudflare Tunnel, the locally running LegalRAG service can be accessible via a public endpoint. The process involves launching the cloudflared subprocess to create a secure tunnel, capturing the temporary public URL generated by the tunnel, and ensuring the local service is fully initialized before it becomes publicly reachable.

Starts a Cloudflare Tunnel for the local service
proc = subprocess.Popen(
    ["./cloudflared", "tunnel", "--url", f"http://127.0.0.1:{PORT}", "--no-autoupdate"],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    text=True,
)

threading.Thread(
    target=stream_logs,
    args=(proc, logger),
    daemon=True,
).start()

if not url_ready.wait(timeout=60):
    raise RuntimeError("Failed to obtain Cloudflare Tunnel URL")
Waits for server warm-up before exposing the endpoint
ready = False
for _ in range(1000):
    try:
        r = requests.get(f"http://127.0.0.1:{PORT}/ready").json()
        if r.get("warmup_done"):
            ready = True
            break
    except Exception:
        pass
    time.sleep(3)

if not ready:
    raise RuntimeError("Server not ready after warmup")

print("Public URL:", public_url)

4 Interactive UI Overview

Through this UI, users can submit queries, visualize retrieved legal provisions, obtain evidence-grounded results, and upload additional reliable legal corpora.