Marik1337 commited on
Commit
6c2fd34
·
verified ·
1 Parent(s): e158292

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -112
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
- # CHAT MESSAGES RENDER
48
- # =========================================
49
- # Спочатку малюємо історію чату
50
- st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
51
- for role, msg in st.session_state.messages:
52
- if role == "user":
53
- st.markdown(f"<div class='chat-message user-msg'><b>You:</b> {msg}</div>", unsafe_allow_html=True)
54
- else:
55
- st.markdown(f"<div class='chat-message assistant-msg'><b>Assistant:</b> {msg}</div>", unsafe_allow_html=True)
56
- st.markdown("</div>", unsafe_allow_html=True)
57
-
58
- # =========================================
59
- # USER INPUT & LOGIC
60
- # =========================================
61
- user_input = st.chat_input("Type your question here...")
62
-
63
- if user_input:
64
- st.session_state.messages.append(("user", user_input))
65
- st.rerun()
66
-
67
- if st.session_state.messages and st.session_state.messages[-1][0] == "user":
68
- last_user_msg = st.session_state.messages[-1][1]
69
-
70
- with st.spinner("Thinking and querying database..."):
71
- try:
72
- raw_state = agent.invoke({"question": last_user_msg})
73
-
74
- answer = raw_state.get("answer", "No answer generated.")
75
- sql_query = raw_state.get("sql_query")
76
- rows = raw_state.get("rows")
77
- reasoning = raw_state.get("reasoning")
78
-
79
- st.session_state.messages.append(("assistant", answer))
80
-
81
- st.session_state.last_details = {
82
- "sql": sql_query,
83
- "rows": rows,
84
- "reasoning": reasoning
85
- }
86
-
87
- st.rerun()
88
-
89
- except Exception as e:
90
- st.session_state.messages.append(("assistant", f"❌ Error: {e}"))
91
- st.rerun()
92
-
93
- # =========================================
94
- # DEBUG / DETAILS SECTION
95
- # =========================================
96
- if st.session_state.last_details:
97
- with st.expander("🔍 See Technical Details (SQL & Data)", expanded=False):
98
- details = st.session_state.last_details
99
-
100
- if details["reasoning"]:
101
- st.write("**Reasoning:**")
102
- st.info(details["reasoning"])
103
-
104
- if details["sql"]:
105
- st.write("**Generated SQL:**")
106
- st.code(details["sql"], language="sql")
107
-
108
- if details["rows"]:
109
- st.write(f"**Data Found ({len(details['rows'])} rows):**")
110
- df = pd.DataFrame(details["rows"])
111
- st.dataframe(df)
112
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")