forked from WVaihau/NLSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
76 lines (53 loc) · 1.82 KB
/
controller.py
File metadata and controls
76 lines (53 loc) · 1.82 KB
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
76
import model as md
import pandas as pd
import streamlit as st
import vanna as vn
import sqlite3
@st.cache_resource(show_spinner="Getting sqlite connexion..")
def get_db_conn():
db_connexion = sqlite3.connect(
st.secrets.sqlite.file_name, check_same_thread=False
)
return db_connexion
@st.cache_data(show_spinner="Running sql query..")
def run_sql(sql: str) -> pd.DataFrame:
"""Take in SQL query as string and return DataFrame."""
conn = get_db_conn()
df = pd.read_sql_query(
sql,
conn
)
return df
def setup_agent():
"""Return vanna agent."""
vn.set_api_key(st.secrets.vanna.api_key)
vn.set_model(st.secrets.vanna.model)
vn.run_sql = run_sql
vn.run_sql_is_set = True
st.toast('Vanna Agent [OK]')
def setup():
"""Setup variables"""
st.session_state["my_question"] = None
setup_agent()
def read_readme_file():
with open(md.readme, 'r') as file:
content = file.read()
return content
@st.cache_data(show_spinner="Generating sample questions ...")
def generate_questions_cached():
return vn.generate_questions()
@st.cache_data(show_spinner="Generating SQL query ...")
def generate_sql_cached(question: str):
return vn.generate_sql(question=question)
def run_sql_cached(sql: str):
return vn.run_sql(sql=sql)
@st.cache_data(show_spinner="Generating Plotly code ...")
def generate_plotly_code_cached(question, sql, df):
code = vn.generate_plotly_code(question=question, sql=sql, df=df)
return code
@st.cache_data(show_spinner="Running Plotly code ...")
def generate_plot_cached(code, df):
return vn.get_plotly_figure(plotly_code=code, df=df)
@st.cache_data(show_spinner="Generating followup questions ...")
def generate_followup_cached(question, df):
return vn.generate_followup_questions(question=question, df=df)