Spaces:
Sleeping
Sleeping
Update markdown.py
Browse files- markdown.py +119 -1
markdown.py
CHANGED
|
@@ -1,4 +1,122 @@
|
|
| 1 |
-
description = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
footer = """
|
| 4 |
<div style="background-color: #1d2938; color: white; padding: 10px; width: 100%; bottom: 0; left: 0; display: flex; justify-content: space-between; align-items: center; padding: .2rem 35px; box-sizing: border-box; font-size: 16px;">
|
|
|
|
| 1 |
+
description = """
|
| 2 |
+
**DB Assistant** is an interactive **AI-powered SQL agent** built with **Gradio** and **LangChain**.
|
| 3 |
+
It enables users to connect to any relational database, ask natural language questions, and receive data-driven answers in real time — no manual SQL writing required.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
#### 🚀 Key Features
|
| 8 |
+
|
| 9 |
+
- **Natural Language to SQL Conversion**
|
| 10 |
+
Ask questions like _“Which genre has the longest tracks?”_ or _“List the top 5 customers by purchase amount.”_
|
| 11 |
+
The assistant automatically generates and executes optimized SQL queries against your connected database.
|
| 12 |
+
|
| 13 |
+
- **Live SQL Agent Streaming**
|
| 14 |
+
The model’s reasoning, query generation, and intermediate steps are streamed to the interface, allowing you to see exactly how the LLM builds and refines its queries.
|
| 15 |
+
|
| 16 |
+
- **Collapsible Debug Panels (AI & Tool Messages)**
|
| 17 |
+
Intermediate tool calls and SQL executions are neatly displayed in collapsible **accordions**, making the output transparent yet organized.
|
| 18 |
+
|
| 19 |
+
- **Multi-Provider LLM Support**
|
| 20 |
+
Choose from a variety of providers:
|
| 21 |
+
- 🧠 **Google GenAI** (Gemini 2.x)
|
| 22 |
+
- 💬 **OpenAI** (GPT-4, GPT-4o, GPT-3.5)
|
| 23 |
+
- 🦣 **Anthropic** (Claude-3 series)
|
| 24 |
+
- ☁️ **Azure OpenAI**, **AWS Bedrock**, **xAI**, **DeepSeek**, **Perplexity**, and more.
|
| 25 |
+
The interface automatically updates the available model list based on your provider choice.
|
| 26 |
+
|
| 27 |
+
- **Secure Database Connectivity**
|
| 28 |
+
Connect to SQL databases via a simple connection string — supporting **SQLite**, **PostgreSQL**, **MySQL**, and others through LangChain’s `SQLDatabase` utility.
|
| 29 |
+
|
| 30 |
+
- **Transparent Query Execution**
|
| 31 |
+
The agent inspects tables, analyzes schemas, and constructs precise SQL queries, always respecting safety rules (no DML statements like INSERT, UPDATE, DELETE, DROP).
|
| 32 |
+
|
| 33 |
+
- **Intuitive Gradio Interface**
|
| 34 |
+
Built with `gr.Blocks()` for clean layout:
|
| 35 |
+
- **Left Panel:** LLM setup and database connection
|
| 36 |
+
- **Right Panel:** Interactive chatbot and conversation history
|
| 37 |
+
- **Expandable sections** for metadata and project credits
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
#### ⚙️ Tech Stack Overview
|
| 42 |
+
|
| 43 |
+
| Component | Purpose |
|
| 44 |
+
|------------|----------|
|
| 45 |
+
| **LangChain** | Framework for connecting LLMs with external data tools |
|
| 46 |
+
| **SQLDatabaseToolkit** | Provides database inspection, schema parsing, and safe query execution |
|
| 47 |
+
| **Gradio** | Creates a user-friendly web UI for chatting with the agent |
|
| 48 |
+
| **Python** | Core application logic and integration |
|
| 49 |
+
| **Markdown + HTML** | Enhanced output formatting with collapsible accordions |
|
| 50 |
+
| **Base64 Encoding** | Used to embed custom logo images directly in the interface |
|
| 51 |
+
|
| 52 |
+
---
|
| 53 |
+
|
| 54 |
+
#### 🧩 How It Works
|
| 55 |
+
|
| 56 |
+
1. **Setup LLM**
|
| 57 |
+
Select your preferred provider and model, enter the corresponding API key, and click **Setup LLM**.
|
| 58 |
+
|
| 59 |
+
2. **Connect Database**
|
| 60 |
+
Paste your SQL connection string (e.g., `sqlite:///Chinook.db` or `postgresql+psycopg2://user:pass@host/db`) and click **Connect**.
|
| 61 |
+
The app automatically lists available tables.
|
| 62 |
+
|
| 63 |
+
3. **Ask Questions**
|
| 64 |
+
Type a natural language question in the chat box — the agent:
|
| 65 |
+
- Retrieves the database schema,
|
| 66 |
+
- Generates SQL queries,
|
| 67 |
+
- Executes them safely,
|
| 68 |
+
- Returns a human-readable explanation and result summary.
|
| 69 |
+
|
| 70 |
+
4. **Inspect Reasoning**
|
| 71 |
+
Expand the *AI Message* or *Tool Message* accordions to see exactly what the model did under the hood (SQL statements, schema checks, etc.).
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
#### 🧠 Example Interaction
|
| 76 |
+
|
| 77 |
+
**User:**
|
| 78 |
+
> Which genre on average has the longest tracks?
|
| 79 |
+
|
| 80 |
+
**AI Assistant:**
|
| 81 |
+
- 📊 Generates a SQL query joining `Track` and `Genre` tables
|
| 82 |
+
- 🧩 Computes average `Milliseconds` per genre
|
| 83 |
+
- 🧠 Returns:
|
| 84 |
+
|
| 85 |
+
> On average, the genre with the longest tracks is **"Sci-Fi & Fantasy"**,
|
| 86 |
+
> followed by **"Science Fiction"**, **"Drama"**, and **"TV Shows"**.
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
#### 🧑💻 Ideal Use Cases
|
| 91 |
+
|
| 92 |
+
- Data analysts and engineers exploring new databases
|
| 93 |
+
- BI professionals needing quick insights without SQL writing
|
| 94 |
+
- Educators demonstrating database–AI integration
|
| 95 |
+
- Developers testing and debugging database schemas via conversational interface
|
| 96 |
+
|
| 97 |
+
---
|
| 98 |
+
|
| 99 |
+
#### 🪄 Future Enhancements
|
| 100 |
+
|
| 101 |
+
- ✅ Add visualizations (bar charts, pie charts) for query results
|
| 102 |
+
- ✅ Support multiple database sessions
|
| 103 |
+
- ✅ Enable query caching and history tracking
|
| 104 |
+
- ✅ Integrate authentication for enterprise deployments
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
#### 💡 Credits
|
| 109 |
+
|
| 110 |
+
Developed using:
|
| 111 |
+
- **LangChain Community Modules**
|
| 112 |
+
- **Gradio UI Framework**
|
| 113 |
+
- **Python 3.12+**
|
| 114 |
+
|
| 115 |
+
This project bridges the gap between **conversational AI** and **structured data analytics**, giving everyone a personal **SQL-powered data assistant** at their fingertips. 🤖💾
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
"""
|
| 120 |
|
| 121 |
footer = """
|
| 122 |
<div style="background-color: #1d2938; color: white; padding: 10px; width: 100%; bottom: 0; left: 0; display: flex; justify-content: space-between; align-items: center; padding: .2rem 35px; box-sizing: border-box; font-size: 16px;">
|