Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +98 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import networkx as nx
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from groq import Groq
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
GROQ_API_KEY = "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" # Replace with your Groq API key
|
| 10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
+
|
| 12 |
+
# Function to upload the dataset
|
| 13 |
+
def upload_file():
|
| 14 |
+
uploaded_file = st.file_uploader("Upload your Excel dataset", type=["xlsx"])
|
| 15 |
+
if uploaded_file is not None:
|
| 16 |
+
return pd.read_excel(uploaded_file)
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# Function to construct the graph and visualize
|
| 20 |
+
def construct_graph(dataset):
|
| 21 |
+
G = nx.DiGraph()
|
| 22 |
+
|
| 23 |
+
# Add nodes and edges based on the dataset
|
| 24 |
+
for _, row in dataset.iterrows():
|
| 25 |
+
# Replace with your column names if necessary
|
| 26 |
+
G.add_edge(row['Vendor Name'], row['Meter Number'], weight=row['Consumption (HCF)'])
|
| 27 |
+
|
| 28 |
+
# Visualize the graph (water distribution network)
|
| 29 |
+
plt.figure(figsize=(12, 8))
|
| 30 |
+
pos = nx.spring_layout(G) # Layout for visualization
|
| 31 |
+
nx.draw(G, pos, with_labels=True, node_color='skyblue', node_size=1500, edge_color='gray')
|
| 32 |
+
|
| 33 |
+
# Render the graph as an image and return it
|
| 34 |
+
buf = BytesIO()
|
| 35 |
+
plt.savefig(buf, format="png")
|
| 36 |
+
buf.seek(0)
|
| 37 |
+
return buf
|
| 38 |
+
|
| 39 |
+
# Function to query Groq for optimization suggestions based on serial number
|
| 40 |
+
def query_groq(serial_number):
|
| 41 |
+
chat_completion = client.chat.completions.create(
|
| 42 |
+
messages=[
|
| 43 |
+
{
|
| 44 |
+
"role": "user",
|
| 45 |
+
"content": f"Optimize water distribution network for Serial Number {serial_number} to minimize consumption fluctuations."
|
| 46 |
+
}
|
| 47 |
+
],
|
| 48 |
+
model="llama3-8b-8192",
|
| 49 |
+
)
|
| 50 |
+
return chat_completion.choices[0].message.content
|
| 51 |
+
|
| 52 |
+
# Streamlit UI
|
| 53 |
+
st.set_page_config(page_title="Water Distribution Network Optimization", page_icon="💧")
|
| 54 |
+
|
| 55 |
+
# Title and description
|
| 56 |
+
st.title("💧 Intelligent Water Distribution Network Optimization")
|
| 57 |
+
st.write("Upload your water distribution dataset and optimize the network based on specific serial numbers.")
|
| 58 |
+
|
| 59 |
+
# Step 1: Upload file
|
| 60 |
+
dataset = upload_file()
|
| 61 |
+
|
| 62 |
+
if dataset is not None:
|
| 63 |
+
# Show uploaded dataset details
|
| 64 |
+
st.subheader("Dataset Preview:")
|
| 65 |
+
st.write(dataset.head())
|
| 66 |
+
|
| 67 |
+
# Check if there's a serial number column, add if missing
|
| 68 |
+
if 'Serial Number' not in dataset.columns:
|
| 69 |
+
dataset['Serial Number'] = range(1, len(dataset) + 1) # Adding Serial Number column
|
| 70 |
+
|
| 71 |
+
# Step 2: Select Serial Number for analysis
|
| 72 |
+
serial_number = st.number_input("Enter Serial Number for analysis", min_value=1, max_value=int(dataset['Serial Number'].max()), step=1)
|
| 73 |
+
|
| 74 |
+
if serial_number in dataset['Serial Number'].values:
|
| 75 |
+
# Filter the dataset for the selected serial number
|
| 76 |
+
filtered_data = dataset[dataset['Serial Number'] == serial_number]
|
| 77 |
+
st.write(f"Filtered data for Serial Number {serial_number}:", filtered_data)
|
| 78 |
+
|
| 79 |
+
# Step 3: Display the graph
|
| 80 |
+
st.subheader(f"Graph for Serial Number {serial_number}")
|
| 81 |
+
graph_image = construct_graph(filtered_data)
|
| 82 |
+
st.image(graph_image, caption=f"Water Distribution Network for Serial Number {serial_number}", use_column_width=True)
|
| 83 |
+
|
| 84 |
+
# Step 4: Query Groq for optimization
|
| 85 |
+
if st.button(f"Optimize for Serial Number {serial_number}"):
|
| 86 |
+
optimization_response = query_groq(serial_number)
|
| 87 |
+
st.subheader("Optimization Suggestions:")
|
| 88 |
+
st.write(optimization_response)
|
| 89 |
+
|
| 90 |
+
else:
|
| 91 |
+
st.info("Please upload a dataset to get started.")
|
| 92 |
+
|
| 93 |
+
# Footer
|
| 94 |
+
st.markdown("""
|
| 95 |
+
---
|
| 96 |
+
Developed by [Your Name].
|
| 97 |
+
For more information, visit our [website](https://example.com).
|
| 98 |
+
""")
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
networkx
|
| 4 |
+
matplotlib
|
| 5 |
+
plotly
|
| 6 |
+
faiss-cpu
|
| 7 |
+
groq
|
| 8 |
+
openpyxl
|