-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
104 lines (86 loc) · 3.21 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
import asyncio
import logging
import streamlit as st
from base import AppSettings, Topic, render, search
logging.basicConfig(level=logging.DEBUG)
ALL_TOPICS = [
Topic(
name="xdd-covid-19",
label="COVID-19",
preset_questions_path="preset_questions/preset_covid_q.txt",
),
Topic(
name="dolomites",
label="Dolomites",
preset_questions_path="preset_questions/preset_dolomites_q.txt",
),
Topic(
name="climate-change-modeling",
label="Climate Change",
preset_questions_path="preset_questions/preset_climate_change_q.txt",
),
Topic(
name="criticalmaas",
label="CriticalMAAS",
preset_questions_path="preset_questions/preset_critical_maas.txt",
),
Topic(
name="geoarchive",
label="Geoarchive",
preset_questions_path="preset_questions/preset_geoarchive_q.txt",
),
]
app_settings = AppSettings(
title="ASK-xDD",
topics=ALL_TOPICS,
model_names=["gpt-3.5-turbo-16k", "gpt-4", "gpt-4-1106-preview"],
)
async def main(app_settings: AppSettings) -> None:
"""Streamlit app."""
st.set_page_config(page_title=app_settings.title, page_icon="📖")
st.title(app_settings.title)
# Initialize states
if "messages" not in st.session_state:
st.session_state.messages = []
if "search_settings" not in st.session_state:
st.session_state.search_settings = {}
# Sidebar
search_settings = st.session_state["search_settings"]
with st.sidebar:
st.subheader("Topic")
selected_topic = st.radio(
"Choose a topic", app_settings.topics, format_func=lambda topic: topic.label
)
search_settings["topic"] = selected_topic.name
st.subheader("Ask preset questions")
# Topic specific preset questions
preset_question = st.selectbox(
"Ask preset questions", selected_topic.preset_questions
)
run_from_preset = st.button("Run")
st.subheader("Advanced settings")
st.markdown(
"You can customize the QA system, all of these settings are available in the [API route](http://cosmos0001.chtc.wisc.edu:4502/docs) as well."
)
search_settings["model_name"] = st.radio("model", app_settings.model_names)
search_settings["top_k"] = st.number_input("retriever top-k", value=5)
search_settings["screening_top_k"] = st.number_input(
"elastic search screening phase top-k", value=100
)
search_settings["verbose"] = st.checkbox("verbose", value=True)
# Chat history
for message in st.session_state.messages:
render(message)
# Chat input
if question := st.chat_input("Ask a question.", key="question"):
try:
await search(question=question, **st.session_state.search_settings)
except Exception as e:
st.error(f"Error occur when searching relevant documents: {e}")
if run_from_preset:
try:
await search(question=preset_question, **st.session_state.search_settings)
except Exception as e:
st.error(f"Error occur when searching relevant documents: {e}")
if __name__ == "__main__":
asyncio.run(main(app_settings))