Upload 2 files
Browse files- main.py +205 -0
- requirements.txt +13 -0
main.py
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Main.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/github/payal15604/ONDC-Test/blob/main/Main.ipynb
|
8 |
+
"""
|
9 |
+
|
10 |
+
# Commented out IPython magic to ensure Python compatibility.
|
11 |
+
!pip install git+https://github.com/PrithivirajDamodaran/ZSIC.git
|
12 |
+
!pip install transformers -U
|
13 |
+
!pip install pyDecision
|
14 |
+
!git clone https://github.com/payal15604/ONDC-Test
|
15 |
+
# %cd ONDC-Test
|
16 |
+
|
17 |
+
from zero_shot_text import zero_shot_text as ztext
|
18 |
+
from zero_shot_image import zero_shot_image as zimg
|
19 |
+
|
20 |
+
from text_summarizer import text_summarizer as t_sum
|
21 |
+
from similarity_scoring import calculate_similarity as c_sim
|
22 |
+
|
23 |
+
import numpy as np
|
24 |
+
|
25 |
+
"""JSON FILE WILL COME"""
|
26 |
+
|
27 |
+
import json
|
28 |
+
import requests
|
29 |
+
|
30 |
+
# Define the URL of your API endpoint
|
31 |
+
url = "http://localhost:4500/api/datasender"
|
32 |
+
|
33 |
+
def parse_json(json_data):
|
34 |
+
try:
|
35 |
+
# Convert boolean literals to uppercase
|
36 |
+
json_data = json_data.replace("true", "True").replace("false", "False")
|
37 |
+
# Parse JSON data
|
38 |
+
parsed_json = json.loads(json_data)
|
39 |
+
return parsed_json
|
40 |
+
except json.JSONDecodeError:
|
41 |
+
print("Error: Invalid JSON data")
|
42 |
+
return None
|
43 |
+
|
44 |
+
try:
|
45 |
+
# Send a POST request to the API endpoint
|
46 |
+
response = requests.post(url)
|
47 |
+
|
48 |
+
# Check if the request was successful (status code 200)
|
49 |
+
if response.status_code == 200:
|
50 |
+
# Extract the JSON data from the response
|
51 |
+
received_data = response.text
|
52 |
+
|
53 |
+
# Parse the JSON data
|
54 |
+
parsed_data = parse_json(received_data)
|
55 |
+
|
56 |
+
if parsed_data:
|
57 |
+
# Process each received item
|
58 |
+
for item in parsed_data.get("Received data", []):
|
59 |
+
# Accessing fields from the item
|
60 |
+
name = item.get("name", "")
|
61 |
+
short_disc = item.get("short_desc", "")
|
62 |
+
long_disc = item.get("long_desc", "")
|
63 |
+
image = item.get("images", [])
|
64 |
+
symbol = item.get("symbol", "")
|
65 |
+
|
66 |
+
# Example: Print the fields of each item
|
67 |
+
print("Name:", name)
|
68 |
+
print("Short Description:", short_disc)
|
69 |
+
print("Long Description:", long_disc)
|
70 |
+
print("Images:", image)
|
71 |
+
print("Symbol:", symbol)
|
72 |
+
|
73 |
+
# TODO: Process the fields in your ML model
|
74 |
+
|
75 |
+
else:
|
76 |
+
print("Error: Failed to parse JSON data")
|
77 |
+
|
78 |
+
else:
|
79 |
+
# Handle the case where the request was not successful
|
80 |
+
print("Error: Failed to fetch data from the API. Status code:", response.status_code)
|
81 |
+
|
82 |
+
except Exception as e:
|
83 |
+
# Handle any exceptions that occur during the request
|
84 |
+
print("Error:", e)
|
85 |
+
labels = ['coffee','tea','shampoo','face serum','bread','honey','soap','biscuit','milk','chocolate','juice']
|
86 |
+
|
87 |
+
result = ztext(name, labels)
|
88 |
+
|
89 |
+
def zero_shot_text_formatted(text, labels):
|
90 |
+
result = ztext(text, labels)
|
91 |
+
|
92 |
+
temp_text_sequence = result['labels']
|
93 |
+
temp_text_scores = result['scores']
|
94 |
+
formatted_output = f"{temp_text_sequence[0]} = {temp_text_scores[0]}"
|
95 |
+
|
96 |
+
print(formatted_output)
|
97 |
+
return formatted_output
|
98 |
+
|
99 |
+
def zero_shot_image_formatted(img, labels):
|
100 |
+
result = zimg(img, labels)
|
101 |
+
|
102 |
+
result_score = result['scores'][0]
|
103 |
+
result_label = result['labels'][0]
|
104 |
+
img_res = f"{result_label} = {result_score}"
|
105 |
+
|
106 |
+
print(img_res)
|
107 |
+
return img_res
|
108 |
+
|
109 |
+
# temp_name_score = zero_shot_text_formatted(name, labels)
|
110 |
+
# temp_sdisc_score = zero_shot_text_formatted(short_disc, labels)
|
111 |
+
# temp_ldisc_score = zero_shot_text_formatted(long_disc, labels)
|
112 |
+
|
113 |
+
# name_sdisc_score = c_sim(temp_name_score, temp_sdisc_score, model="en_core_web_sm")
|
114 |
+
# name_ldisc_score = c_sim(temp_name_score, temp_ldisc_score, model="en_core_web_sm")
|
115 |
+
# name_sldisc_score = c_sim(temp_sdisc_score, temp_ldisc_score, model="en_core_web_sm")
|
116 |
+
|
117 |
+
# print(name_sdisc_score)
|
118 |
+
# print(name_ldisc_score)
|
119 |
+
# print(name_sldisc_score)
|
120 |
+
|
121 |
+
# temp_summary = t_sum(long_disc)
|
122 |
+
# print(temp_summary)
|
123 |
+
|
124 |
+
# print(temp_name_score)
|
125 |
+
# print(temp_sdisc_score)
|
126 |
+
# print(temp_ldisc_score)
|
127 |
+
# print(temp_summary)
|
128 |
+
|
129 |
+
"""## **SCORING**"""
|
130 |
+
|
131 |
+
def name_disc_score(name, short_disc, long_disc, labels):
|
132 |
+
n_compute = zero_shot_text_formatted(name, labels)
|
133 |
+
sd_compute = zero_shot_text_formatted(short_disc, labels)
|
134 |
+
ld_compute = zero_shot_text_formatted(long_disc, labels)
|
135 |
+
|
136 |
+
n_sd_score = c_sim(n_compute, sd_compute, model="en_core_web_sm")
|
137 |
+
n_ld_score = c_sim(n_compute, ld_compute, model="en_core_web_sm")
|
138 |
+
sd_ld_score = c_sim(sd_compute, ld_compute, model="en_core_web_sm")
|
139 |
+
|
140 |
+
return n_sd_score, n_ld_score, sd_ld_score
|
141 |
+
|
142 |
+
def name_symbol_score(name, symbol, labels):
|
143 |
+
n_compute = zero_shot_text_formatted(name, labels)
|
144 |
+
s_compute = zero_shot_image_formatted(symbol, labels)
|
145 |
+
|
146 |
+
n_s_score = c_sim(n_compute, s_compute, model="en_core_web_sm")
|
147 |
+
|
148 |
+
return n_s_score
|
149 |
+
|
150 |
+
def name_image_score(name, image_list, labels):
|
151 |
+
n_compute = zero_shot_text_formatted(name, labels)
|
152 |
+
n_i_scores = []
|
153 |
+
|
154 |
+
for i in range(len(image_list)):
|
155 |
+
i_compute = zero_shot_image_formatted(image_list[i], labels)
|
156 |
+
n_i_score = c_sim(n_compute, i_compute)
|
157 |
+
n_i_scores.append(n_i_score)
|
158 |
+
|
159 |
+
return sum(n_i_scores)/len(image_list) #average
|
160 |
+
|
161 |
+
N_Sd_score, N_Ld_score, Sd_Ld_score = name_disc_score(name, short_disc, long_disc, labels)
|
162 |
+
|
163 |
+
print((N_Sd_score + N_Ld_score + Sd_Ld_score)/3)
|
164 |
+
|
165 |
+
N_S_score = name_symbol_score(name, symbol, labels)
|
166 |
+
|
167 |
+
N_S_score = name_image_score(name, image, labels)
|
168 |
+
print(N_S_score)
|
169 |
+
|
170 |
+
# Required Libraries
|
171 |
+
import numpy as np
|
172 |
+
|
173 |
+
from pyDecision.algorithm import topsis_method
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
# TOPSIS
|
178 |
+
def topsis(name, long_disc, short_disc, image, symbol,labels):
|
179 |
+
|
180 |
+
# Weights
|
181 |
+
#[0.3,0.25, 0.2, 0.1, 0.15]
|
182 |
+
weights = [0.2,0.2,0.2,0.2,0.2] #assigned manually
|
183 |
+
N_i_score = name_image_score(name, image, labels)
|
184 |
+
N_Sd_score, N_Ld_score, Sd_Ld_score = name_disc_score(name, short_disc, long_disc, labels)
|
185 |
+
N_S_score = name_symbol_score(name, symbol, labels)
|
186 |
+
# Load Criterion Type: 'max' or 'min'
|
187 |
+
criterion_type = ['max', 'max', 'max', 'max','max']
|
188 |
+
|
189 |
+
# Dataset
|
190 |
+
dataset = np.array([
|
191 |
+
[N_S_score, N_i_score, N_Sd_score, N_Ld_score, Sd_Ld_score],
|
192 |
+
[0.87, 0.9, 0.47, 0.46, 0.5],
|
193 |
+
[1,0.67,0.57,0.56,0.8]#demo data
|
194 |
+
])
|
195 |
+
|
196 |
+
# Call TOPSIS
|
197 |
+
relative_closeness = topsis_method(dataset, weights, criterion_type, graph = False, verbose = True)
|
198 |
+
return relative_closeness
|
199 |
+
|
200 |
+
relative_closeness=topsis(name, long_disc, short_disc, image, symbol,labels)
|
201 |
+
print(relative_closeness)
|
202 |
+
|
203 |
+
## Call TOPSIS
|
204 |
+
#relative_closeness = topsis_method(dataset, weights, criterion_type, graph = False, verbose = True)
|
205 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
transformers -U
|
3 |
+
pipeline
|
4 |
+
AutoTokenizer
|
5 |
+
AutoModelForSequenceClassification
|
6 |
+
T5ForConditionalGeneration
|
7 |
+
spacy
|
8 |
+
NLTK
|
9 |
+
git+https://github.com/PrithivirajDamodaran/ZSIC.git
|
10 |
+
ZSIC
|
11 |
+
pyDecision
|
12 |
+
numpy
|
13 |
+
|