Spaces:
Sleeping
Sleeping
Ari
commited on
Update app.py
Browse files
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")
|
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"])
|
@@ -43,7 +43,8 @@ data.to_sql(table_name, conn, index=False, if_exists='replace')
|
|
43 |
valid_columns = list(data.columns)
|
44 |
st.write(f"Valid columns: {valid_columns}")
|
45 |
|
46 |
-
# Step 3: Set up the LLM
|
|
|
47 |
sql_template = """
|
48 |
You are an expert data scientist. Given a natural language question, the name of the table, and a list of valid columns, generate a valid SQL query that answers the question.
|
49 |
|
@@ -66,6 +67,20 @@ sql_prompt = PromptTemplate(template=sql_template, input_variables=['question',
|
|
66 |
llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
|
67 |
sql_generation_chain = LLMChain(llm=llm, prompt=sql_prompt)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Optional: Clean up function to remove incorrect COLLATE NOCASE usage
|
70 |
def clean_sql_query(query):
|
71 |
"""Removes incorrect usage of COLLATE NOCASE from the SQL query."""
|
@@ -115,28 +130,23 @@ def process_input():
|
|
115 |
try:
|
116 |
result = pd.read_sql_query(generated_sql, conn)
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
# Append the assistant's answer to the history
|
137 |
-
st.session_state.history.append({"role": "assistant", "content": assistant_answer})
|
138 |
-
# Append the result DataFrame to the history
|
139 |
-
st.session_state.history.append({"role": "assistant", "content": result})
|
140 |
except Exception as e:
|
141 |
logging.error(f"An error occurred during SQL execution: {e}")
|
142 |
assistant_response = f"Error executing SQL query: {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 Data 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"])
|
|
|
43 |
valid_columns = list(data.columns)
|
44 |
st.write(f"Valid columns: {valid_columns}")
|
45 |
|
46 |
+
# Step 3: Set up the LLM Chains
|
47 |
+
# SQL Generation Chain
|
48 |
sql_template = """
|
49 |
You are an expert data scientist. Given a natural language question, the name of the table, and a list of valid columns, generate a valid SQL query that answers the question.
|
50 |
|
|
|
67 |
llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
|
68 |
sql_generation_chain = LLMChain(llm=llm, prompt=sql_prompt)
|
69 |
|
70 |
+
# AnswerScript for generating insights based on query results
|
71 |
+
insights_template = """
|
72 |
+
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.
|
73 |
+
|
74 |
+
User's Question: {question}
|
75 |
+
|
76 |
+
SQL Query Result:
|
77 |
+
{result}
|
78 |
+
|
79 |
+
Analysis and Recommendations:
|
80 |
+
"""
|
81 |
+
insights_prompt = PromptTemplate(template=insights_template, input_variables=['question', 'result'])
|
82 |
+
insights_chain = LLMChain(llm=llm, prompt=insights_prompt)
|
83 |
+
|
84 |
# Optional: Clean up function to remove incorrect COLLATE NOCASE usage
|
85 |
def clean_sql_query(query):
|
86 |
"""Removes incorrect usage of COLLATE NOCASE from the SQL query."""
|
|
|
130 |
try:
|
131 |
result = pd.read_sql_query(generated_sql, conn)
|
132 |
|
133 |
+
if result.empty:
|
134 |
+
assistant_response = "The query returned no results. Please try a different question."
|
135 |
+
st.session_state.history.append({"role": "assistant", "content": assistant_response})
|
136 |
+
else:
|
137 |
+
# Convert the result to a string for the insights prompt
|
138 |
+
result_str = result.head(10).to_string(index=False) # Limit to first 10 rows
|
139 |
+
|
140 |
+
# Generate insights and recommendations
|
141 |
+
insights = insights_chain.run({
|
142 |
+
'question': user_prompt,
|
143 |
+
'result': result_str
|
144 |
+
})
|
145 |
+
|
146 |
+
# Append the assistant's insights to the history
|
147 |
+
st.session_state.history.append({"role": "assistant", "content": insights})
|
148 |
+
# Append the result DataFrame to the history
|
149 |
+
st.session_state.history.append({"role": "assistant", "content": result})
|
|
|
|
|
|
|
|
|
|
|
150 |
except Exception as e:
|
151 |
logging.error(f"An error occurred during SQL execution: {e}")
|
152 |
assistant_response = f"Error executing SQL query: {e}"
|