forked from qiskit-community/qiskit-presentations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.py
65 lines (51 loc) · 1.58 KB
/
AI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
# Copyright 2018, IBM.
#
# This source code is licensed under the Apache License, Version 2.0 found in
# the LICENSE.txt file in the root directory of this source tree.
import sys
sys.path.append('./demos/AI/')
from run_classify_local import classify
from presentation_helpers import *
mode = "offline"
if check_connection("https://www.qiskit.org")==1:
mode = "online"
else:
mode = "offline"
result_offline = ""
def execution_mode(status):
global mode
mode = status
def result_AI_inference():
data = {}
global mode
global result_offline
result = result_offline.split("\n")
data["time"] = result[:1][0]
data["metadata"] = result[1:-1]
data["quality"] = 100
data["labels"] = result[-1].replace("-1.", "B,").replace("1.", "A,").replace("[","").replace("]","").replace(" ","").split(",")[:-1]
data["label"] = data["labels"][0]
# print(data["labels"])
data["inference"] = "cat" if result[-1] == "[-1. -1. -1. -1.]" else "dog"
return data
def execute_AI(data):
points = "0,0"
global mode
global result_offline
if data == "🐱":
points = "53,65,9,32,53,82,10,17"
elif data == "🐶":
points = "96,16,7,24,93,92,99,30"
else:
return "error in input data"
result_offline = classify(points)
if __name__ == "__main__":
# execute only if run as a script
element = "🐱"
execute_AI(element)
print("--->", element)
result = result_AI_inference()
print("--->",result["inference"])
print("===>",result["time"])
print("===>",result["metadata"])