Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import geopandas as gpd
|
3 |
+
import shapely
|
4 |
+
from shapely.geometry import Polygon
|
5 |
+
from io import BytesIO
|
6 |
+
from transformers import pipeline, RagTokenizer, RagRetriever, RagSequenceForGeneration
|
7 |
+
import os
|
8 |
+
import zipfile
|
9 |
+
import json
|
10 |
+
import xml.etree.ElementTree as ET
|
11 |
+
from shapely.ops import transform
|
12 |
+
from pyproj import Proj, transform as proj_transform
|
13 |
+
|
14 |
+
# Load pre-trained Hugging Face RAG model
|
15 |
+
def load_rag_model():
|
16 |
+
tokenizer = RagTokenizer.from_pretrained("facebook/rag-sequence-nq")
|
17 |
+
retriever = RagRetriever.from_pretrained("facebook/rag-sequence-nq")
|
18 |
+
model = RagSequenceForGeneration.from_pretrained("facebook/rag-sequence-nq")
|
19 |
+
return tokenizer, retriever, model
|
20 |
+
|
21 |
+
tokenizer, retriever, model = load_rag_model()
|
22 |
+
|
23 |
+
# Function to load shapefile (SHP, DBF, etc.)
|
24 |
+
def load_shapefile(uploaded_file):
|
25 |
+
if uploaded_file is not None:
|
26 |
+
if uploaded_file.name.endswith('.zip'):
|
27 |
+
with zipfile.ZipFile(uploaded_file, 'r') as zip_ref:
|
28 |
+
zip_ref.extractall("extracted_files")
|
29 |
+
shp_file = [f for f in os.listdir("extracted_files") if f.endswith(".shp")][0]
|
30 |
+
shapefile_path = os.path.join("extracted_files", shp_file)
|
31 |
+
else:
|
32 |
+
shapefile_path = uploaded_file
|
33 |
+
return gpd.read_file(shapefile_path)
|
34 |
+
return None
|
35 |
+
|
36 |
+
# Function to get land summary based on a KML or KMZ file
|
37 |
+
def get_land_summary_by_kml(kml_file):
|
38 |
+
tree = ET.parse(kml_file)
|
39 |
+
root = tree.getroot()
|
40 |
+
ns = {'kml': 'http://www.opengis.net/kml/2.2'}
|
41 |
+
coordinates = []
|
42 |
+
for coord in root.findall('.//kml:coordinates', ns):
|
43 |
+
coords = coord.text.strip().split()
|
44 |
+
coordinates.extend([(float(x.split(',')[0]), float(x.split(',')[1])) for x in coords])
|
45 |
+
|
46 |
+
# Create a polygon from coordinates
|
47 |
+
poly = Polygon(coordinates)
|
48 |
+
return poly.area
|
49 |
+
|
50 |
+
# Function to summarize floodland areas, acreage, and usable land
|
51 |
+
def summarize_land_data(shapefile_data):
|
52 |
+
# Example: Getting floodland areas, acreage, and usable land from shapefile data
|
53 |
+
total_area = shapefile_data['geometry'].area.sum() # Sum of all area
|
54 |
+
usable_land = shapefile_data[shapefile_data['use_type'] == 'Usable Land'] # Filter usable land
|
55 |
+
usable_land_area = usable_land['geometry'].area.sum() # Area of usable land
|
56 |
+
return total_area, usable_land_area
|
57 |
+
|
58 |
+
# Function to generate a response using RAG model
|
59 |
+
def generate_rag_response(query):
|
60 |
+
inputs = tokenizer(query, return_tensors="pt")
|
61 |
+
retriever_outputs = retriever(inputs['input_ids'], return_tensors="pt")
|
62 |
+
generated_ids = model.generate(input_ids=inputs['input_ids'],
|
63 |
+
context_input_ids=retriever_outputs['context_input_ids'],
|
64 |
+
context_attention_mask=retriever_outputs['context_attention_mask'])
|
65 |
+
response = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
66 |
+
return response
|
67 |
+
|
68 |
+
# Streamlit app interface
|
69 |
+
def main():
|
70 |
+
st.title("Geospatial Data Summary and Chatbot")
|
71 |
+
|
72 |
+
# Buttons for interaction
|
73 |
+
summary_button = st.button("Get Land Summary")
|
74 |
+
chatbot_button = st.button("Chat with the Bot")
|
75 |
+
|
76 |
+
# Upload file option
|
77 |
+
uploaded_file = st.file_uploader("Upload SHP/DBF/ZIP file", type=["zip", "shp", "dbf"])
|
78 |
+
kml_file = st.file_uploader("Upload KML or KMZ boundary file", type=["kml", "kmz"])
|
79 |
+
|
80 |
+
if summary_button:
|
81 |
+
if uploaded_file is not None:
|
82 |
+
# Load the shapefile
|
83 |
+
shapefile_data = load_shapefile(uploaded_file)
|
84 |
+
if shapefile_data is not None:
|
85 |
+
total_area, usable_land_area = summarize_land_data(shapefile_data)
|
86 |
+
st.write(f"Total floodland area: {total_area:.2f} sq meters")
|
87 |
+
st.write(f"Usable land area: {usable_land_area:.2f} sq meters")
|
88 |
+
elif kml_file is not None:
|
89 |
+
# Process KML for land summary
|
90 |
+
land_area = get_land_summary_by_kml(kml_file)
|
91 |
+
st.write(f"Floodland area in KML boundary: {land_area:.2f} sq meters")
|
92 |
+
else:
|
93 |
+
st.write("Please upload either a shapefile or KML/KMZ file.")
|
94 |
+
|
95 |
+
elif chatbot_button:
|
96 |
+
if uploaded_file is not None or kml_file is not None:
|
97 |
+
query = st.text_input("Ask the bot a question about the data:")
|
98 |
+
if query:
|
99 |
+
answer = generate_rag_response(query)
|
100 |
+
st.write(answer)
|
101 |
+
else:
|
102 |
+
st.write("Please upload a file before interacting with the chatbot.")
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
main()
|