ShuklaShreyansh commited on
Commit
5718192
1 Parent(s): 7dc6438

Upload app.py

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