A Streamlit app that converts English questions into SQL queries using AI models. Simply ask questions about your data and get instant results!
- Clone and navigate to the project:
git clone [email protected]:mdarshad1000/Text-2-SQL.git
cd [email protected]:mdarshad1000/Text-2-SQL.git
- Create
.env
file with your API keys:
OPENAI_API_KEY=your_key_here
GEMINI_API_KEY=your_key_here
- Start the app:
docker compose up -d
Visit http://0.0.0.0:8501
in your browser.
- Select an AI model (GPT-4, Llama3.2:latest, or Gemini) {{ If you want to use the Llama3.2 model, ensure the Ollama server is running. }}
- Type your question (e.g., "Show me all sales from last month")
- Click "Generate Results"
- View your data in tables or charts
- Download results as CSV if needed
`Chinook_PostgreSql.sql`: SQL script to create database schema
`Dockerfile`: Docker file to build Docker image
`README.md`: This README file
`app.py`: Main application file
`config.py`: Configuration file for LLM models
`docker-compose.yml`: Docker Compose file to run Docker image
`llm_integration.py`: Abstract base class for LLM integrations
`query_executor.py`: Database executor class
`requirements.txt`: Python dependencies
Base class for all AI model integrations.
generate_sql_from_nl(db_schema, nl_query)
: Abstract method to convert natural language to SQL
Handles GPT-4 integration.
__init__(model, sys_prompt, user_prompt)
: Initializes with model settingsgenerate_sql_from_nl(db_schema, nl_query)
: Generates SQL using OpenAI API
Manages Llama3.2:latest integration.
__init__(model, sys_prompt, user_prompt)
: Sets up Ollama configurationgenerate_sql_from_nl(db_schema, nl_query)
: Generates SQL using Ollama
Handles Google's Gemini integration.
__init__(model, sys_prompt, user_prompt)
: Configures Gemini settingsgenerate_sql_from_nl(db_schema, nl_query)
: Generates SQL using Gemini API
Manages database operations.
connect()
: Establishes database connectiondisconnect()
: Closes database connectionexecute_query(query)
: Runs SQL queries and returns results as DataFrameget_schema_info()
: Returns database structure information
-
Initialization
- App starts and creates
DatabaseExecutor
instance - Initializes AI models (GPT-4, Llama3.2, Gemini)
- App starts and creates
-
User Interaction
- User selects AI model
- Views database schema
- Enters natural language question
-
Query Processing
User Question β AI Model β SQL Query β Database β Results β Visualization
-
Data Display
- Results shown in table format
- Optional visualization in charts
- CSV export available
If something's not working:
- Check container status:
docker compose ps
- View logs:
docker compose logs app
- Restart if needed:
docker compose restart
- Uses Chinook sample database (digital media store data)
- Supports basic data visualization
- Downloads available in CSV format