api_for_unity / app.py
ldhldh's picture
Update app.py
98df8de
raw
history blame
4.67 kB
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
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 chat(id, prompt):
return "AI μ‘λ‹΅μž…λ‹ˆλ‹€."
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
def get_peers(name):
data = requests.get("https://health.petals.dev/api/v1/state").json()
out = []
for d in data['model_reports']:
if d['name'] == "petals-team/StableBeluga2":
for r in d['server_rows']:
out.append(r['peer_id'])
return out
blockchain = Blockchain()
def add_transaction(id, kind, data):
blockchain.new_transaction(id, kind, data)
def proof(name):
peers = get_peers(name)
for p in gpu_add_list:
if not p in peers:
add_transaction(get_id_from_p2p(peer), "out", 0)
def get_coin(id):
c = Blockchain.get_user_balance(id)
return c
def get_gpus():
outout = []
for id, mem in Blockchain.user_gpus.items():
outout.append({"name":get_name(id),"gpu":mem})
return output
def get_data():
output = []
output.append({"gpus":get_gpus()})
output.append({"total":{"total" : Blockchain.get_total_gpu_mem, "used":38}})
output.append({"chain":blockchain.chain})
return output
with gr.Blocks() as demo:
count = 0
aa = gr.Interface(
fn=chat,
inputs=["text","text"],
outputs="text",
description="chat, ai 응닡을 λ°˜ν™˜ν•©λ‹ˆλ‹€. λ‚΄λΆ€μ μœΌλ‘œ νŠΈλžœμž­μ…˜ 생성. \n /run/predict",
)
rr = gr.Interface(
fn=register,
inputs=["text", "text"],
outputs="text",
description="register, νšŒμ›κ°€μž…(μ„±κ³΅μ‹œ:ok, μ€‘λ³΅μ‹œ:exist λ°˜ν™˜)\n /run/predict_1",
)
ll = gr.Interface(
fn=login,
inputs=["text", "text"],
outputs="text",
description="login, 둜그인(μ„±κ³΅μ‹œ: ok, μ‹€νŒ¨μ‹œ: password error, 아이디가 μ—†μœΌλ©΄: no id) \n /run/predict_2",
)
ad = gr.Interface(
fn=add_name,
inputs=["text", "text"],
outputs="text",
description="add_name, id둜 λ‹‰λ„€μž„ μΆ”κ°€. ok λ°˜ν™˜.\n /run/predict_3",
)
nn = gr.Interface(
fn=get_name,
inputs=["text"],
outputs="text",
description="get_name, id둜 λ‹‰λ„€μž„ λ°˜ν™˜(μ—†μœΌλ©΄ no id)\n /run/predict_4",
)
adp = gr.Interface(
fn=add_p,
inputs=["text", "text"],
outputs="text",
description="add_p, id둜 p2p id μΆ”κ°€. ok λ°˜ν™˜. \n /run/predict_5",
)
gc = gr.Interface(
fn=get_coin,
inputs=["text"],
outputs="text",
description="get_coin, id둜 μž”μ—¬ 코인(행동λ ₯) λ°˜ν™˜. \n /run/predict_6",
)
demo.queue(max_size=32).launch(enable_queue=True)