razaali10 commited on
Commit
9b839a8
·
verified ·
1 Parent(s): 991494f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -18
app.py CHANGED
@@ -1,19 +1,67 @@
1
  import streamlit as st
2
- from tsnet_agents import run_agentic_query
3
- import base64
4
- from PIL import Image
5
- import io
6
-
7
- st.set_page_config(page_title="TSNet + LLM Final App", layout="wide")
8
- st.title("🌊 TSNet Transient Simulation with LLM + Plot + RAG")
9
-
10
- query = st.text_area("Ask a question or describe a valve closure:")
11
- if st.button("Run"):
12
- response = run_agentic_query(query)
13
- if response["type"] == "plot":
14
- img_data = base64.b64decode(response["image"])
15
- image = Image.open(io.BytesIO(img_data))
16
- st.image(image, caption="Simulation Result")
17
- else:
18
- st.markdown("**LLM Answer:**")
19
- st.write(response["content"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import tempfile
3
+ import matplotlib.pyplot as plt
4
+ import pickle
5
+
6
+ st.set_page_config(page_title="TSNet Transient Simulator", layout="wide")
7
+ st.title("🌊 TSNet Transient Simulation App")
8
+
9
+ # Step 1: Upload EPANET .inp File
10
+ st.header("1. Upload EPANET .inp File")
11
+ uploaded_file = st.file_uploader("Upload your .inp file", type=["inp"])
12
+
13
+ if uploaded_file is not None:
14
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".inp") as temp_inp:
15
+ temp_inp.write(uploaded_file.read())
16
+ inp_path = temp_inp.name
17
+
18
+ st.success("File uploaded and saved.")
19
+ st.header("2. Set Simulation Parameters")
20
+
21
+ wave_speed = st.number_input("Wave Speed (m/s)", value=1000.0)
22
+ dt = st.number_input("Time Step (s)", value=0.01)
23
+ duration = st.number_input("Simulation Duration (s)", value=10.0)
24
+
25
+ if st.button("Run TSNet Simulation"):
26
+ import tsnet
27
+ from tsnet.network.model import TransientModel
28
+ from tsnet.simulation import MOCSimulator
29
+
30
+ net = TransientModel(inp_path)
31
+ net.set_wavespeed(wave_speed)
32
+ net.set_time(duration, dt)
33
+
34
+ for pipe in net.pipes():
35
+ if not hasattr(pipe, 'initial_head'):
36
+ pipe.initial_head = (pipe.start_node.head + pipe.end_node.head) / 2
37
+
38
+ sim = MOCSimulator(net, 'results')
39
+ with open("results.obj", "wb") as f:
40
+ pickle.dump(sim, f)
41
+
42
+ st.session_state["sim_results"] = "results.obj"
43
+ st.success("Simulation complete.")
44
+
45
+ if "sim_results" in st.session_state:
46
+ st.header("3. Plot Results")
47
+ sim = pickle.load(open(st.session_state["sim_results"], "rb"))
48
+
49
+ node_id = st.selectbox("Select Node to Plot Head", sim.node_name_list)
50
+ pipe_id = st.selectbox("Select Pipe to Plot Flow", sim.pipe_name_list)
51
+
52
+ head = sim.get_node_head(node_id)
53
+ flow = sim.get_link_flow(pipe_id)
54
+
55
+ fig1, ax1 = plt.subplots()
56
+ ax1.plot(sim.Time, head)
57
+ ax1.set_title(f"Head at Node {node_id}")
58
+ ax1.set_xlabel("Time (s)")
59
+ ax1.set_ylabel("Head (m)")
60
+ st.pyplot(fig1)
61
+
62
+ fig2, ax2 = plt.subplots()
63
+ ax2.plot(sim.Time, flow)
64
+ ax2.set_title(f"Flow in Pipe {pipe_id}")
65
+ ax2.set_xlabel("Time (s)")
66
+ ax2.set_ylabel("Flow (L/s)")
67
+ st.pyplot(fig2)