Spaces:
Sleeping
Sleeping
ThiyagaB
commited on
Commit
·
5f4d347
1
Parent(s):
e9b97bb
panda query
Browse files- app.py +14 -5
- 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.
|
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
|
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=
|
50 |
top_p=1,
|
51 |
stream=False,
|
52 |
stop=None,
|
53 |
)
|
54 |
|
55 |
sql_command = completion.choices[0].message.content
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
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
|
|