Mustehson commited on
Commit
ca86d9c
·
1 Parent(s): e44c00b

Fixed Reason, Type extraction

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -139,7 +139,7 @@ Recommend a visualization:
139
  ])
140
 
141
  final_prompt = prompt.format_prompt(question=text_query,
142
- sql_query=sql_query, results=sql_result)
143
  response = run_llm(final_prompt)
144
  response = response.replace('```', '')
145
  lines = response.strip().split('\n')
@@ -157,7 +157,7 @@ def format_data(text_query, sql_query, sql_result, visualization_type):
157
 
158
  template = ChatPromptTemplate.from_messages([
159
  ("system", "You are a Data expert who formats data according to the required needs. You are given the question asked by the user, it's sql query, the result of the query and the format you need to format it in."),
160
- ("human", "For the given question: {question}\n\nSQL query: {sql_query}\n\Result: {results}\n\nUse the following example to structure the data: {instructions}. If there is None in Result please change it to '0'. Just give the json string. Do not format it. Do not give backticks."),
161
  ])
162
 
163
 
@@ -165,6 +165,8 @@ def format_data(text_query, sql_query, sql_result, visualization_type):
165
  results=sql_result, instructions=instruction)
166
  print(prompt)
167
  formatted_data = run_llm(prompt)
 
 
168
  print(f'Formatted Data {formatted_data}')
169
  return json.loads(formatted_data.replace('.', '').strip())
170
 
@@ -226,6 +228,11 @@ def main(table, text_query):
226
  except Exception as e:
227
  return generate_output(schema, prompt, generated_sql_query, fig, pd.DataFrame([{"error": f"❌ Unable to execute the SQL query. {e}"}]))
228
 
 
 
 
 
 
229
  try:
230
  visualization_type, reason = get_visualization_type(text_query=text_query,
231
  sql_query=generated_sql_query, sql_result=sql_query_result)
@@ -237,9 +244,9 @@ def main(table, text_query):
237
  return generate_output(schema, prompt, generated_sql_query,
238
  fig, sql_query_df)
239
 
 
240
  if visualization_type != 'none':
241
  try:
242
-
243
  plot = visualize_result(text_query=text_query, sql_query=generated_sql_query,
244
  sql_result=sql_query_result, visualization_type=visualization_type)
245
 
 
139
  ])
140
 
141
  final_prompt = prompt.format_prompt(question=text_query,
142
+ sql_query=sql_query, results=sql_result[:20])
143
  response = run_llm(final_prompt)
144
  response = response.replace('```', '')
145
  lines = response.strip().split('\n')
 
157
 
158
  template = ChatPromptTemplate.from_messages([
159
  ("system", "You are a Data expert who formats data according to the required needs. You are given the question asked by the user, it's sql query, the result of the query and the format you need to format it in."),
160
+ ("human", "For the given question: {question}\n\nSQL query: {sql_query}\n\Result: {results}\n\nUse the following example to structure the data: {instructions}. If there is None in Result please change it to '0'. Just give the json string. Do not format it."),
161
  ])
162
 
163
 
 
165
  results=sql_result, instructions=instruction)
166
  print(prompt)
167
  formatted_data = run_llm(prompt)
168
+ formatted_data = formatted_data.reaplce('```', '')
169
+ formatted_data = formatted_data.reaplce('json', '')
170
  print(f'Formatted Data {formatted_data}')
171
  return json.loads(formatted_data.replace('.', '').strip())
172
 
 
228
  except Exception as e:
229
  return generate_output(schema, prompt, generated_sql_query, fig, pd.DataFrame([{"error": f"❌ Unable to execute the SQL query. {e}"}]))
230
 
231
+ if len(sql_query_result) >= 100:
232
+ gr.Warning(f"⚠️ Data is too large for visualization. Please refine your query.")
233
+ return generate_output(schema, prompt, generated_sql_query,
234
+ fig, sql_query_df)
235
+
236
  try:
237
  visualization_type, reason = get_visualization_type(text_query=text_query,
238
  sql_query=generated_sql_query, sql_result=sql_query_result)
 
244
  return generate_output(schema, prompt, generated_sql_query,
245
  fig, sql_query_df)
246
 
247
+
248
  if visualization_type != 'none':
249
  try:
 
250
  plot = visualize_result(text_query=text_query, sql_query=generated_sql_query,
251
  sql_result=sql_query_result, visualization_type=visualization_type)
252