Update app.py
Browse files
app.py
CHANGED
|
@@ -42,7 +42,7 @@ class SALTAnalytics:
|
|
| 42 |
|
| 43 |
# Get schema information
|
| 44 |
schema_result = self.con.execute("DESCRIBE salt_data").fetchall()
|
| 45 |
-
self.schema_info = "\n".join([f"{col}: {col}" for col in schema_result])
|
| 46 |
|
| 47 |
self.data_loaded = True
|
| 48 |
return f"✅ Successfully loaded {len(df)} records into DuckDB"
|
|
@@ -94,6 +94,22 @@ class SALTAnalytics:
|
|
| 94 |
except Exception as e:
|
| 95 |
return f"Error generating insights: {str(e)}"
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def natural_language_query(self, question: str, api_key: str):
|
| 98 |
"""Convert natural language to SQL and execute"""
|
| 99 |
if not self.data_loaded:
|
|
@@ -132,11 +148,8 @@ class SALTAnalytics:
|
|
| 132 |
|
| 133 |
sql_query = response.choices.message.content.strip()
|
| 134 |
|
| 135 |
-
# Clean SQL query
|
| 136 |
-
|
| 137 |
-
sql_query = sql_query[6:-3]
|
| 138 |
-
elif sql_query.startswith('```
|
| 139 |
-
sql_query = sql_query[3:-3]
|
| 140 |
|
| 141 |
# Execute query
|
| 142 |
result_df = self.con.execute(sql_query).fetchdf()
|
|
@@ -289,7 +302,7 @@ with gr.Blocks(title="SAP SALT Analytics Demo", theme=gr.themes.Soft()) as demo:
|
|
| 289 |
- **DuckDB**: High-performance analytics database
|
| 290 |
- **OpenAI GPT-4**: Natural language to SQL conversion
|
| 291 |
- **Hugging Face**: Dataset hosting and deployment
|
| 292 |
-
- **Gradio
|
| 293 |
|
| 294 |
**Business Value**:
|
| 295 |
- Automate sales order completion (70-80% accuracy)
|
|
|
|
| 42 |
|
| 43 |
# Get schema information
|
| 44 |
schema_result = self.con.execute("DESCRIBE salt_data").fetchall()
|
| 45 |
+
self.schema_info = "\n".join([f"{col[0]}: {col[1]}" for col in schema_result])
|
| 46 |
|
| 47 |
self.data_loaded = True
|
| 48 |
return f"✅ Successfully loaded {len(df)} records into DuckDB"
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
return f"Error generating insights: {str(e)}"
|
| 96 |
|
| 97 |
+
def clean_sql_response(self, sql_query: str) -> str:
|
| 98 |
+
"""Safely clean SQL response from OpenAI - FIXED VERSION"""
|
| 99 |
+
# Remove markdown code blocks safely
|
| 100 |
+
sql_markers = ['``````']
|
| 101 |
+
|
| 102 |
+
for marker in sql_markers:
|
| 103 |
+
if sql_query.startswith(marker):
|
| 104 |
+
sql_query = sql_query[len(marker):]
|
| 105 |
+
break
|
| 106 |
+
|
| 107 |
+
# Remove trailing backticks
|
| 108 |
+
if sql_query.endswith('```
|
| 109 |
+
sql_query = sql_query[:-3]
|
| 110 |
+
|
| 111 |
+
return sql_query.strip()
|
| 112 |
+
|
| 113 |
def natural_language_query(self, question: str, api_key: str):
|
| 114 |
"""Convert natural language to SQL and execute"""
|
| 115 |
if not self.data_loaded:
|
|
|
|
| 148 |
|
| 149 |
sql_query = response.choices.message.content.strip()
|
| 150 |
|
| 151 |
+
# Clean SQL query using safe method
|
| 152 |
+
sql_query = self.clean_sql_response(sql_query)
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
# Execute query
|
| 155 |
result_df = self.con.execute(sql_query).fetchdf()
|
|
|
|
| 302 |
- **DuckDB**: High-performance analytics database
|
| 303 |
- **OpenAI GPT-4**: Natural language to SQL conversion
|
| 304 |
- **Hugging Face**: Dataset hosting and deployment
|
| 305 |
+
- **Gradio 5.43+**: Secure interactive web interface
|
| 306 |
|
| 307 |
**Business Value**:
|
| 308 |
- Automate sales order completion (70-80% accuracy)
|