Omnibus commited on
Commit
9053d54
·
1 Parent(s): 1848074

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -20
app.py CHANGED
@@ -6,6 +6,7 @@ import json
6
  import os
7
  import requests
8
  from blockchain import Blockchain
 
9
  from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
10
 
11
  main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/chains/'
@@ -201,8 +202,8 @@ class Blockchain1:
201
 
202
  return out, ind, mes
203
  #def check_balance(sender,amount):
204
-
205
 
 
206
 
207
  def bc_transactions(sender,recipient,amount):
208
  mes, out = issue_tokens(sender,recipient,amount)
@@ -381,12 +382,54 @@ def add_node(this_space,repo,space,chain_file):
381
  pass
382
 
383
 
384
-
385
-
386
- start_tokens = 10000000
387
- source="Main"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  def issue_tokens(send,rec,amount):
389
- #print(f"{api.whoami(['name'])}")
390
  #repo = f'omnibus/{space}'
391
  #is_valid='True'
392
  try:
@@ -394,8 +437,9 @@ def issue_tokens(send,rec,amount):
394
  if z == True:
395
  mes = None
396
  try:
397
- r = requests.get(f'{main_balance}{send}.json')
398
- lod = json.loads(r.text)
 
399
  p=True
400
  except:
401
  lod=[]
@@ -403,10 +447,13 @@ def issue_tokens(send,rec,amount):
403
  mes = "Sender has no wallet"
404
  pass
405
  if p==True:
406
- balance = lod[-1]["balance"]
407
  print (balance)
408
  balance =int(balance)-int(amount)
409
  if balance >=0:
 
 
 
410
  block = {'index': len(lod) + 1,
411
  'timestamp': str(datetime.datetime.now()),
412
  'sender': f'{send}',
@@ -469,6 +516,7 @@ def issue_tokens(send,rec,amount):
469
  pass
470
  if mes == None:
471
  mes = f'{amount} sent from {send} to {rec}'
 
472
  if balance < 0:
473
  mes ="Not enough tokens"
474
  p = False
@@ -479,18 +527,15 @@ def issue_tokens(send,rec,amount):
479
  mes = "Blockchain not loaded?"
480
  p=False
481
  return mes,p
482
- #api = HfApi(token=token)
483
- repo = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[0]
484
- name = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[1]
485
 
486
- f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset"))
487
- send_list =[]
488
- for i,ea in enumerate(f_ist):
489
- if "balance/" in ea:
490
- try:
491
- send_list.append(ea.split("/",1)[1].split(".",1)[0])
492
- except Exception:
493
- pass
494
  with gr.Blocks() as bc:
495
  with gr.Row(visible=True) as invalid:
496
  pass_box = gr.Textbox()
 
6
  import os
7
  import requests
8
  from blockchain import Blockchain
9
+ from mychain import MyChain
10
  from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
11
 
12
  main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/chains/'
 
202
 
203
  return out, ind, mes
204
  #def check_balance(sender,amount):
 
205
 
206
+ ######################## BLOCKCHAIN START ###############################
207
 
208
  def bc_transactions(sender,recipient,amount):
209
  mes, out = issue_tokens(sender,recipient,amount)
 
382
  pass
383
 
384
 
385
+
386
+ #api = HfApi(token=token)
387
+ repo = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[0]
388
+ name = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[1]
389
+
390
+ f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset"))
391
+ send_list =[]
392
+ for i,ea in enumerate(f_ist):
393
+ if "balance/" in ea:
394
+ try:
395
+ send_list.append(ea.split("/",1)[1].split(".",1)[0])
396
+ except Exception:
397
+ pass
398
+
399
+
400
+ ############################## BLOCKCHAIN END ###############################
401
+ ############################## MYCHAIN START ###############################
402
+
403
+
404
+ def get_my_chain(sender_name=None):
405
+ try:
406
+ r = requests.get(f'{main_balance}{chain_name}.json')
407
+ global mychain
408
+ mychain = MyChain(chain_load=main_balance,load=r.text)
409
+ response = {'chain': mychain.chain,
410
+ 'length': len(mychain.chain)}
411
+ message = f"Blockchain loaded from: {main_balance}{chain_name}.json"
412
+ return response,message
413
+ except:
414
+ message = f"Error loading from: {sender_name}"
415
+ return ["Error Loading Chain"],message
416
+
417
+ def mychain_transaction(sender,recipient,amount):
418
+ mes, out = issue_tokens(sender,recipient,amount)
419
+ if out == True:
420
+ mychain.new_transaction(f"{sender}",f"{recipient}",f"{amount}",f"{balance}")
421
+ message = "Transaction Added to Pool"
422
+ data = pd.DataFrame(mychain.pending_transactions)
423
+ if out == False:
424
+ message = mes
425
+ data = None
426
+ return data,message,None,None,None
427
+
428
+
429
+
430
+
431
  def issue_tokens(send,rec,amount):
432
+ print(f"CURRENT USER:: {api.whoami(['name'])}")
433
  #repo = f'omnibus/{space}'
434
  #is_valid='True'
435
  try:
 
437
  if z == True:
438
  mes = None
439
  try:
440
+ response,message = get_my_chain(send)
441
+ #r = requests.get(f'{main_balance}{send}.json')
442
+ lod = json.loads(response)
443
  p=True
444
  except:
445
  lod=[]
 
447
  mes = "Sender has no wallet"
448
  pass
449
  if p==True:
450
+ balance = lod["chain"][-1]["balance"]
451
  print (balance)
452
  balance =int(balance)-int(amount)
453
  if balance >=0:
454
+ p==True
455
+ '''
456
+ #mychain.new_transaction(sender=send, recipient=rec, amount=amount, balance=balance)
457
  block = {'index': len(lod) + 1,
458
  'timestamp': str(datetime.datetime.now()),
459
  'sender': f'{send}',
 
516
  pass
517
  if mes == None:
518
  mes = f'{amount} sent from {send} to {rec}'
519
+ '''
520
  if balance < 0:
521
  mes ="Not enough tokens"
522
  p = False
 
527
  mes = "Blockchain not loaded?"
528
  p=False
529
  return mes,p
 
 
 
530
 
531
+
532
+ ############################## MYCHAIN END ###############################
533
+
534
+
535
+
536
+
537
+
538
+
539
  with gr.Blocks() as bc:
540
  with gr.Row(visible=True) as invalid:
541
  pass_box = gr.Textbox()