Ari commited on
Commit
e69e246
·
verified ·
1 Parent(s): 770a1d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -20,7 +20,7 @@ if not openai_api_key:
20
  st.stop()
21
 
22
  # Step 1: Upload CSV data file (or use default)
23
- st.title("Natural Language to SQL Query App with Enhanced Insights")
24
  st.write("Upload a CSV file to get started, or use the default dataset.")
25
 
26
  csv_file = st.file_uploader("Upload your CSV file", type=["csv"])
@@ -71,29 +71,29 @@ sql_generation_chain = LLMChain(llm=llm, prompt=sql_prompt)
71
 
72
  # Insights Generation Chain
73
  insights_template = """
74
- You are an expert data scientist. Based on the user's question and the SQL query result provided below, generate a concise and informative analysis that includes data insights and actionable recommendations.
75
 
76
  User's Question: {question}
77
 
78
  SQL Query Result:
79
  {result}
80
 
81
- Analysis and Recommendations:
82
  """
83
  insights_prompt = PromptTemplate(template=insights_template, input_variables=['question', 'result'])
84
  insights_chain = LLMChain(llm=llm, prompt=insights_prompt)
85
 
86
- # General Insights and Recommendations Chain
87
- general_insights_template = """
88
- You are an expert data scientist. Based on the entire dataset provided below, generate a comprehensive analysis that includes key insights and actionable recommendations.
89
 
90
  Dataset Summary:
91
  {dataset_summary}
92
 
93
- Analysis and Recommendations:
94
  """
95
- general_insights_prompt = PromptTemplate(template=general_insights_template, input_variables=['dataset_summary'])
96
- general_insights_chain = LLMChain(llm=llm, prompt=general_insights_prompt)
97
 
98
  # Optional: Clean up function to remove incorrect COLLATE NOCASE usage
99
  def clean_sql_query(query):
@@ -121,7 +121,7 @@ def clean_sql_query(query):
121
  def classify_query(question):
122
  """Classify the user query as either 'SQL' or 'INSIGHTS'."""
123
  classification_template = """
124
- You are an AI assistant that classifies user queries into two categories: 'SQL' for specific data retrieval queries and 'INSIGHTS' for general analytical or recommendation queries.
125
 
126
  Determine the appropriate category for the following user question.
127
 
@@ -178,19 +178,8 @@ def process_input():
178
  }).strip()
179
 
180
  if generated_sql.upper() == "NO_SQL":
181
- # Handle cases where no SQL should be generated
182
- assistant_response = "Sure, let's discuss some general insights and recommendations based on the data."
183
-
184
- # Generate dataset summary
185
- dataset_summary = generate_dataset_summary(data)
186
-
187
- # Generate general insights and recommendations
188
- general_insights = general_insights_chain.run({
189
- 'dataset_summary': dataset_summary
190
- })
191
-
192
- # Append the assistant's insights to the history
193
- st.session_state.history.append({"role": "assistant", "content": general_insights})
194
  else:
195
  # Clean the SQL query
196
  cleaned_sql = clean_sql_query(generated_sql)
@@ -207,7 +196,7 @@ def process_input():
207
  # Convert the result to a string for the insights prompt
208
  result_str = result.head(10).to_string(index=False) # Limit to first 10 rows
209
 
210
- # Generate insights and recommendations based on the query result
211
  insights = insights_chain.run({
212
  'question': user_prompt,
213
  'result': result_str
@@ -222,16 +211,29 @@ def process_input():
222
  assistant_response = f"Error executing SQL query: {e}"
223
  st.session_state.history.append({"role": "assistant", "content": assistant_response})
224
  else: # INSIGHTS category
225
- # Generate dataset summary
226
- dataset_summary = generate_dataset_summary(data)
 
227
 
228
- # Generate general insights and recommendations
229
- general_insights = general_insights_chain.run({
230
- 'dataset_summary': dataset_summary
231
- })
232
 
233
- # Append the assistant's insights to the history
234
- st.session_state.history.append({"role": "assistant", "content": general_insights})
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
  except Exception as e:
237
  logging.error(f"An error occurred: {e}")
 
20
  st.stop()
21
 
22
  # Step 1: Upload CSV data file (or use default)
23
+ st.title("Natural Language to SQL Query App with Dynamic Insights")
24
  st.write("Upload a CSV file to get started, or use the default dataset.")
25
 
26
  csv_file = st.file_uploader("Upload your CSV file", type=["csv"])
 
71
 
72
  # Insights Generation Chain
73
  insights_template = """
74
+ You are an expert data scientist. Based on the user's question and the SQL query result provided below, generate a concise and informative analysis that includes data insights.
75
 
76
  User's Question: {question}
77
 
78
  SQL Query Result:
79
  {result}
80
 
81
+ Analysis:
82
  """
83
  insights_prompt = PromptTemplate(template=insights_template, input_variables=['question', 'result'])
84
  insights_chain = LLMChain(llm=llm, prompt=insights_prompt)
85
 
86
+ # Recommendations Generation Chain
87
+ recommendations_template = """
88
+ You are an expert data scientist. Based on the dataset summary provided below, generate actionable recommendations for improving performance.
89
 
90
  Dataset Summary:
91
  {dataset_summary}
92
 
93
+ Recommendations:
94
  """
95
+ recommendations_prompt = PromptTemplate(template=recommendations_template, input_variables=['dataset_summary'])
96
+ recommendations_chain = LLMChain(llm=llm, prompt=recommendations_prompt)
97
 
98
  # Optional: Clean up function to remove incorrect COLLATE NOCASE usage
99
  def clean_sql_query(query):
 
121
  def classify_query(question):
122
  """Classify the user query as either 'SQL' or 'INSIGHTS'."""
123
  classification_template = """
124
+ You are an AI assistant that classifies user queries into two categories: 'SQL' for specific data retrieval queries and 'INSIGHTS' for general analytical queries.
125
 
126
  Determine the appropriate category for the following user question.
127
 
 
178
  }).strip()
179
 
180
  if generated_sql.upper() == "NO_SQL":
181
+ assistant_response = "This query is too vague for generating SQL. Please ask a more specific question."
182
+ st.session_state.history.append({"role": "assistant", "content": assistant_response})
 
 
 
 
 
 
 
 
 
 
 
183
  else:
184
  # Clean the SQL query
185
  cleaned_sql = clean_sql_query(generated_sql)
 
196
  # Convert the result to a string for the insights prompt
197
  result_str = result.head(10).to_string(index=False) # Limit to first 10 rows
198
 
199
+ # Generate insights based on the query result
200
  insights = insights_chain.run({
201
  'question': user_prompt,
202
  'result': result_str
 
211
  assistant_response = f"Error executing SQL query: {e}"
212
  st.session_state.history.append({"role": "assistant", "content": assistant_response})
213
  else: # INSIGHTS category
214
+ if "recommendations" in user_prompt.lower():
215
+ # Generate dataset summary for recommendations
216
+ dataset_summary = generate_dataset_summary(data)
217
 
218
+ # Generate recommendations based on the dataset summary
219
+ recommendations = recommendations_chain.run({
220
+ 'dataset_summary': dataset_summary
221
+ })
222
 
223
+ # Append the assistant's recommendations to the history
224
+ st.session_state.history.append({"role": "assistant", "content": recommendations})
225
+ else:
226
+ # Generate dataset summary for general insights (without recommendations)
227
+ dataset_summary = generate_dataset_summary(data)
228
+
229
+ # Generate general insights
230
+ general_insights = insights_chain.run({
231
+ 'question': user_prompt,
232
+ 'result': dataset_summary
233
+ })
234
+
235
+ # Append the assistant's general insights to the history
236
+ st.session_state.history.append({"role": "assistant", "content": general_insights})
237
 
238
  except Exception as e:
239
  logging.error(f"An error occurred: {e}")