Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,16 +67,25 @@ def text_to_sql(query: str, schema: str) -> str:
|
|
| 67 |
return f"Error: {e}"
|
| 68 |
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def execute_sql(sql_query: str) -> Tuple[Optional[pd.DataFrame], Optional[str]]:
|
| 72 |
try:
|
| 73 |
conn = sqlite3.connect(DB_PATH)
|
|
|
|
| 74 |
df = pd.read_sql_query(sql_query, conn)
|
| 75 |
conn.close()
|
| 76 |
return df, None
|
| 77 |
except Exception as e:
|
| 78 |
return None, f"SQL Execution Error: {e}"
|
| 79 |
|
|
|
|
| 80 |
# Function: Generate Dynamic Visualization
|
| 81 |
def visualize_data(df: pd.DataFrame) -> Optional[str]:
|
| 82 |
if df.empty or df.shape[1] < 2:
|
|
|
|
| 67 |
return f"Error: {e}"
|
| 68 |
|
| 69 |
|
| 70 |
+
def preprocess_sql_for_sqlite(sql_query: str) -> str:
|
| 71 |
+
"""
|
| 72 |
+
Replace non-SQLite functions with SQLite-compatible equivalents.
|
| 73 |
+
"""
|
| 74 |
+
sql_query = re.sub(r"\bMONTH\s*\(\s*([\w.]+)\s*\)", r"strftime('%m', \1)", sql_query)
|
| 75 |
+
sql_query = re.sub(r"\bYEAR\s*\(\s*([\w.]+)\s*\)", r"strftime('%Y', \1)", sql_query)
|
| 76 |
+
return sql_query
|
| 77 |
+
|
| 78 |
def execute_sql(sql_query: str) -> Tuple[Optional[pd.DataFrame], Optional[str]]:
|
| 79 |
try:
|
| 80 |
conn = sqlite3.connect(DB_PATH)
|
| 81 |
+
sql_query = preprocess_sql_for_sqlite(sql_query) # Convert to SQLite-compatible SQL
|
| 82 |
df = pd.read_sql_query(sql_query, conn)
|
| 83 |
conn.close()
|
| 84 |
return df, None
|
| 85 |
except Exception as e:
|
| 86 |
return None, f"SQL Execution Error: {e}"
|
| 87 |
|
| 88 |
+
|
| 89 |
# Function: Generate Dynamic Visualization
|
| 90 |
def visualize_data(df: pd.DataFrame) -> Optional[str]:
|
| 91 |
if df.empty or df.shape[1] < 2:
|