ojas121 commited on
Commit
25226bc
Β·
verified Β·
1 Parent(s): 7c95a67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -92
app.py CHANGED
@@ -2,9 +2,6 @@ import streamlit as st
2
  import hashlib
3
  import json
4
  import time
5
- import requests
6
-
7
-
8
 
9
  # Blockchain Class
10
  class Blockchain:
@@ -12,6 +9,20 @@ class Blockchain:
12
  self.chain = []
13
  self.voters = set()
14
  self.load_data()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def create_block(self, votes, previous_hash):
17
  block = {
@@ -21,7 +32,6 @@ class Blockchain:
21
  "previous_hash": previous_hash,
22
  "hash": self.hash_block(votes, previous_hash),
23
  }
24
- block["ipfs_hash"] = self.upload_to_ipfs(block)
25
  self.chain.append(block)
26
  self.save_data()
27
  return block
@@ -33,16 +43,6 @@ class Blockchain:
33
  def get_latest_block(self):
34
  return self.chain[-1] if self.chain else None
35
 
36
- def upload_to_ipfs(self, block):
37
- """Uploads block data to IPFS and returns the IPFS hash."""
38
- try:
39
- response = requests.post(IPFS_API_URL, headers=IPFS_HEADERS, json={"pinataContent": block})
40
- if response.status_code == 200:
41
- return response.json().get("IpfsHash", "N/A")
42
- except Exception as e:
43
- return f"IPFS Error: {str(e)}"
44
- return "Upload Failed"
45
-
46
  def save_data(self):
47
  with open("votes.json", "w") as f:
48
  json.dump({"chain": self.chain, "voters": list(self.voters)}, f, indent=4)
@@ -63,79 +63,16 @@ blockchain = Blockchain()
63
  # Set page config
64
  st.set_page_config(page_title="Blockchain Voting", page_icon="πŸ—³", layout="wide")
65
 
66
- # Apply Dark Theme CSS Styling
67
- st.markdown(
68
- """
69
- <style>
70
- /* Dark background */
71
- .stApp {
72
- background-color: #121212;
73
- color: white;
74
- }
75
-
76
- /* Header Styling */
77
- h1 {
78
- text-align: center;
79
- font-size: 45px;
80
- background: linear-gradient(90deg, #ff8a00, #e52e71);
81
- -webkit-background-clip: text;
82
- color: transparent;
83
- font-weight: bold;
84
- padding: 10px;
85
- border-radius: 10px;
86
- }
87
-
88
- /* Sidebar Styling */
89
- .sidebar .sidebar-content {
90
- background-color: #1e1e1e;
91
- border-radius: 10px;
92
- padding: 15px;
93
- color: white;
94
- }
95
-
96
- /* Buttons with Neon Effect */
97
- .stButton>button {
98
- background-color: #ff6b6b;
99
- color: white;
100
- font-size: 18px;
101
- padding: 10px;
102
- width: 100%;
103
- border-radius: 10px;
104
- transition: 0.3s ease-in-out;
105
- box-shadow: 0px 0px 15px rgba(255, 107, 107, 0.8);
106
- }
107
-
108
- .stButton>button:hover {
109
- background-color: #ff4c4c;
110
- box-shadow: 0px 0px 25px rgba(255, 76, 76, 1);
111
- }
112
-
113
- /* Vote Count Section */
114
- .results {
115
- background-color: rgba(30, 30, 30, 0.95);
116
- padding: 20px;
117
- border-radius: 10px;
118
- box-shadow: 2px 2px 10px rgba(255, 255, 255, 0.2);
119
- }
120
-
121
- /* Candidate Selection */
122
- label[data-testid="stMarkdownContainer"] > div {
123
- font-size: 18px;
124
- font-weight: bold;
125
- color: #ffcc00;
126
- }
127
-
128
- </style>
129
- """,
130
- unsafe_allow_html=True,
131
- )
132
-
133
  st.markdown("<h1>πŸ—³ Secure Blockchain-Based Voting System</h1>", unsafe_allow_html=True)
134
 
135
  # Sidebar for Voter Input
136
  st.sidebar.header("πŸ” Voter Authentication")
137
  voter_id = st.sidebar.text_input("πŸ”‘ Enter your Unique Voter ID:", max_chars=10)
138
 
 
 
 
 
139
  # Candidate Selection
140
  st.sidebar.header("πŸ—³ Vote Now")
141
  candidates = ["BJP", "Shiv Sena", "NCP", "NOTA"]
@@ -145,18 +82,20 @@ selected_candidate = st.sidebar.radio("Select your candidate:", candidates)
145
  if st.sidebar.button("βœ… Submit Vote"):
146
  if not voter_id:
147
  st.sidebar.warning("⚠ Please enter a valid Voter ID!")
148
- elif voter_id in blockchain.voters:
149
- st.sidebar.error("❌ You have already voted!")
150
  else:
151
- last_block = blockchain.get_latest_block()
152
- new_votes = last_block["votes"].copy() if last_block else {}
 
 
 
 
153
 
154
- new_votes[selected_candidate] = new_votes.get(selected_candidate, 0) + 1
155
- blockchain.create_block(new_votes, last_block["hash"] if last_block else "0")
156
- blockchain.voters.add(voter_id)
157
- blockchain.save_data()
158
 
159
- st.sidebar.success(f"βœ… Vote cast for {selected_candidate}!")
160
 
161
  # Display Vote Count
162
  st.markdown("<h2 style='text-align: center; color:#ffcc00;'>πŸ“Š Live Vote Count</h2>", unsafe_allow_html=True)
@@ -166,7 +105,7 @@ if latest_block:
166
  with st.container():
167
  col1, col2, col3, col4 = st.columns(4)
168
  vote_data = latest_block["votes"]
169
-
170
  col1.metric(label="πŸ”Ή BJP", value=vote_data.get("BJP", 0))
171
  col2.metric(label="πŸ”Ή Shiv Sena", value=vote_data.get("Shiv Sena", 0))
172
  col3.metric(label="πŸ”Ή NCP", value=vote_data.get("NCP", 0))
@@ -174,4 +113,4 @@ if latest_block:
174
 
175
  # Show Blockchain Data
176
  if st.checkbox("πŸ“œ View Blockchain Data"):
177
- st.json(blockchain.chain)
 
2
  import hashlib
3
  import json
4
  import time
 
 
 
5
 
6
  # Blockchain Class
7
  class Blockchain:
 
9
  self.chain = []
10
  self.voters = set()
11
  self.load_data()
12
+ if not self.chain:
13
+ self.create_genesis_block()
14
+
15
+ def create_genesis_block(self):
16
+ """Creates the first block in the blockchain."""
17
+ genesis_block = {
18
+ "index": 1,
19
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
20
+ "votes": {},
21
+ "previous_hash": "0",
22
+ "hash": self.hash_block({}, "0"),
23
+ }
24
+ self.chain.append(genesis_block)
25
+ self.save_data()
26
 
27
  def create_block(self, votes, previous_hash):
28
  block = {
 
32
  "previous_hash": previous_hash,
33
  "hash": self.hash_block(votes, previous_hash),
34
  }
 
35
  self.chain.append(block)
36
  self.save_data()
37
  return block
 
43
  def get_latest_block(self):
44
  return self.chain[-1] if self.chain else None
45
 
 
 
 
 
 
 
 
 
 
 
46
  def save_data(self):
47
  with open("votes.json", "w") as f:
48
  json.dump({"chain": self.chain, "voters": list(self.voters)}, f, indent=4)
 
63
  # Set page config
64
  st.set_page_config(page_title="Blockchain Voting", page_icon="πŸ—³", layout="wide")
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  st.markdown("<h1>πŸ—³ Secure Blockchain-Based Voting System</h1>", unsafe_allow_html=True)
67
 
68
  # Sidebar for Voter Input
69
  st.sidebar.header("πŸ” Voter Authentication")
70
  voter_id = st.sidebar.text_input("πŸ”‘ Enter your Unique Voter ID:", max_chars=10)
71
 
72
+ # Hash the voter ID for security
73
+ def hash_voter_id(voter_id):
74
+ return hashlib.sha256(voter_id.encode()).hexdigest()
75
+
76
  # Candidate Selection
77
  st.sidebar.header("πŸ—³ Vote Now")
78
  candidates = ["BJP", "Shiv Sena", "NCP", "NOTA"]
 
82
  if st.sidebar.button("βœ… Submit Vote"):
83
  if not voter_id:
84
  st.sidebar.warning("⚠ Please enter a valid Voter ID!")
 
 
85
  else:
86
+ voter_hash = hash_voter_id(voter_id) # Hash voter ID
87
+ if voter_hash in blockchain.voters:
88
+ st.sidebar.error("❌ You have already voted!")
89
+ else:
90
+ last_block = blockchain.get_latest_block()
91
+ new_votes = last_block["votes"].copy() if last_block else {}
92
 
93
+ new_votes[selected_candidate] = new_votes.get(selected_candidate, 0) + 1
94
+ blockchain.create_block(new_votes, last_block["hash"] if last_block else "0")
95
+ blockchain.voters.add(voter_hash) # Store hashed ID
96
+ blockchain.save_data()
97
 
98
+ st.sidebar.success(f"βœ… Vote cast for {selected_candidate}!")
99
 
100
  # Display Vote Count
101
  st.markdown("<h2 style='text-align: center; color:#ffcc00;'>πŸ“Š Live Vote Count</h2>", unsafe_allow_html=True)
 
105
  with st.container():
106
  col1, col2, col3, col4 = st.columns(4)
107
  vote_data = latest_block["votes"]
108
+
109
  col1.metric(label="πŸ”Ή BJP", value=vote_data.get("BJP", 0))
110
  col2.metric(label="πŸ”Ή Shiv Sena", value=vote_data.get("Shiv Sena", 0))
111
  col3.metric(label="πŸ”Ή NCP", value=vote_data.get("NCP", 0))
 
113
 
114
  # Show Blockchain Data
115
  if st.checkbox("πŸ“œ View Blockchain Data"):
116
+ st.json(blockchain.chain)