SVashishta1
commited on
Commit
·
d33fd46
1
Parent(s):
3133b04
Error Fix
Browse files
app.py
CHANGED
|
@@ -107,18 +107,31 @@ def process_text_query(query, history):
|
|
| 107 |
if not query:
|
| 108 |
return "", history
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Build context with table information
|
| 123 |
table_info = []
|
| 124 |
for table in tables:
|
|
@@ -129,9 +142,13 @@ def process_text_query(query, history):
|
|
| 129 |
# Create question with context
|
| 130 |
question_with_context = f"The database contains the following tables:\n{chr(10).join(table_info)}\n\n{query}"
|
| 131 |
|
| 132 |
-
# Generate SQL query using the
|
| 133 |
sql_query = query_engine.generate_response(query_prompt.format(question=question_with_context))
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
try:
|
| 136 |
# Execute the query
|
| 137 |
result_df = pd.read_sql_query(sql_query, conn)
|
|
@@ -142,15 +159,6 @@ def process_text_query(query, history):
|
|
| 142 |
else:
|
| 143 |
data_str = result_df.to_string()
|
| 144 |
|
| 145 |
-
# Interpret the results using the existing query engine
|
| 146 |
-
interpretation = query_engine.generate_response(
|
| 147 |
-
interpret_prompt.format(
|
| 148 |
-
question=query,
|
| 149 |
-
sql_query=sql_query,
|
| 150 |
-
data=data_str
|
| 151 |
-
)
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
# Create the response
|
| 155 |
response = f"**SQL Query:**\n```sql\n{sql_query}\n```\n\n"
|
| 156 |
|
|
@@ -159,19 +167,20 @@ def process_text_query(query, history):
|
|
| 159 |
else:
|
| 160 |
response += "**No results found.**\n\n"
|
| 161 |
|
| 162 |
-
response += f"**Analysis:**\n{interpretation}"
|
| 163 |
-
|
| 164 |
except Exception as e:
|
| 165 |
response = f"**SQL Query:**\n```sql\n{sql_query}\n```\n\n**Error executing query:** {str(e)}"
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
response = document_assistant.process_query(query)
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
response = document_assistant.process_query(query)
|
| 176 |
|
| 177 |
# Update history with message format
|
|
|
|
| 107 |
if not query:
|
| 108 |
return "", history
|
| 109 |
|
| 110 |
+
# More specific SQL detection - look for actual SQL-like patterns or explicit SQL requests
|
| 111 |
+
sql_keywords = ['select', 'from', 'where', 'group by', 'order by', 'having', 'join']
|
| 112 |
+
data_analysis_keywords = ['average', 'count', 'sum', 'maximum', 'minimum', 'mean', 'analyze', 'calculate']
|
| 113 |
+
|
| 114 |
+
# Check if this is explicitly about the CSV/database data
|
| 115 |
+
is_sql_query = (
|
| 116 |
+
any(keyword in query.lower() for keyword in sql_keywords) or
|
| 117 |
+
('csv' in query.lower() and any(keyword in query.lower() for keyword in data_analysis_keywords)) or
|
| 118 |
+
'database' in query.lower() or
|
| 119 |
+
'table' in query.lower()
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
# Connect to the SQLite database to check if we have any tables
|
| 124 |
+
conn = sqlite3.connect(DB_PATH)
|
| 125 |
+
cursor = conn.cursor()
|
| 126 |
+
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
| 127 |
+
tables = [row[0] for row in cursor.fetchall()]
|
| 128 |
+
conn.close()
|
| 129 |
+
|
| 130 |
+
if is_sql_query and tables:
|
| 131 |
+
try:
|
| 132 |
+
conn = sqlite3.connect(DB_PATH)
|
| 133 |
+
cursor = conn.cursor()
|
| 134 |
+
|
| 135 |
# Build context with table information
|
| 136 |
table_info = []
|
| 137 |
for table in tables:
|
|
|
|
| 142 |
# Create question with context
|
| 143 |
question_with_context = f"The database contains the following tables:\n{chr(10).join(table_info)}\n\n{query}"
|
| 144 |
|
| 145 |
+
# Generate SQL query using the query engine
|
| 146 |
sql_query = query_engine.generate_response(query_prompt.format(question=question_with_context))
|
| 147 |
|
| 148 |
+
# Verify the response is actually a SQL query
|
| 149 |
+
if not any(keyword in sql_query.lower() for keyword in ['select', 'from']):
|
| 150 |
+
raise ValueError("Generated response is not a valid SQL query")
|
| 151 |
+
|
| 152 |
try:
|
| 153 |
# Execute the query
|
| 154 |
result_df = pd.read_sql_query(sql_query, conn)
|
|
|
|
| 159 |
else:
|
| 160 |
data_str = result_df.to_string()
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
# Create the response
|
| 163 |
response = f"**SQL Query:**\n```sql\n{sql_query}\n```\n\n"
|
| 164 |
|
|
|
|
| 167 |
else:
|
| 168 |
response += "**No results found.**\n\n"
|
| 169 |
|
|
|
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
response = f"**SQL Query:**\n```sql\n{sql_query}\n```\n\n**Error executing query:** {str(e)}"
|
| 172 |
+
|
| 173 |
+
conn.close()
|
| 174 |
+
|
| 175 |
+
except Exception as e:
|
| 176 |
+
# If there's an error with SQL processing, fall back to document query
|
| 177 |
+
response = document_assistant.process_query(query)
|
| 178 |
+
else:
|
| 179 |
+
# Process regular document query
|
| 180 |
response = document_assistant.process_query(query)
|
| 181 |
+
|
| 182 |
+
except Exception as e:
|
| 183 |
+
# If there's any database connection error, fall back to document query
|
| 184 |
response = document_assistant.process_query(query)
|
| 185 |
|
| 186 |
# Update history with message format
|