ThiyagaB commited on
Commit
5f4d347
·
1 Parent(s): e9b97bb

panda query

Browse files
Files changed (2) hide show
  1. app.py +14 -5
  2. election_results.csv +0 -0
app.py CHANGED
@@ -10,6 +10,7 @@ For more information on `huggingface_hub` Inference API support, please check th
10
 
11
  import pandas as pd
12
  import pandasql
 
13
 
14
  # Create a sample DataFrame
15
  data = [
@@ -19,8 +20,11 @@ data = [
19
  {"Name": "Alice", "Age": 24, "Gender": "female", "Votes": 120},
20
  ]
21
 
 
 
 
22
  # Create a pandas dataframe from the list of dictionaries
23
- df = pd.DataFrame(data)
24
 
25
 
26
  def respond(
@@ -32,7 +36,7 @@ def respond(
32
  messages = [
33
  {
34
  "role": "system",
35
- "content": "Your task is to convert the input query into a sql statement to be used against a panda dataframe.\n\nGiven the below columns, \n\nColumn1: Age\nColumn2: Name\nColumn3: Gender\nColumn4: Votes\nColumn5: Location\nColumn6: Party\n\n and Table name as df \n and the user input text, \n\nconvert it into a proper sql statement.\n\nIn the where condition make sure you do a case insensitive comparison for text columns, and where possible use like, instead of 'equal' condition. Also when you compare with text always use a lowercase, for example use 'female', not 'Female'. \n\nOutput format:\nIn the response give only the SQL statement starts with 'SELECT', do not add any note or any other explanations"
36
  }
37
  ]
38
  for val in history:
@@ -46,15 +50,20 @@ def respond(
46
  model="llama3-70b-8192",
47
  messages=messages,
48
  temperature=1,
49
- max_tokens=1024,
50
  top_p=1,
51
  stream=False,
52
  stop=None,
53
  )
54
 
55
  sql_command = completion.choices[0].message.content
56
- result = pandasql.sqldf(sql_command, globals())
57
- yield str(result)
 
 
 
 
 
58
 
59
  """
60
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
10
 
11
  import pandas as pd
12
  import pandasql
13
+ from collections import defaultdict
14
 
15
  # Create a sample DataFrame
16
  data = [
 
20
  {"Name": "Alice", "Age": 24, "Gender": "female", "Votes": 120},
21
  ]
22
 
23
+ # types = defaultdict(str,'Votes'=int,'Votes Percentage'=float)
24
+ # {'Votes':int,'Votes Percentage':float}
25
+ #
26
  # Create a pandas dataframe from the list of dictionaries
27
+ df = pd.read_csv('election_results.csv',dtype={'Votes':'int64'})
28
 
29
 
30
  def respond(
 
36
  messages = [
37
  {
38
  "role": "system",
39
+ "content": "You are an election result analysis bot. Your task is to convert the user's natural language query into a SQL SELECT statement suitable for a pandas DataFrame named df.\n\nAvailable Columns:\n\nCandidate (string)\nParty (string)\nVotes (integer)\nVotes Percentage (decimal)\nConstituency (string)\nState (string)\n\nCase-Insensitive Text Comparisons:\n\nUse UPPER function to convert text columns (Candidate, Party, Constituency, State) to uppercase for case-insensitive comparisons.\nEmploy LIKE operator with wildcards (%) for pattern matching when appropriate.\n\nUser Input:\n\nThe system will provide the user's query in natural language.\n\nOutput Format:\n\nGenerate the SQL SELECT statement only, starting with SELECT. Do not add any other extra instruction text, your output will be directly executed, so send only the SQL Statement\n\nExample:\n\nUser Input: Find all candidates from Coimbatore who received more than 50% of the votes.\n\nYour Output:\nSELECT * FROM df\nWHERE UPPER(State) = 'COIMBATORE' AND Votes Percentage > 50"
40
  }
41
  ]
42
  for val in history:
 
50
  model="llama3-70b-8192",
51
  messages=messages,
52
  temperature=1,
53
+ max_tokens=2048,
54
  top_p=1,
55
  stream=False,
56
  stop=None,
57
  )
58
 
59
  sql_command = completion.choices[0].message.content
60
+ print(sql_command)
61
+
62
+ if sql_command.startswith('SELECT'):
63
+ result = pandasql.sqldf(sql_command, globals())
64
+ yield result.to_string()
65
+ else:
66
+ yield str(sql_command)
67
 
68
  """
69
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
election_results.csv ADDED
The diff for this file is too large to render. See raw diff