Spaces:
Build error
Build error
Add src/streamlit_app.py
Browse files- src/streamlit_app.py +17 -26
src/streamlit_app.py
CHANGED
|
@@ -2,13 +2,6 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import web3
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
PINATA_JWT = os.environ.get("PinataJWT")
|
| 8 |
-
INFURA_URL = os.environ.get("infura")
|
| 9 |
-
CONTRACT_ADDRESS = os.environ.get("ContractAddress")
|
| 10 |
-
ETH_WALLET = os.environ.get("EthWallet")
|
| 11 |
-
PRIVATE_KEY = os.environ.get("pk")
|
| 12 |
|
| 13 |
abi = [{"anonymous":False,"inputs":[{"indexed":False,"internalType":"string","name":"ipfsHash","type":"string"}],"name":"Store","type":"event"},{"inputs":[],"name":"getHashes","outputs":[{"internalType":"string[]","name":"","type":"string[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"ipfsHash","type":"string"}],"name":"storeHash","outputs":[],"stateMutability":"nonpayable","type":"function"}]
|
| 14 |
# function that uploads the results to ipfs
|
|
@@ -23,7 +16,7 @@ def upload_json_to_ipfs(data):
|
|
| 23 |
st.write("No JWT secret found, please add your JWT in a secret titled \"PinataJWT\"")
|
| 24 |
return
|
| 25 |
|
| 26 |
-
jwt_token =
|
| 27 |
headers = {
|
| 28 |
"Authorization": f"Bearer {jwt_token}",
|
| 29 |
"Content-Type": "application/json"
|
|
@@ -46,18 +39,18 @@ def upload_json_to_ipfs(data):
|
|
| 46 |
# function that uploads to blockchain
|
| 47 |
def upload_to_blockchain(hash):
|
| 48 |
print("starting blockchain upload")
|
| 49 |
-
w3 = web3.Web3(web3.HTTPProvider(
|
| 50 |
|
| 51 |
# create an instance of our contract
|
| 52 |
-
contract = w3.eth.contract(address=
|
| 53 |
|
| 54 |
# Call your function: 11155111 is Sepolia's id
|
| 55 |
call_function = contract.functions.storeHash(hash).build_transaction({"chainId": 11155111,
|
| 56 |
-
"from":
|
| 57 |
-
"nonce": w3.eth.get_transaction_count(
|
| 58 |
|
| 59 |
# Sign transaction
|
| 60 |
-
signed_tx = w3.eth.account.sign_transaction(call_function, private_key=
|
| 61 |
|
| 62 |
# Send transaction
|
| 63 |
send_tx = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
|
|
@@ -71,10 +64,10 @@ def upload_to_blockchain(hash):
|
|
| 71 |
return tx_receipt.logs[0].transactionHash.hex()
|
| 72 |
|
| 73 |
def get_ipfs_hashes():
|
| 74 |
-
w3 = web3.Web3(web3.HTTPProvider(
|
| 75 |
|
| 76 |
# Create an instance of the contract
|
| 77 |
-
contract = w3.eth.contract(address=
|
| 78 |
|
| 79 |
# Call the getHashes function
|
| 80 |
try:
|
|
@@ -112,7 +105,6 @@ total_number_pages = 4
|
|
| 112 |
placeholder_buttons = None
|
| 113 |
|
| 114 |
Q1_radio_options = ["Pizza","Burgers","Pasta","Hot Dogs"]
|
| 115 |
-
Q2_radio_options = ["Water","Soda","Coffee","Tea"]
|
| 116 |
Q3_radio_options = ["Yes","No"]
|
| 117 |
Q4_radio_options = ["N/A", "Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"]
|
| 118 |
|
|
@@ -152,7 +144,7 @@ if "current_page" not in st.session_state:
|
|
| 152 |
# Page 1; Video
|
| 153 |
if st.session_state["current_page"] == 1:
|
| 154 |
|
| 155 |
-
st.markdown("""<p class="big-font">This is a
|
| 156 |
|
| 157 |
st.radio(label = "What is your favorite food?",
|
| 158 |
options = Q1_radio_options,
|
|
@@ -164,14 +156,13 @@ if st.session_state["current_page"] == 1:
|
|
| 164 |
st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
|
| 165 |
|
| 166 |
|
| 167 |
-
st.
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
args = (Q2_radio_options, "Q2", "Q2_multi",))
|
| 173 |
|
| 174 |
-
st.markdown("""<style> div[class*="
|
| 175 |
|
| 176 |
|
| 177 |
placeholder = st.empty()
|
|
@@ -195,7 +186,7 @@ if st.session_state["current_page"] == 1:
|
|
| 195 |
elif st.session_state["current_page"] == 2:
|
| 196 |
|
| 197 |
st.video("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
|
| 198 |
-
st.markdown("""<p style='font-size:18px;'>Get
|
| 199 |
st.selectbox(label = "Was that a good joke?",
|
| 200 |
options = Q3_radio_options,
|
| 201 |
index = None if st.session_state["Q3"] == None else st.session_state["Q3"],
|
|
@@ -242,7 +233,7 @@ elif st.session_state["current_page"] == 3:
|
|
| 242 |
|
| 243 |
if st.session_state["Q5"] == None:
|
| 244 |
st.session_state["Q5"] = 50
|
| 245 |
-
st.slider(label="How
|
| 246 |
value= st.session_state["Q5"],
|
| 247 |
key = "Q5_slider",
|
| 248 |
on_change = answer_change,
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import web3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
abi = [{"anonymous":False,"inputs":[{"indexed":False,"internalType":"string","name":"ipfsHash","type":"string"}],"name":"Store","type":"event"},{"inputs":[],"name":"getHashes","outputs":[{"internalType":"string[]","name":"","type":"string[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"ipfsHash","type":"string"}],"name":"storeHash","outputs":[],"stateMutability":"nonpayable","type":"function"}]
|
| 7 |
# function that uploads the results to ipfs
|
|
|
|
| 16 |
st.write("No JWT secret found, please add your JWT in a secret titled \"PinataJWT\"")
|
| 17 |
return
|
| 18 |
|
| 19 |
+
jwt_token = st.secrets["PinataJWT"].strip()
|
| 20 |
headers = {
|
| 21 |
"Authorization": f"Bearer {jwt_token}",
|
| 22 |
"Content-Type": "application/json"
|
|
|
|
| 39 |
# function that uploads to blockchain
|
| 40 |
def upload_to_blockchain(hash):
|
| 41 |
print("starting blockchain upload")
|
| 42 |
+
w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
|
| 43 |
|
| 44 |
# create an instance of our contract
|
| 45 |
+
contract = w3.eth.contract(address=st.secrets["ContractAddress"], abi = abi)
|
| 46 |
|
| 47 |
# Call your function: 11155111 is Sepolia's id
|
| 48 |
call_function = contract.functions.storeHash(hash).build_transaction({"chainId": 11155111,
|
| 49 |
+
"from": st.secrets["EthWallet"],
|
| 50 |
+
"nonce": w3.eth.get_transaction_count(st.secrets["EthWallet"])})
|
| 51 |
|
| 52 |
# Sign transaction
|
| 53 |
+
signed_tx = w3.eth.account.sign_transaction(call_function, private_key=st.secrets["pk"])
|
| 54 |
|
| 55 |
# Send transaction
|
| 56 |
send_tx = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
|
|
|
|
| 64 |
return tx_receipt.logs[0].transactionHash.hex()
|
| 65 |
|
| 66 |
def get_ipfs_hashes():
|
| 67 |
+
w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
|
| 68 |
|
| 69 |
# Create an instance of the contract
|
| 70 |
+
contract = w3.eth.contract(address=st.secrets["ContractAddress"], abi=abi)
|
| 71 |
|
| 72 |
# Call the getHashes function
|
| 73 |
try:
|
|
|
|
| 105 |
placeholder_buttons = None
|
| 106 |
|
| 107 |
Q1_radio_options = ["Pizza","Burgers","Pasta","Hot Dogs"]
|
|
|
|
| 108 |
Q3_radio_options = ["Yes","No"]
|
| 109 |
Q4_radio_options = ["N/A", "Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"]
|
| 110 |
|
|
|
|
| 144 |
# Page 1; Video
|
| 145 |
if st.session_state["current_page"] == 1:
|
| 146 |
|
| 147 |
+
st.markdown("""<p class="big-font">This is a test survey</p>""", unsafe_allow_html=True)
|
| 148 |
|
| 149 |
st.radio(label = "What is your favorite food?",
|
| 150 |
options = Q1_radio_options,
|
|
|
|
| 156 |
st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
|
| 157 |
|
| 158 |
|
| 159 |
+
st.text_area(label = "How is your day?",
|
| 160 |
+
value= "" if st.session_state["Q2"] == None else st.session_state["Q2"],
|
| 161 |
+
key = 'Q2_text',
|
| 162 |
+
on_change = answer_change,
|
| 163 |
+
args = ( "Q2", "Q2_text",))
|
|
|
|
| 164 |
|
| 165 |
+
st.markdown("""<style> div[class*="stText"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
|
| 166 |
|
| 167 |
|
| 168 |
placeholder = st.empty()
|
|
|
|
| 186 |
elif st.session_state["current_page"] == 2:
|
| 187 |
|
| 188 |
st.video("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
|
| 189 |
+
st.markdown("""<p style='font-size:18px;'>Get RickRolled!</p>""", unsafe_allow_html=True)
|
| 190 |
st.selectbox(label = "Was that a good joke?",
|
| 191 |
options = Q3_radio_options,
|
| 192 |
index = None if st.session_state["Q3"] == None else st.session_state["Q3"],
|
|
|
|
| 233 |
|
| 234 |
if st.session_state["Q5"] == None:
|
| 235 |
st.session_state["Q5"] = 50
|
| 236 |
+
st.slider(label="How much do you enjoy food?",min_value=0,max_value=100,
|
| 237 |
value= st.session_state["Q5"],
|
| 238 |
key = "Q5_slider",
|
| 239 |
on_change = answer_change,
|