Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import hashlib
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
# IPFS API URL (Use a public IPFS gateway or set up your own node)
|
| 8 |
+
IPFS_API_URL = "https://api.pinata.cloud/pinning/pinJSONToIPFS"
|
| 9 |
+
IPFS_HEADERS = {
|
| 10 |
+
"pinata_api_key": "your_pinata_api_key",
|
| 11 |
+
"pinata_secret_api_key": "your_pinata_secret_api_key",
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Blockchain Class
|
| 15 |
+
class Blockchain:
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.chain = []
|
| 18 |
+
self.voters = set()
|
| 19 |
+
self.load_data()
|
| 20 |
+
|
| 21 |
+
def create_block(self, votes, previous_hash):
|
| 22 |
+
block = {
|
| 23 |
+
"index": len(self.chain) + 1,
|
| 24 |
+
"timestamp": time.time(),
|
| 25 |
+
"votes": votes,
|
| 26 |
+
"previous_hash": previous_hash,
|
| 27 |
+
"hash": self.hash_block(votes, previous_hash)
|
| 28 |
+
}
|
| 29 |
+
ipfs_hash = self.upload_to_ipfs(block)
|
| 30 |
+
block["ipfs_hash"] = ipfs_hash # Store IPFS hash
|
| 31 |
+
self.chain.append(block)
|
| 32 |
+
self.save_data()
|
| 33 |
+
return block
|
| 34 |
+
|
| 35 |
+
def hash_block(self, votes, previous_hash):
|
| 36 |
+
block_string = json.dumps(votes, sort_keys=True) + previous_hash
|
| 37 |
+
return hashlib.sha256(block_string.encode()).hexdigest()
|
| 38 |
+
|
| 39 |
+
def get_latest_block(self):
|
| 40 |
+
return self.chain[-1] if self.chain else None
|
| 41 |
+
|
| 42 |
+
def upload_to_ipfs(self, block):
|
| 43 |
+
"""Uploads block data to IPFS and returns the IPFS hash."""
|
| 44 |
+
try:
|
| 45 |
+
response = requests.post(IPFS_API_URL, headers=IPFS_HEADERS, json={"pinataContent": block})
|
| 46 |
+
if response.status_code == 200:
|
| 47 |
+
return response.json()["IpfsHash"]
|
| 48 |
+
else:
|
| 49 |
+
return "Error uploading to IPFS"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return str(e)
|
| 52 |
+
|
| 53 |
+
def save_data(self):
|
| 54 |
+
with open("votes.json", "w") as f:
|
| 55 |
+
json.dump({"chain": self.chain, "voters": list(self.voters)}, f, indent=4)
|
| 56 |
+
|
| 57 |
+
def load_data(self):
|
| 58 |
+
try:
|
| 59 |
+
with open("votes.json", "r") as f:
|
| 60 |
+
data = json.load(f)
|
| 61 |
+
self.chain = data.get("chain", [])
|
| 62 |
+
self.voters = set(data.get("voters", []))
|
| 63 |
+
except FileNotFoundError:
|
| 64 |
+
self.chain = []
|
| 65 |
+
self.voters = set()
|
| 66 |
+
|
| 67 |
+
# Initialize Blockchain
|
| 68 |
+
blockchain = Blockchain()
|
| 69 |
+
|
| 70 |
+
# Streamlit UI
|
| 71 |
+
st.title("π³ Blockchain-Based Voting System (IPFS-Enabled)")
|
| 72 |
+
|
| 73 |
+
# Voter ID for uniqueness
|
| 74 |
+
voter_id = st.text_input("Enter your Voter ID (Unique & Unchangeable)", max_chars=10)
|
| 75 |
+
|
| 76 |
+
# Candidates
|
| 77 |
+
candidates = ["Alice", "Bob", "Charlie"]
|
| 78 |
+
selected_candidate = st.selectbox("Select a candidate to vote for:", candidates)
|
| 79 |
+
|
| 80 |
+
# Voting Button
|
| 81 |
+
if st.button("Vote"):
|
| 82 |
+
if not voter_id:
|
| 83 |
+
st.warning("β Please enter a valid Voter ID!")
|
| 84 |
+
elif voter_id in blockchain.voters:
|
| 85 |
+
st.error("β You have already voted!")
|
| 86 |
+
else:
|
| 87 |
+
last_block = blockchain.get_latest_block()
|
| 88 |
+
new_votes = last_block["votes"].copy() if last_block else {}
|
| 89 |
+
|
| 90 |
+
new_votes[selected_candidate] = new_votes.get(selected_candidate, 0) + 1
|
| 91 |
+
blockchain.create_block(new_votes, last_block["hash"] if last_block else "0")
|
| 92 |
+
blockchain.voters.add(voter_id)
|
| 93 |
+
blockchain.save_data()
|
| 94 |
+
|
| 95 |
+
st.success(f"β
Vote cast for {selected_candidate}!")
|
| 96 |
+
|
| 97 |
+
# Display Vote Count
|
| 98 |
+
st.subheader("π’ Live Vote Count")
|
| 99 |
+
latest_block = blockchain.get_latest_block()
|
| 100 |
+
if latest_block:
|
| 101 |
+
for candidate, votes in latest_block["votes"].items():
|
| 102 |
+
st.write(f"β
{candidate}: {votes} votes")
|
| 103 |
+
|
| 104 |
+
# Show Blockchain
|
| 105 |
+
if st.checkbox("Show Blockchain Data"):
|
| 106 |
+
st.json(blockchain.chain)
|
| 107 |
+
|
| 108 |
+
# Show IPFS Hashes for each Block
|
| 109 |
+
st.subheader("π IPFS Storage Links")
|
| 110 |
+
if latest_block:
|
| 111 |
+
for block in blockchain.chain:
|
| 112 |
+
st.write(f"π Block {block['index']} stored on IPFS: [View](https://gateway.pinata.cloud/ipfs/{block.get('ipfs_hash', 'N/A')})")
|