Spaces:
Sleeping
Sleeping
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 | |