Spaces:
Sleeping
Sleeping
from threading import Thread | |
import gradio as gr | |
import inspect | |
from gradio import routes | |
from typing import List, Type | |
import requests, os, re, asyncio, json | |
import math, random | |
import time | |
import datetime | |
import hashlib | |
from Blockchain import Blockchain | |
loop = asyncio.get_event_loop() | |
# init code | |
def get_types(cls_set: List[Type], component: str): | |
docset = [] | |
types = [] | |
if component == "input": | |
for cls in cls_set: | |
doc = inspect.getdoc(cls) | |
doc_lines = doc.split("\n") | |
docset.append(doc_lines[1].split(":")[-1]) | |
types.append(doc_lines[1].split(")")[0].split("(")[-1]) | |
else: | |
for cls in cls_set: | |
doc = inspect.getdoc(cls) | |
doc_lines = doc.split("\n") | |
docset.append(doc_lines[-1].split(":")[-1]) | |
types.append(doc_lines[-1].split(")")[0].split("(")[-1]) | |
return docset, types | |
routes.get_types = get_types | |
# App code | |
account_list = dict() | |
account_list['id'] = "pass" | |
name_list = dict() | |
name_list['id'] = 'name' | |
p2p_list = dict() | |
p2p_list['id'] = '11111111' | |
gpu_add_list = [] | |
def register(id, pw): | |
if id in account_list: | |
return "exist" | |
else: | |
account_list[id] = pw | |
return "ok" | |
def login(id, pw): | |
if id in account_list: | |
if account_list[id] == pw: | |
return "ok" | |
else: | |
return "password error" | |
else: | |
return "no id" | |
def add_name(id, name): | |
name_list[id] = name | |
return "ok" | |
def get_name(id): | |
if id in name_list: | |
return name_list[id] | |
else: | |
return "no id" | |
def get_id(name): | |
reverse_dict= dict(map(reversed,name_list.items())) | |
if name in reverse_dict: | |
return reverse_dict[name] | |
else: | |
return "no name" | |
def add_p(id, p_id): | |
p2p_list[id] = p_id | |
return "ok" | |
def get_p(id): | |
if id in p2p_list: | |
return p2p_list[id] | |
else: | |
return "no id" | |
def get_id_from_p2p(i): | |
reverse_dict= dict(map(reversed,p2p_list.items())) | |
if i in reverse_dict: | |
return reverse_dict[i] | |
else: | |
return "no id" | |
# Blockchain code | |
model_name = "quantumaikr/llama-2-70b-fb16-korean" | |
def get_peers(model_name): | |
data = requests.get("https://health.petals.dev/api/v1/state").json() | |
out = [] | |
for d in data['model_reports']: | |
if d['name'] == model_name: | |
for r in d['server_rows']: | |
out.append(r['peer_id']) | |
return out | |
def get_used_gpus(model_name): | |
data = requests.get("https://health.petals.dev/api/v1/state").json() | |
out = [] | |
for d in data['model_reports']: | |
if d['name'] == model_name: | |
return round(d['num_blocks']*0.45, 2) | |
return 0 | |
blockchain = Blockchain() | |
def add_transaction(id, kind, data): | |
if kind == "add" or kind == "inference" or kind == "out": | |
blockchain.new_transaction(id, kind, data) | |
if len(blockchain.current_transactions)>=5: | |
peers = get_peers(model_name) | |
for id in blockchain.user_gpus: | |
if not get_p(id) in peers: | |
blockchain.new_transaction(id, "out", 0) | |
new_block = blockchain.new_block() | |
return "ok" | |
else: | |
return "fail" | |
def get_coin(id): | |
c = blockchain.get_user_balance(id) | |
return c | |
def get_gpus(): | |
output = {} | |
for id, mem in blockchain.user_gpus.items(): | |
output[get_name(id)]=mem | |
return output | |
def used_gpu(): | |
output = 0 | |
for mem in get_gpus(): | |
output += mem * (5 + random.randrange(1,4)/10) | |
return output | |
def get_data(): | |
output = [] | |
output.append({"gpus":get_gpus(), "total":{"total" : blockchain.get_total_gpu_mem(), "used":get_used_gpus(model_name)}, "chain":blockchain.chain}) | |
return output[0] | |
def chat(id, npc, prompt): | |
if get_coin(id) == 0: | |
return "no coin" | |
# model inference | |
output = "AI ์๋ต์ ๋๋ค." | |
add_transaction(id, "inference", {"prompt":prompt, "output":output}) | |
return output | |
with gr.Blocks() as demo: | |
rr = gr.Interface( | |
fn=register, | |
inputs=["text", "text"], | |
outputs="text", | |
description="register, ํ์๊ฐ์ (์ฑ๊ณต์:ok, ์ค๋ณต์:exist ๋ฐํ)\n /run/predict", | |
) | |
ll = gr.Interface( | |
fn=login, | |
inputs=["text", "text"], | |
outputs="text", | |
description="login, ๋ก๊ทธ์ธ(์ฑ๊ณต์: ok, ์คํจ์: password error, ์์ด๋๊ฐ ์์ผ๋ฉด: no id) \n /run/predict_", | |
) | |
ad = gr.Interface( | |
fn=add_name, | |
inputs=["text", "text"], | |
outputs="text", | |
description="add_name, id๋ก ๋๋ค์ ์ถ๊ฐ. ok ๋ฐํ.\n /run/predict_2", | |
) | |
nn = gr.Interface( | |
fn=get_name, | |
inputs=["text"], | |
outputs="text", | |
description="get_name, id๋ก ๋๋ค์ ๋ฐํ(์์ผ๋ฉด no id)\n /run/predict_3", | |
) | |
adp = gr.Interface( | |
fn=add_p, | |
inputs=["text", "text"], | |
outputs="text", | |
description="add_p, id๋ก p2p id ์ถ๊ฐ. ok ๋ฐํ. \n /run/predict_4", | |
) | |
addp = gr.Interface( | |
fn=add_transaction, | |
inputs=["text", "text", "text"], | |
outputs="text", | |
description="add_transaction \n /run/predict_5", | |
) | |
gc = gr.Interface( | |
fn=get_coin, | |
inputs=["text"], | |
outputs="text", | |
description="get_coin, id๋ก ์์ฌ ์ฝ์ธ(ํ๋๋ ฅ) ๋ฐํ. \n /run/predict_6", | |
) | |
gd = gr.Interface( | |
fn=get_data, | |
inputs=[], | |
outputs="text", | |
description="get_data, ์๊ฐํ์ฉ ๋ชจ๋ ๋ฐ์ดํฐ ๋ฐํ. gpu๊ธฐ์ฌ ๋ชฉ๋ก, total/used, chain \n /run/predict_7", | |
) | |
demo.queue(max_size=32).launch(enable_queue=True) | |