chat-at / bc_utils.py
Omnibus's picture
Create bc_utils.py
f1b09fd
raw
history blame
4.41 kB
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]