Spaces:
Runtime error
Runtime error
File size: 4,414 Bytes
f1b09fd |
1 2 3 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import requests
from mychain import Blockchain
from mychain import MyChainTrans
import pandas as pd
import json
main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/chains/'
main_balance='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/balance/'
main_trans='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/transact/'
trans_name = 'trans1.json'
repo_d='Omnibus/static-bin'
chain_d='chain1.json'
def bc_transactions(block):
#mes, out = issue_tokens(sender,recipient,amount)
print (f'block:: {block}')
try:
blockchain.new_transaction(block)
message = "Transaction Added to Pool"
data = pd.DataFrame(blockchain.trans_data_out)
except Exception as e:
message = e
data = None
return data,message
def create_chain(create=None):
global blockchain
blockchain = Blockchain(chain_load=main_chain,create=create)
#blockchain.reset(create=create)
return "New Chain Created",None,display_chain()
def display_chain():
response = {'chain': blockchain.chain,
'length': len(blockchain.chain)}
return response
def mine_block(chain_r=None,chain_n=None):
if chain_n=="":
chain_n = chain_d
previous_block = blockchain.print_previous_block()
previous_proof = previous_block['proof']
proof = blockchain.proof_of_work(previous_proof)
previous_hash = blockchain.hash(previous_block)
block = blockchain.create_block(proof, previous_hash,chain_r,chain_n)
response = {'message': 'A block is MINED',
'index': block['index'],
'timestamp': block['timestamp'],
'proof': block['proof'],
'previous_hash': block['previous_hash']}
message = "A block is MINED"
show_chain = display_chain()
if len(blockchain.chain) > 1000:
blockchain.reset()
response = None
show_chain=display_chain()
message = "New Chain Created at Max 20 Blocks"
#MyChainTrans.reset(create=chain_d)
return response, show_chain,pd.DataFrame(blockchain.pending_transactions), message
def valid():
valid,ind,mes = blockchain.chain_valid(blockchain.chain)
if valid:
response = 'The Blockchain is valid.'
z=True
else:
response = f'Blockchain is not valid. {mes} at Index {ind}'
z=False
return response,z
def get_chain(repo_name=None,chain_name=None,token=None):
if repo_name == "":
repo_name = repo_d
if chain_name=="":
chain_name = chain_d
try:
r = requests.get(f'{main_chain}{chain_name}')
#create_chain(load=r.text)
global blockchain
blockchain = Blockchain(chain_load=main_chain,load=r.text)
response = {'chain': blockchain.chain,
'length': len(blockchain.chain)}
message = f"Blockchain loaded from: {main_chain}{chain_name}"
return response,message
except:
message = f"Error loading from: {src}"
return ["Error Loading Chain"],message
def checkp(inp):
if inp == pa:
return gr.update(visible=False), gr.update(visible=True)
elif inp != pa:
return gr.update(visible=True), gr.update(visible=False)
def add_node(this_space,repo,space,chain_file):
#print(f"{api.whoami(['name'])}")
#repo = f'omnibus/{space}'
is_valid='True'
r = requests.get(f'{main_nodes}')
try:
lod = json.loads(r.text)
except:
lod=[]
pass
block = {'index': len(lod) + 1,
'timestamp': str(datetime.datetime.now()),
'url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{chain_file}',
'valid': f'{is_valid}'}
lod.append(block)
json_object = json.dumps(lod, indent=4)
with open("tmp1.json", "w") as outfile:
outfile.write(json_object)
try:
api.upload_file(
path_or_fileobj="tmp1.json",
path_in_repo=main_nodes.split('main/',1)[1],
repo_id=main_nodes.split('datasets/',1)[1].split('/raw',1)[0],
token=token_self,
repo_type="dataset",
)
os.remove("tmp1.json")
except Exception as e:
pass
#api = HfApi(token=token)
repo = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[0]
name = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[1] |