Ashhar
commited on
Commit
·
af1fee3
1
Parent(s):
557b97f
debug logs shown only in localhost
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
|
|
2 |
import os
|
3 |
import pandas as pd
|
4 |
from typing import Literal, TypedDict
|
5 |
-
from sqlalchemy import create_engine, inspect
|
6 |
import json
|
7 |
from transformers import AutoTokenizer
|
8 |
from utils import pprint
|
@@ -155,7 +155,58 @@ def get_table_schema(table_name):
|
|
155 |
|
156 |
inspector = inspect(st.session_state.engine)
|
157 |
columns = inspector.get_columns(table_name)
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
|
161 |
def get_sample_data(table_name):
|
@@ -223,10 +274,12 @@ User Query: {user_query}
|
|
223 |
pprint(f"\n[{MODEL}] Prompt tokens for SQL generation: {prompt_tokens}")
|
224 |
|
225 |
# Debug prompt in a Streamlit expander for better organization
|
226 |
-
|
227 |
-
|
228 |
-
st.
|
229 |
-
|
|
|
|
|
230 |
|
231 |
start_time = time.time()
|
232 |
with st.spinner(f"Generating SQL query using {MODEL}..."):
|
|
|
2 |
import os
|
3 |
import pandas as pd
|
4 |
from typing import Literal, TypedDict
|
5 |
+
from sqlalchemy import create_engine, inspect, text
|
6 |
import json
|
7 |
from transformers import AutoTokenizer
|
8 |
from utils import pprint
|
|
|
155 |
|
156 |
inspector = inspect(st.session_state.engine)
|
157 |
columns = inspector.get_columns(table_name)
|
158 |
+
schema = {col['name']: str(col['type']) for col in columns}
|
159 |
+
|
160 |
+
# Get table comment
|
161 |
+
table_comment_query = """
|
162 |
+
SELECT obj_description(c.oid) as table_comment
|
163 |
+
FROM pg_class c
|
164 |
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
165 |
+
WHERE c.relname = :table_name
|
166 |
+
AND n.nspname = 'public'
|
167 |
+
"""
|
168 |
+
|
169 |
+
# Get column comments
|
170 |
+
column_comments_query = """
|
171 |
+
SELECT
|
172 |
+
cols.column_name,
|
173 |
+
(
|
174 |
+
SELECT pg_catalog.col_description(c.oid, cols.ordinal_position::int)
|
175 |
+
FROM pg_catalog.pg_class c
|
176 |
+
WHERE c.oid = (SELECT ('"' || cols.table_name || '"')::regclass::oid)
|
177 |
+
AND c.relname = cols.table_name
|
178 |
+
) as column_comment
|
179 |
+
FROM information_schema.columns cols
|
180 |
+
WHERE cols.table_name = :table_name
|
181 |
+
AND cols.table_schema = 'public'
|
182 |
+
"""
|
183 |
+
|
184 |
+
try:
|
185 |
+
with st.session_state.engine.connect() as conn:
|
186 |
+
# Get table comment
|
187 |
+
table_comment_result = conn.execute(text(table_comment_query), {"table_name": table_name}).fetchone()
|
188 |
+
table_comment = table_comment_result[0] if table_comment_result else None
|
189 |
+
|
190 |
+
# Get column comments
|
191 |
+
column_comments_result = conn.execute(text(column_comments_query), {"table_name": table_name}).fetchall()
|
192 |
+
column_comments = {row[0]: row[1] for row in column_comments_result}
|
193 |
+
|
194 |
+
# Create enhanced schema dictionary
|
195 |
+
enhanced_schema = {
|
196 |
+
"table_comment": table_comment,
|
197 |
+
"columns": {
|
198 |
+
col_name: {
|
199 |
+
"type": schema[col_name],
|
200 |
+
"comment": column_comments.get(col_name)
|
201 |
+
}
|
202 |
+
for col_name in schema
|
203 |
+
}
|
204 |
+
}
|
205 |
+
|
206 |
+
return enhanced_schema
|
207 |
+
except Exception as e:
|
208 |
+
st.error(f"Error fetching schema comments: {str(e)}")
|
209 |
+
return schema # Fallback to basic schema if comment retrieval fails
|
210 |
|
211 |
|
212 |
def get_sample_data(table_name):
|
|
|
274 |
pprint(f"\n[{MODEL}] Prompt tokens for SQL generation: {prompt_tokens}")
|
275 |
|
276 |
# Debug prompt in a Streamlit expander for better organization
|
277 |
+
# Check if running locally based on Streamlit's origin header
|
278 |
+
if 'localhost' in st.context.headers.get("Origin", ""):
|
279 |
+
with st.expander("Debug: Prompt Generation"):
|
280 |
+
st.write(f"\nUser Query: {user_query}")
|
281 |
+
st.write("\nFull Prompt:")
|
282 |
+
st.code(prompt, language="text")
|
283 |
|
284 |
start_time = time.time()
|
285 |
with st.spinner(f"Generating SQL query using {MODEL}..."):
|