Update README.md
Browse files
README.md
CHANGED
|
@@ -4,10 +4,99 @@ emoji: π
|
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.9.1
|
| 8 |
+
python_version: '3.11'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Text-to-SQL Trace RAG β Prototype (Gradio / HF Spaces)
|
| 14 |
+
|
| 15 |
+
A minimal prototype of **reasoning-trace RAG** applied to retail BI queries.
|
| 16 |
+
|
| 17 |
+
Instead of retrieving documents/facts, this retrieves **verified past
|
| 18 |
+
question β SQL β reasoning examples** and uses them as few-shot patterns
|
| 19 |
+
to guide new SQL generation. Built for repeat-shaped BI questions
|
| 20 |
+
("total X by Y for period Z", "top N by metric", "compare A vs B").
|
| 21 |
+
|
| 22 |
+
Runs as a **Gradio app** β deploys directly on HuggingFace Spaces'
|
| 23 |
+
free Gradio SDK. (Docker Spaces are no longer free on HF, so this
|
| 24 |
+
version drops the Dockerfile entirely.)
|
| 25 |
+
|
| 26 |
+
## How it works
|
| 27 |
+
|
| 28 |
+
1. `TRACE_LIBRARY` holds verified past questions with their correct SQL
|
| 29 |
+
and the reasoning steps that produced it.
|
| 30 |
+
2. Each question is embedded with `sentence-transformers` and indexed in
|
| 31 |
+
FAISS.
|
| 32 |
+
3. A new question is embedded, FAISS retrieves the most structurally
|
| 33 |
+
similar past example(s).
|
| 34 |
+
4. Those examples are inserted into the prompt as exemplars β the model
|
| 35 |
+
(via Groq) reuses the query-building *pattern*, not just column names.
|
| 36 |
+
5. The generated SQL runs against a DuckDB table and returns real results.
|
| 37 |
+
6. A second tab lets you manually confirm a generated query was correct
|
| 38 |
+
and add it back into the trace library β this is how the library
|
| 39 |
+
compounds over time.
|
| 40 |
+
|
| 41 |
+
This is the same retrieve β augment β generate mechanism as normal RAG.
|
| 42 |
+
The only difference is what gets indexed: solved query patterns instead
|
| 43 |
+
of documents.
|
| 44 |
+
|
| 45 |
+
## Files
|
| 46 |
+
|
| 47 |
+
- `app.py` β Gradio app (toy DuckDB table + FAISS index + Groq generation
|
| 48 |
+
+ UI to grow the trace library)
|
| 49 |
+
- `requirements.txt` β pinned dependencies (includes `gradio`)
|
| 50 |
+
- `README.md` β this file
|
| 51 |
+
|
| 52 |
+
## Run locally
|
| 53 |
+
|
| 54 |
+
```bash
|
| 55 |
+
pip install -r requirements.txt --break-system-packages
|
| 56 |
+
export GROQ_API_KEY=your_key_here
|
| 57 |
+
python app.py
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
Opens at `http://127.0.0.1:7860`.
|
| 61 |
+
|
| 62 |
+
## Deploy on HuggingFace Spaces (free, no Docker)
|
| 63 |
+
|
| 64 |
+
1. Create a new Space β SDK: **Gradio** (not Docker).
|
| 65 |
+
2. Upload `app.py` and `requirements.txt` to the Space repo (matches
|
| 66 |
+
your usual HF Spaces deployment pattern).
|
| 67 |
+
3. Go to **Settings β Repository secrets** on the Space and add:
|
| 68 |
+
- `GROQ_API_KEY` = your Groq key
|
| 69 |
+
4. Space builds automatically and launches `app.py`.
|
| 70 |
+
|
| 71 |
+
No Dockerfile needed β Spaces installs `requirements.txt` and runs
|
| 72 |
+
`app.py` directly on the Gradio SDK runtime.
|
| 73 |
+
|
| 74 |
+
## Making this real (not a toy)
|
| 75 |
+
|
| 76 |
+
1. **Replace the toy `sales` table** in `app.py` with a real extract of
|
| 77 |
+
your actual schema (or connect DuckDB to your real source instead of
|
| 78 |
+
the in-memory `CREATE TABLE` block).
|
| 79 |
+
2. **Replace `TRACE_LIBRARY`** with your team's actual most-common BI
|
| 80 |
+
question shapes. Hand-verify the correct SQL for each entry before
|
| 81 |
+
adding it β a wrong example teaches the model a wrong pattern, which
|
| 82 |
+
is worse than no example at all.
|
| 83 |
+
3. **Use the "Add verified example" tab** as the feedback loop: after a
|
| 84 |
+
generated query is confirmed correct, add it there so future similar
|
| 85 |
+
questions benefit. Note the library currently resets on app
|
| 86 |
+
restart (it's in-memory) β for a persistent version, write
|
| 87 |
+
`TRACE_LIBRARY` to a small JSON file or SQLite table on add, and load
|
| 88 |
+
it on startup, instead of keeping it as a plain Python list.
|
| 89 |
+
4. **Don't force-fit this everywhere.** This pattern only earns its
|
| 90 |
+
complexity on repeat-shaped analytical/diagnostic questions. For
|
| 91 |
+
fact-lookup use cases (order status, policy answers β e.g. Reem),
|
| 92 |
+
plain document RAG is the right tool; this adds nothing there.
|
| 93 |
+
|
| 94 |
+
## Caveats
|
| 95 |
+
|
| 96 |
+
- The trace library needs *coverage of distinct question shapes*, not
|
| 97 |
+
raw volume β a few dozen verified examples covering your real question
|
| 98 |
+
patterns will outperform hundreds of near-duplicates.
|
| 99 |
+
- Never auto-add unverified traces to the library; incorrect examples
|
| 100 |
+
compound into worse output over time, not just missing info.
|
| 101 |
+
- In-memory `TRACE_LIBRARY` resets every time the Space restarts/sleeps
|
| 102 |
+
on the free tier β fine for a prototype demo, not for production use.
|