forked from earth-genome/ChatGeoPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
144 lines (112 loc) · 6.68 KB
/
app.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import folium
import openai
import requests
import streamlit as st
import tiktoken
import streamlit_folium
import os
# Define the Overpass API endpoint URL
OVERPASS_API_URL = "https://overpass-api.de/api/interpreter"
# Set the OpenAI API key
openai.api_key = os.environ.get("OPENAI_API_KEY")
# Chat template string, to be used for generating Overpass API queries
CHAT_TEMPLATE = """Assistant is an expert OpenStreetMap Overpass API assistant.
For each question that the user supplies, the assistant will reply with:
(1) A statement consenting to help.
(2) The text of a valid Overpass API query that can be used to answer the question. The query should be enclosed by three backticks on new lines, denoting that it is a code block.
(3) A fun fact relating to the question, or a very funny joke or pun related to the question. The joke or pun should also relate to maps, geospatial tech, geography or similar concepts. There is no need to label the fact, joke, or pun.
Assistant has a whimsical personality. Assistant will reply with a geospatial themed joke or a pun if the user asks a question that is not relevant to the Overpass API.
{history}
Human: {human_input}
Assistant:"""
# Reader template string, to be used for generating text responses drawing on Overpass API responses
READER_TEMPLATE = """Read the following Overpass API response carefully. Use the information in it to answer the prompt "{prompt}" Your answer should not mention the words "API" or "Overpass." Your answer should sound like it was spoken by someone with personal knowledge of the question's answer. Your answer should be very concise, but also informative and fun. Format any names or places you get from the API response as bold text in Markdown.
Overpass API Response:
Answer: {response}
"""
# Create a tokenizer
ENC = tiktoken.encoding_for_model("text-davinci-003")
# Define a function to query the Overpass API and return the JSON response
def query_overpass(query):
payload = {"data": query}
response = requests.post(OVERPASS_API_URL, data=payload)
return response.json()
# Define the Streamlit app
def main():
# Set the app title and description
st.set_page_config(layout="wide", page_title="OSM Overpass Query App", page_icon=":earth_africa:")
st.title("Chat:earth_africa:")
st.write("Hello! :wave: I'm Chat:earth_africa:, a Geospatial AI assistant. For any question you ask in the textbox below, "
"I'll generate an OpenStreetMap Overpass query to answer your question, and plot the results on a map. "
"I'll remember our conversation, so feel free to ask follow ups. I'm also a geospatial themed joke and pun expert. :smile:")
# Define the layout of the app
col1, col2 = st.columns([1, 1])
if 'chat_history' not in st.session_state:
st.session_state.chat_history = ""
if 'overpass_query' not in st.session_state:
st.session_state.overpass_query = None
if 'prompt_history' not in st.session_state:
st.session_state.prompt_history = ""
# Define the query input box in the left pane
with col1:
chat = st.text_area("What can I help you find? :thinking_face:")
if st.button("Ask"):
response = openai.Completion.create(
model="text-davinci-003",
prompt=CHAT_TEMPLATE.format(history=st.session_state.chat_history, human_input=chat),
temperature=0,
max_tokens=516,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# Display the response as pure text
st.write(response["choices"][0]["text"])
# Update the history string
st.session_state.chat_history = st.session_state.chat_history + f"Human: {chat}\nAssistant: {response['choices'][0]['text']}\n"
# Update the prompt history string
st.session_state.prompt_history = st.session_state.prompt_history + f"{chat} "
# Update the Overpass query. The query is enclosed by three backticks, denoting that is a code block.
# does the response contain a query? If so, update the query
if "```" in response["choices"][0]["text"]:
st.session_state.overpass_query = response["choices"][0]["text"].split("```")[1]
else:
st.session_state.overpass_query = None
# Define the query button in the left pane
with col2:
if st.session_state.overpass_query:
# Query the Overpass API
response = query_overpass(st.session_state.overpass_query)
# Check if the response is valid
if "elements" in response and len(response["elements"]) > 0:
# Create a new Folium map in the right pane
m = folium.Map(location=[response["elements"][0]["lat"], response["elements"][0]["lon"]], zoom_start=11)
# Add markers for each element in the response
for element in response["elements"]:
if "lat" in element and "lon" in element:
folium.Marker([element["lat"], element["lon"]]).add_to(m)
# Display the map
streamlit_folium.folium_static(m)
# If the request for summary of the API response is shorter than 1500 tokens,
# use the Reader model to generate a response
query_reader_prompt = READER_TEMPLATE.format(prompt=st.session_state.prompt_history,
response=str(response))
query_reader_prompt_tokens = len(ENC.encode(query_reader_prompt))
if query_reader_prompt_tokens < 1500:
response = openai.Completion.create(
model="text-davinci-003",
prompt=query_reader_prompt,
temperature=0.5,
max_tokens=2047 - query_reader_prompt_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# Display the response as pure text
st.write(response["choices"][0]["text"])
else:
st.write("The API response is too long for me to read. Try asking for something slightly more specific! :smile:")
else:
st.write("No results found :cry:")
if __name__ == "__main__":
main()