Spaces:
Build error
Build error
Max-Dupler
commited on
Commit
·
693ede2
1
Parent(s):
20da062
adding ipfs
Browse files
app.py
CHANGED
|
@@ -1,30 +1,74 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def main():
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def store_in_ipfs(data):
|
| 7 |
+
url = 'https://ipfs.infura.io:5001/api/v0/add'
|
| 8 |
+
# Convert the data dictionary to JSON format
|
| 9 |
+
files = {'file': json.dumps(data)}
|
| 10 |
+
# Send POST request to IPFS using Infura endpoint
|
| 11 |
+
response = requests.post(
|
| 12 |
+
url,
|
| 13 |
+
files=files,
|
| 14 |
+
auth=(st.secrets["username"], st.secrets["password"])
|
| 15 |
+
)
|
| 16 |
+
# Check for successful response
|
| 17 |
+
if response.status_code == 200:
|
| 18 |
+
return response.json()['Hash'] # Return IPFS hash of the stored data
|
| 19 |
+
else:
|
| 20 |
+
raise Exception(f"Failed to store in IPFS: {response.text}")
|
| 21 |
+
|
| 22 |
|
| 23 |
def main():
|
| 24 |
+
st.title("Basic Survey")
|
| 25 |
+
|
| 26 |
+
# Introductory text
|
| 27 |
+
st.write("Welcome to our basic survey! Please fill out the information below.")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Name input
|
| 31 |
+
name = st.text_input("What's your name?")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Age input
|
| 35 |
+
age = st.number_input("How old are you?", min_value=1, max_value=100)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Gender selection
|
| 39 |
+
gender = st.radio("Select your gender:", ("Male", "Female", "Other"))
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Feedback text area
|
| 43 |
+
feedback = st.text_area("Your feedback:")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Submit button
|
| 47 |
+
if st.button("Submit"):
|
| 48 |
+
st.success(f"Thank you {name} for your submission!")
|
| 49 |
+
st.write(f"Name: {name}")
|
| 50 |
+
st.write(f"Age: {age}")
|
| 51 |
+
st.write(f"Gender: {gender}")
|
| 52 |
+
st.write(f"Feedback: {feedback}")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
data = {
|
| 56 |
+
"name": name,
|
| 57 |
+
"age": age,
|
| 58 |
+
"gender": gender,
|
| 59 |
+
"feedback": feedback
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# Store data in IPFS via Infura
|
| 63 |
+
try:
|
| 64 |
+
ipfs_hash = store_in_ipfs(data)
|
| 65 |
+
st.success(f"Thank you {name} for your submission!")
|
| 66 |
+
st.write(f"Stored on IPFS with hash: {ipfs_hash}")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"Error storing data in IPFS: {e}")
|
| 69 |
+
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
main()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|