Spaces:
Sleeping
Sleeping
File size: 8,412 Bytes
89a1200 c9a96c4 89a1200 a9870ed 89a1200 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
import os
import sys
import streamlit as st
from streamlit_extras.switch_page_button import switch_page
import pandas as pd
import numpy as np
import torch
import faiss
from sentence_transformers import SentenceTransformer
import csv
################################
######### Variables ############
################################
# -- Loading Variables
script_directory = os.path.dirname(os.path.abspath(sys.argv[0]))
source_df = pd.DataFrame()
destination_df = pd.DataFrame()
model = SentenceTransformer('all-mpnet-base-v2')
# -- Loading Session Data
if 'project_data' not in st.session_state:
st.session_state.project_data = pd.read_csv(script_directory+'/data/project.csv')
################################
####### GenericFunctions #######
################################
# -- Create Embedding - all-mpnet-base-v2 - https://www.sbert.net/docs/pretrained_models.html
def embed_text(text):
embedding = model.encode(text)
return embedding
def embed_list(list):
embeddings = []
for text in list:
embeddings.append(embed_text(text))
return embeddings
# -- Store embeddings in a FAISS Vector database
def store_embeddings(embeddings):
dimension = embeddings[0].shape[0]
index = faiss.IndexFlatIP(dimension)
index.add(np.array(embeddings))
# faiss.write_index(index, "data/vector_db.index")
return index
# -- Perform semantic search using embeddings
def semantic_search(query_embedding, index, k=1):
D, I = index.search(np.array([query_embedding]), k)
return I[0][0]
################################
####### Display of data ########
################################
# -- Streamlit Settings
st.set_page_config(layout='wide')
st.title("Mapping")
# -- Add Project Dropdown
st.text("")
st.text("")
st.text("")
col1, col2, col3 = st.columns(3)
option = col1.selectbox('Select Project',st.session_state.project_data['Project'])
col1, col2, col3 = st.columns(3)
# -- Destination File Name
st.text("")
st.text("")
col1, col2, col3 = st.columns(3)
cond = (st.session_state.project_data['Project'] == option)
result = st.session_state.project_data[cond].Destination.values[0]
with col1:
destination_file_format = st.file_uploader(
"Destination file name - "+str(result)+".csv",
type="csv",
key="destination_file_format",
accept_multiple_files=True
)
if destination_file_format is not None:
for file in destination_file_format:
destination_df = pd.read_csv(file)
# -- Source File Name
cond = (st.session_state.project_data['Project'] == option)
result = st.session_state.project_data[cond].Source.values[0]
with col3:
source_file_format = st.file_uploader(
"Source file name - "+str(result)+".csv",
type="csv",
key="source_file_format",
accept_multiple_files=True
)
if source_file_format is not None:
for file in source_file_format:
source_df = pd.read_csv(file)
# -- Suggest Button
st.text("")
st.text("")
col1, col2, col3 = st.columns([0.25,0.2,2.55])
if col1.button("AI Suggest"):
st.session_state.mapping_df = pd.DataFrame(columns=["Sno","DestinationColumn","SourceColumn","Type","Expression"])
if len(destination_df) == 0 or len(source_df) == 0:
st.error("Select Source and Destination Files")
else:
new_data = []
# Source - KnowledgeBase
input_text = source_df["Columns"].tolist()
embeddings = embed_list(input_text)
index = store_embeddings(embeddings)
# Map to Source
for i in range(len(destination_df)):
search_text = destination_df.loc[i, "Columns"]
query_embeddings = embed_text(search_text)
result = input_text[semantic_search(query_embeddings, index)]
row = {
"Sno": i+1,
"DestinationColumn": destination_df.loc[i, "Columns"],
"SourceColumn": result,
"Type": None,
"Expression":None
}
new_data.append(row)
# Saving Mapping and displaying
if new_data or len(mapping_df) <0:
st.session_state.mapping_df = pd.concat(
[ st.session_state.mapping_df, pd.DataFrame(new_data)],
ignore_index=True
)
else:
st.error("Unable to map Source and Destination Files")
# -- Save Button
if col2.button("Save"):
if (len(destination_df) > 0 and len(source_df) > 0 and len(st.session_state.mapping_df)>0):
cond = (st.session_state.project_data['Project'] == option)
file_name = script_directory+'/data/'+str(st.session_state.project_data[cond].Id.values[0])+"_"+st.session_state.project_data[cond].Source.values[0]+"_"+st.session_state.project_data[cond].Destination.values[0]+'.csv'
st.session_state.mapping_df.to_csv(file_name, index=False, sep="|",quoting=csv.QUOTE_NONE)
else:
st.error("Transformation not created")
# -- Load Exisitng Mapping
if col3.button("Load Mapping"):
cond = (st.session_state.project_data['Project'] == option)
file_name = script_directory+'/data/'+str(st.session_state.project_data[cond].Id.values[0])+"_"+st.session_state.project_data[cond].Source.values[0]+"_"+st.session_state.project_data[cond].Destination.values[0]+'.csv'
st.session_state.mapping_df = pd.read_csv(file_name,sep="|",quoting=csv.QUOTE_NONE)
# -- Display Mapping Table
if (len(destination_df) > 0 and len(source_df) > 0 and len(st.session_state.mapping_df)>0):
st.text("")
st.header("Mapping Details")
st.text("")
st.text("")
st.session_state.mapping_df = st.data_editor(
st.session_state.mapping_df,
height=400,
width=1200,
hide_index=True,
column_config={
"Sno": st.column_config.TextColumn(
"Sno"
),
"DestinationColumn": st.column_config.TextColumn(
"DestinationColumn"
),
"SourceColumn": st.column_config.SelectboxColumn(
"SourceColumn",
width="medium",
options= source_df["Columns"],
),
"Type": st.column_config.SelectboxColumn(
"Type",
width="medium",
options=[
"Pandas",
"Constant"
]
),
"Expression": st.column_config.TextColumn(
"Expression"
)
},
disabled=["Sno","DestinationColumn"]
) |