-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOpenBB-GPT.py
More file actions
87 lines (77 loc) · 2 KB
/
Copy pathOpenBB-GPT.py
File metadata and controls
87 lines (77 loc) · 2 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
77
78
79
80
81
82
83
84
85
86
87
import streamlit as st
from openbb_terminal.sdk import openbb
import pandas as pd
import plotly.graph_objects as go
import sqlite3
from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
st.title("Dashboard powered by OpenBB :butterfly: & ChatGPT :robot_face:")
ticker = st.selectbox(
label = "Choose a dataset to load",
options = (
"anes96",
"cancer",
"ccard",
"cancer_china",
"co2",
"committee",
"copper",
"cpunish",
"danish_data",
"elnino",
"engel",
"fair",
"fertility",
"grunfeld",
"heart",
"interest_inflation",
"longley",
"macrodata",
"modechoice",
"nile",
"randhie",
"scotland",
"spector",
"stackloss",
"star98",
"statecrim",
"strikes",
"sunspots",
"wage_panel"
)
)
uploaded_file = st.file_uploader("...Or load your own custom CSV dataset")
table_name = 'statesdb'
uri = "file:memory?cache=shared&mode=memory"
openai_key = st.secrets["OPENAI_KEY"]
@st.cache_data()
def load(ticker):
df = openbb.econometrics.load(ticker)
return df
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.write(df)
else:
df = load(ticker)
st.write(df)
query = st.text_input(
label = "Any questions?",
help = "Ask any question based on the loaded dataset")
conn = sqlite3.connect(uri, uri=True)
df.to_sql(table_name, conn, if_exists = 'replace', index = False)
db_eng = create_engine(
url = 'sqlite:///file:memdb1?mode=memory&cache=shared',
poolclass = StaticPool,
creator = lambda: conn
)
db = SQLDatabase(engine = db_eng)
lang_model = OpenAI(
openai_api_key = openai_key,
temperature = 0,
max_tokens = 300
)
db_chain = SQLDatabaseChain(llm = lang_model, database = db, verbose = True)
if query:
response = db_chain.run(query)
st.write(response)