Spaces:
Runtime error
Runtime error
File size: 13,391 Bytes
785022a 089bad2 274de88 49d4a4b d9650af 62bd81a adb6843 3e020b7 61bde4a b81d2c3 9e5632d d9650af 785022a 089bad2 a656504 a364a23 a656504 d9650af 785022a ecdaa51 49c209b 211b0d2 49c209b 9887260 49c209b f2c7505 adb6843 49c209b 274de88 49c209b 274de88 49c209b adb6843 49c209b 211b0d2 49c209b 9887260 f2c7505 9887260 49c209b 274de88 49c209b 274de88 49c209b 274de88 49c209b a896de0 ecdaa51 a896de0 ecdaa51 b81d2c3 f8aa69b 36cfd06 f8aa69b b81d2c3 f8aa69b 36cfd06 f8aa69b b81d2c3 9cb8f46 080d833 b81d2c3 080d833 b81d2c3 ca41c8e b81d2c3 7a6cbf7 ca41c8e b81d2c3 7a6cbf7 8926d36 b81d2c3 8926d36 b81d2c3 1376c6a 8926d36 b81d2c3 bde08e8 b81d2c3 785022a d9650af 842bf1d 9fde426 785022a 9fde426 0425e00 a9c4c7d 9fde426 089bad2 7cdec99 089bad2 7cdec99 9fde426 5a53979 9fde426 5d5df1b 9fde426 058c475 bb04772 46d1226 058c475 e234549 879824c 91bd47e 8557582 0cd9b58 46d1226 06c929d e234549 cd913f0 e234549 cd913f0 d947750 46d1226 7cdec99 c967d02 47fa8e4 76c5a6d 46d1226 7cdec99 7bda97a cc8ff99 46d1226 06c929d 88bf8f7 0cd9b58 91bd47e 3dd1e60 058c475 9fde426 9cb8f46 089bad2 9cb8f46 ff16456 089bad2 62bd81a a364a23 4cb6097 7ceaf08 12790ec bde08e8 94df3c5 7ceaf08 3dd1e60 33a841b 49d4a4b 9cb8f46 3dece81 49d4a4b 785022a |
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
import gradio as gr
import chatchain
from chatchain import MyChainSend,MyChainRec
import crypt
import os
from huggingface_hub import (upload_file,HfApi)
import requests
import pandas as pd
import json
rec_list = []
pa=os.environ['PASS']
main_chain='https://huggingface.co/datasets/Omnibus/chat-at/raw/main/chat/'
rec_list, rec_drop = chatchain.update_rec_list(main_chain)
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 get_my_chain_send(sender_name=None):
global mychain_send
print(sender_name)
mes_error = ""
try:
r1 = requests.get(f'{main_chain}{sender_name}/{sender_name}.json')
print (f's={r1.text}')
mychain_send = chatchain.MyChainSend(chain_load=f'{main_chain}{sender_name}/',load=r1.text)
response = {'chain': mychain_send.chain,
'length': len(mychain_send.chain)}
#print (f'response={response}')
message = f"Blockchain loaded from: {main_chain}{sender_name}/{sender_name}.json"
return response,message
except Exception:
message = f"Error loading from: {sender_name}"
print (message)
return {"MESSAGE":"Error Loading Chain"},message
def get_my_chain_rec(recipient_name=None):
global mychain_rec
print(recipient_name)
try:
r2 = requests.get(f'{main_chain}{recipient_name}/{recipient_name}.json')
print (f'r={r2.text}')
mychain_rec = MyChainRec(chain_load=f'{main_chain}{recipient_name}/',load=r2.text)
response = {'chain': mychain_rec.chain,
'length': len(mychain_rec.chain)}
message = f"Blockchain loaded from: {main_chain}{recipient_name}/{recipient_name}.json"
return response,message
except Exception:
try:
mychain_rec = MyChainRec(chain_load=main_chain,create=recipient_name)
response = {'chain': mychain_rec.chain,
'length': len(mychain_rec.chain)}
message = f"Blockchain loaded from: {main_chain}{recipient_name}/{recipient_name}.json"
return response,message
except Exception:
message = f"Error loading from: {recipient_name}"
return ["Error Loading Chain"],message
def display_chain_send():
response = {'chain': mychain_send.chain,
'length': len(mychain_send.chain)}
return response
def display_chain_rec():
response = {'chain': mychain_rec.chain,
'length': len(mychain_rec.chain)}
return response
def valid_send():
valid,ind,mes = mychain_send.chain_valid(mychain_send.chain)
if valid:
response = 'The Blockchain is valid.'
z=True
else:
response = f'Sender Blockchain is not valid. {mes} at Index {ind}'
z=False
return response,z,mes,ind
def valid_rec():
valid,ind,mes = mychain_rec.chain_valid(mychain_rec.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, mes, ind
def mychain_mine_block_send(chain_r=None,chain_n=None):
previous_block = mychain_send.print_previous_block()
previous_proof = previous_block['proof']
proof = mychain_send.proof_of_work(previous_proof)
previous_hash = mychain_send.hash(previous_block)
block = mychain_send.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_send()
if len(mychain_send.chain) > 1000:
mychain_send.reset()
response = None
show_chain=display_chain_send()
message = "New Chain Created at Max 20 Blocks"
return response, show_chain,message
def mychain_mine_block_rec(chain_r=None,chain_n=None):
previous_block = mychain_rec.print_previous_block()
previous_proof = previous_block['proof']
proof = mychain_rec.proof_of_work(previous_proof)
previous_hash = mychain_rec.hash(previous_block)
block = mychain_rec.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_rec()
if len(mychain_rec.chain) > 1000:
mychain_rec.reset()
response = None
show_chain=display_chain_rec()
message = "New Chain Created at Max 20 Blocks"
return response, show_chain, message
##############################################
############################################
def send_message(send,rec,message):
response_send={}
response_rec={}
show_chain_send={}
show_chain_rec={}
data_send=None
data_rec=None
rec_send=None
rec_drop=None
message_send=None
message_rec=None
trans_data=None
trans_mes=None
balance_send = 0
balance_rec = 0
mes = "blank message"
try:
response,message_out = get_my_chain_send(send)
print(f'response1:: {response}')
print(f'message1:: {message_out}')
send_rec = response
response,message_out = get_my_chain_rec(rec)
print(f'response2:: {response}')
print(f'message2:: {message_out}')
lod_rec = response
p=True
except Exception as e:
mes = f"An Error Occured: {e}"
p=False
#print (mes)
if p==False:
return (mes, p,None,None,None,None,None,None,None,None,None,None,None)
if p==True:
print(mychain_send.chain[0]["message"])
mychain_send.new_transaction(f"{send}",f"{rec}",f"{message}",mychain_send.chain[0]["message"])
message_send = "Transaction Added to Pool"
data_send = pd.DataFrame(mychain_send.pending_transactions)
mychain_rec.new_transaction(f"{send}",f"{rec}",f"{message}",mychain_rec.chain[0]["message"])
message_rec = "Transaction Added to Pool"
data_rec = pd.DataFrame(mychain_rec.pending_transactions)
response_send, show_chain_send, message_send = mychain_mine_block_send(chain_r=None,chain_n=send)
#mychain_mine_block_trans(balance_send, chain_r=None,chain_n=send)
response_rec, show_chain_rec, message_rec = mychain_mine_block_rec(chain_r=None,chain_n=rec)
mes = (f'Send: {message_send} :: Recieve: {message_rec}')
#_,rec_send=update_send_list()
_,rec_drop=chatchain.update_rec_list(main_chain)
#trans_bx = merge_trans()
#trans_data, mes = bc_utils.bc_transactions(trans_bx)
return (mes,show_chain_send)
with gr.Blocks() as app:
with gr.Row(visible=True) as invalid:
pass_box = gr.Textbox()
pass_btn = gr.Button()
with gr.Group(visible=False) as valida:
gr.Column()
with gr.Column():
with gr.Row():
with gr.Tab("Messages"):
with gr.Accordion("Key"):
input_key = gr.Image(label="Key",type="filepath")
with gr.Row():
with gr.Column():
sender=gr.Textbox(label = "Sender", interactive = False)
rec=gr.Dropdown(label="Recipient", choices=[f for f in rec_list], allow_custom_value=True, interactive=True)
send_mes=gr.Textbox(label="Message", lines=6)
send_mes_btn=gr.Button()
with gr.Column():
block_text = gr.Textbox(label = "System", interactive = False)
rec_mes = gr.JSON()
response_json=gr.JSON()
#rec_mes = gr.Textbox(lines=6)
with gr.Tab("BC"):
with gr.Row():
with gr.Tab("Gen Wal"):
gen_wal_btn=gr.Button()
seed = gr.Textbox(label='Seed Phrase')
img1=gr.Image(label='Private Key',type='filepath')
out1 = gr.Textbox(label='Private Key',max_lines=4)
img2=gr.Pil(label='Public Key')
out2 = gr.Textbox(label='Public Key',max_lines=4)
img3=gr.Pil(label='Address')
out3 = gr.Textbox(label='Address')
with gr.Tab("Encrypt"):
rsa_to_enc = gr.Textbox(label="txt to encrypt")
pub_key_in = gr.Image(label="Public Key", type="filepath")
priv_key_in1 = gr.Image(label="Private Key(sig)", type="filepath")
rsa_enc_btn = gr.Button("RSA Encrypt")
rsa_enc_mes = gr.Textbox(label="encoded", max_lines=4)
qr_enc_mes = gr.Image(type="filepath")
with gr.Tab("Decrypt"):
mes_in = gr.Image(label="Message", type="filepath")
priv_key_in = gr.Image(label="Private Key", type="filepath")
rsa_dec_btn = gr.Button("RSA Decrypt")
rsa_dec_mes = gr.Textbox(label="decoded")
in_chain=gr.Textbox(value=main_chain,visible=False)
gr.Column()
def display_messages(im):
out = []
address,key=crypt.address(im)
response, message_out = get_my_chain_send(address)
#print (f'response :: {response}')
#[demoDictionary[dict_key] for dict_key in demoDictionary]
#for ip,dictValue in enumerate(response):
for i,ip in enumerate(response['chain']):
#print (f'i === {i}')
#print (f'ip === {ip}')
if i == 0:
pass
else:
try:
ts = response['chain'][i]['timestamp']
ea = response['chain'][i]['message']
#print (ea)
ea = ea[0]
#print (ea)
dec = crypt.decrypt_trans(ea,im)
print(f'dec === {dec}')
dec=eval(dec)
#json_dec=json.loads(f"{dec}")
#dec = dec.strip('"')
out1 = {
'timestamp':ts,
'sender': dec['sender'] if dec['sender'] != address else 'Sender',
'recipient': dec['recipient'] if dec['recipient'] != address else 'Recipient',
'message': dec['message']
}
out.append(out1)
#out.append(dec.strip('"'))
#out.append("#########################")
#out.append(response['chain'][i]['message'])
except Exception as e:
print (f'decrypt error ::: {e}')
pass
#out.append(response[ip]["message"] for ip in response)
return (address,message_out,response,out)
def test_fn(im):
return (im)
def create_new_chain(address,pub_key,im):
address = str(address.strip("b").strip("'"))
mychain_rec = chatchain.MyChainRec(chain_load=f'{main_chain}{address}/',create=address,pub_key=pub_key)
chatchain.store_image(img=im,chain_load=f'{main_chain}',address=address)
response = {'chain': mychain_rec.chain,
'length': len(mychain_rec.chain)}
message = f"Blockchain loaded from: {main_chain}{address}.json"
#send_list,send_drop = update_send_list()
rec_list,rec_drop = chatchain.update_rec_list()
#rec_drop = gr.Dropdown.update(label="Recipient", choices=[f for f in rec_list])
return response,message,rec_drop
def rec_list_up(inp):
_,out = chatchain.update_rec_list(inp)
return (out)
send_mes_btn.click(send_message,[sender,rec,send_mes],[block_text,response_json])
input_key.change(display_messages,input_key,[sender,block_text,response_json,rec_mes]).then(rec_list_up,in_chain,rec)
pass_btn.click(checkp,pass_box,[invalid,valida])
gen_wal_btn.click(crypt.generate_keys,None,[out2,out1,img3,out3,img1,img2]).then(create_new_chain,[out3,out2,img1],[response_json,block_text,rec]).then(test_fn,[img1],[input_key])
rsa_enc_btn.click(crypt.encrypt_text,[rsa_to_enc,pub_key_in,priv_key_in1,out3],[rsa_enc_mes,qr_enc_mes])
rsa_dec_btn.click(crypt.decrypt_text,[mes_in,priv_key_in],rsa_dec_mes)
app.launch() |