Defalt-404 commited on
Commit
c42e723
1 Parent(s): 441a00d

Add application file

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import http
3
+ import ssl
4
+ import json
5
+ import warnings
6
+ warnings.filterwarnings("ignore")
7
+
8
+ def retrieve_api_key(url):
9
+
10
+ context = ssl.create_default_context()
11
+ context.check_hostname = True
12
+ conn = http.client.HTTPSConnection(url, context=context)
13
+ conn.request("GET", "/admin/api-keys/")
14
+ api_key_response = conn.getresponse()
15
+ api_keys_data = (
16
+ api_key_response.read().decode("utf-8").replace("\n", "").replace("\t", "")
17
+ )
18
+ api_keys_json = json.loads(api_keys_data)
19
+ api_key = api_keys_json[0]["api_key"]
20
+ conn.close()
21
+ return api_key
22
+
23
+
24
+ def get_benchmark_uids(num_miner):
25
+
26
+ url="test.neuralinternet.ai"
27
+ api_key = retrieve_api_key(url)
28
+
29
+ context = ssl.create_default_context()
30
+ context.check_hostname = True
31
+ conn = http.client.HTTPSConnection(url, context=context)
32
+
33
+ headers = {
34
+ "Content-Type": "application/json",
35
+ "Authorization": f"Bearer {api_key}",
36
+ "Endpoint-Version": "2023-05-19",
37
+ }
38
+
39
+ conn.request("GET", f"/top_miner_uids?n={num_miner}", headers=headers)
40
+ miner_response = conn.getresponse()
41
+ miner_data = (
42
+ miner_response.read().decode("utf-8").replace("\n", "").replace("\t", "")
43
+ )
44
+ uids = json.loads(miner_data)
45
+ return uids
46
+
47
+
48
+
49
+ def retrieve_response(payload):
50
+
51
+ url="d509-65-108-32-175.ngrok-free.app"
52
+ api_key = retrieve_api_key(url)
53
+ headers = {
54
+ "Content-Type": "application/json",
55
+ "Authorization": f"Bearer {api_key}",
56
+ "Endpoint-Version": "2023-05-19",
57
+ }
58
+ payload = json.dumps(payload)
59
+ context = ssl.create_default_context()
60
+ context.check_hostname = True
61
+ conn = http.client.HTTPSConnection(url, context=context)
62
+ conn.request("POST", "/chat", payload, headers)
63
+ init_response = conn.getresponse()
64
+ init_data = init_response.read().decode("utf-8").replace("\n", "").replace("\t", "")
65
+ init_json = json.loads(init_data)
66
+
67
+ response_dict = dict()
68
+ for choice in init_json['choices']:
69
+ uid = choice['uid']
70
+ resp = choice['message']['content']
71
+ resp = resp.replace("\n", "").replace("\t", "")
72
+ response_dict[uid] = resp
73
+ response_text = '\n\n'.join([f'"{key}": "{value}"' for key, value in response_dict.items()])
74
+ return response_text
75
+
76
+
77
+
78
+ def interface_fn(system_prompt, optn, arg, user_prompt):
79
+
80
+ if len(system_prompt) == 0:
81
+ system_prompt = "You are an AI Assistant, created by bittensor and powered by NI(Neural Internet). Your task is to provide consise response to user's prompt"
82
+
83
+ messages = [{"role": "system", "content": system_prompt},{"role": "user", "content": user_prompt}]
84
+ payload = dict()
85
+
86
+ if optn == 'TOP':
87
+
88
+ if int(arg) > 30:
89
+ arg = 30
90
+ payload['top_n'] = int(arg)
91
+ payload['messages'] = messages
92
+ response = retrieve_response(payload)
93
+ return response
94
+
95
+ elif optn == 'BENCHMARK':
96
+
97
+ if int(arg) > 30:
98
+ arg = 30
99
+ uids = get_benchmark_uids(int(arg))
100
+ payload['uids'] = uids
101
+ payload['messages'] = messages
102
+ response = retrieve_response(payload)
103
+ return response
104
+
105
+ else:
106
+
107
+ uids = list()
108
+ if ',' in arg:
109
+ uids = [int(x) for x in arg.split(',')]
110
+ else:
111
+ uids = [arg]
112
+ payload['uids'] = uids
113
+ payload['messages'] = messages
114
+ response = retrieve_response(payload)
115
+ return response
116
+
117
+
118
+ interface = gr.Interface(
119
+ fn=interface_fn,
120
+ inputs=[
121
+ gr.inputs.Textbox(label="System Prompt", optional=True),
122
+ gr.inputs.Dropdown(["TOP", "BENCHMARK", "UIDs"], label="Select Function"),
123
+ gr.inputs.Textbox(label="Arguement"),
124
+ gr.inputs.Textbox(label="Enter your question")
125
+ ],
126
+ outputs=gr.outputs.Textbox(label="Model Responses"),
127
+ title="Explore Bittensor Miners",
128
+ description="Enter parameters as per you want and get response",
129
+
130
+ )
131
+
132
+ interface.launch(enable_queue=True)