File size: 4,657 Bytes
47fc572
ad6f695
47fc572
 
 
 
 
ad6f695
47fc572
 
ad6f695
 
 
47fc572
09e2bc4
86fa3b8
938a3f9
86fa3b8
09e2bc4
86fa3b8
938a3f9
86fa3b8
938a3f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86fa3b8
09e2bc4
86fa3b8
09e2bc4
86fa3b8
938a3f9
86fa3b8
09e2bc4
938a3f9
09e2bc4
938a3f9
09e2bc4
86fa3b8
938a3f9
86fa3b8
 
09e2bc4
86fa3b8
938a3f9
 
 
 
09e2bc4
938a3f9
86fa3b8
938a3f9
86fa3b8
938a3f9
 
 
 
 
09e2bc4
 
 
 
86fa3b8
938a3f9
 
 
86fa3b8
938a3f9
86fa3b8
938a3f9
 
 
 
 
86fa3b8
938a3f9
 
 
 
 
 
 
 
 
 
 
09e2bc4
86fa3b8
938a3f9
86fa3b8
938a3f9
 
 
 
 
86fa3b8
938a3f9
6c0b5ec
938a3f9
86fa3b8
938a3f9
86fa3b8
938a3f9
 
 
 
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
---
title: PostgreSQL Query Agent with Visualization
emoji: 🀯
colorFrom: pink
colorTo: yellow
sdk: docker
pinned: false
short_description: PostgreSQL Query Agent with Visualization
---

[HF URL](https://huggingface.co/spaces/intellica/talk2data)
[Gradio APP URL](https://intellica-talk2data.hf.space/)


# Natural Language SQL Query Agent with Visualization

A smart and interactive PostgreSQL query system that translates natural language requests into SQL queries, executes them, and generates visualizations using PandasAI. Built with modern technologies including LangChain, FastMCP, and Gradio.

![Architecture](resources/visualization_demo.png)

## 🌟 Features

- **Natural Language to SQL**: Convert plain English questions into SQL queries
- **Interactive Chat Interface**: User-friendly Gradio web interface
- **Smart Visualization**: Automated chart generation based on query results
- **Conversation Memory**: Maintains context across multiple queries
- **Database Schema Understanding**: Intelligent handling of database structure
- **Multiple LLM Support**: Compatible with both OpenAI and Google's Gemini models

## πŸ—οΈ Architecture

The project is structured into several key components:

### 1. Query Processing Layer
- **LangChain Client** (`langchain_mcp_client.py`):
  - Manages LLM interactions for query understanding
  - Handles conversation flow and context
  - Integrates with MCP tools
  - Supports multiple LLM providers (OpenAI/Gemini)

### 2. Database Layer
- **PostgreSQL MCP Server** (`postgre_mcp_server.py`):
  - Manages PostgreSQL connections and query execution
  - Implements connection pooling for efficiency
  - Provides database schema information
  - Handles query result processing

### 3. Visualization Layer
- **PandasAI Integration** (`pandasai_visualization.py`):
  - Intelligent chart generation from query results
  - Support for multiple chart types
  - Automated visualization selection
  - Exports charts to `exports/charts/` directory

### 4. User Interface
- **Gradio Web Interface** (`gradio_app.py`):
  - Clean and intuitive chat interface
  - Real-time query processing
  - Visualization display
  - Interactive session management

### 5. Memory Management
- **Conversation Store** (`memory_store.py`):
  - Maintains conversation history
  - Implements singleton pattern for global state
  - Enables contextual query understanding

## πŸš€ Getting Started

### Prerequisites

- Python 3.11 or lower
- PostgreSQL database 
- Access to either OpenAI API or Google Gemini API

### Installation

1. **Clone the Repository**
   ```bash
   git clone <repository-url>
   cd query_mcp_server
   ```

2. **Set Up Virtual Environment**
   ```bash
   python -m venv venv
   source venv/bin/activate  # Linux/Mac
   # or
   .\venv\Scripts\activate   # Windows
   ```

3. **Install Dependencies**
   ```bash
   pip install -r requirements.txt
   ```

4. **Environment Configuration**
   Create a `.env` file using the .env.example template:
   ```bash
   cp .env.example .env
   ```
   Fill in the required environment variables.

## πŸƒβ€β™‚οΈ Running the Application

1. **Start the Application**
   ```bash
   python gradio_app.py
   ```
   or using run.sh
   ```bash
   chmod +x run.sh
   ./run.sh
   ```

2. **Access the Interface**
   - Open your browser and navigate to `http://localhost:7860`
   - Start querying your database using natural language!

## πŸ§ͺ Testing

To test the visualization component independently:
```bash
python pandasai_visualization.py
```
This will generate sample visualizations to verify the PandasAI setup.

## πŸ“ Project Structure
```
query_mcp_server/
β”œβ”€β”€ gradio_app.py           # Web interface
β”œβ”€β”€ langchain_mcp_client.py # LLM integration
β”œβ”€β”€ postgre_mcp_server.py   # Database handler
β”œβ”€β”€ pandasai_visualization.py # Visualization logic
β”œβ”€β”€ memory_store.py         # Conversation management
β”œβ”€β”€ exports/               
β”‚   └── charts/            # Generated visualizations
└── resources/             # Static resources
```

## πŸ› οΈ Contributing

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

## ✨ Acknowledgments

- LangChain for the powerful LLM framework
- PandasAI for intelligent visualization capabilities
- Gradio for the intuitive web interface
- FastMCP for efficient database communication