arthurcarvalho commited on
Commit
b50be44
·
1 Parent(s): 2afb448

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +153 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+ import web3
5
+
6
+ # Global Variables
7
+ radio_options = ["Comedy", "Drama", "Documentary"]
8
+
9
+ # Auxiliary function to disable buttons
10
+ def button_disable():
11
+ st.session_state["disabled"] = True
12
+
13
+
14
+ # Changing the App title
15
+ st.set_page_config(page_title="IPFS-Based Survey",)
16
+
17
+ # Page title
18
+ st.title('Sample Survey')
19
+
20
+ # The following code centralizes all the buttons
21
+ st.markdown("<style>.row-widget.stButton {text-align: center;}</style>", unsafe_allow_html=True)
22
+
23
+ # Initialize state
24
+ if "current_page" not in st.session_state:
25
+ st.session_state["current_page"] = 1
26
+ st.session_state["Q1"] = ""
27
+ st.session_state["Q2"] = None
28
+ st.session_state["Q3"] = False
29
+ st.session_state["disabled"] = False
30
+
31
+
32
+ # Page 1
33
+ if st.session_state["current_page"] == 1:
34
+
35
+ st.header('Please start by watching the video below', divider = 'gray')
36
+
37
+ video_file = open('./media/sample-5s.mp4', 'rb')
38
+ video_bytes = video_file.read()
39
+
40
+ st.video(video_bytes)
41
+
42
+ if st.button('Next'):
43
+ st.session_state["current_page"] += 1
44
+ st.rerun()
45
+
46
+
47
+ # Page 2
48
+ elif st.session_state["current_page"] == 2:
49
+
50
+ st.session_state["Q1"] = st.text_area(r"$\textsf{\Large Q1: What is the meaning of life?}$",
51
+ value = st.session_state["Q1"])
52
+
53
+ st.markdown("# <br>", unsafe_allow_html=True)
54
+
55
+ option_selected = st.radio(r"$\textsf{\Large Q2: What's your favorite movie genre?}$",
56
+ radio_options,
57
+ index = st.session_state["Q2"])
58
+ for i, x in enumerate(radio_options):
59
+ if x == option_selected:
60
+ st.session_state["Q2"] = i
61
+ break
62
+
63
+
64
+ st.markdown("# <br>", unsafe_allow_html=True)
65
+
66
+ col1, col2 = st.columns(2)
67
+ with col1:
68
+ if st.button('Back'):
69
+ st.session_state["current_page"] -= 1
70
+ st.rerun()
71
+
72
+ with col2:
73
+ if st.button('Next'):
74
+ st.session_state["current_page"] += 1
75
+ st.rerun()
76
+
77
+
78
+ # Page 3
79
+ elif st.session_state["current_page"] == 3:
80
+
81
+ st.session_state["Q3"] = st.checkbox('Check if you like this survey',
82
+ value = st.session_state["Q3"],
83
+ disabled = st.session_state["disabled"])
84
+
85
+ st.markdown("# <br>", unsafe_allow_html=True)
86
+
87
+ col1, col2 = st.columns(2)
88
+ with col1:
89
+ button1 = st.button('Back', disabled = st.session_state["disabled"])
90
+ if button1:
91
+ st.session_state["current_page"] -= 1
92
+ st.rerun()
93
+
94
+ with col2:
95
+ button2 = st.button('Submit Responses',
96
+ disabled = st.session_state["disabled"],
97
+ on_click = button_disable)
98
+
99
+ st.markdown("# <br>", unsafe_allow_html=True)
100
+ if st.session_state["disabled"]:
101
+ with st.spinner(r"$\textsf{\normalsize Storing data on IPFS and Ethereum. This operation might take a few minutes. Please wait!}$"):
102
+ try:
103
+ response = {'file': json.dumps({"Q1":st.session_state["Q1"],
104
+ "Q2":radio_options[st.session_state["Q2"]],
105
+ "Q3":st.session_state["Q3"]
106
+ })
107
+ }
108
+
109
+ response = requests.post('https://ipfs.infura.io:5001/api/v0/add',
110
+ files=response,
111
+ auth=(st.secrets["username"], st.secrets["password"]))
112
+
113
+ IPFS_hash = response.json()["Hash"]
114
+ print(IPFS_hash)
115
+
116
+
117
+ # Connect to the Ethereum blockchain
118
+ w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
119
+
120
+ # Get the contract address
121
+ caller = "0xbdB61Ada027eF27ef0709D3848Db1Dd13c96F970"
122
+
123
+ ABI = '[ { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "Store", "type": "event" }, { "inputs": [ { "internalType": "string", "name": "_IPFSHash", "type": "string" } ], "name": "storeHash", "outputs": [], "stateMutability": "nonpayable", "type": "function" } ]'
124
+
125
+ # Create a contract instance. Address is equal to the address of the smart contract
126
+ contract = w3.eth.contract(address= "0x1e14B06209ACD932EF7731632999Cf4aE1b86e5c", abi=ABI)
127
+
128
+ # Call your function: 11155111 is Sepolia's id
129
+ call_function = contract.functions.storeHash(IPFS_hash).build_transaction({"chainId": 11155111,
130
+ "from": caller,
131
+ "nonce": w3.eth.get_transaction_count(caller)}) # Initialize address nonce
132
+
133
+
134
+ # Sign transaction
135
+ signed_tx = w3.eth.account.sign_transaction(call_function, private_key=st.secrets["pk"])
136
+
137
+ # Send transaction
138
+ send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
139
+
140
+ # Wait for transaction receipt
141
+ tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
142
+
143
+ print(tx_receipt)
144
+
145
+ st.success('Data successfully stored. Thank you for taking the survey!', icon="✅")
146
+ st.info(f'The IPFS hash is: {IPFS_hash}', icon="ℹ️")
147
+ st.info(f'The Ethereum hash is: {tx_receipt.logs[0].transactionHash.hex()}', icon="ℹ️")
148
+
149
+ except Exception as e:
150
+ print(e)
151
+ st.error(f'An error ocurred. Here is the error message: {e}', icon="🚨")
152
+
153
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ web3==6.11.2
2
+ requests==2.31.0