File size: 5,685 Bytes
e48ab6b
 
 
 
 
cf7a07e
5c54d1b
7cc0b92
e48ab6b
 
5c54d1b
750173b
669a4c0
e48ab6b
24fbd15
 
e48ab6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24fbd15
 
e48ab6b
 
 
 
 
 
 
9e16fe0
e48ab6b
 
 
 
 
 
 
 
9e16fe0
e48ab6b
 
 
9e16fe0
e48ab6b
 
 
 
 
9e16fe0
e48ab6b
 
 
 
 
 
06f7edb
e48ab6b
 
 
c42a6a7
e48ab6b
 
 
 
 
06f7edb
e48ab6b
 
 
 
 
 
c42a6a7
e48ab6b
ccdb9b2
24fbd15
96906d9
 
91a2d51
e48ab6b
 
96906d9
e48ab6b
 
 
ccdb9b2
dee7d82
 
 
 
 
 
a24216b
dee7d82
 
24fbd15
 
 
96906d9
 
f33f723
 
 
 
 
96906d9
 
 
24fbd15
96906d9
 
92d7e15
 
 
 
24fbd15
8ad6bff
81ec765
8ad6bff
 
98df8de
 
4350de7
81ec765
4350de7
98df8de
7cc0b92
 
 
 
 
98df8de
 
 
0315d84
 
98df8de
96906d9
 
cc5346d
96906d9
 
 
 
 
fd2e34b
96906d9
 
 
e48ab6b
f223851
e48ab6b
 
 
 
47fb575
e48ab6b
 
 
 
 
 
47fb575
e48ab6b
 
 
 
 
 
47fb575
e48ab6b
 
 
 
 
 
47fb575
e48ab6b
 
 
 
 
 
47fb575
e48ab6b
 
96906d9
 
 
 
47fb575
96906d9
 
98df8de
 
 
 
47fb575
98df8de
1ba73e4
 
 
 
 
47fb575
1ba73e4
e48ab6b
 
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
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 = "petals-team/StableBeluga2"
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)>=10:
            proof(model_name)        
            new_block = blockchain.new_block()
        
        return "ok"
    else:
        return "fail"

def proof(model_name):
    peers = get_peers(model_name)
    for id in blockchain.user_gpus:
        if not get_p(id) in peers:
            add_transaction(id, "out", 0)
    

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)