Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,187 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import hivemind
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def main_page():
|
| 29 |
-
return updater.state_html
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
def api_v1_state():
|
| 34 |
-
return app.response_class(response=updater.state_json, status=200, mimetype="application/json")
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
your_ip=request.remote_addr,
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from threading import Thread
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import inspect
|
| 4 |
+
from gradio import routes
|
| 5 |
+
from typing import List, Type
|
| 6 |
|
| 7 |
+
import requests, os, re, asyncio, queue, sys, git
|
| 8 |
+
import math
|
| 9 |
+
import time
|
| 10 |
+
import datetime
|
| 11 |
+
import requests, json
|
| 12 |
+
|
| 13 |
+
from pprint import pprint
|
| 14 |
import hivemind
|
| 15 |
+
from petals.constants import PUBLIC_INITIAL_PEERS
|
| 16 |
+
from health import fetch_health_state
|
| 17 |
+
|
| 18 |
+
dht = hivemind.DHT(initial_peers=PUBLIC_INITIAL_PEERS, client_mode=True, start=True)
|
| 19 |
+
model_name = "quantumaikr/llama-2-70b-fb16-korean"
|
| 20 |
+
|
| 21 |
+
loop = asyncio.get_event_loop()
|
| 22 |
+
# Monkey patch
|
| 23 |
+
def get_types(cls_set: List[Type], component: str):
|
| 24 |
+
docset = []
|
| 25 |
+
types = []
|
| 26 |
+
if component == "input":
|
| 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 |
+
else:
|
| 33 |
+
for cls in cls_set:
|
| 34 |
+
doc = inspect.getdoc(cls)
|
| 35 |
+
doc_lines = doc.split("\n")
|
| 36 |
+
docset.append(doc_lines[-1].split(":")[-1])
|
| 37 |
+
types.append(doc_lines[-1].split(")")[0].split("(")[-1])
|
| 38 |
+
return docset, types
|
| 39 |
+
routes.get_types = get_types
|
| 40 |
+
|
| 41 |
+
# App code
|
| 42 |
+
|
| 43 |
+
account_list = dict()
|
| 44 |
+
|
| 45 |
+
account_list['id'] = "pass"
|
| 46 |
+
|
| 47 |
+
name_list = dict()
|
| 48 |
+
name_list['id'] = 'name'
|
| 49 |
+
|
| 50 |
+
p2p_list = dict()
|
| 51 |
+
p2p_list['id'] = '11111111'
|
| 52 |
+
|
| 53 |
+
def chat(x):
|
| 54 |
+
|
| 55 |
+
return "AI μλ΅μ
λλ€."
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def register(id, pw):
|
| 59 |
+
if id in account_list:
|
| 60 |
+
return "exist"
|
| 61 |
+
else:
|
| 62 |
+
account_list[id] = pw
|
| 63 |
+
return "ok"
|
| 64 |
|
| 65 |
+
def login(id, pw):
|
| 66 |
+
if id in account_list:
|
| 67 |
+
if account_list[id] == pw:
|
| 68 |
+
return "ok"
|
| 69 |
+
else:
|
| 70 |
+
return "password error"
|
| 71 |
+
else:
|
| 72 |
+
return "no id"
|
| 73 |
|
| 74 |
+
def add_name(id, name):
|
| 75 |
+
name_list[id] = name
|
| 76 |
+
return "ok"
|
| 77 |
|
| 78 |
+
def get_name(id):
|
| 79 |
+
if id in name_list:
|
| 80 |
+
return name_list[id]
|
| 81 |
+
else:
|
| 82 |
+
return "no id"
|
| 83 |
|
| 84 |
+
def get_id(name):
|
| 85 |
+
reverse_dict= dict(map(reversed,name_list.items()))
|
| 86 |
+
if name in reverse_dict:
|
| 87 |
+
return reverse_dict[name]
|
| 88 |
+
else:
|
| 89 |
+
return "no name"
|
| 90 |
|
| 91 |
+
def add_p(id, p_id):
|
| 92 |
+
p2p_list[id] = p_id
|
| 93 |
+
return "ok"
|
| 94 |
|
| 95 |
+
def get_p(id):
|
| 96 |
+
if id in p2p_list:
|
| 97 |
+
return p2p_list[id]
|
| 98 |
+
else:
|
| 99 |
+
return "no id"
|
| 100 |
|
| 101 |
+
def get_id_from_p2p(i):
|
| 102 |
+
reverse_dict= dict(map(reversed,p2p_list.items()))
|
| 103 |
+
if i in reverse_dict:
|
| 104 |
+
return reverse_dict[i]
|
| 105 |
+
else:
|
| 106 |
+
return "no id"
|
| 107 |
|
| 108 |
+
# Blockchain code
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
def get_peers():
|
| 111 |
+
data = fetch_health_state(dht)
|
| 112 |
+
out = []
|
| 113 |
+
for d in data['model_reports']:
|
| 114 |
+
if d['name'] == model_name:
|
| 115 |
+
for r in d['server_rows']:
|
| 116 |
+
out.append(r['peer_id'])
|
| 117 |
|
| 118 |
+
return out
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
get_peers()
|
| 121 |
|
| 122 |
+
with gr.Blocks() as demo:
|
| 123 |
+
count = 0
|
| 124 |
+
aa = gr.Interface(
|
| 125 |
+
fn=chat,
|
| 126 |
+
inputs=["text"],
|
| 127 |
+
outputs="text",
|
| 128 |
+
description="chat, ai μλ΅μ λ°νν©λλ€.\n /run/predict",
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
+
rr = gr.Interface(
|
| 132 |
+
fn=register,
|
| 133 |
+
inputs=["text", "text"],
|
| 134 |
+
outputs="text",
|
| 135 |
+
description="register, νμκ°μ
(μ±κ³΅μ:ok, μ€λ³΅μ:exist λ°ν)\n /run/predict_1",
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
ll = gr.Interface(
|
| 139 |
+
fn=login,
|
| 140 |
+
inputs=["text", "text"],
|
| 141 |
+
outputs="text",
|
| 142 |
+
description="login, λ‘κ·ΈμΈ(μ±κ³΅μ: ok, μ€ν¨μ: password error, μμ΄λκ° μμΌλ©΄: no id) \n /run/predict_2",
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
ad = gr.Interface(
|
| 146 |
+
fn=add_name,
|
| 147 |
+
inputs=["text", "text"],
|
| 148 |
+
outputs="text",
|
| 149 |
+
description="add_name, idλ‘ λλ€μ μΆκ°. ok λ°ν.\n /run/predict_3",
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
nn = gr.Interface(
|
| 153 |
+
fn=get_name,
|
| 154 |
+
inputs=["text"],
|
| 155 |
+
outputs="text",
|
| 156 |
+
description="get_name, idλ‘ λλ€μ λ°ν(μμΌλ©΄ no id)\n /run/predict_4",
|
| 157 |
+
)
|
| 158 |
|
| 159 |
+
nnn = gr.Interface(
|
| 160 |
+
fn=get_id,
|
| 161 |
+
inputs=["text"],
|
| 162 |
+
outputs="text",
|
| 163 |
+
description="get_name, λλ€μμΌλ‘ id λ°ν(μμΌλ©΄ no name)\n /run/predict_5",
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
adp = gr.Interface(
|
| 167 |
+
fn=add_p,
|
| 168 |
+
inputs=["text", "text"],
|
| 169 |
+
outputs="text",
|
| 170 |
+
description="add_p, idλ‘ p2p id μΆκ°. ok λ°ν. \n /run/predict_6",
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
nnp = gr.Interface(
|
| 174 |
+
fn=get_p,
|
| 175 |
+
inputs=["text"],
|
| 176 |
+
outputs="text",
|
| 177 |
+
description="get_p, idλ‘ p2p id λ°ν. μμΌλ©΄ no id. \n /run/predict_7",
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
nnp = gr.Interface(
|
| 181 |
+
fn=get_id_from_p2p,
|
| 182 |
+
inputs=["text"],
|
| 183 |
+
outputs="text",
|
| 184 |
+
description="get_p, p2p idλ‘ μΌλ° id λ°ν. μμΌλ©΄ no id. \n /run/predict_8",
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
demo.queue(max_size=32).launch(enable_queue=True)
|