ldhldh commited on
Commit
e48ab6b
β€’
1 Parent(s): 90e26fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -34
app.py CHANGED
@@ -1,51 +1,187 @@
1
- from functools import partial
 
 
 
 
2
 
 
 
 
 
 
 
 
3
  import hivemind
4
- from flask import Flask, jsonify, request
5
- from flask_cors import CORS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- import config
8
- from p2p_utils import check_reachability
9
- from state_updater import StateUpdaterThread
 
 
 
 
 
10
 
11
- logger = hivemind.get_logger(__name__)
 
 
12
 
 
 
 
 
 
13
 
14
- logger.info("Connecting to DHT")
15
- dht = hivemind.DHT(initial_peers=config.INITIAL_PEERS, client_mode=True, num_workers=32, start=True)
 
 
 
 
16
 
17
- logger.info("Starting Flask app")
18
- app = Flask(__name__)
19
- CORS(app)
20
 
21
- logger.info("Starting updater")
22
- updater = StateUpdaterThread(dht, app, daemon=True)
23
- updater.start()
24
- updater.ready.wait()
 
25
 
 
 
 
 
 
 
26
 
27
- @app.route("/")
28
- def main_page():
29
- return updater.state_html
30
 
 
 
 
 
 
 
 
31
 
32
- @app.route("/api/v1/state")
33
- def api_v1_state():
34
- return app.response_class(response=updater.state_json, status=200, mimetype="application/json")
35
 
 
36
 
37
- @app.route("/api/v1/is_reachable/<peer_id>")
38
- def api_v1_is_reachable(peer_id):
39
- peer_id = hivemind.PeerID.from_base58(peer_id)
40
- rpc_info = dht.run_coroutine(partial(check_reachability, peer_id, use_cache=False))
41
- return jsonify(
42
- success=rpc_info["ok"],
43
- message=rpc_info.get("error"),
44
- your_ip=request.remote_addr,
45
  )
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- @app.route("/metrics")
49
- @app.route("/api/prometheus")
50
- def metrics():
51
- return app.response_class(response=updater.prometheus_metrics, status=200, mimetype="text/plain")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)