ldhldh/api_for_unity
0
1from threading import Thread2import gradio as gr3import inspect4from gradio import routes5from typing import List, Type6 7import requests, os, re, asyncio, json8import math, random9import time10import datetime11import hashlib12from Blockchain import Blockchain13 14loop = asyncio.get_event_loop()15 16# init code17def get_types(cls_set: List[Type], component: str):18 docset = []19 types = []20 if component == "input":21 for cls in cls_set:22 doc = inspect.getdoc(cls)23 doc_lines = doc.split("\n")24 docset.append(doc_lines[1].split(":")[-1])25 types.append(doc_lines[1].split(")")[0].split("(")[-1])26 else:27 for cls in cls_set:28 doc = inspect.getdoc(cls)29 doc_lines = doc.split("\n")30 docset.append(doc_lines[-1].split(":")[-1])31 types.append(doc_lines[-1].split(")")[0].split("(")[-1])32 return docset, types33routes.get_types = get_types34 35# App code36 37account_list = dict()38 39account_list['id'] = "pass"40 41name_list = dict()42name_list['id'] = 'name'43 44p2p_list = dict()45p2p_list['id'] = '11111111'46 47gpu_add_list = []48 49 50def register(id, pw):51 if id in account_list:52 return "exist"53 else:54 account_list[id] = pw55 return "ok"56 57def login(id, pw):58 if id in account_list:59 if account_list[id] == pw:60 return "ok"61 else:62 return "password error"63 else:64 return "no id"65 66def add_name(id, name):67 name_list[id] = name68 return "ok"69 70def get_name(id):71 if id in name_list:72 return name_list[id]73 else:74 return "no id"75 76def get_id(name):77 reverse_dict= dict(map(reversed,name_list.items()))78 if name in reverse_dict:79 return reverse_dict[name]80 else:81 return "no name"82 83def add_p(id, p_id):84 p2p_list[id] = p_id85 return "ok"86 87def get_p(id):88 if id in p2p_list:89 return p2p_list[id]90 else:91 return "no id"92 93def get_id_from_p2p(i):94 reverse_dict= dict(map(reversed,p2p_list.items()))95 if i in reverse_dict:96 return reverse_dict[i]97 else:98 return "no id"99 100# Blockchain code101 102 103model_name = "quantumaikr/llama-2-70b-fb16-korean"104def get_peers(model_name):105 data = requests.get("https://health.petals.dev/api/v1/state").json()106 out = []107 for d in data['model_reports']:108 if d['name'] == model_name:109 for r in d['server_rows']:110 out.append(r['peer_id'])111 return out112 113 114def get_used_gpus(model_name):115 data = requests.get("https://health.petals.dev/api/v1/state").json()116 out = []117 for d in data['model_reports']:118 if d['name'] == model_name:119 return round(d['num_blocks']*0.45, 2)120 return 0121 122blockchain = Blockchain()123 124def add_transaction(id, kind, data):125 if kind == "add" or kind == "inference" or kind == "out":126 blockchain.new_transaction(id, kind, data)127 128 if len(blockchain.current_transactions)>=5:129 peers = get_peers(model_name)130 for id in blockchain.user_gpus:131 if not get_p(id) in peers:132 blockchain.new_transaction(id, "out", 0) 133 new_block = blockchain.new_block()134 135 return "ok"136 else:137 return "fail"138 139 140def get_coin(id):141 c = blockchain.get_user_balance(id)142 return c143 144 145def get_gpus():146 output = {}147 for id, mem in blockchain.user_gpus.items():148 output[get_name(id)]=mem149 return output150def used_gpu():151 output = 0152 for mem in get_gpus():153 output += mem * (5 + random.randrange(1,4)/10)154 return output155 156def get_data():157 output = []158 output.append({"gpus":get_gpus(), "total":{"total" : blockchain.get_total_gpu_mem(), "used":get_used_gpus(model_name)}, "chain":blockchain.chain})159 return output[0]160 161 162def chat(id, npc, prompt):163 if get_coin(id) == 0:164 return "no coin"165 166 # model inference167 output = "AI 응답입니다."168 add_transaction(id, "inference", {"prompt":prompt, "output":output})169 170 return output171 172 173with gr.Blocks() as demo:174 175 rr = gr.Interface(176 fn=register,177 inputs=["text", "text"],178 outputs="text",179 description="register, 회원가입(성공시:ok, 중복시:exist 반환)\n /run/predict",180 )181 182 ll = gr.Interface(183 fn=login,184 inputs=["text", "text"],185 outputs="text",186 description="login, 로그인(성공시: ok, 실패시: password error, 아이디가 없으면: no id) \n /run/predict_",187 )188 189 ad = gr.Interface(190 fn=add_name,191 inputs=["text", "text"],192 outputs="text",193 description="add_name, id로 닉네임 추가. ok 반환.\n /run/predict_2",194 )195 196 nn = gr.Interface(197 fn=get_name,198 inputs=["text"],199 outputs="text",200 description="get_name, id로 닉네임 반환(없으면 no id)\n /run/predict_3",201 )202 203 adp = gr.Interface(204 fn=add_p,205 inputs=["text", "text"],206 outputs="text",207 description="add_p, id로 p2p id 추가. ok 반환. \n /run/predict_4",208 )209 210 addp = gr.Interface(211 fn=add_transaction,212 inputs=["text", "text", "text"],213 outputs="text",214 description="add_transaction \n /run/predict_5",215 )216 217 gc = gr.Interface(218 fn=get_coin,219 inputs=["text"],220 outputs="text",221 description="get_coin, id로 잔여 코인(행동력) 반환. \n /run/predict_6",222 )223 224 gd = gr.Interface(225 fn=get_data,226 inputs=[],227 outputs="text",228 description="get_data, 시각화용 모든 데이터 반환. gpu기여 목록, total/used, chain \n /run/predict_7",229 )230 231 demo.queue(max_size=32).launch(enable_queue=True)232 