cboettig commited on
Commit
44be70c
Β·
1 Parent(s): 9d04331
Files changed (2) hide show
  1. app.py +121 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ import duckdb
4
+
5
+ EXAMPLE_PROMPTS = [
6
+ "What are the unique values of Mang Type?",
7
+ "What is the mean species richness of each GAP_Sts?",
8
+ "STAT, LOC, FED, and DIST Mang types are all 'Public' land types. PVT and NGO are 'Private' land types. What is the average richness in public, private, and other land types?",
9
+ ]
10
+
11
+ TITLE = "Protected Lands SQL Assistant"
12
+ # DESCRIPTION = """[DBRX Instruct](https://huggingface.co/databricks/dbrx-instruct) is a mixture-of-experts (MoE) large language model trained by the Mosaic Research team at Databricks. Users can interact with this model in the [DBRX Playground](https://huggingface.co/spaces/databricks/dbrx-instruct), subject to the terms and conditions below.
13
+ # This demo is powered by [Databricks Foundation Model APIs](https://docs.databricks.com/en/machine-learning/foundation-models/index.html).
14
+ DESCRIPTION="""
15
+ This is a test
16
+ """
17
+
18
+
19
+
20
+ duckdb.install_extension("spatial")
21
+ duckdb.load_extension("spatial")
22
+ duckdb.install_extension("httpfs")
23
+ duckdb.load_extension("httpfs")
24
+
25
+ duckdb.sql("create or replace view pad as select * from read_parquet('https://data.source.coop/cboettig/pad-us-3/pad-mobi.parquet')")
26
+
27
+ st.title(TITLE)
28
+
29
+ # Set OpenAI API key from Streamlit secrets
30
+ client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
31
+
32
+ # Set a default model
33
+ if "openai_model" not in st.session_state:
34
+ st.session_state["openai_model"] = "gpt-3.5-turbo"
35
+ # "gpt-4"
36
+
37
+ # Initialize chat history
38
+ if "messages" not in st.session_state:
39
+ st.session_state.messages = []
40
+
41
+
42
+ setup = '''
43
+ You are a database administrator, and expert in SQL. You will be helping me write complex SQL queries. I will explain you my needs, you will generate SQL queries against my database.
44
+
45
+ My application does: Conservation prioritization of protected areas to help meet US 30x30 conservation goals.
46
+
47
+ Please reply only with the SQL code that I will need to execute. Do not include an explanation of the code.
48
+ Please reply with raw SQL, do not use markdown formatting.
49
+
50
+ The database is a POSTGIS Postgres database, please take it into consideration when generating PLSQL/SQL. Please avoid ST_Within queries if possible, because they are so slow.
51
+
52
+ I will provide you with a description of the structure of my tables. You must remember them and use them for generating SQL queries. Once you read them all, just remember them for future and say nothing else.
53
+
54
+ Here are the tables :
55
+
56
+ Table "pad"
57
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
58
+ β”‚ column_name β”‚ column_type β”‚ null β”‚ key β”‚ default β”‚ extra β”‚
59
+ β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚
60
+ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
61
+ β”‚ FID β”‚ INTEGER β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
62
+ β”‚ time β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
63
+ β”‚ rsr β”‚ DOUBLE β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
64
+ β”‚ richness β”‚ DOUBLE β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
65
+ β”‚ bucket β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
66
+ β”‚ FeatClass β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
67
+ β”‚ Mang_Name β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
68
+ β”‚ Mang_Type β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
69
+ β”‚ Des_Tp β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
70
+ β”‚ Pub_Access β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
71
+ β”‚ GAP_Sts β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
72
+ β”‚ IUCN_Cat β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
73
+ β”‚ Unit_Nm β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
74
+ β”‚ area β”‚ DOUBLE β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
75
+ β”‚ geometry β”‚ BLOB β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
76
+ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
77
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
78
+ '''
79
+
80
+ # Display chat messages from history on app rerun
81
+ #for message in st.session_state.messages:
82
+ # with st.chat_message(message["role"]):
83
+ # st.markdown(message["content"])
84
+
85
+ # Accept user input
86
+ if prompt := st.chat_input("What is the total area in each GAP_Sts?"):
87
+ # Add user message to chat history
88
+ st.session_state.messages.append({"role": "system", "content": setup})
89
+
90
+ st.session_state.messages.append({"role": "user", "content": prompt})
91
+ # Display user message in chat message container
92
+ # with st.chat_message("user"):
93
+ #st.markdown(prompt)
94
+
95
+ # Display assistant response in chat message container
96
+ with st.chat_message("assistant"):
97
+ stream = client.chat.completions.create(
98
+ model=st.session_state["openai_model"],
99
+ messages=[
100
+ {"role": m["role"], "content": m["content"]}
101
+ for m in st.session_state.messages
102
+ ],
103
+ stream=True,
104
+ )
105
+ response = st.write_stream(stream)
106
+ st.divider()
107
+ # st.write(response)
108
+ df = duckdb.sql(response).df()
109
+ st.table(df)
110
+ st.session_state.messages.append({"role": "assistant", "content": response})
111
+
112
+
113
+
114
+ with st.sidebar:
115
+ with st.container():
116
+ st.title("Examples")
117
+ for prompt in EXAMPLE_PROMPTS:
118
+ st.markdown(prompt)
119
+
120
+
121
+ # st.button(prompt, args=(prompt,)), on_click=handle_user_input)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ pandas
2
+ pydeck
3
+ pmtiles
4
+ streamlit
5
+ leafmap
6
+ ibis-framework[duckdb]
7
+ altair
8
+ openai
9
+