Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlparse
|
| 2 |
+
import typer
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from groq import Groq
|
| 5 |
+
import json
|
| 6 |
+
app = typer.Typer()
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import os
|
| 9 |
+
load_dotenv()
|
| 10 |
+
import streamlit as st
|
| 11 |
+
import gradio as gr
|
| 12 |
+
# client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 13 |
+
# print("API Key Loaded:", os.getenv("GROQ_API_KEY"))
|
| 14 |
+
# st.title("π SQL Copilot ")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 18 |
+
|
| 19 |
+
from datetime import datetime
|
| 20 |
+
|
| 21 |
+
# ---------------------------
|
| 22 |
+
# Tools (Agents)
|
| 23 |
+
# ---------------------------
|
| 24 |
+
def format_agent(query: str = typer.Option(..., "--query", "-q", help="The SQL query to format")):
|
| 25 |
+
"""Formatter agent π"""
|
| 26 |
+
return sqlparse.format(query, reindent=True, keyword_case="upper")
|
| 27 |
+
|
| 28 |
+
def explainer_agent(query: str = typer.Option(..., "--query", "-q", help="The SQL query to format")):
|
| 29 |
+
"""AI-powered SQL explanation"""
|
| 30 |
+
prompt = f"""
|
| 31 |
+
You are an expert SQL analyst.
|
| 32 |
+
Analyze this SQL query and return:
|
| 33 |
+
1. Business logic in plain English point wise in client or business person who is not technical understandable format.
|
| 34 |
+
2. Tables used
|
| 35 |
+
3. Joins used
|
| 36 |
+
4. Filters/conditions
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
SQL Query:
|
| 41 |
+
{query}
|
| 42 |
+
"""
|
| 43 |
+
completion = client.chat.completions.create(
|
| 44 |
+
model="llama-3.3-70b-versatile",
|
| 45 |
+
messages=[{"role": "user", "content": prompt}],
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return (completion.choices[0].message.content)
|
| 49 |
+
|
| 50 |
+
AGENTS = {
|
| 51 |
+
"Formatter π": format_agent,
|
| 52 |
+
"Explainer π€": explainer_agent
|
| 53 |
+
|
| 54 |
+
}
|
| 55 |
+
AGENT_COLORS = {
|
| 56 |
+
"Formatter π": "#ADD8E6", # light blue
|
| 57 |
+
"Explainer π€": "#90EE90" # light green
|
| 58 |
+
|
| 59 |
+
}
|
| 60 |
+
# ---------------------------
|
| 61 |
+
# Process Queries
|
| 62 |
+
# ---------------------------
|
| 63 |
+
def process_queries(query_text, uploaded_file):
|
| 64 |
+
queries = []
|
| 65 |
+
|
| 66 |
+
if uploaded_file is not None:
|
| 67 |
+
content = uploaded_file.read().decode("utf-8")
|
| 68 |
+
queries = [q.strip() for q in sqlparse.split(content) if q.strip()]
|
| 69 |
+
elif query_text and query_text.strip():
|
| 70 |
+
queries = [query_text.strip()]
|
| 71 |
+
|
| 72 |
+
if not queries:
|
| 73 |
+
return [], None, None
|
| 74 |
+
|
| 75 |
+
results = []
|
| 76 |
+
formatted_list = []
|
| 77 |
+
rules_list = []
|
| 78 |
+
|
| 79 |
+
for q in queries:
|
| 80 |
+
formatted_q = format_agent(q)
|
| 81 |
+
rules_q = explainer_agent(q)
|
| 82 |
+
|
| 83 |
+
results.append(["Formatter π", formatted_q])
|
| 84 |
+
results.append(["Explainer π€", rules_q])
|
| 85 |
+
|
| 86 |
+
formatted_list.append(formatted_q)
|
| 87 |
+
rules_list.append(f"Query:\n{q}\n\nBusiness Rules:\n{rules_q}\n{'-'*50}\n")
|
| 88 |
+
|
| 89 |
+
# Save files for download
|
| 90 |
+
formatted_file = "formatted_queries.sql"
|
| 91 |
+
rules_file = "business_rules.txt"
|
| 92 |
+
|
| 93 |
+
with open(formatted_file, "w", encoding="utf-8") as f:
|
| 94 |
+
f.write("\n\n".join(formatted_list))
|
| 95 |
+
|
| 96 |
+
with open(rules_file, "w", encoding="utf-8") as f:
|
| 97 |
+
f.write("\n".join(rules_list))
|
| 98 |
+
|
| 99 |
+
return results, formatted_file, rules_file
|
| 100 |
+
|
| 101 |
+
# ---------------------------
|
| 102 |
+
# Gradio UI
|
| 103 |
+
# ---------------------------
|
| 104 |
+
with gr.Blocks(title="π SQL Copilot Agent") as demo:
|
| 105 |
+
gr.Markdown("## π SQL Copilot Agent")
|
| 106 |
+
|
| 107 |
+
with gr.Tab("Single Query / File"):
|
| 108 |
+
query_box = gr.Textbox(label="Paste your SQL query here", lines=6)
|
| 109 |
+
file_box = gr.File(label="Or upload SQL file", file_types=[".sql"])
|
| 110 |
+
process_btn = gr.Button("Process")
|
| 111 |
+
|
| 112 |
+
output_display = gr.Dataframe(headers=["Agent", "Output"], interactive=False)
|
| 113 |
+
download_formatted = gr.File(label="Download Formatted SQL")
|
| 114 |
+
download_rules = gr.File(label="Download Business Rules")
|
| 115 |
+
|
| 116 |
+
process_btn.click(
|
| 117 |
+
fn=process_queries,
|
| 118 |
+
inputs=[query_box, file_box],
|
| 119 |
+
outputs=[output_display, download_formatted, download_rules],
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
demo.launch()
|
| 123 |
+
|