Defalt-404 commited on
Commit
fe8aaa6
1 Parent(s): 4d7010b

Add application file

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import ssl
4
+ import http.client
5
+
6
+ def get_api_key():
7
+
8
+ context = ssl.create_default_context()
9
+ context.check_hostname = True
10
+
11
+ conn = http.client.HTTPSConnection("test.neuralinternet.ai", context=context)
12
+ conn.request("GET", "/admin/api-keys/")
13
+ api_key_resp = conn.getresponse()
14
+ api_key_string = api_key_resp.read().decode("utf-8").replace("\n", "").replace("\t", "")
15
+ api_key_json = json.loads(api_key_string)
16
+ api_key = api_key_json[0]['api_key']
17
+ conn.close()
18
+ return api_key
19
+
20
+ def generate_top_response(system_prompt,model_input, api_key):
21
+ payload = json.dumps(
22
+ {"top_n": 100, "messages": [{"role": "system", "content": system_prompt},{"role": "user", "content": model_input}]}
23
+ )
24
+
25
+ headers = {
26
+ "Content-Type": "application/json",
27
+ "Authorization": f"Bearer {api_key}",
28
+ "Endpoint-Version": "2023-05-19",
29
+ }
30
+
31
+ context = ssl.create_default_context()
32
+ context.check_hostname = True
33
+
34
+ conn = http.client.HTTPSConnection("test.neuralinternet.ai", context=context)
35
+ conn.request("POST", "/chat", payload, headers)
36
+ response = conn.getresponse()
37
+ utf_string = response.read().decode("utf-8").replace("\n", "").replace("\t", "")
38
+ print(utf_string)
39
+ json_resp = json.loads(utf_string)
40
+ conn.close()
41
+ for choice in json_resp['choices']:
42
+ uid = choice['uid']
43
+ return uid, choice['message']['content']
44
+
45
+ def generate_benchmark_response(system_prompt, model_input, api_key):
46
+
47
+ context = ssl.create_default_context()
48
+ context.check_hostname = True
49
+
50
+
51
+ conn = http.client.HTTPSConnection("test.neuralinternet.ai", context=context)
52
+ conn.request("GET", "/top_miner_uids")
53
+ benchmark_uid_resp = conn.getresponse()
54
+ benchmark_uid_string = benchmark_uid_resp.read().decode("utf-8").replace("\n", "").replace("\t", "")
55
+ benchmark_uid_json = json.loads(benchmark_uid_string)
56
+ conn.close()
57
+
58
+ payload = json.dumps(
59
+ {"uids": benchmark_uid_json , "messages": [{"role": "system", "content": system_prompt},{"role": "user", "content": model_input}]}
60
+ )
61
+
62
+ headers = {
63
+ "Content-Type": "application/json",
64
+ "Authorization": f"Bearer {api_key}",
65
+ "Endpoint-Version": "2023-05-19",
66
+ }
67
+
68
+ conn = http.client.HTTPSConnection("test.neuralinternet.ai", context=context)
69
+ conn.request("POST", "/chat", payload, headers)
70
+ response = conn.getresponse()
71
+ utf_string = response.read().decode("utf-8").replace("\n", "").replace("\t", "")
72
+ json_resp = json.loads(utf_string)
73
+ #print(utf_string)
74
+ conn.close()
75
+
76
+ for choice in json_resp['choices']:
77
+ uid = choice['uid']
78
+ model_resp = choice['message']['content']
79
+ return uid, model_resp
80
+
81
+ def dynamic_function(system_prompt, prompt):
82
+
83
+ if len(system_prompt) == 0:
84
+ 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"
85
+ api_key = get_api_key()
86
+ top_uid, top_response = generate_top_response(system_prompt, prompt, api_key)
87
+ benchmark_uid, benchmark_response = generate_benchmark_response(system_prompt, prompt, api_key)
88
+
89
+ return f"TOP_{top_uid}: {top_response}\n\n\nBenchmark_{benchmark_uid}:{benchmark_response}"
90
+
91
+ interface = gr.Interface(
92
+ fn=dynamic_function,
93
+ inputs=[
94
+ gr.inputs.Textbox(label="System Prompt", optional=True),
95
+ gr.inputs.Textbox(label="Enter your question")
96
+ ],
97
+ outputs=gr.outputs.Textbox(label="Responses"),
98
+ title="Bittensor Compare Util",
99
+ )
100
+
101
+
102
+ # Launch the Gradio Interface
103
+ interface.launch(share=False, enable_queue=True)
104
+