Process nested JSON files (#10)
Browse files- Process nested JSON files (65f76848f4a55e1f02c7c1df6a7c54c3446fac8a)
- data_sources/upload_file.py +32 -1
- functions/chat_functions.py +2 -2
- functions/sqlite_functions.py +4 -0
data_sources/upload_file.py
CHANGED
@@ -1,6 +1,20 @@
|
|
1 |
import pandas as pd
|
2 |
import sqlite3
|
3 |
import csv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def get_delimiter(file_path, bytes = 4096):
|
6 |
sniffer = csv.Sniffer()
|
@@ -12,7 +26,14 @@ def read_file(file):
|
|
12 |
if file.endswith(('.csv', '.tsv', '.txt')) :
|
13 |
df = pd.read_csv(file, sep=get_delimiter(file))
|
14 |
elif file.endswith('.json'):
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
elif file.endswith('.xml'):
|
17 |
df = pd.read_xml(file)
|
18 |
elif file.endswith(('.xls','xlsx')):
|
@@ -22,6 +43,13 @@ def read_file(file):
|
|
22 |
return df
|
23 |
|
24 |
def process_data_upload(data_file, session_hash):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
df = read_file(data_file)
|
26 |
|
27 |
# Read each sheet and store data in a DataFrame
|
@@ -34,6 +62,9 @@ def process_data_upload(data_file, session_hash):
|
|
34 |
for column in df.columns:
|
35 |
if "date" in column.lower() or "time" in column.lower():
|
36 |
df[column] = pd.to_datetime(df[column])
|
|
|
|
|
|
|
37 |
|
38 |
connection = sqlite3.connect(f'data_source_{session_hash}.db')
|
39 |
print("Opened database successfully");
|
|
|
1 |
import pandas as pd
|
2 |
import sqlite3
|
3 |
import csv
|
4 |
+
import json
|
5 |
+
import time
|
6 |
+
|
7 |
+
def is_file_done_saving(file_path):
|
8 |
+
try:
|
9 |
+
with open(file_path, 'r') as f:
|
10 |
+
contents = f
|
11 |
+
|
12 |
+
if contents:
|
13 |
+
return True
|
14 |
+
else:
|
15 |
+
return False
|
16 |
+
except PermissionError:
|
17 |
+
return False
|
18 |
|
19 |
def get_delimiter(file_path, bytes = 4096):
|
20 |
sniffer = csv.Sniffer()
|
|
|
26 |
if file.endswith(('.csv', '.tsv', '.txt')) :
|
27 |
df = pd.read_csv(file, sep=get_delimiter(file))
|
28 |
elif file.endswith('.json'):
|
29 |
+
with open(file, 'r') as f:
|
30 |
+
contents = json.load(f)
|
31 |
+
df = pd.json_normalize(contents)
|
32 |
+
elif file.endswith('.ndjson'):
|
33 |
+
with open(file, 'r') as f:
|
34 |
+
contents = f.read()
|
35 |
+
data = [json.loads(str(item)) for item in contents.strip().split('\n')]
|
36 |
+
df = pd.json_normalize(data)
|
37 |
elif file.endswith('.xml'):
|
38 |
df = pd.read_xml(file)
|
39 |
elif file.endswith(('.xls','xlsx')):
|
|
|
43 |
return df
|
44 |
|
45 |
def process_data_upload(data_file, session_hash):
|
46 |
+
total_time = 0
|
47 |
+
while not is_file_done_saving(data_file):
|
48 |
+
total_time += .5
|
49 |
+
time.sleep(.5)
|
50 |
+
if total_time > 10:
|
51 |
+
break
|
52 |
+
|
53 |
df = read_file(data_file)
|
54 |
|
55 |
# Read each sheet and store data in a DataFrame
|
|
|
62 |
for column in df.columns:
|
63 |
if "date" in column.lower() or "time" in column.lower():
|
64 |
df[column] = pd.to_datetime(df[column])
|
65 |
+
if df[column].dtype == 'object' and isinstance(df[column].iloc[0], list):
|
66 |
+
df[column] = df[column].explode()
|
67 |
+
|
68 |
|
69 |
connection = sqlite3.connect(f'data_source_{session_hash}.db')
|
70 |
print("Opened database successfully");
|
functions/chat_functions.py
CHANGED
@@ -73,7 +73,7 @@ def example_display(input):
|
|
73 |
|
74 |
css= ".file_marker .large{min-height:50px !important;} .example_btn{max-width:300px;}"
|
75 |
|
76 |
-
with gr.Blocks(css=css) as demo:
|
77 |
title = gr.HTML("<h1 style='text-align:center;'>Virtual Data Analyst</h1>")
|
78 |
description = gr.HTML("""<p style='text-align:center;'>Upload a data file and chat with our virtual data analyst
|
79 |
to get insights on your data set. Currently accepts CSV, TSV, TXT, XLS, XLSX, XML, and JSON files.
|
@@ -87,7 +87,7 @@ with gr.Blocks(css=css) as demo:
|
|
87 |
example_btn_1 = gr.Button(value="Try Me: bank_marketing_campaign.csv", elem_classes="example_btn", size="md", variant="primary")
|
88 |
example_btn_2 = gr.Button(value="Try Me: online_retail_data.csv", elem_classes="example_btn", size="md", variant="primary")
|
89 |
|
90 |
-
file_output = gr.File(label="Data File (CSV, TSV, TXT, XLS, XLSX, XML, JSON)", show_label=True, elem_classes="file_marker", file_types=['.csv','.xlsx','.txt','.json','.xml','.xls','.tsv'])
|
91 |
example_btn_1.click(fn=run_example, inputs=example_file_1, outputs=file_output)
|
92 |
example_btn_2.click(fn=run_example, inputs=example_file_2, outputs=file_output)
|
93 |
file_output.change(fn=example_display, inputs=file_output, outputs=[example_btn_1, example_btn_2])
|
|
|
73 |
|
74 |
css= ".file_marker .large{min-height:50px !important;} .example_btn{max-width:300px;}"
|
75 |
|
76 |
+
with gr.Blocks(css=css, delete_cache=(3600,3600)) as demo:
|
77 |
title = gr.HTML("<h1 style='text-align:center;'>Virtual Data Analyst</h1>")
|
78 |
description = gr.HTML("""<p style='text-align:center;'>Upload a data file and chat with our virtual data analyst
|
79 |
to get insights on your data set. Currently accepts CSV, TSV, TXT, XLS, XLSX, XML, and JSON files.
|
|
|
87 |
example_btn_1 = gr.Button(value="Try Me: bank_marketing_campaign.csv", elem_classes="example_btn", size="md", variant="primary")
|
88 |
example_btn_2 = gr.Button(value="Try Me: online_retail_data.csv", elem_classes="example_btn", size="md", variant="primary")
|
89 |
|
90 |
+
file_output = gr.File(label="Data File (CSV, TSV, TXT, XLS, XLSX, XML, JSON)", show_label=True, elem_classes="file_marker", file_types=['.csv','.xlsx','.txt','.json','.ndjson','.xml','.xls','.tsv'])
|
91 |
example_btn_1.click(fn=run_example, inputs=example_file_1, outputs=file_output)
|
92 |
example_btn_2.click(fn=run_example, inputs=example_file_2, outputs=file_output)
|
93 |
file_output.change(fn=example_display, inputs=file_output, outputs=[example_btn_1, example_btn_2])
|
functions/sqlite_functions.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
from typing import List
|
2 |
from haystack import component
|
3 |
import pandas as pd
|
|
|
|
|
|
|
|
|
4 |
import sqlite3
|
5 |
|
6 |
@component
|
|
|
1 |
from typing import List
|
2 |
from haystack import component
|
3 |
import pandas as pd
|
4 |
+
pd.set_option('display.max_rows', None)
|
5 |
+
pd.set_option('display.max_columns', None)
|
6 |
+
pd.set_option('display.width', None)
|
7 |
+
pd.set_option('display.max_colwidth', None)
|
8 |
import sqlite3
|
9 |
|
10 |
@component
|