Create classification.py
Browse files- classification.py +111 -0
classification.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from transformers import AutoTokenizer, AutoModel
|
3 |
+
from sentence_transformers import SentenceTransformer, util
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
|
7 |
+
def load_data(file_obj):
|
8 |
+
# Assuming file_obj is a file-like object uploaded via Gradio, use `pd.read_excel` directly on it
|
9 |
+
return pd.read_excel(file_obj)
|
10 |
+
|
11 |
+
def generate_embeddings(df, model, Column):
|
12 |
+
embeddings_list = []
|
13 |
+
for index, row in df.iterrows():
|
14 |
+
if type(row["title"]) == str and type(row[Column]) == str:
|
15 |
+
print(index)
|
16 |
+
content = row["title"] + "\n" + row[Column]
|
17 |
+
embeddings = model.encode(content, convert_to_tensor=True)
|
18 |
+
embeddings_list.append(embeddings)
|
19 |
+
else:
|
20 |
+
embeddings_list.append(np.nan)
|
21 |
+
df['Embeddings'] = embeddings_list
|
22 |
+
return df
|
23 |
+
|
24 |
+
|
25 |
+
def process_categories(categories, model):
|
26 |
+
embeddings_listc = [model.encode(cat['bio'], convert_to_tensor=True) for cat in categories]
|
27 |
+
category_df = pd.DataFrame({
|
28 |
+
'Category': [d['bio'] for d in categories],
|
29 |
+
'Expert': [d['expert'] for d in categories],
|
30 |
+
'Embeddings': embeddings_listc
|
31 |
+
})
|
32 |
+
return category_df
|
33 |
+
|
34 |
+
|
35 |
+
def match_categories(df, category_df):
|
36 |
+
categories_list, experts_list, scores_list = [], [], []
|
37 |
+
for ebd_content in df['Embeddings']:
|
38 |
+
if type(ebd_content) != float:
|
39 |
+
cos_scores = util.cos_sim(ebd_content, torch.stack(list(category_df['Embeddings']), dim=0))[0]
|
40 |
+
max_index = cos_scores.tolist().index(max(cos_scores))
|
41 |
+
categories_list.append(category_df.loc[max_index, 'Category'])
|
42 |
+
experts_list.append(category_df.loc[max_index, 'Expert'])
|
43 |
+
scores_list.append(float(max(cos_scores)))
|
44 |
+
else:
|
45 |
+
categories_list.append(np.nan)
|
46 |
+
experts_list.append(np.nan)
|
47 |
+
scores_list.append(np.nan)
|
48 |
+
df["Category"] = categories_list
|
49 |
+
df["Expert"] = experts_list
|
50 |
+
df["Score"] = scores_list
|
51 |
+
return df
|
52 |
+
|
53 |
+
|
54 |
+
def save_data(df, filename):
|
55 |
+
new_filename = filename.replace(".", "_classified.")
|
56 |
+
df.to_excel(new_filename, index=False)
|
57 |
+
return new_filename
|
58 |
+
|
59 |
+
|
60 |
+
def classification(column, file_path):
|
61 |
+
# Load data
|
62 |
+
df = load_data(file_path)
|
63 |
+
|
64 |
+
# Initialize models
|
65 |
+
model_ST = SentenceTransformer("all-mpnet-base-v2")
|
66 |
+
|
67 |
+
# Generate embeddings for df
|
68 |
+
df = generate_embeddings(df, model_ST, column)
|
69 |
+
|
70 |
+
# Categories
|
71 |
+
categories = [
|
72 |
+
{
|
73 |
+
"expert": "mireille",
|
74 |
+
"bio":"expert in security, interested in protection of confidentiality, privacy, integrity, authentication and authorization. Also interested in distributed trust, end-user trust models, secure element, key provisioning, Residential Gateway"
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"expert":"khawla",
|
78 |
+
"bio":"expert in inter-connection of Standalone Non-Public Network (SNPN) and cyber-security related topics of such types of networks including distributed trust, distributed ledge, blockchain, authentication, private networks security, provisioning of credentials"
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"expert":"guillaume",
|
82 |
+
"bio":"expert in distributed networks and communication, such as mesh network, ad-hoc networks multi-hop network, and the cyber-security of such topologies. Swarm of Drones and Unmanned Aerial Vehicles may deployed such network infrastructure. It is essential to look at how devices/UE authenticate to these networks, and assess the threats and provide counter measures"
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"expert":"vincent",
|
86 |
+
"bio":"expert in USIM and related over-the-air services to manage the USIM e.g. Steering of Roaming (SoR), roaming services, network selection, UE configuration or configuration in the Secure Element or USIM"
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"expert":"pierre",
|
90 |
+
"bio":"expert in eco-design, intereted in societal impact of technology, wants to push Key Value concepts to 3GPP and in particular defines Key Value Indicators (KVI) in the service requirements. Energy saving and energy efficiency are key aspects in eco-design, as well as carbon emissions and global use of the telecommunication technologies"
|
91 |
+
},
|
92 |
+
{
|
93 |
+
"expert":"ly-thanh",
|
94 |
+
"bio":"expert in service requirements of new services defines in new Study Items (SID) ad Work Items (WID). Has to detect low signals of new trends and technologies e.g. Artificial Intelligence (AI/ML), Metaverse new trust concepts, new network topologies, and new topics that may have an impact on the USIM services or over-the-air services. Thes impacts may by new threats or opportunities for the USIM/Secure Element/Card/Roaming services business."
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"expert":"nicolas",
|
98 |
+
"bio":"expert in satellite, and Non Terrestrial Network NTN, is interested in Private Networks SNPN, IoT, Inter Satellite communication, Geo Stationnary Satellite GEO, Low Orbite Satellite LEO, Medium Orbite Satellite MEO, Radio Access Network RAN"
|
99 |
+
},
|
100 |
+
{
|
101 |
+
"expert":"dorin",
|
102 |
+
"bio":"Public Safety Communication, Military Communication, Emeregency Calls, Emergency Services, Disaster Communication Access, PLMN Access During Disasters, Emergency Communication Enhancements, Ultra reliable low latency communication URLLC, Tactical Bubble, Private Network, Proximity Services PROSE, Radio Access Network RAN, Mission Critical Services MCS"
|
103 |
+
}
|
104 |
+
]
|
105 |
+
category_df = process_categories(categories, model_ST)
|
106 |
+
|
107 |
+
# Match categories
|
108 |
+
df = match_categories(df, category_df)
|
109 |
+
|
110 |
+
# Save data
|
111 |
+
return save_data(df,file_path), df
|