Spaces:
Sleeping
Sleeping
File size: 5,670 Bytes
5718192 |
1 2 3 4 5 6 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import streamlit as st
import hashlib
# Helper function to hash passwords
def hash_password(password):
return hashlib.sha256(str.encode(password)).hexdigest()
# Initialize session state for login and signup data
if 'logged_in' not in st.session_state:
st.session_state['logged_in'] = False
if 'users' not in st.session_state:
st.session_state['users'] = {}
# Function to handle login
def handle_login(username, password):
if username in st.session_state['users']:
hashed_password = hash_password(password)
if st.session_state['users'][username] == hashed_password:
st.session_state['logged_in'] = True
st.session_state['username'] = username
st.success(f"Successfully logged in as {username}")
st.rerun() # Redirect to the main page
else:
st.error("Incorrect password")
else:
st.error("Username does not exist")
# Function to handle logout
def handle_logout():
st.session_state['logged_in'] = False
st.session_state['username'] = None
st.rerun() # Rerun to show the login page
# Signup functionality
def handle_signup(username, password, confirm_password):
if username in st.session_state['users']:
st.error("Username already exists! Please choose another.")
elif password != confirm_password:
st.error("Passwords do not match!")
else:
st.session_state['users'][username] = hash_password(password)
st.success(f"User {username} successfully signed up! You can now log in.")
def main_page():
st.title("Main Page")
st.write(f"Welcome, {st.session_state['username']}!")
# Create multiple tabs for different functionalities
tab1, tab2, tab3 = st.tabs(["Predict and Save in Blockchain", "Retrieve from Blockchain", "Logout"])
# Tab 1: Predict and Save in Blockchain
with tab1:
st.header("Predict and Save in Blockchain")
# Multiple input fields for crop data
nitrogen = st.number_input("Nitrogen (N) content in soil (mg/kg)", min_value=0, step=1)
phosphorus = st.number_input("Phosphorus (P) content in soil (mg/kg)", min_value=0, step=1)
potassium = st.number_input("Potassium (K) content in soil (mg/kg)", min_value=0, step=1)
soil_type = st.selectbox("Select Soil Type", ["Clay", "Loamy", "Sandy", "Silt"])
temperature = st.number_input("Temperature (°C)", min_value=-50.0, max_value=60.0, step=0.1)
rainfall = st.number_input("Rainfall (mm)", min_value=0.0, step=0.1)
humidity = st.number_input("Humidity (%)", min_value=0, max_value=100, step=1)
pH = st.number_input("Soil pH", min_value=0.0, max_value=14.0, step=0.1)
# Button to make predictions and save to blockchain
if st.button("Predict and Save"):
# Check if all required fields are filled
if nitrogen and phosphorus and potassium and temperature and rainfall and humidity and pH:
# Placeholder for prediction logic (can use a machine learning model here)
prediction_result = f"Best crop based on N: {nitrogen}, P: {phosphorus}, K: {potassium}, Soil Type: {soil_type}, Temp: {temperature}, Rainfall: {rainfall}, Humidity: {humidity}, pH: {pH}"
# Display the prediction
st.success(prediction_result)
# Save the prediction result to the blockchain (replace with actual blockchain logic)
st.session_state['blockchain'].add_block(prediction_result)
st.success("Prediction saved in the blockchain!")
else:
st.error("Please fill out all the fields!")
# Tab 2: Retrieve from Blockchain
with tab2:
st.header("Retrieve Data from Blockchain")
if st.button("Retrieve Blockchain Data"):
# Assuming blockchain retrieval logic here
st.success("Blockchain data retrieved!")
# Display blockchain data (assuming JSON format for simplicity)
st.json({"block_1": "Sample Data", "block_2": "More Data"}) # Example data
# Tab 3: Logout
with tab3:
st.header("Logout")
if st.button("Logout"):
handle_logout()
# Define a simple logout function
def handle_logout():
st.session_state['logged_in'] = False
st.rerun() # Rerun to show the login page again
# Login/signup interface
def login_page():
st.title("Crop Recomendation")
# Tabs for login and signup
tab1, tab2 = st.tabs(["Login", "Signup"])
# Login Tab
with tab1:
st.header("Login")
login_username = st.text_input("Username", key="login_username")
login_password = st.text_input("Password", type="password", key="login_password")
if st.button("Login"):
handle_login(login_username, login_password)
# Signup Tab
with tab2:
st.header("Signup")
signup_username = st.text_input("Create Username", key="signup_username")
signup_password = st.text_input("Create Password", type="password", key="signup_password")
signup_confirm_password = st.text_input("Confirm Password", type="password", key="signup_confirm_password")
if st.button("Signup"):
handle_signup(signup_username, signup_password, signup_confirm_password)
# Conditional display based on login state
if st.session_state['logged_in']:
main_page() # Show main page with logout
else:
login_page() # Show login/signup page
|