File size: 2,760 Bytes
a8f65a9
e6d4ed8
991b668
0f3a499
a75f490
e6d4ed8
a6a85af
a21a3e0
 
 
991b668
 
4f8926e
 
 
31be0ff
6723b04
 
3f1dae8
4f8926e
 
 
 
 
 
 
f77260d
4f8926e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f65a9
 
4f8926e
72518a3
4f8926e
 
 
72518a3
4f8926e
 
 
72518a3
4f8926e
 
f77260d
 
 
 
4f8926e
 
 
 
b5e0c7e
4f8926e
 
a21a3e0
 
 
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
import os
import streamlit as st
import torch
import uuid

from st_app import launch_bot

import nest_asyncio
import asyncio

torch.classes.__path__ = []

import sqlite3
from datasets import load_dataset

# Setup for HTTP API Calls to Amplitude Analytics
if 'device_id' not in st.session_state:
    st.session_state.device_id = str(uuid.uuid4())

def setup_db():
    db_path = 'ev_database.db'
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()        

    with st.spinner("Loading data... Please wait..."):
        def tables_populated() -> bool:
            tables = ['ev_population', 'county_registrations', 'ev_registrations', 'washington_population']        
            for table in tables:
                cursor.execute(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'")
                result = cursor.fetchone()
                if not result:
                    return False
            return True            

        if tables_populated():
            print("Database tables already populated, skipping setup")
            conn.close()
            return
        else:
            print("Populating database tables")

        # Execute the SQL commands to create tables
        with open('create_tables.sql', 'r') as sql_file:
            sql_script = sql_file.read()
            cursor.executescript(sql_script)

        hf_token = os.getenv('HF_TOKEN')

        # Load data into ev_population table
        df = load_dataset("vectara/ev-dataset", data_files="Electric_Vehicle_Population_Data.csv", token=hf_token)['train'].to_pandas()
        df.to_sql('ev_population', conn, if_exists='replace', index=False)

        # Load data into county_registrations table
        df = load_dataset("vectara/ev-dataset", data_files="Electric_Vehicle_Population_Size_History_By_County.csv", token=hf_token)['train'].to_pandas()
        df.to_sql('county_registrations', conn, if_exists='replace', index=False)

        # Load data into ev_registrations table
        df = load_dataset("vectara/ev-dataset", data_files="Electric_Vehicle_Title_and_Registration_Activity.csv", token=hf_token)['train'].to_pandas()
        df.to_sql('ev_registrations', conn, if_exists='replace', index=False)

        # Load data into washington_population table
        df = load_dataset("vectara/ev-dataset", data_files="washington_population.csv", token=hf_token)['train'].to_pandas()
        df.to_sql('washington_population', conn, if_exists='replace', index=False)

        # Commit changes and close connection
        conn.commit()
        conn.close()

if __name__ == "__main__":
    st.set_page_config(page_title="Electric Vehicles Assistant", layout="wide")
    setup_db()

    nest_asyncio.apply()
    asyncio.run(launch_bot())