Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,130 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
from agent import agent, AgentState
|
| 4 |
-
|
| 5 |
-
# =========================================
|
| 6 |
-
# STREAMLIT SETTINGS
|
| 7 |
-
# =========================================
|
| 8 |
-
st.set_page_config(page_title="Weather AI Assistant", page_icon="🌤️", layout="wide")
|
| 9 |
-
|
| 10 |
-
# Custom CSS
|
| 11 |
-
st.markdown("""
|
| 12 |
-
<style>
|
| 13 |
-
body { background-color: #f0f4ff; }
|
| 14 |
-
.chat-message {
|
| 15 |
-
padding: 12px; border-radius: 12px; margin-bottom: 12px;
|
| 16 |
-
max-width: 80%; line-height: 1.5;
|
| 17 |
-
}
|
| 18 |
-
.user-msg {
|
| 19 |
-
background: #e3edff; color: #0f1e46;
|
| 20 |
-
align-self: flex-end; text-align: right; margin-left: auto;
|
| 21 |
-
}
|
| 22 |
-
.assistant-msg {
|
| 23 |
-
background: #d5e8ff; color: #0b1b33;
|
| 24 |
-
align-self: flex-start; margin-right: auto;
|
| 25 |
-
}
|
| 26 |
-
.chat-container {
|
| 27 |
-
display: flex; flex-direction: column; gap: 10px;
|
| 28 |
-
}
|
| 29 |
-
</style>
|
| 30 |
-
""", unsafe_allow_html=True)
|
| 31 |
-
|
| 32 |
-
# =========================================
|
| 33 |
-
# SESSION STATE
|
| 34 |
-
# =========================================
|
| 35 |
-
if "messages" not in st.session_state:
|
| 36 |
-
st.session_state.messages = []
|
| 37 |
-
if "last_details" not in st.session_state:
|
| 38 |
-
st.session_state.last_details = None
|
| 39 |
-
|
| 40 |
-
# =========================================
|
| 41 |
-
# MAIN TITLE
|
| 42 |
-
# =========================================
|
| 43 |
-
st.title("🌤️ Weather Data Chat Assistant")
|
| 44 |
-
st.write("Ask questions about weather data — I will generate SQL, run it, and answer.")
|
| 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 |
st.warning("No data returned from SQL.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from agent import agent, AgentState
|
| 4 |
+
|
| 5 |
+
# =========================================
|
| 6 |
+
# STREAMLIT SETTINGS
|
| 7 |
+
# =========================================
|
| 8 |
+
st.set_page_config(page_title="Weather AI Assistant", page_icon="🌤️", layout="wide")
|
| 9 |
+
|
| 10 |
+
# Custom CSS
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
body { background-color: #f0f4ff; }
|
| 14 |
+
.chat-message {
|
| 15 |
+
padding: 12px; border-radius: 12px; margin-bottom: 12px;
|
| 16 |
+
max-width: 80%; line-height: 1.5;
|
| 17 |
+
}
|
| 18 |
+
.user-msg {
|
| 19 |
+
background: #e3edff; color: #0f1e46;
|
| 20 |
+
align-self: flex-end; text-align: right; margin-left: auto;
|
| 21 |
+
}
|
| 22 |
+
.assistant-msg {
|
| 23 |
+
background: #d5e8ff; color: #0b1b33;
|
| 24 |
+
align-self: flex-start; margin-right: auto;
|
| 25 |
+
}
|
| 26 |
+
.chat-container {
|
| 27 |
+
display: flex; flex-direction: column; gap: 10px;
|
| 28 |
+
}
|
| 29 |
+
</style>
|
| 30 |
+
""", unsafe_allow_html=True)
|
| 31 |
+
|
| 32 |
+
# =========================================
|
| 33 |
+
# SESSION STATE
|
| 34 |
+
# =========================================
|
| 35 |
+
if "messages" not in st.session_state:
|
| 36 |
+
st.session_state.messages = []
|
| 37 |
+
if "last_details" not in st.session_state:
|
| 38 |
+
st.session_state.last_details = None
|
| 39 |
+
|
| 40 |
+
# =========================================
|
| 41 |
+
# MAIN TITLE
|
| 42 |
+
# =========================================
|
| 43 |
+
st.title("🌤️ Weather Data Chat Assistant")
|
| 44 |
+
st.write("Ask questions about weather data — I will generate SQL, run it, and answer.")
|
| 45 |
+
|
| 46 |
+
# =========================================
|
| 47 |
+
# RULES / CONSTRAINTS (EN)
|
| 48 |
+
# =========================================
|
| 49 |
+
ALLOWED_COUNTRIES = ["Austria", "Belgium", "Bulgaria", "Switzerland", "Czechia"]
|
| 50 |
+
START_DATE = "1980-01-01"
|
| 51 |
+
END_DATE = "2019-12-31"
|
| 52 |
+
|
| 53 |
+
st.info(
|
| 54 |
+
f"""
|
| 55 |
+
**Data constraints**
|
| 56 |
+
- **Allowed countries only:** {", ".join(ALLOWED_COUNTRIES)}
|
| 57 |
+
- **Allowed time range (UTC):** from **{START_DATE}** to **{END_DATE}**
|
| 58 |
+
|
| 59 |
+
If your question mentions another country or a date outside this range, I may not be able to answer from the database.
|
| 60 |
+
""".strip()
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# =========================================
|
| 64 |
+
# CHAT MESSAGES RENDER
|
| 65 |
+
# =========================================
|
| 66 |
+
# Спочатку малюємо історію чату
|
| 67 |
+
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
|
| 68 |
+
for role, msg in st.session_state.messages:
|
| 69 |
+
if role == "user":
|
| 70 |
+
st.markdown(f"<div class='chat-message user-msg'><b>You:</b> {msg}</div>", unsafe_allow_html=True)
|
| 71 |
+
else:
|
| 72 |
+
st.markdown(f"<div class='chat-message assistant-msg'><b>Assistant:</b> {msg}</div>", unsafe_allow_html=True)
|
| 73 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 74 |
+
|
| 75 |
+
# =========================================
|
| 76 |
+
# USER INPUT & LOGIC
|
| 77 |
+
# =========================================
|
| 78 |
+
user_input = st.chat_input("Type your question here...")
|
| 79 |
+
|
| 80 |
+
if user_input:
|
| 81 |
+
st.session_state.messages.append(("user", user_input))
|
| 82 |
+
st.rerun()
|
| 83 |
+
|
| 84 |
+
if st.session_state.messages and st.session_state.messages[-1][0] == "user":
|
| 85 |
+
last_user_msg = st.session_state.messages[-1][1]
|
| 86 |
+
|
| 87 |
+
with st.spinner("Typing..."):
|
| 88 |
+
try:
|
| 89 |
+
raw_state = agent.invoke({"question": last_user_msg})
|
| 90 |
+
|
| 91 |
+
answer = raw_state.get("answer", "No answer generated.")
|
| 92 |
+
sql_query = raw_state.get("sql_query")
|
| 93 |
+
rows = raw_state.get("rows")
|
| 94 |
+
reasoning = raw_state.get("reasoning")
|
| 95 |
+
|
| 96 |
+
st.session_state.messages.append(("assistant", answer))
|
| 97 |
+
|
| 98 |
+
st.session_state.last_details = {
|
| 99 |
+
"sql": sql_query,
|
| 100 |
+
"rows": rows,
|
| 101 |
+
"reasoning": reasoning
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
st.rerun()
|
| 105 |
+
|
| 106 |
+
except Exception as e:
|
| 107 |
+
st.session_state.messages.append(("assistant", f"❌ Error: {e}"))
|
| 108 |
+
st.rerun()
|
| 109 |
+
|
| 110 |
+
# =========================================
|
| 111 |
+
# DEBUG / DETAILS SECTION
|
| 112 |
+
# =========================================
|
| 113 |
+
if st.session_state.last_details:
|
| 114 |
+
with st.expander("🔍 See Technical Details (SQL & Data)", expanded=False):
|
| 115 |
+
details = st.session_state.last_details
|
| 116 |
+
|
| 117 |
+
if details["reasoning"]:
|
| 118 |
+
st.write("**Reasoning:**")
|
| 119 |
+
st.info(details["reasoning"])
|
| 120 |
+
|
| 121 |
+
if details["sql"]:
|
| 122 |
+
st.write("**Generated SQL:**")
|
| 123 |
+
st.code(details["sql"], language="sql")
|
| 124 |
+
|
| 125 |
+
if details["rows"]:
|
| 126 |
+
st.write(f"**Data Found ({len(details['rows'])} rows):**")
|
| 127 |
+
df = pd.DataFrame(details["rows"])
|
| 128 |
+
st.dataframe(df)
|
| 129 |
+
else:
|
| 130 |
st.warning("No data returned from SQL.")
|