Al1Abdullah commited on
Commit
0ccb622
·
verified ·
1 Parent(s): 33555e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +207 -205
app.py CHANGED
@@ -1,112 +1,114 @@
1
  from flask import Flask, request, render_template
2
- import os
3
- import json
4
- import re
5
- import logging
6
- import tkinter as tk
7
- from tkinter import simpledialog, messagebox
8
- import importlib.util
9
- from groq import Groq
10
-
11
- app = Flask(__name__)
12
-
13
- # Configure logging
14
- logging.basicConfig(filename='AI_SQL_Assistant.log', level=logging.INFO)
15
- logger = logging.getLogger(__name__)
16
-
17
- # Prompt for Groq API key using Tkinter
18
- def get_groq_api_key():
19
- groq_api_key = os.getenv('GROQ_API_KEY')
20
- if not groq_api_key:
21
- root = tk.Tk()
22
- root.withdraw()
23
- groq_api_key = simpledialog.askstring("Groq API Key", "Enter your Groq API key (get from https://console.groq.com):", show='*')
24
- if groq_api_key:
25
- os.environ['GROQ_API_KEY'] = groq_api_key
26
- with open('groq_config.json', 'w') as f:
27
- json.dump({'GROQ_API_KEY': groq_api_key}, f)
28
- else:
29
- messagebox.showerror("Error", "Groq API key is required to run the app.")
30
- logger.error("No Groq API key provided")
31
- exit(1)
32
- root.destroy()
33
- return groq_api_key
34
-
35
- # Load Groq API key
36
- if os.path.exists('groq_config.json'):
37
- try:
38
- with open('groq_config.json', 'r') as f:
39
- config = json.load(f)
40
- os.environ['GROQ_API_KEY'] = config.get('GROQ_API_KEY', '')
41
- except Exception as e:
42
- logger.error("Failed to load groq_config.json: %s", str(e))
43
-
44
- try:
45
- groq_client = Groq(api_key=get_groq_api_key())
46
- logger.info("Groq client initialized successfully")
47
- except Exception as e:
48
- groq_client = None
49
- logger.error("Failed to initialize Groq client: %s", str(e))
50
- messagebox.showerror("Error", f"Failed to initialize Groq client: {str(e)}")
51
- exit(1)
52
-
53
- # Storage for current schema, summary, and mock data
54
- current_schema = {}
55
- current_summary = {}
56
- current_db_name = None
57
- results = None
58
- generated_query = None
59
- mock_data = {}
60
-
61
- def parse_sql_file(file_content):
62
- """Parse SQL file to extract schema and mock data."""
63
- global current_db_name, current_schema, mock_data
64
- file_content = file_content.decode('utf-8') if isinstance(file_content, bytes) else file_content
65
- statements = []
66
- current_statement = ""
67
- in_comment = False
68
-
69
- db_name_match = re.search(r"CREATE\s+DATABASE\s+[`']?(\w+)[`']?", file_content, re.IGNORECASE)
70
- current_db_name = db_name_match.group(1) if db_name_match else f"temp_db_{uuid.uuid4().hex[:8]}"
71
- logger.info("Parsed SQL file: database name=%s", current_db_name)
72
-
73
- for line in file_content.splitlines():
74
- line = line.strip()
75
- if not line or line.startswith('--'):
76
- continue
77
- if line.startswith('/*'):
78
- in_comment = True
79
- continue
80
- if line.endswith('*/'):
81
- in_comment = False
82
- continue
83
- if not in_comment:
84
- current_statement += line + ' '
85
- if line.endswith(';'):
86
- statements.append(current_statement.strip())
87
- current_statement = ""
88
-
89
- current_schema = {}
90
- mock_data = {}
91
- for statement in statements:
92
- if statement.startswith("CREATE TABLE"):
93
- table_match = re.search(r"CREATE TABLE\s+[`']?(\w+)[`']?\s*\(", statement, re.IGNORECASE)
94
- if table_match:
95
- table_name = table_match.group(1)
96
- columns = re.findall(r"\b(\w+)\s+\w+(?:\(.*?\))?(?:,\s|\))", statement)
97
- current_schema[table_name] = columns
98
- # Mock some data (e.g., 3 rows with sample values)
99
- mock_data[table_name] = [[f"val{i}_{col}" for col in columns] for i in range(3)]
100
- logger.info("Extracted schema: %s", current_schema)
101
- return True, current_schema, None
102
-
103
- def generate_sql_query(question, schema):
104
- """Generate SQL query using Groq API based on schema."""
105
- if not groq_client:
106
- logger.error("Groq client not initialized")
107
- return "ERROR: Groq client not initialized. Check API key and try again."
108
- schema_text = "\n".join([f"Table: {table}\nColumns: {', '.join(columns)}" for table, columns in schema.items()])
109
- prompt = f"""
 
 
110
  You are a SQL expert. Based on the following database schema from an uploaded .sql file, generate a valid MySQL query for the user's question. Only use tables and columns that exist in the schema. Use user-friendly aliases for column names (e.g., 'cust_id' becomes 'Customer ID', 'admission_date' becomes 'Admission Date'). Return ONLY the SQL query, without explanations, markdown, or code block formatting (e.g., no ```). If the question references non-existent tables or columns, return an error message starting with 'ERROR:'. Do not use GROUP BY or aggregation functions (e.g., SUM, COUNT, AVG) unless the question explicitly requests aggregation (e.g., 'sum of all bills', 'average cost', 'count of patients'). Treat 'total bill amount' as the individual bill amount (e.g., bill.amount) unless aggregation is clearly specified. For names, concatenate first_name and last_name if applicable (e.g., CONCAT(first_name, ' ', last_name) AS 'Full Name'). Use direct JOINs with correct foreign key relationships if implied (e.g., table_id columns). Avoid subqueries unless absolutely necessary. Place filtering conditions (e.g., department name, status) in the WHERE clause, not JOIN clauses. Handle case sensitivity in string comparisons by using LOWER() for status fields (e.g., LOWER(status) = 'unpaid'). Verify table relationships before joining.
111
 
112
  Schema:
@@ -114,100 +116,100 @@ Schema:
114
 
115
  User Question: {question}
116
  """
117
- try:
118
- response = groq_client.chat.completions.create(
119
- messages=[{"role": "user", "content": prompt}],
120
- model="llama3-70b-8192"
121
- )
122
- query = response.choices[0].message.content.strip()
123
- query = re.sub(r'```(?:sql)?\n?', '', query)
124
- query = query.strip()
125
- logger.info("Generated SQL query: %s", query[:100])
126
- return query
127
- except Exception as e:
128
- logger.error("Failed to generate SQL query: %s", str(e))
129
- return f"ERROR: Failed to generate SQL query: {str(e)}"
130
-
131
- def execute_mock_query(query):
132
- """Simulate query execution with mock data."""
133
- if not current_schema or not mock_data:
134
- logger.error("No schema or mock data available")
135
- return False, "No data loaded. Please upload an .sql file first.", None
136
- try:
137
- # Simple mock execution: return data from the first table if query matches
138
- table_match = re.search(r"FROM\s+[`']?(\w+)[`']?", query, re.IGNORECASE)
139
- if table_match and table_match.group(1) in mock_data:
140
- return True, mock_data[table_match.group(1)], None
141
- return False, "Mock query execution failed: Table not found or query not supported.", None
142
- except Exception as e:
143
- logger.error("Mock query execution failed: %s", str(e))
144
- return False, f"Mock query execution failed: {str(e)}", None
145
-
146
- @app.route('/', methods=['GET', 'POST'])
147
- def index():
148
- global current_schema, current_summary, results, generated_query
149
- error = None
150
-
151
- if not groq_client:
152
- error = "Groq client not initialized. Please restart the app and enter a valid Groq API key."
153
- logger.error(error)
154
-
155
- if request.method == 'POST':
156
- logger.info("Received POST request")
157
- if 'sql_file' in request.files:
158
- file = request.files['sql_file']
159
- logger.info("SQL file upload detected: %s", file.filename if file else "No file")
160
- if file and file.filename.endswith('.sql'):
161
- success, schema, _ = parse_sql_file(file.read())
162
- if success:
163
- current_schema = schema
164
- logger.info("SQL file parsed successfully")
165
- else:
166
- error = "Failed to parse SQL file."
167
- logger.error(error)
168
- else:
169
- error = "Please upload a valid .sql file."
170
- logger.error(error)
171
- elif 'question' in request.form:
172
- question = request.form['question']
173
- logger.info("Received question: %s", question)
174
- if not current_schema:
175
- error = "No schema loaded. Please upload an .sql file first."
176
- logger.error(error)
177
- else:
178
- generated_query = generate_sql_query(question, current_schema)
179
- if not generated_query.startswith('ERROR:'):
180
- success, result, _ = execute_mock_query(generated_query)
181
- if success:
182
- results = result
183
- logger.info("Mock query executed successfully, results: %d rows", len(result))
184
- else:
185
- error = result
186
- logger.error(error)
187
- else:
188
- error = generated_query
189
- logger.error(error)
190
-
191
- logger.info("Rendering index.html: error=%s, schema=%s, summary=%s, results=%s",
192
- error, bool(current_schema), bool(current_summary), bool(results))
193
- return render_template('index.html', error=error, schema=current_schema, summary=current_summary, results=results, query=generated_query)
194
-
195
- if __name__ == '__main__':
196
- try:
197
- spec = importlib.util.find_spec("webbrowser")
198
- if spec is None:
199
- logger.error("Standard library webbrowser module not found")
200
- raise ImportError("Could not find webbrowser module")
201
- webbrowser = importlib.util.module_from_spec(spec)
202
- spec.loader.exec_module(webbrowser)
203
- except ImportError as e:
204
- logger.error("Failed to import webbrowser: %s", str(e))
205
- raise
206
-
207
- url = 'http://localhost:7860'
208
- try:
209
- webbrowser.open(url)
210
- except AttributeError:
211
- logger.error("webbrowser.open() failed, possibly due to environment issue")
212
- print(f"Warning: Could not open browser automatically. Please navigate to {url} manually.")
213
- app.run(host='0.0.0.0', port=7860, debug=False)
 
1
  from flask import Flask, request, render_template
2
+ import os
3
+ import json
4
+ import re
5
+ import logging
6
+ import importlib.util
7
+ import tkinter as tk
8
+ from tkinter import simpledialog, messagebox
9
+ import uuid
10
+ import webbrowser
11
+ from groq import Groq
12
+
13
+ app = Flask(__name__)
14
+
15
+ # Configure logging
16
+ logging.basicConfig(filename='AI_SQL_Assistant.log', level=logging.INFO)
17
+ logger = logging.getLogger(__name__)
18
+
19
+ # Prompt for Groq API key using Tkinter
20
+ def get_groq_api_key():
21
+ groq_api_key = os.getenv('GROQ_API_KEY')
22
+ if not groq_api_key:
23
+ root = tk.Tk()
24
+ root.withdraw()
25
+ groq_api_key = simpledialog.askstring("Groq API Key", "Enter your Groq API key (get from https://console.groq.com):", show='*')
26
+ root.destroy()
27
+ if groq_api_key:
28
+ os.environ['GROQ_API_KEY'] = groq_api_key
29
+ with open('groq_config.json', 'w') as f:
30
+ json.dump({'GROQ_API_KEY': groq_api_key}, f)
31
+ else:
32
+ messagebox.showerror("Error", "Groq API key is required to run the app.")
33
+ logger.error("No Groq API key provided")
34
+ exit(1)
35
+ return groq_api_key
36
+
37
+ # Load Groq API key
38
+ if os.path.exists('groq_config.json'):
39
+ try:
40
+ with open('groq_config.json', 'r') as f:
41
+ config = json.load(f)
42
+ os.environ['GROQ_API_KEY'] = config.get('GROQ_API_KEY', '')
43
+ except Exception as e:
44
+ logger.error("Failed to load groq_config.json: %s", str(e))
45
+
46
+ try:
47
+ groq_client = Groq(api_key=get_groq_api_key())
48
+ logger.info("Groq client initialized successfully")
49
+ except Exception as e:
50
+ groq_client = None
51
+ logger.error("Failed to initialize Groq client: %s", str(e))
52
+ messagebox.showerror("Error", f"Failed to initialize Groq client: {str(e)}")
53
+ exit(1)
54
+
55
+ # Storage for current schema, summary, and mock data
56
+ current_schema = {}
57
+ current_summary = {}
58
+ current_db_name = None
59
+ results = None
60
+ generated_query = None
61
+ mock_data = {}
62
+
63
+ def parse_sql_file(file_content):
64
+ """Parse SQL file to extract schema and mock data."""
65
+ global current_db_name, current_schema, mock_data
66
+ file_content = file_content.decode('utf-8') if isinstance(file_content, bytes) else file_content
67
+ statements = []
68
+ current_statement = ""
69
+ in_comment = False
70
+
71
+ db_name_match = re.search(r"CREATE\s+DATABASE\s+[`']?(\w+)[`']?", file_content, re.IGNORECASE)
72
+ current_db_name = db_name_match.group(1) if db_name_match else f"temp_db_{uuid.uuid4().hex[:8]}"
73
+ logger.info("Parsed SQL file: database name=%s", current_db_name)
74
+
75
+ for line in file_content.splitlines():
76
+ line = line.strip()
77
+ if not line or line.startswith('--'):
78
+ continue
79
+ if line.startswith('/*'):
80
+ in_comment = True
81
+ continue
82
+ if line.endswith('*/'):
83
+ in_comment = False
84
+ continue
85
+ if not in_comment:
86
+ current_statement += line + ' '
87
+ if line.endswith(';'):
88
+ statements.append(current_statement.strip())
89
+ current_statement = ""
90
+
91
+ current_schema = {}
92
+ mock_data = {}
93
+ for statement in statements:
94
+ if statement.startswith("CREATE TABLE"):
95
+ table_match = re.search(r"CREATE TABLE\s+[`']?(\w+)[`']?\s*\(", statement, re.IGNORECASE)
96
+ if table_match:
97
+ table_name = table_match.group(1)
98
+ columns = re.findall(r"\b(\w+)\s+\w+(?:\(.*?\))?(?:,\s|\))", statement)
99
+ current_schema[table_name] = columns
100
+ # Mock some data (e.g., 3 rows with sample values)
101
+ mock_data[table_name] = [[f"val{i}_{col}" for col in columns] for i in range(3)]
102
+ logger.info("Extracted schema: %s", current_schema)
103
+ return True, current_schema, None
104
+
105
+ def generate_sql_query(question, schema):
106
+ """Generate SQL query using Groq API based on schema."""
107
+ if not groq_client:
108
+ logger.error("Groq client not initialized")
109
+ return "ERROR: Groq client not initialized. Check API key and try again."
110
+ schema_text = "\n".join([f"Table: {table}\nColumns: {', '.join(columns)}" for table, columns in schema.items()])
111
+ prompt = f"""
112
  You are a SQL expert. Based on the following database schema from an uploaded .sql file, generate a valid MySQL query for the user's question. Only use tables and columns that exist in the schema. Use user-friendly aliases for column names (e.g., 'cust_id' becomes 'Customer ID', 'admission_date' becomes 'Admission Date'). Return ONLY the SQL query, without explanations, markdown, or code block formatting (e.g., no ```). If the question references non-existent tables or columns, return an error message starting with 'ERROR:'. Do not use GROUP BY or aggregation functions (e.g., SUM, COUNT, AVG) unless the question explicitly requests aggregation (e.g., 'sum of all bills', 'average cost', 'count of patients'). Treat 'total bill amount' as the individual bill amount (e.g., bill.amount) unless aggregation is clearly specified. For names, concatenate first_name and last_name if applicable (e.g., CONCAT(first_name, ' ', last_name) AS 'Full Name'). Use direct JOINs with correct foreign key relationships if implied (e.g., table_id columns). Avoid subqueries unless absolutely necessary. Place filtering conditions (e.g., department name, status) in the WHERE clause, not JOIN clauses. Handle case sensitivity in string comparisons by using LOWER() for status fields (e.g., LOWER(status) = 'unpaid'). Verify table relationships before joining.
113
 
114
  Schema:
 
116
 
117
  User Question: {question}
118
  """
119
+ try:
120
+ response = groq_client.chat.completions.create(
121
+ messages=[{"role": "user", "content": prompt}],
122
+ model="llama3-70b-8192"
123
+ )
124
+ query = response.choices[0].message.content.strip()
125
+ query = re.sub(r'```(?:sql)?\n?', '', query)
126
+ query = query.strip()
127
+ logger.info("Generated SQL query: %s", query[:100])
128
+ return query
129
+ except Exception as e:
130
+ logger.error("Failed to generate SQL query: %s", str(e))
131
+ return f"ERROR: Failed to generate SQL query: {str(e)}"
132
+
133
+ def execute_mock_query(query):
134
+ """Simulate query execution with mock data."""
135
+ if not current_schema or not mock_data:
136
+ logger.error("No schema or mock data available")
137
+ return False, "No data loaded. Please upload an .sql file first.", None
138
+ try:
139
+ # Simple mock execution: return data from the first table if query matches
140
+ table_match = re.search(r"FROM\s+[`']?(\w+)[`']?", query, re.IGNORECASE)
141
+ if table_match and table_match.group(1) in mock_data:
142
+ return True, mock_data[table_match.group(1)], None
143
+ return False, "Mock query execution failed: Table not found or query not supported.", None
144
+ except Exception as e:
145
+ logger.error("Mock query execution failed: %s", str(e))
146
+ return False, f"Mock query execution failed: {str(e)}", None
147
+
148
+ @app.route('/', methods=['GET', 'POST'])
149
+ def index():
150
+ global current_schema, current_summary, results, generated_query
151
+ error = None
152
+
153
+ if not groq_client:
154
+ error = "Groq client not initialized. Please restart the app and enter a valid Groq API key."
155
+ logger.error(error)
156
+
157
+ if request.method == 'POST':
158
+ logger.info("Received POST request")
159
+ if 'sql_file' in request.files:
160
+ file = request.files['sql_file']
161
+ logger.info("SQL file upload detected: %s", file.filename if file else "No file")
162
+ if file and file.filename.endswith('.sql'):
163
+ success, schema, _ = parse_sql_file(file.read())
164
+ if success:
165
+ current_schema = schema
166
+ logger.info("SQL file parsed successfully")
167
+ else:
168
+ error = "Failed to parse SQL file."
169
+ logger.error(error)
170
+ else:
171
+ error = "Please upload a valid .sql file."
172
+ logger.error(error)
173
+ elif 'question' in request.form:
174
+ question = request.form['question']
175
+ logger.info("Received question: %s", question)
176
+ if not current_schema:
177
+ error = "No schema loaded. Please upload an .sql file first."
178
+ logger.error(error)
179
+ else:
180
+ generated_query = generate_sql_query(question, current_schema)
181
+ if not generated_query.startswith('ERROR:'):
182
+ success, result, _ = execute_mock_query(generated_query)
183
+ if success:
184
+ results = result
185
+ logger.info("Mock query executed successfully, results: %d rows", len(result))
186
+ else:
187
+ error = result
188
+ logger.error(error)
189
+ else:
190
+ error = generated_query
191
+ logger.error(error)
192
+
193
+ logger.info("Rendering index.html: error=%s, schema=%s, summary=%s, results=%s",
194
+ error, bool(current_schema), bool(current_summary), bool(results))
195
+ return render_template('index.html', error=error, schema=current_schema, summary=current_summary, results=results, query=generated_query)
196
+
197
+ if __name__ == '__main__':
198
+ try:
199
+ spec = importlib.util.find_spec("webbrowser")
200
+ if spec is None:
201
+ logger.error("Standard library webbrowser module not found")
202
+ raise ImportError("Could not find webbrowser module")
203
+ webbrowser = importlib.util.module_from_spec(spec)
204
+ spec.loader.exec_module(webbrowser)
205
+ except ImportError as e:
206
+ logger.error("Failed to import webbrowser: %s", str(e))
207
+ raise
208
+
209
+ url = 'http://localhost:7860'
210
+ try:
211
+ webbrowser.open(url)
212
+ except AttributeError:
213
+ logger.error("webbrowser.open() failed, possibly due to environment issue")
214
+ print(f"Warning: Could not open browser automatically. Please navigate to {url} manually.")
215
+ app.run(host='0.0.0.0', port=7860, debug=False)