ldhldh commited on
Commit
7b0437a
β€’
1 Parent(s): 6867517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -175
app.py CHANGED
@@ -5,11 +5,6 @@ from gradio import routes
5
  from typing import List, Type
6
 
7
  import requests, os, re, asyncio, json
8
- import math
9
- import time
10
- import datetime
11
- import hashlib
12
- from Blockchain import Blockchain
13
 
14
  loop = asyncio.get_event_loop()
15
 
@@ -34,126 +29,32 @@ routes.get_types = get_types
34
 
35
  # App code
36
 
37
- account_list = dict()
38
-
39
- account_list['id'] = "pass"
40
-
41
- name_list = dict()
42
- name_list['id'] = 'name'
43
-
44
- p2p_list = dict()
45
- p2p_list['id'] = '11111111'
46
-
47
- gpu_add_list = []
48
-
49
-
50
- def register(id, pw):
51
- if id in account_list:
52
- return "exist"
53
- else:
54
- account_list[id] = pw
55
- return "ok"
56
-
57
- def 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
-
66
- def add_name(id, name):
67
- name_list[id] = name
68
- return "ok"
69
-
70
- def get_name(id):
71
- if id in name_list:
72
- return name_list[id]
73
- else:
74
- return "no id"
75
-
76
- def 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
-
83
- def add_p(id, p_id):
84
- p2p_list[id] = p_id
85
- return "ok"
86
-
87
- def get_p(id):
88
- if id in p2p_list:
89
- return p2p_list[id]
90
- else:
91
- return "no id"
92
-
93
- def 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 code
101
-
102
-
103
- model_name = "petals-team/StableBeluga2"
104
- def 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 out
112
-
113
- blockchain = Blockchain()
114
-
115
- def add_transaction(id, kind, data):
116
- if kind == "add" or kind == "inference" or kind == "out":
117
- blockchain.new_transaction(id, kind, data)
118
- return "ok"
119
- else:
120
- return "fail"
121
-
122
- def proof(model_name):
123
- peers = get_peers(model_name)
124
- for p in gpu_add_list:
125
- if not p in peers:
126
- add_transaction(get_id_from_p2p(peer), "out", 0)
127
-
128
- def get_coin(id):
129
- c = blockchain.get_user_balance(id)
130
- return c
131
-
132
-
133
- def get_gpus():
134
- output = []
135
- for id, mem in blockchain.user_gpus.items():
136
- output.append({"name":get_name(id),"gpu":mem})
137
- return output
138
-
139
- def get_data():
140
- output = []
141
- output.append({"gpus":get_gpus(), "total":{"total" : blockchain.get_total_gpu_mem(), "used":38}, "chain":blockchain.chain})
142
-
143
- return output
144
-
145
-
146
  def chat(id, npc, prompt):
147
- if get_coin(id) == 0:
 
 
 
 
 
 
 
 
148
  return "no coin"
149
 
150
  # model inference
151
  output = "AI μ‘λ‹΅μž…λ‹ˆλ‹€."
152
- add_transaction(id, "inference", {"prompt":prompt, "output":output})
153
 
154
- if len(blockchain.current_transactions)>=10:
155
- proof(model_name)
156
- new_block = blockchain.new_block()
 
 
 
 
 
 
 
 
157
  return output
158
 
159
 
@@ -165,61 +66,5 @@ with gr.Blocks() as demo:
165
  outputs="text",
166
  description="chat, ai 응닡을 λ°˜ν™˜ν•©λ‹ˆλ‹€. λ‚΄λΆ€μ μœΌλ‘œ νŠΈλžœμž­μ…˜ 생성. \n /run/predict",
167
  )
168
-
169
- rr = gr.Interface(
170
- fn=register,
171
- inputs=["text", "text"],
172
- outputs="text",
173
- description="register, νšŒμ›κ°€μž…(μ„±κ³΅μ‹œ:ok, μ€‘λ³΅μ‹œ:exist λ°˜ν™˜)\n /run/predict_1",
174
- )
175
-
176
- ll = gr.Interface(
177
- fn=login,
178
- inputs=["text", "text"],
179
- outputs="text",
180
- description="login, 둜그인(μ„±κ³΅μ‹œ: ok, μ‹€νŒ¨μ‹œ: password error, 아이디가 μ—†μœΌλ©΄: no id) \n /run/predict_2",
181
- )
182
-
183
- ad = gr.Interface(
184
- fn=add_name,
185
- inputs=["text", "text"],
186
- outputs="text",
187
- description="add_name, id둜 λ‹‰λ„€μž„ μΆ”κ°€. ok λ°˜ν™˜.\n /run/predict_3",
188
- )
189
-
190
- nn = gr.Interface(
191
- fn=get_name,
192
- inputs=["text"],
193
- outputs="text",
194
- description="get_name, id둜 λ‹‰λ„€μž„ λ°˜ν™˜(μ—†μœΌλ©΄ no id)\n /run/predict_4",
195
- )
196
-
197
- adp = gr.Interface(
198
- fn=add_p,
199
- inputs=["text", "text"],
200
- outputs="text",
201
- description="add_p, id둜 p2p id μΆ”κ°€. ok λ°˜ν™˜. \n /run/predict_5",
202
- )
203
-
204
- addp = gr.Interface(
205
- fn=add_transaction,
206
- inputs=["text", "text", "text"],
207
- outputs="text",
208
- description="add_transaction \n /run/predict_6",
209
- )
210
-
211
- gc = gr.Interface(
212
- fn=get_coin,
213
- inputs=["text"],
214
- outputs="text",
215
- description="get_coin, id둜 μž”μ—¬ 코인(행동λ ₯) λ°˜ν™˜. \n /run/predict_7",
216
- )
217
-
218
- gd = gr.Interface(
219
- fn=get_data,
220
- inputs=[],
221
- outputs="text",
222
- description="get_data, μ‹œκ°ν™”μš© λͺ¨λ“  데이터 λ°˜ν™˜. gpuκΈ°μ—¬ λͺ©λ‘, total/used, chain \n /run/predict_8",
223
- )
224
 
225
  demo.queue(max_size=32).launch(enable_queue=True)
 
5
  from typing import List, Type
6
 
7
  import requests, os, re, asyncio, json
 
 
 
 
 
8
 
9
  loop = asyncio.get_event_loop()
10
 
 
29
 
30
  # App code
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def chat(id, npc, prompt):
33
+
34
+ # get_coin endpoint
35
+ response = requests.post("https://ldhldh-api-for-unity.hf.space/run/predict_6", json={
36
+ "data": [
37
+ id,
38
+ ]}).json()
39
+
40
+ coin = response["data"]
41
+ if int(coin) == 0:
42
  return "no coin"
43
 
44
  # model inference
45
  output = "AI μ‘λ‹΅μž…λ‹ˆλ‹€."
 
46
 
47
+
48
+ # add_transaction endpoint
49
+ response = requests.post("https://ldhldh-api-for-unity.hf.space/run/predict_5", json={
50
+ "data": [
51
+ id,
52
+ "inference",
53
+ {"prompt":prompt, "output":output}
54
+ ]}).json()
55
+
56
+ d = response["data"]
57
+
58
  return output
59
 
60
 
 
66
  outputs="text",
67
  description="chat, ai 응닡을 λ°˜ν™˜ν•©λ‹ˆλ‹€. λ‚΄λΆ€μ μœΌλ‘œ νŠΈλžœμž­μ…˜ 생성. \n /run/predict",
68
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  demo.queue(max_size=32).launch(enable_queue=True)