Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,8 @@ import requests
|
|
| 8 |
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
|
| 9 |
|
| 10 |
main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/'
|
|
|
|
|
|
|
| 11 |
main_nodes='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/node_file1.json'
|
| 12 |
main_pending='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/pending1.json'
|
| 13 |
token_self = os.environ['HF_TOKEN']
|
|
@@ -18,6 +20,53 @@ node_file='node_file.json'
|
|
| 18 |
space='blockchain-simulator-dev1'
|
| 19 |
api = HfApi(token=token_self)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
class Blockchain:
|
| 22 |
|
| 23 |
def __init__(self,load=None,create=None):
|
|
@@ -150,10 +199,15 @@ class Blockchain:
|
|
| 150 |
block_index += 1
|
| 151 |
|
| 152 |
return out, ind, mes
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
def bc_transactions(sender,recipient,amount):
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
return pd.DataFrame(blockchain.pending_transactions),message,None,None,None
|
| 158 |
|
| 159 |
def create_chain(create=None):
|
|
@@ -319,6 +373,79 @@ def add_node(this_space,repo,space,chain_file):
|
|
| 319 |
except Exception as e:
|
| 320 |
pass
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
with gr.Blocks() as bc:
|
| 323 |
with gr.Row(visible=True) as invalid:
|
| 324 |
pass_box = gr.Textbox()
|
|
@@ -356,7 +483,11 @@ with gr.Blocks() as bc:
|
|
| 356 |
node_space=gr.Textbox(label="Node Space")
|
| 357 |
node_file=gr.Textbox(label="Node File")
|
| 358 |
node_add=gr.Button("Add Node")
|
|
|
|
|
|
|
| 359 |
|
|
|
|
|
|
|
| 360 |
node_add.click(add_node,[this_space,node_repo,node_space,node_file],block_text)
|
| 361 |
pass_btn.click(checkp,pass_box,[invalid,valida])
|
| 362 |
in_chain_btn.click(get_chain,[chain_repo,chain_n],[chain_json,block_text])
|
|
|
|
| 8 |
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
|
| 9 |
|
| 10 |
main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/'
|
| 11 |
+
main_balance='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/balance/'
|
| 12 |
+
|
| 13 |
main_nodes='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/node_file1.json'
|
| 14 |
main_pending='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/pending1.json'
|
| 15 |
token_self = os.environ['HF_TOKEN']
|
|
|
|
| 20 |
space='blockchain-simulator-dev1'
|
| 21 |
api = HfApi(token=token_self)
|
| 22 |
|
| 23 |
+
class Balance_sheet:
|
| 24 |
+
def create_block(self, proof, previous_hash,chain_r=None,chain_n=None,balance=None):
|
| 25 |
+
if chain_r=="" or chain_r==None:
|
| 26 |
+
chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
|
| 27 |
+
if chain_n=="" or chain_n==None:
|
| 28 |
+
chain_n=chain_d
|
| 29 |
+
block = {'index': len(self.chain) + 1,
|
| 30 |
+
'timestamp': str(datetime.datetime.now()),
|
| 31 |
+
'transactions': self.pending_transactions,
|
| 32 |
+
'balance':self.balance
|
| 33 |
+
'proof': proof,
|
| 34 |
+
'previous_hash': previous_hash}
|
| 35 |
+
if self.block_valid(block) == True:
|
| 36 |
+
|
| 37 |
+
self.pending_transactions = []
|
| 38 |
+
self.chain.append(block)
|
| 39 |
+
json_object = json.dumps(self.chain, indent=4)
|
| 40 |
+
with open("tmp.json", "w") as outfile:
|
| 41 |
+
outfile.write(json_object)
|
| 42 |
+
try:
|
| 43 |
+
api.upload_file(
|
| 44 |
+
path_or_fileobj="tmp.json",
|
| 45 |
+
path_in_repo=chain_n,
|
| 46 |
+
repo_id=chain_r,
|
| 47 |
+
token=token_self,
|
| 48 |
+
repo_type="dataset",
|
| 49 |
+
)
|
| 50 |
+
os.remove("tmp.json")
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(e)
|
| 54 |
+
pass
|
| 55 |
+
return block
|
| 56 |
+
else:
|
| 57 |
+
block = {"Not Valid"}
|
| 58 |
+
print("not Valid")
|
| 59 |
+
return block
|
| 60 |
+
def print_previous_block(self):
|
| 61 |
+
return self.chain[-1]
|
| 62 |
+
def new_transaction(self, sender, recipient, amount):
|
| 63 |
+
transaction = {
|
| 64 |
+
'sender': sender,
|
| 65 |
+
'recipient': recipient,
|
| 66 |
+
'amount': amount
|
| 67 |
+
}
|
| 68 |
+
self.pending_transactions.append(transaction)
|
| 69 |
+
|
| 70 |
class Blockchain:
|
| 71 |
|
| 72 |
def __init__(self,load=None,create=None):
|
|
|
|
| 199 |
block_index += 1
|
| 200 |
|
| 201 |
return out, ind, mes
|
| 202 |
+
#def check_balance(sender,amount):
|
| 203 |
+
|
| 204 |
+
|
| 205 |
|
| 206 |
def bc_transactions(sender,recipient,amount):
|
| 207 |
+
out = check_balance(sender,amount)
|
| 208 |
+
if out == true:
|
| 209 |
+
blockchain.new_transaction(f"{sender}",f"{recipient}",f"{amount}")
|
| 210 |
+
message = "Transaction Added to Pool"
|
| 211 |
return pd.DataFrame(blockchain.pending_transactions),message,None,None,None
|
| 212 |
|
| 213 |
def create_chain(create=None):
|
|
|
|
| 373 |
except Exception as e:
|
| 374 |
pass
|
| 375 |
|
| 376 |
+
|
| 377 |
+
start_tokens = 10000000
|
| 378 |
+
source="Main"
|
| 379 |
+
def issue_tokens(send,rec,amount):
|
| 380 |
+
#print(f"{api.whoami(['name'])}")
|
| 381 |
+
#repo = f'omnibus/{space}'
|
| 382 |
+
#is_valid='True'
|
| 383 |
+
mes = None
|
| 384 |
+
try:
|
| 385 |
+
r = requests.get(f'{main_balance}/{send}')
|
| 386 |
+
lod = json.loads(r.text)
|
| 387 |
+
except:
|
| 388 |
+
lod=[]
|
| 389 |
+
pass
|
| 390 |
+
block = {'index': len(lod) + 1,
|
| 391 |
+
'timestamp': str(datetime.datetime.now()),
|
| 392 |
+
'sender': f'{send}',
|
| 393 |
+
'recipient':f'{rec}',
|
| 394 |
+
|
| 395 |
+
'amount': f'{amount}'}
|
| 396 |
+
lod.append(block)
|
| 397 |
+
|
| 398 |
+
json_object = json.dumps(lod, indent=4)
|
| 399 |
+
with open("tmp1.json", "w") as outfile:
|
| 400 |
+
outfile.write(json_object)
|
| 401 |
+
try:
|
| 402 |
+
api.upload_file(
|
| 403 |
+
path_or_fileobj="tmp1.json",
|
| 404 |
+
path_in_repo=f'{main_balance.split('main/',1)[1]}/{send}.json',
|
| 405 |
+
repo_id=main_balance.split('datasets/',1)[1].split('/raw',1)[0],
|
| 406 |
+
token=token_self,
|
| 407 |
+
repo_type="dataset",
|
| 408 |
+
)
|
| 409 |
+
os.remove("tmp1.json")
|
| 410 |
+
lod.clear()
|
| 411 |
+
except Exception as e:
|
| 412 |
+
mes=e
|
| 413 |
+
pass
|
| 414 |
+
|
| 415 |
+
try:
|
| 416 |
+
r = requests.get(f'{main_balance}/{rec}')
|
| 417 |
+
lod = json.loads(r.text)
|
| 418 |
+
except:
|
| 419 |
+
lod=[]
|
| 420 |
+
pass
|
| 421 |
+
block = {'index': len(lod) + 1,
|
| 422 |
+
'timestamp': str(datetime.datetime.now()),
|
| 423 |
+
'sender': f'{send}',
|
| 424 |
+
'recipient':f'{rec}',
|
| 425 |
+
|
| 426 |
+
'amount': f'{amount}'}
|
| 427 |
+
lod.append(block)
|
| 428 |
+
|
| 429 |
+
json_object = json.dumps(lod, indent=4)
|
| 430 |
+
with open("tmp1.json", "w") as outfile:
|
| 431 |
+
outfile.write(json_object)
|
| 432 |
+
try:
|
| 433 |
+
api.upload_file(
|
| 434 |
+
path_or_fileobj="tmp1.json",
|
| 435 |
+
path_in_repo=f'{main_balance.split('main/',1)[1]}/{rec}',
|
| 436 |
+
repo_id=main_balance.split('datasets/',1)[1].split('/raw',1)[0],
|
| 437 |
+
token=token_self,
|
| 438 |
+
repo_type="dataset",
|
| 439 |
+
)
|
| 440 |
+
os.remove("tmp1.json")
|
| 441 |
+
|
| 442 |
+
except Exception as e:
|
| 443 |
+
mes = e
|
| 444 |
+
pass
|
| 445 |
+
if mes = None:
|
| 446 |
+
mes = f'{amount} sent from {send} to {rec}'
|
| 447 |
+
return mes
|
| 448 |
+
|
| 449 |
with gr.Blocks() as bc:
|
| 450 |
with gr.Row(visible=True) as invalid:
|
| 451 |
pass_box = gr.Textbox()
|
|
|
|
| 483 |
node_space=gr.Textbox(label="Node Space")
|
| 484 |
node_file=gr.Textbox(label="Node File")
|
| 485 |
node_add=gr.Button("Add Node")
|
| 486 |
+
with gr.Accordion("Tokens", open=False):
|
| 487 |
+
issue_btn=gr.Button()
|
| 488 |
|
| 489 |
+
issue_btn.click(issue_tokens,[send,rec,am],block_text)
|
| 490 |
+
|
| 491 |
node_add.click(add_node,[this_space,node_repo,node_space,node_file],block_text)
|
| 492 |
pass_btn.click(checkp,pass_box,[invalid,valida])
|
| 493 |
in_chain_btn.click(get_chain,[chain_repo,chain_n],[chain_json,block_text])
|