Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from io import BytesIO
|
|
| 8 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
|
| 9 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 10 |
from reportlab.lib import colors
|
| 11 |
-
from langchain_community.vectorstores import FAISS
|
| 12 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 13 |
from langchain.document_loaders import DataFrameLoader
|
| 14 |
from langchain.chains import RetrievalQA
|
|
@@ -27,4 +27,161 @@ llm = HuggingFaceHub(
|
|
| 27 |
huggingfacehub_api_token=hf_api_key
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
|
| 9 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 10 |
from reportlab.lib import colors
|
| 11 |
+
from langchain_community.vectorstores import FAISS
|
| 12 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 13 |
from langchain.document_loaders import DataFrameLoader
|
| 14 |
from langchain.chains import RetrievalQA
|
|
|
|
| 27 |
huggingfacehub_api_token=hf_api_key
|
| 28 |
)
|
| 29 |
|
| 30 |
+
def read_data(file):
|
| 31 |
+
"""Read Excel data into a DataFrame."""
|
| 32 |
+
try:
|
| 33 |
+
df = pd.read_excel(file)
|
| 34 |
+
return df
|
| 35 |
+
except Exception as e:
|
| 36 |
+
st.error(f"Error reading Excel file: {e}")
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
def analyze_data(df):
|
| 40 |
+
"""Analyze water level depletion and return critical areas."""
|
| 41 |
+
required_columns = ['Original Water Level in Feet', 'Present Water Level in Feet', 'Depth of Tubewell', 'Latitude', 'Longitude', 'Name of Scheme']
|
| 42 |
+
missing_columns = [col for col in required_columns if col not in df.columns]
|
| 43 |
+
|
| 44 |
+
if missing_columns:
|
| 45 |
+
raise KeyError(f"The following required columns are missing from the data: {', '.join(missing_columns)}")
|
| 46 |
+
|
| 47 |
+
df['Water Depletion'] = df['Original Water Level in Feet'] - df['Present Water Level in Feet']
|
| 48 |
+
df['Depletion Rate'] = df['Water Depletion'] / df['Depth of Tubewell']
|
| 49 |
+
|
| 50 |
+
scaler = MinMaxScaler()
|
| 51 |
+
df['Normalized Depletion'] = scaler.fit_transform(df[['Depletion Rate']])
|
| 52 |
+
|
| 53 |
+
threshold = df['Normalized Depletion'].quantile(0.9)
|
| 54 |
+
critical_areas = df[df['Normalized Depletion'] >= threshold]
|
| 55 |
+
return df, critical_areas
|
| 56 |
+
|
| 57 |
+
def generate_map(df, critical_areas):
|
| 58 |
+
"""Generate an interactive map highlighting critical areas."""
|
| 59 |
+
try:
|
| 60 |
+
base_map = folium.Map(location=[df['Latitude'].mean(), df['Longitude'].mean()], zoom_start=10)
|
| 61 |
+
marker_cluster = MarkerCluster().add_to(base_map)
|
| 62 |
+
|
| 63 |
+
for _, row in df.iterrows():
|
| 64 |
+
folium.Marker(
|
| 65 |
+
location=[row['Latitude'], row['Longitude']],
|
| 66 |
+
popup=f"Scheme: {row['Name of Scheme']}\nDepletion: {row['Water Depletion']:.2f} ft",
|
| 67 |
+
icon=folium.Icon(color='blue')
|
| 68 |
+
).add_to(marker_cluster)
|
| 69 |
+
|
| 70 |
+
for _, row in critical_areas.iterrows():
|
| 71 |
+
folium.Marker(
|
| 72 |
+
location=[row['Latitude'], row['Longitude']],
|
| 73 |
+
popup=f"Critical: {row['Name of Scheme']}\nDepletion: {row['Water Depletion']:.2f} ft",
|
| 74 |
+
icon=folium.Icon(color='red')
|
| 75 |
+
).add_to(marker_cluster)
|
| 76 |
+
|
| 77 |
+
return base_map
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Error generating map: {e}")
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
def generate_report(df, critical_areas):
|
| 83 |
+
"""Generate a PDF report."""
|
| 84 |
+
try:
|
| 85 |
+
buffer = BytesIO()
|
| 86 |
+
doc = SimpleDocTemplate(buffer)
|
| 87 |
+
styles = getSampleStyleSheet()
|
| 88 |
+
story = []
|
| 89 |
+
|
| 90 |
+
story.append(Paragraph("Water Level Depletion Report", styles['Title']))
|
| 91 |
+
story.append(Spacer(1, 12))
|
| 92 |
+
|
| 93 |
+
story.append(Paragraph(f"Total Tubewells Analyzed: {len(df)}", styles['Normal']))
|
| 94 |
+
story.append(Paragraph(f"Critical Areas Identified: {len(critical_areas)}", styles['Normal']))
|
| 95 |
+
|
| 96 |
+
data = [[f"Scheme: {row['Name of Scheme']}", f"Depletion: {row['Water Depletion']:.2f} ft"] for _, row in critical_areas.iterrows()]
|
| 97 |
+
table = Table(data)
|
| 98 |
+
table_style = [
|
| 99 |
+
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
|
| 100 |
+
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
| 101 |
+
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
| 102 |
+
('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
|
| 103 |
+
('FONTSIZE', (0, 0), (-1, -1), 10),
|
| 104 |
+
('BOTTOMPADDING', (0, 0), (-1, -1), 12),
|
| 105 |
+
('TOPPADDING', (0, 0), (-1, -1), 12),
|
| 106 |
+
('LINEBELOW', (0, 0), (-1, -1), 1, colors.black)
|
| 107 |
+
]
|
| 108 |
+
table.setStyle(TableStyle(table_style))
|
| 109 |
+
story.append(table)
|
| 110 |
+
|
| 111 |
+
doc.build(story)
|
| 112 |
+
buffer.seek(0)
|
| 113 |
+
return buffer
|
| 114 |
+
except Exception as e:
|
| 115 |
+
st.error(f"An error occurred while generating the report: {e}")
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
def store_data_in_vector_db(df):
|
| 119 |
+
"""Store data in a vector database."""
|
| 120 |
+
try:
|
| 121 |
+
loader = DataFrameLoader(df)
|
| 122 |
+
docs = loader.load()
|
| 123 |
+
embeddings = HuggingFaceEmbeddings()
|
| 124 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 125 |
+
return vectorstore
|
| 126 |
+
except Exception as e:
|
| 127 |
+
st.error(f"Error storing data in vector database: {e}")
|
| 128 |
+
return None
|
| 129 |
+
|
| 130 |
+
def question_answer(vectorstore):
|
| 131 |
+
"""Provide a Q&A interface."""
|
| 132 |
+
try:
|
| 133 |
+
retriever = vectorstore.as_retriever()
|
| 134 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
| 135 |
+
return qa_chain
|
| 136 |
+
except Exception as e:
|
| 137 |
+
st.error(f"Error initializing question-answering chain: {e}")
|
| 138 |
+
return None
|
| 139 |
+
|
| 140 |
+
def main():
|
| 141 |
+
st.title("Water Level Depletion Analysis")
|
| 142 |
+
|
| 143 |
+
uploaded_file = st.file_uploader("Upload Excel File", type="xlsx")
|
| 144 |
+
|
| 145 |
+
if uploaded_file is not None:
|
| 146 |
+
try:
|
| 147 |
+
df = read_data(uploaded_file)
|
| 148 |
+
if df is not None:
|
| 149 |
+
df, critical_areas = analyze_data(df)
|
| 150 |
+
|
| 151 |
+
st.subheader("Data Analysis")
|
| 152 |
+
st.dataframe(df)
|
| 153 |
+
|
| 154 |
+
st.subheader("Critical Areas")
|
| 155 |
+
st.dataframe(critical_areas)
|
| 156 |
+
|
| 157 |
+
st.subheader("Interactive Map")
|
| 158 |
+
map = generate_map(df, critical_areas)
|
| 159 |
+
if map:
|
| 160 |
+
folium_static(map)
|
| 161 |
+
|
| 162 |
+
st.subheader("Generate Report")
|
| 163 |
+
if st.button("Download PDF Report"):
|
| 164 |
+
report = generate_report(df, critical_areas)
|
| 165 |
+
if report:
|
| 166 |
+
st.download_button(
|
| 167 |
+
label="Download Report",
|
| 168 |
+
data=report,
|
| 169 |
+
file_name="water_depletion_report.pdf",
|
| 170 |
+
mime="application/pdf"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
vectorstore = store_data_in_vector_db(df)
|
| 174 |
+
if vectorstore:
|
| 175 |
+
st.subheader("Ask a Question")
|
| 176 |
+
user_query = st.text_input("Enter your question:")
|
| 177 |
+
if user_query:
|
| 178 |
+
qa_chain = question_answer(vectorstore)
|
| 179 |
+
if qa_chain:
|
| 180 |
+
answer = qa_chain.run(user_query)
|
| 181 |
+
st.write("Answer:", answer)
|
| 182 |
+
|
| 183 |
+
except Exception as e:
|
| 184 |
+
st.error(f"An error occurred during data processing: {e}")
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
main()
|