Spaces:
Runtime error
Runtime error
File size: 12,441 Bytes
e37eda0 3eb59a4 75829f5 1746d1f 6a2a63a e37eda0 82bfc51 1746d1f 32ba6b8 1746d1f e84e3a8 82bfc51 110bab7 82bfc51 1746d1f 110bab7 32ba6b8 110bab7 32ba6b8 1746d1f 32ba6b8 1746d1f 5ad9e6e 110bab7 cd60664 ec5af14 0bb1965 cd60664 0bb1965 82bfc51 5189e45 9e9d1c1 cd60664 5189e45 cd60664 82bfc51 cd60664 82bfc51 1746d1f 5189e45 1746d1f 0bb1965 82bfc51 0bb1965 82bfc51 1d00adc 82bfc51 746d24c 82bfc51 0bb1965 1746d1f 82bfc51 5189e45 1746d1f b21f6bf 1746d1f b21f6bf 1746d1f 1d00adc 1746d1f 1d00adc 1746d1f 1d00adc 0bb1965 1d00adc 1746d1f 1d00adc 1746d1f 1d00adc 1746d1f 1d00adc 1746d1f 1d00adc 1746d1f 1d00adc ec5af14 1746d1f ec5af14 82bfc51 1d00adc 82bfc51 1d00adc 82bfc51 0bb1965 82bfc51 1d00adc 82bfc51 1d00adc 1746d1f 1d00adc 1746d1f 1d00adc 0bb1965 1d00adc 5189e45 1d00adc 1746d1f 1d00adc b21f6bf 1d00adc 1746d1f ec5af14 1746d1f 82bfc51 a3c9c61 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
import os
import streamlit as st
import pandas as pd
import sqlite3
from langchain import LLMChain, PromptTemplate
import sqlparse
import logging
# Import necessary modules from transformers and langchain
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from langchain.llms import HuggingFacePipeline
# Initialize conversation history
if 'history' not in st.session_state:
st.session_state['history'] = []
# Set up the Llama-2-7b-chat-hf model
model_id = "meta-llama/Llama-2-7b-chat-hf"
# Get your Hugging Face token (it's stored as a secret in your Space)
hf_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
if hf_token is None:
st.error("Hugging Face API token is not set. Please set the HUGGINGFACEHUB_API_TOKEN secret in your Space.")
st.stop()
# Import torch
import torch
# Set device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load the tokenizer and model with the token
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=hf_token)
model = AutoModelForCausalLM.from_pretrained(
model_id,
use_auth_token=hf_token,
device_map=None, # We'll set the device manually
torch_dtype=torch.float32 # Use float32 to avoid half-precision issues
).to(device)
# Create the text-generation pipeline with appropriate parameters
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=512,
temperature=0.1,
repetition_penalty=1.1,
do_sample=True, # Use sampling to introduce some randomness
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.eos_token_id,
device=0 if torch.cuda.is_available() else -1 # Use GPU if available
)
# Wrap the pipeline with HuggingFacePipeline for use in LangChain
llm = HuggingFacePipeline(pipeline=pipe)
# Step 1: Upload CSV data file (or use default)
st.title("Natural Language to SQL Query App with Enhanced Insights")
st.write("Upload a CSV file to get started, or use the default dataset.")
csv_file = st.file_uploader("Upload your CSV file", type=["csv"])
if csv_file is None:
data = pd.read_csv("default_data.csv") # Ensure this file exists in your working directory
st.write("Using default_data.csv file.")
table_name = "default_table"
else:
data = pd.read_csv(csv_file)
table_name = csv_file.name.split('.')[0]
st.write(f"Data Preview ({csv_file.name}):")
st.dataframe(data.head())
# Step 2: Load CSV data into a persistent SQLite database
db_file = 'my_database.db'
conn = sqlite3.connect(db_file)
data.to_sql(table_name, conn, index=False, if_exists='replace')
# SQL table metadata (for validation and schema)
valid_columns = list(data.columns)
st.write(f"Valid columns: {valid_columns}")
# Step 3: Set up the LLM Chains with adjusted prompts
# SQL Generation Chain
sql_template = """
[INST] <<SYS>>
You are an expert data scientist.
<</SYS>>
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.
Ensure that:
- You only use the columns provided.
- When performing string comparisons in the WHERE clause, make them case-insensitive by using 'COLLATE NOCASE' or the LOWER() function.
- Do not use 'COLLATE NOCASE' in ORDER BY clauses unless sorting a string column.
- Do not apply 'COLLATE NOCASE' to numeric columns.
If the question is vague or open-ended and does not pertain to specific data retrieval, respond with "NO_SQL" to indicate that a SQL query should not be generated.
Question: {question}
Table name: {table_name}
Valid columns: {columns}
SQL Query:
[/INST]
"""
sql_prompt = PromptTemplate(template=sql_template, input_variables=['question', 'table_name', 'columns'])
sql_generation_chain = LLMChain(llm=llm, prompt=sql_prompt)
# Insights Generation Chain
insights_template = """
[INST] <<SYS>>
You are an expert data scientist.
<</SYS>>
Based on the user's question and the SQL query result provided below, generate a concise analysis that includes key data insights and actionable recommendations. Limit the response to a maximum of 150 words.
User's Question: {question}
SQL Query Result:
{result}
Concise Analysis (max 200 words):
[/INST]
"""
insights_prompt = PromptTemplate(template=insights_template, input_variables=['question', 'result'])
insights_chain = LLMChain(llm=llm, prompt=insights_prompt)
# General Insights and Recommendations Chain
general_insights_template = """
[INST] <<SYS>>
You are an expert data scientist.
<</SYS>>
Based on the entire dataset provided below, generate a concise analysis with key insights and recommendations. Limit the response to 150 words.
Dataset Summary:
{dataset_summary}
Concise Analysis and Recommendations (max 150 words):
[/INST]
"""
general_insights_prompt = PromptTemplate(template=general_insights_template, input_variables=['dataset_summary'])
general_insights_chain = LLMChain(llm=llm, prompt=general_insights_prompt)
# Optional: Clean up function to remove incorrect COLLATE NOCASE usage
def clean_sql_query(query):
"""Removes incorrect usage of COLLATE NOCASE from the SQL query."""
parsed = sqlparse.parse(query)
statements = []
for stmt in parsed:
tokens = []
idx = 0
while idx < len(stmt.tokens):
token = stmt.tokens[idx]
if (token.ttype is sqlparse.tokens.Keyword and token.value.upper() == 'COLLATE'):
# Check if the next token is 'NOCASE'
next_token = stmt.tokens[idx + 2] if idx + 2 < len(stmt.tokens) else None
if next_token and next_token.value.upper() == 'NOCASE':
# Skip 'COLLATE' and 'NOCASE' tokens
idx += 3 # Skip 'COLLATE', whitespace, 'NOCASE'
continue
tokens.append(token)
idx += 1
statements.append(''.join([str(t) for t in tokens]))
return ' '.join(statements)
# Function to classify user query
def classify_query(question):
"""Classify the user query as either 'SQL' or 'INSIGHTS'."""
classification_template = """
[INST] <<SYS>>
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.
<</SYS>>
Determine the appropriate category for the following user question.
Question: "{question}"
Category (SQL/INSIGHTS):
[/INST]
"""
classification_prompt = PromptTemplate(template=classification_template, input_variables=['question'])
classification_chain = LLMChain(llm=llm, prompt=classification_prompt)
category = classification_chain.run({'question': question}).strip().upper()
if category.startswith('SQL'):
return 'SQL'
else:
return 'INSIGHTS'
# Function to generate dataset summary
def generate_dataset_summary(data):
"""Generate a summary of the dataset for general insights."""
summary_template = """
[INST] <<SYS>>
You are an expert data scientist.
<</SYS>>
Based on the dataset provided below, generate a concise summary that includes the number of records, number of columns, data types, and any notable features.
Dataset:
{data}
Dataset Summary:
[/INST]
"""
summary_prompt = PromptTemplate(template=summary_template, input_variables=['data'])
summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
summary = summary_chain.run({'data': data.head().to_string(index=False)})
return summary
# Define the callback function
def process_input():
user_prompt = st.session_state['user_input']
if user_prompt:
try:
# Append user message to history
st.session_state.history.append({"role": "user", "content": user_prompt})
# Classify the user query
category = classify_query(user_prompt)
logging.info(f"User query classified as: {category}")
if "COLUMNS" in user_prompt.upper():
assistant_response = f"The columns are: {', '.join(valid_columns)}"
st.session_state.history.append({"role": "assistant", "content": assistant_response})
elif category == 'SQL':
columns = ', '.join(valid_columns)
generated_sql = sql_generation_chain.run({
'question': user_prompt,
'table_name': table_name,
'columns': columns
}).strip()
if generated_sql.upper() == "NO_SQL":
# Handle cases where no SQL should be generated
assistant_response = "Sure, let's discuss some general insights and recommendations based on the data."
# Generate dataset summary
dataset_summary = generate_dataset_summary(data)
# Generate general insights and recommendations
general_insights = general_insights_chain.run({
'dataset_summary': dataset_summary
})
# Append the assistant's insights to the history
st.session_state.history.append({"role": "assistant", "content": general_insights})
else:
# Clean the SQL query
cleaned_sql = clean_sql_query(generated_sql)
logging.info(f"Generated SQL Query: {cleaned_sql}")
# Attempt to execute SQL query and handle exceptions
try:
result = pd.read_sql_query(cleaned_sql, conn)
if result.empty:
assistant_response = "The query returned no results. Please try a different question."
st.session_state.history.append({"role": "assistant", "content": assistant_response})
else:
# Convert the result to a string for the insights prompt
result_str = result.head(10).to_string(index=False) # Limit to first 10 rows
# Generate insights and recommendations based on the query result
insights = insights_chain.run({
'question': user_prompt,
'result': result_str
})
# Append the assistant's insights to the history
st.session_state.history.append({"role": "assistant", "content": insights})
# Append the result DataFrame to the history
st.session_state.history.append({"role": "assistant", "content": result})
except Exception as e:
logging.error(f"An error occurred during SQL execution: {e}")
assistant_response = f"Error executing SQL query: {e}"
st.session_state.history.append({"role": "assistant", "content": assistant_response})
else: # INSIGHTS category
# Generate dataset summary
dataset_summary = generate_dataset_summary(data)
# Generate general insights and recommendations
general_insights = general_insights_chain.run({
'dataset_summary': dataset_summary
})
# Append the assistant's insights to the history
st.session_state.history.append({"role": "assistant", "content": general_insights})
except Exception as e:
logging.error(f"An error occurred: {e}")
assistant_response = f"Error: {e}"
st.session_state.history.append({"role": "assistant", "content": assistant_response})
# Reset the user_input in session state
st.session_state['user_input'] = ''
# Display the conversation history
for message in st.session_state.history:
if message['role'] == 'user':
st.markdown(f"**User:** {message['content']}")
elif message['role'] == 'assistant':
if isinstance(message['content'], pd.DataFrame):
st.markdown("**Assistant:** Query Results:")
st.dataframe(message['content'])
else:
st.markdown(f"**Assistant:** {message['content']}")
# Place the input field at the bottom with the callback
st.text_input("Enter your message:", key='user_input', on_change=process_input)
|