Spaces:
Runtime error
Runtime error
96abhishekarora
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import linktransformer as lt
|
4 |
+
|
5 |
+
# Function to convert DataFrame to CSV for download
|
6 |
+
def convert_df_to_csv(df):
|
7 |
+
return df.to_csv().encode('utf-8')
|
8 |
+
|
9 |
+
st.title('DataFrame Merger using LinkTransformer')
|
10 |
+
|
11 |
+
# Function to load DataFrame
|
12 |
+
def load_dataframe(upload, path):
|
13 |
+
if upload is not None:
|
14 |
+
return pd.read_csv(upload)
|
15 |
+
elif path != "":
|
16 |
+
return pd.read_csv(path)
|
17 |
+
else:
|
18 |
+
return None
|
19 |
+
|
20 |
+
# Options for DataFrame 1
|
21 |
+
df1_upload = st.file_uploader("Upload DataFrame 1 (CSV)", type=['csv'], key='df1_upload')
|
22 |
+
df1_path = st.text_input("...or enter path for DataFrame 1 (CSV)", key='df1_path')
|
23 |
+
|
24 |
+
# Options for DataFrame 2
|
25 |
+
df2_upload = st.file_uploader("Upload DataFrame 2 (CSV)", type=['csv'], key='df2_upload')
|
26 |
+
df2_path = st.text_input("...or enter path for DataFrame 2 (CSV)", key='df2_path')
|
27 |
+
|
28 |
+
# Load and display the DataFrames
|
29 |
+
df1 = load_dataframe(df1_upload, df1_path)
|
30 |
+
df2 = load_dataframe(df2_upload, df2_path)
|
31 |
+
|
32 |
+
if df1 is not None:
|
33 |
+
st.write("DataFrame 1 Preview:")
|
34 |
+
st.dataframe(df1.head())
|
35 |
+
|
36 |
+
if df2 is not None:
|
37 |
+
st.write("DataFrame 2 Preview:")
|
38 |
+
st.dataframe(df2.head())
|
39 |
+
|
40 |
+
|
41 |
+
# Model selection
|
42 |
+
model_path = st.text_input("Model path (HuggingFace or local)", value="all-MiniLM-L6-v2")
|
43 |
+
|
44 |
+
# Checkbox for columns to match on
|
45 |
+
if not df1.empty and not df2.empty:
|
46 |
+
columns_df1 = df1.columns.tolist()
|
47 |
+
columns_df2 = df2.columns.tolist()
|
48 |
+
|
49 |
+
selected_columns_df1 = st.multiselect("Select columns from DataFrame 1 to match on:", columns_df1, default=columns_df1[0])
|
50 |
+
selected_columns_df2 = st.multiselect("Select columns from DataFrame 2 to match on:", columns_df2, default=columns_df2[0])
|
51 |
+
|
52 |
+
# Perform merge
|
53 |
+
if st.button("Merge DataFrames"):
|
54 |
+
model=lt.LinkTransformer(model_path)
|
55 |
+
df_lm_matched = lt.merge(df2, df1, merge_type='1:m', on=None, model=model, left_on=selected_columns_df1, right_on=selected_columns_df2)
|
56 |
+
st.write("Merged DataFrame Preview:")
|
57 |
+
st.dataframe(df_lm_matched.head())
|
58 |
+
|
59 |
+
# Download button for merged DataFrame
|
60 |
+
csv = convert_df_to_csv(df_lm_matched)
|
61 |
+
st.download_button(
|
62 |
+
label="Download merged DataFrame as CSV",
|
63 |
+
data=csv,
|
64 |
+
file_name='merged_dataframe.csv',
|
65 |
+
mime='text/csv',
|
66 |
+
)
|