ojas121 commited on
Commit
11676d1
Β·
verified Β·
1 Parent(s): f29f3b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -32
app.py CHANGED
@@ -15,13 +15,12 @@ class Blockchain:
15
  def create_block(self, votes, previous_hash):
16
  block = {
17
  "index": len(self.chain) + 1,
18
- "timestamp": time.time(),
19
  "votes": votes,
20
  "previous_hash": previous_hash,
21
- "hash": self.hash_block(votes, previous_hash)
22
  }
23
- ipfs_hash = self.upload_to_ipfs(block)
24
- block["ipfs_hash"] = ipfs_hash # Store IPFS hash
25
  self.chain.append(block)
26
  self.save_data()
27
  return block
@@ -38,11 +37,10 @@ class Blockchain:
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()["IpfsHash"]
42
- else:
43
- return "Error uploading to IPFS"
44
  except Exception as e:
45
- return str(e)
 
46
 
47
  def save_data(self):
48
  with open("votes.json", "w") as f:
@@ -62,21 +60,40 @@ class Blockchain:
62
  blockchain = Blockchain()
63
 
64
  # Streamlit UI
65
- st.title("πŸ—³ Blockchain-Based Voting System (IPFS-Enabled)")
66
-
67
- # Voter ID for uniqueness
68
- voter_id = st.text_input("Enter your Voter ID (Unique & Unchangeable)", max_chars=10)
69
-
70
- # Candidates
71
- candidates = ["bjp", "ss", "Ncp","nota"]
72
- selected_candidate = st.selectbox("Select a candidate to vote for:", candidates)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  # Voting Button
75
- if st.button("Vote"):
76
  if not voter_id:
77
- st.warning("⚠ Please enter a valid Voter ID!")
78
  elif voter_id in blockchain.voters:
79
- st.error("❌ You have already voted!")
80
  else:
81
  last_block = blockchain.get_latest_block()
82
  new_votes = last_block["votes"].copy() if last_block else {}
@@ -85,22 +102,23 @@ if st.button("Vote"):
85
  blockchain.create_block(new_votes, last_block["hash"] if last_block else "0")
86
  blockchain.voters.add(voter_id)
87
  blockchain.save_data()
88
-
89
- st.success(f"βœ… Vote cast for {selected_candidate}!")
90
 
91
  # Display Vote Count
92
- st.subheader("πŸ”’ Live Vote Count")
93
  latest_block = blockchain.get_latest_block()
94
- if latest_block:
95
- for candidate, votes in latest_block["votes"].items():
96
- st.write(f"βœ… {candidate}: {votes} votes")
97
 
98
- # Show Blockchain
99
- if st.checkbox("Show Blockchain Data"):
 
 
 
 
 
 
 
 
 
100
  st.json(blockchain.chain)
101
 
102
- # Show IPFS Hashes for each Block
103
- st.subheader("πŸ”— IPFS Storage Links")
104
- if latest_block:
105
- for block in blockchain.chain:
106
- st.write(f"πŸ”— Block {block['index']} stored on IPFS: [View](https://gateway.pinata.cloud/ipfs/{block.get('ipfs_hash', 'N/A')})")
 
15
  def create_block(self, votes, previous_hash):
16
  block = {
17
  "index": len(self.chain) + 1,
18
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
19
  "votes": votes,
20
  "previous_hash": previous_hash,
21
+ "hash": self.hash_block(votes, previous_hash),
22
  }
23
+ block["ipfs_hash"] = self.upload_to_ipfs(block)
 
24
  self.chain.append(block)
25
  self.save_data()
26
  return block
 
37
  try:
38
  response = requests.post(IPFS_API_URL, headers=IPFS_HEADERS, json={"pinataContent": block})
39
  if response.status_code == 200:
40
+ return response.json().get("IpfsHash", "N/A")
 
 
41
  except Exception as e:
42
+ return f"IPFS Error: {str(e)}"
43
+ return "Upload Failed"
44
 
45
  def save_data(self):
46
  with open("votes.json", "w") as f:
 
60
  blockchain = Blockchain()
61
 
62
  # Streamlit UI
63
+ st.set_page_config(page_title="Blockchain Voting", page_icon="πŸ—³", layout="wide")
64
+
65
+ # Apply CSS styling
66
+ st.markdown(
67
+ """
68
+ <style>
69
+ .stApp {background-color: #f5f7fa;}
70
+ h1 {color: #2b6cb0; text-align: center; font-size: 40px;}
71
+ .vote-container {text-align: center; padding: 10px;}
72
+ .stButton>button {width: 100%; background-color: #2b6cb0; color: white; font-size: 16px; padding: 10px;}
73
+ .stButton>button:hover {background-color: #1e4e79;}
74
+ .results {background-color: white; padding: 15px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);}
75
+ </style>
76
+ """,
77
+ unsafe_allow_html=True,
78
+ )
79
+
80
+ st.markdown("<h1>πŸ—³ Secure Blockchain-Based Voting System</h1>", unsafe_allow_html=True)
81
+
82
+ # Voter ID input
83
+ st.sidebar.header("Voter Authentication")
84
+ voter_id = st.sidebar.text_input("πŸ”‘ Enter your Unique Voter ID:", max_chars=10)
85
+
86
+ # Candidate Selection
87
+ st.sidebar.header("Vote Now")
88
+ candidates = ["BJP", "Shiv Sena", "NCP", "NOTA"]
89
+ selected_candidate = st.sidebar.radio("Select your candidate:", candidates)
90
 
91
  # Voting Button
92
+ if st.sidebar.button("βœ… Submit Vote"):
93
  if not voter_id:
94
+ st.sidebar.warning("⚠ Please enter a valid Voter ID!")
95
  elif voter_id in blockchain.voters:
96
+ st.sidebar.error("❌ You have already voted!")
97
  else:
98
  last_block = blockchain.get_latest_block()
99
  new_votes = last_block["votes"].copy() if last_block else {}
 
102
  blockchain.create_block(new_votes, last_block["hash"] if last_block else "0")
103
  blockchain.voters.add(voter_id)
104
  blockchain.save_data()
105
+
106
+ st.sidebar.success(f"βœ… Vote cast for {selected_candidate}!")
107
 
108
  # Display Vote Count
109
+ st.markdown("<h2 style='text-align: center;'>πŸ“Š Live Vote Count</h2>", unsafe_allow_html=True)
110
  latest_block = blockchain.get_latest_block()
 
 
 
111
 
112
+ if latest_block:
113
+ with st.container():
114
+ col1, col2, col3, col4 = st.columns(4)
115
+ vote_data = latest_block["votes"]
116
+ col1.metric(label="πŸ”Ή BJP", value=vote_data.get("BJP", 0))
117
+ col2.metric(label="πŸ”Ή Shiv Sena", value=vote_data.get("Shiv Sena", 0))
118
+ col3.metric(label="πŸ”Ή NCP", value=vote_data.get("NCP", 0))
119
+ col4.metric(label="πŸ”Ή NOTA", value=vote_data.get("NOTA", 0))
120
+
121
+ # Show Blockchain Data
122
+ if st.checkbox("πŸ“œ View Blockchain Data"):
123
  st.json(blockchain.chain)
124