mariadb / app.py
barathm111's picture
Upload app.py
3de53f6 verified
raw
history blame
4.63 kB
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline
import mysql.connector
import os
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
app = FastAPI()
# Initialize the text generation pipeline
pipe = pipeline("text-generation", model="defog/llama-3-sqlcoder-8b", pad_token_id=2)
class QueryRequest(BaseModel):
query: str
def get_db_connection():
"""Create a new database connection."""
try:
connection = mysql.connector.connect(
host=os.getenv("DB_HOST"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD"),
database=os.getenv("DB_NAME"),
raise_on_warnings=True
)
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def get_database_schema():
"""Function to retrieve the database schema dynamically."""
schema = {}
try:
conn = get_db_connection()
if conn is None:
raise Exception("Failed to connect to the database.")
cursor = conn.cursor()
# Query to get table names
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
for table in tables:
table_name = table[0]
cursor.execute(f"DESCRIBE {table_name}")
columns = cursor.fetchall()
schema[table_name] = [column[0] for column in columns]
cursor.close()
conn.close()
except mysql.connector.Error as err:
print(f"Error: {err}")
return {}
except Exception as e:
print(f"An error occurred: {e}")
return {}
return schema
@app.get("/")
def home():
return {"message": "SQL Generation Server is running"}
@app.post("/query")
def handle_query(request: QueryRequest):
try:
text = request.query
# Fetch the database schema
schema = get_database_schema()
schema_str = "\n".join([f"{table}: {', '.join(columns)}" for table, columns in schema.items()])
# Construct the system message
system_message = f"""
You are a helpful, cheerful database assistant.
Use the following dynamically retrieved database schema when creating your answers:
{schema_str}
When creating your answers, consider the following:
1. If a query involves a column or value that is not present in the provided database schema, correct it and mention the correction in the summary. If a column or value is missing, provide an explanation of the issue and adjust the query accordingly.
2. If there is a spelling mistake in the column name or value, attempt to correct it by matching the closest possible column or value from the schema. Mention this correction in the summary to clarify any changes made.
3. Ensure that the correct columns and values are used based on the schema provided. Verify the query against the schema to confirm accuracy.
4. Include column name headers in the query results for clarity.
Always provide your answer in the JSON format below:
{{ "summary": "your-summary", "query": "your-query" }}
Output ONLY JSON.
In the preceding JSON response, substitute "your-query" with a MariaDB query to retrieve the requested data.
In the preceding JSON response, substitute "your-summary" with a summary of the query and any corrections or clarifications made.
Always include all columns in the table.
"""
prompt = f"{system_message}\n\nUser request:\n\n{text}\n\nSQL query:"
output = pipe(prompt, max_new_tokens=100)
generated_text = output[0]['generated_text']
sql_query = generated_text.split("SQL query:")[-1].strip()
# Basic validation
if not sql_query.lower().startswith(('select', 'show', 'describe')):
raise ValueError("Generated text is not a valid SQL query")
# Example: execute the generated SQL query and return the results
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(sql_query)
results = cursor.fetchall()
cursor.close()
conn.close()
return {"sql": sql_query, "results": results}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)