|
|
import streamlit as st |
|
|
import os |
|
|
import pandas as pd |
|
|
import networkx as nx |
|
|
import matplotlib.pyplot as plt |
|
|
from groq import Groq |
|
|
from io import BytesIO |
|
|
|
|
|
client = Groq( |
|
|
api_key=st.secrets["GROQ_API_KEY"] |
|
|
) |
|
|
|
|
|
def upload_file(): |
|
|
uploaded_file = st.file_uploader("Upload your Excel dataset", type=["xlsx"]) |
|
|
if uploaded_file is not None: |
|
|
return pd.read_excel(uploaded_file) |
|
|
return None |
|
|
|
|
|
|
|
|
def construct_graph(dataset): |
|
|
G = nx.DiGraph() |
|
|
|
|
|
|
|
|
for _, row in dataset.iterrows(): |
|
|
|
|
|
G.add_edge(row['Vendor Name'], row['Meter Number'], weight=row['Consumption (HCF)']) |
|
|
|
|
|
|
|
|
plt.figure(figsize=(12, 8)) |
|
|
pos = nx.spring_layout(G) |
|
|
nx.draw(G, pos, with_labels=True, node_color='skyblue', node_size=1500, edge_color='gray') |
|
|
|
|
|
|
|
|
buf = BytesIO() |
|
|
plt.savefig(buf, format="png") |
|
|
buf.seek(0) |
|
|
return buf |
|
|
|
|
|
|
|
|
def query_groq(serial_number): |
|
|
chat_completion = client.chat.completions.create( |
|
|
messages=[ |
|
|
{ |
|
|
"role": "user", |
|
|
"content": f"Optimize water distribution network for Serial Number {serial_number} to minimize consumption fluctuations." |
|
|
} |
|
|
], |
|
|
model="llama3-8b-8192", |
|
|
) |
|
|
return chat_completion.choices[0].message.content |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Water Distribution Network Optimization", page_icon="💧") |
|
|
|
|
|
|
|
|
st.title("💧 Intelligent Water Distribution Network Optimization") |
|
|
st.write("Upload your water distribution dataset and optimize the network based on specific serial numbers.") |
|
|
|
|
|
|
|
|
dataset = upload_file() |
|
|
|
|
|
if dataset is not None: |
|
|
|
|
|
st.subheader("Dataset Preview:") |
|
|
st.write(dataset.head()) |
|
|
|
|
|
|
|
|
if 'Serial Number' not in dataset.columns: |
|
|
dataset['Serial Number'] = range(1, len(dataset) + 1) |
|
|
|
|
|
|
|
|
serial_number = st.number_input("Enter Serial Number for analysis", min_value=1, max_value=int(dataset['Serial Number'].max()), step=1) |
|
|
|
|
|
if serial_number in dataset['Serial Number'].values: |
|
|
|
|
|
filtered_data = dataset[dataset['Serial Number'] == serial_number] |
|
|
st.write(f"Filtered data for Serial Number {serial_number}:", filtered_data) |
|
|
|
|
|
|
|
|
st.subheader(f"Graph for Serial Number {serial_number}") |
|
|
graph_image = construct_graph(filtered_data) |
|
|
st.image(graph_image, caption=f"Water Distribution Network for Serial Number {serial_number}", use_column_width=True) |
|
|
|
|
|
|
|
|
if st.button(f"Optimize for Serial Number {serial_number}"): |
|
|
optimization_response = query_groq(serial_number) |
|
|
st.subheader("Optimization Suggestions:") |
|
|
st.write(optimization_response) |
|
|
|
|
|
else: |
|
|
st.info("Please upload a dataset to get started.") |
|
|
|
|
|
|
|
|
st.markdown(""" |
|
|
--- |
|
|
Developed by [Your Name]. |
|
|
For more information, visit our [website](https://example.com). |
|
|
""") |