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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# ============================================================
# DDS SQL Agent with Modern LangChain Memory + Gradio UI
# Hugging Face Spaces version
# ============================================================

import os
import re
import sqlite3
from pathlib import Path
from uuid import uuid4

import gradio as gr
from langchain.agents import create_agent
from langchain.tools import tool
from langgraph.checkpoint.memory import InMemorySaver


# ------------------------------------------------------------
# 1. Environment configuration
# ------------------------------------------------------------
# Add this in Hugging Face Space Settings -> Variables and Secrets:
# Secret name: OPENAI_API_KEY
#
# Optional Space variables:
# MODEL_NAME = openai:gpt-5.4
# DATABASE_PATH = data/Chinook_Sqlite.sqlite

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME", "openai:gpt-5.4")
DATABASE_PATH = Path(os.getenv("DATABASE_PATH", "data/Chinook_Sqlite.sqlite"))


# ------------------------------------------------------------
# 2. Database helpers
# ------------------------------------------------------------

def resolve_database_path() -> Path:
    """
    Resolve the SQLite database path.

    Default:
    - data/Chinook_Sqlite.sqlite

    You can override it in Hugging Face Spaces with:
    DATABASE_PATH=/path/to/your/database.sqlite
    """

    if DATABASE_PATH.exists():
        return DATABASE_PATH

    common_paths = [
        Path("Chinook_Sqlite.sqlite"),
        Path("chinook.db"),
        Path("Chinook.db"),
        Path("data/chinook.db"),
        Path("data/Chinook.db"),
    ]

    for path in common_paths:
        if path.exists():
            return path

    raise FileNotFoundError(
        "SQLite database file was not found. "
        "Upload your database file or set DATABASE_PATH in Hugging Face Variables."
    )


DB_PATH = resolve_database_path()


def get_database_schema(db_path: Path) -> str:
    """
    Extract table and column information from the SQLite database.
    This schema is injected into the system prompt so the agent knows the DB structure.
    """

    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()

    cursor.execute(
        """
        SELECT name
        FROM sqlite_master
        WHERE type = 'table'
          AND name NOT LIKE 'sqlite_%'
        ORDER BY name;
        """
    )

    tables = [row[0] for row in cursor.fetchall()]
    schema_lines = []

    for table in tables:
        schema_lines.append(f"\nTable: {table}")

        cursor.execute(f"PRAGMA table_info({table});")
        columns = cursor.fetchall()

        for column in columns:
            # PRAGMA table_info columns:
            # cid, name, type, notnull, dflt_value, pk
            _, name, col_type, notnull, _, pk = column

            flags = []
            if pk:
                flags.append("PRIMARY KEY")
            if notnull:
                flags.append("NOT NULL")

            flag_text = f" ({', '.join(flags)})" if flags else ""
            schema_lines.append(f"- {name}: {col_type}{flag_text}")

    conn.close()

    return "\n".join(schema_lines)


DATABASE_SCHEMA = get_database_schema(DB_PATH)


def strip_sql_code_fences(query: str) -> str:
    """
    Removes markdown code fences if the model returns SQL inside ```sql ... ```.
    """

    query = query.strip()

    if query.startswith("```"):
        query = re.sub(r"^```(?:sql)?", "", query, flags=re.IGNORECASE).strip()
        query = re.sub(r"```$", "", query).strip()

    return query


def is_read_only_sql(query: str) -> bool:
    """
    Basic read-only protection.
    Allows SELECT, WITH, PRAGMA, and EXPLAIN.
    Blocks INSERT, UPDATE, DELETE, DROP, ALTER, CREATE, etc.
    """

    cleaned = strip_sql_code_fences(query)
    cleaned = re.sub(r"/\*.*?\*/", "", cleaned, flags=re.DOTALL)
    cleaned = re.sub(r"--.*?$", "", cleaned, flags=re.MULTILINE)
    cleaned = cleaned.strip().lower()

    allowed_starts = ("select", "with", "pragma", "explain")

    if not cleaned.startswith(allowed_starts):
        return False

    blocked_keywords = [
        "insert ",
        "update ",
        "delete ",
        "drop ",
        "alter ",
        "create ",
        "replace ",
        "truncate ",
        "attach ",
        "detach ",
        "vacuum",
        "reindex",
    ]

    return not any(keyword in cleaned for keyword in blocked_keywords)


def rows_to_markdown(columns, rows, max_rows: int = 50) -> str:
    """
    Convert SQL rows to a Markdown table for readable chatbot output.
    """

    if not rows:
        return "Query executed successfully, but returned no rows."

    rows = rows[:max_rows]

    def clean_cell(value):
        if value is None:
            return ""
        text = str(value)
        text = text.replace("\n", " ").replace("|", "\\|")
        return text

    header = "| " + " | ".join(columns) + " |"
    separator = "| " + " | ".join(["---"] * len(columns)) + " |"

    body_lines = []
    for row in rows:
        body_lines.append("| " + " | ".join(clean_cell(value) for value in row) + " |")

    return "\n".join([header, separator] + body_lines)


# ------------------------------------------------------------
# 3. SQL tool
# ------------------------------------------------------------

@tool
def execute_sql(query: str) -> str:
    """
    Execute a read-only SQLite SQL query against the Chinook database.

    Use this tool when the user asks analytical questions that require database access.
    Only SELECT, WITH, PRAGMA, and EXPLAIN queries are allowed.
    """

    query = strip_sql_code_fences(query)

    if not is_read_only_sql(query):
        return (
            "Blocked for safety. Only read-only SQL is allowed. "
            "Please use SELECT, WITH, PRAGMA, or EXPLAIN queries."
        )

    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        cursor.execute(query)

        rows = cursor.fetchall()
        columns = [description[0] for description in cursor.description] if cursor.description else []

        conn.close()

        if not columns:
            return "Query executed successfully."

        result_table = rows_to_markdown(columns, rows)

        if len(rows) > 50:
            result_table += f"\n\nShowing first 50 rows out of {len(rows)} rows."

        return result_table

    except Exception as e:
        return f"SQL execution error: {str(e)}"


# ------------------------------------------------------------
# 4. System prompt
# ------------------------------------------------------------

SYSTEM_PROMPT = f"""
You are a helpful SQL data analyst for the Chinook SQLite database.

Your job:
- Understand the user's business/data question.
- Write correct SQLite queries.
- Use the execute_sql tool to query the database.
- Explain the result clearly and concisely.
- For follow-up questions, use the conversation memory.

Important rules:
- Use only read-only SQL.
- Never modify the database.
- Prefer clear SQL with explicit table joins.
- When useful, explain the SQL logic briefly.
- If the user asks a vague question, make a reasonable interpretation and proceed.
- If the database does not contain enough information, say that clearly.

Available database schema:
{DATABASE_SCHEMA}
"""


# ------------------------------------------------------------
# 5. Create LangChain agent with short-term memory
# ------------------------------------------------------------
# InMemorySaver gives thread-level memory during the live Space session.
# For production-grade persistent memory, replace this with a database-backed checkpointer.

checkpointer = InMemorySaver()

sql_agent_with_memory = create_agent(
    model=MODEL_NAME,
    tools=[execute_sql],
    system_prompt=SYSTEM_PROMPT,
    checkpointer=checkpointer,
)


# ------------------------------------------------------------
# 6. Gradio helpers
# ------------------------------------------------------------

def content_to_text(content):
    """
    Convert LangChain message content into displayable text.
    """

    if isinstance(content, str):
        return content

    if isinstance(content, list):
        text_parts = []

        for item in content:
            if isinstance(item, dict):
                if "text" in item:
                    text_parts.append(item["text"])
                elif "content" in item:
                    text_parts.append(str(item["content"]))
                else:
                    text_parts.append(str(item))
            else:
                text_parts.append(str(item))

        return "\n".join(text_parts)

    return str(content)


def create_thread_id():
    """
    Same thread_id = same LangGraph memory.
    New thread_id = fresh conversation.
    """

    return f"dds-sql-agent-{uuid4()}"


def normalize_history_to_messages(history):
    """
    Gradio expects messages format:
    [
        {"role": "user", "content": "..."},
        {"role": "assistant", "content": "..."}
    ]
    """

    if history is None:
        return []

    normalized = []

    for item in history:
        if isinstance(item, dict) and "role" in item and "content" in item:
            role = item.get("role")
            if role in ["user", "assistant"]:
                normalized.append(
                    {
                        "role": role,
                        "content": content_to_text(item.get("content", "")),
                    }
                )

    return normalized


# ------------------------------------------------------------
# 7. Gradio chat function
# ------------------------------------------------------------

def chat_with_sql_agent(message, history, thread_id):
    """
    Handles one user message from Gradio.

    This returns messages format without passing type="messages"
    to gr.Chatbot, because some Gradio 6 runtimes expect messages
    but do not accept the type argument.
    """

    history = normalize_history_to_messages(history)

    if not OPENAI_API_KEY:
        assistant_message = (
            "OPENAI_API_KEY is missing. In Hugging Face Spaces, go to "
            "Settings → Variables and Secrets → New Secret, then add:\n\n"
            "`OPENAI_API_KEY = your_openai_api_key`"
        )

        return history + [
            {"role": "user", "content": message or ""},
            {"role": "assistant", "content": assistant_message},
        ], "", thread_id or create_thread_id()

    if not thread_id:
        thread_id = create_thread_id()

    if not message or not message.strip():
        return history, "", thread_id

    user_message = message.strip()

    try:
        result = sql_agent_with_memory.invoke(
            {
                "messages": [
                    {
                        "role": "user",
                        "content": user_message,
                    }
                ]
            },
            config={
                "configurable": {
                    "thread_id": thread_id
                }
            },
        )

        assistant_message = content_to_text(result["messages"][-1].content)

    except Exception as e:
        assistant_message = f"""
Something went wrong while running the SQL agent.

Error:

```text
{str(e)}
```

Check:
1. OPENAI_API_KEY is set in Hugging Face Secrets.
2. MODEL_NAME is available in your OpenAI account.
3. The SQLite database file exists at: `{DB_PATH}`
"""

    updated_history = history + [
        {
            "role": "user",
            "content": user_message,
        },
        {
            "role": "assistant",
            "content": assistant_message,
        },
    ]

    return updated_history, "", thread_id


def reset_chat():
    """
    Clears UI history and starts a fresh memory thread.
    """

    return [], create_thread_id()


def example_question(question):
    """
    Puts an example question into the textbox.
    """

    return question


# ------------------------------------------------------------
# 8. Build Gradio app
# ------------------------------------------------------------

custom_css = """
#main-container {
    max-width: 1100px;
    margin: 0 auto;
}

.dds-note {
    font-size: 0.95rem;
    opacity: 0.85;
}
"""

with gr.Blocks(title="DDS SQL Agent", css=custom_css) as demo:

    thread_id_state = gr.State(value=create_thread_id())

    with gr.Column(elem_id="main-container"):
        gr.Markdown(
            f"""
# DDS SQL Agent with Memory

Ask questions about the Chinook SQLite database.  
The agent can generate SQL, execute read-only queries, and remember follow-up questions in the same session.

**Model:** `{MODEL_NAME}`  
**Database:** `{DB_PATH}`  
"""
        )

        if not OPENAI_API_KEY:
            gr.Markdown(
                """
> **Setup needed:** `OPENAI_API_KEY` is not set.  
> Add it in Hugging Face Spaces under **Settings → Variables and Secrets → New Secret**.
"""
            )

        chatbot = gr.Chatbot(
            value=[],
            height=560,
            label="SQL Agent Chat",
            placeholder="Ask a question about the database...",
        )

        with gr.Row():
            user_input = gr.Textbox(
                placeholder="Example: Which customer spent the most money?",
                label="Your question",
                scale=8,
            )

            submit_btn = gr.Button(
                "Ask",
                scale=1,
                variant="primary",
            )

        with gr.Row():
            clear_btn = gr.Button("New Chat / Reset Memory")

        gr.Markdown("### Example questions")

        with gr.Row():
            ex1 = gr.Button("Which customer spent the most money?")
            ex2 = gr.Button("Show total sales by country.")
            ex3 = gr.Button("Which genre has the most tracks?")
            ex4 = gr.Button("What are the top-selling tracks?")

        ex1.click(example_question, inputs=[gr.State("Which customer spent the most money?")], outputs=[user_input])
        ex2.click(example_question, inputs=[gr.State("Show total sales by country.")], outputs=[user_input])
        ex3.click(example_question, inputs=[gr.State("Which genre has the most tracks?")], outputs=[user_input])
        ex4.click(example_question, inputs=[gr.State("What are the top-selling tracks?")], outputs=[user_input])

        submit_btn.click(
            fn=chat_with_sql_agent,
            inputs=[user_input, chatbot, thread_id_state],
            outputs=[chatbot, user_input, thread_id_state],
        )

        user_input.submit(
            fn=chat_with_sql_agent,
            inputs=[user_input, chatbot, thread_id_state],
            outputs=[chatbot, user_input, thread_id_state],
        )

        clear_btn.click(
            fn=reset_chat,
            inputs=[],
            outputs=[chatbot, thread_id_state],
        )


# ------------------------------------------------------------
# 9. Launch for Hugging Face Spaces
# ------------------------------------------------------------

if __name__ == "__main__":
    demo.queue().launch()