Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +66 -64
src/streamlit_app.py
CHANGED
|
@@ -1,68 +1,70 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
-
from huggingface_hub import upload_file
|
| 6 |
-
from streamlit_js_eval import streamlit_js_eval
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
st.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
st.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
import geocoder
|
| 4 |
+
import pandas as pd
|
| 5 |
from datetime import datetime
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Create submissions folder if not exists
|
| 8 |
+
UPLOAD_FOLDER = "submissions"
|
| 9 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
# CSV to track uploads
|
| 12 |
+
CSV_LOG = "submissions_log.csv"
|
| 13 |
+
|
| 14 |
+
# Title
|
| 15 |
+
st.title("πΈ Upload Image with Geo Coordinates")
|
| 16 |
+
|
| 17 |
+
# File uploader
|
| 18 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 19 |
+
|
| 20 |
+
# Detect location on button click
|
| 21 |
+
if st.button("π Detect Location"):
|
| 22 |
+
g = geocoder.ip('me')
|
| 23 |
+
if g.ok:
|
| 24 |
+
lat, lon = g.latlng
|
| 25 |
+
st.session_state['location'] = {'lat': lat, 'lon': lon}
|
| 26 |
+
st.success(f"π Location Detected: ({lat}, {lon})")
|
| 27 |
+
else:
|
| 28 |
+
st.error("β Failed to detect location")
|
| 29 |
+
|
| 30 |
+
# Show current detected coordinates
|
| 31 |
+
if 'location' in st.session_state:
|
| 32 |
+
st.write("**Latitude:**", st.session_state['location']['lat'])
|
| 33 |
+
st.write("**Longitude:**", st.session_state['location']['lon'])
|
| 34 |
+
|
| 35 |
+
# Save file with timestamp and location
|
| 36 |
+
if uploaded_file and 'location' in st.session_state:
|
| 37 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 38 |
+
filename = f"{timestamp}_{uploaded_file.name}"
|
| 39 |
+
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
| 40 |
+
|
| 41 |
+
# Save image
|
| 42 |
+
with open(file_path, "wb") as f:
|
| 43 |
+
f.write(uploaded_file.getbuffer())
|
| 44 |
+
|
| 45 |
+
st.success(f"β
Saved file as {filename}")
|
| 46 |
+
|
| 47 |
+
# Log to CSV
|
| 48 |
+
entry = {
|
| 49 |
+
"timestamp": timestamp,
|
| 50 |
+
"filename": filename,
|
| 51 |
+
"latitude": st.session_state['location']['lat'],
|
| 52 |
+
"longitude": st.session_state['location']['lon']
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# Append to log
|
| 56 |
+
if os.path.exists(CSV_LOG):
|
| 57 |
+
df = pd.read_csv(CSV_LOG)
|
| 58 |
+
df = pd.concat([df, pd.DataFrame([entry])], ignore_index=True)
|
| 59 |
+
else:
|
| 60 |
+
df = pd.DataFrame([entry])
|
| 61 |
+
df.to_csv(CSV_LOG, index=False)
|
| 62 |
+
|
| 63 |
+
# Show the saved log
|
| 64 |
+
st.subheader("π Upload History")
|
| 65 |
+
st.dataframe(df)
|
| 66 |
+
else:
|
| 67 |
+
if not uploaded_file:
|
| 68 |
+
st.info("π Please upload an image.")
|
| 69 |
+
elif 'location' not in st.session_state:
|
| 70 |
+
st.info("π Click 'Detect Location' first.")
|