Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from difflib import SequenceMatcher
|
| 4 |
+
|
| 5 |
+
def read_csv_or_excel(file):
|
| 6 |
+
# Read CSV or Excel file
|
| 7 |
+
if file.name.endswith('.csv'):
|
| 8 |
+
return pd.read_csv(file)
|
| 9 |
+
elif file.name.endswith('.xlsx') or file.name.endswith('.xls'):
|
| 10 |
+
return pd.read_excel(file)
|
| 11 |
+
else:
|
| 12 |
+
raise ValueError("Unsupported file format. Only CSV and Excel files are supported.")
|
| 13 |
+
|
| 14 |
+
def find_exact_matches(df1, df2, column_name):
|
| 15 |
+
# Find rows with exact matches in the specified column
|
| 16 |
+
matches = pd.merge(df1, df2, on=column_name, how='inner')
|
| 17 |
+
return matches
|
| 18 |
+
|
| 19 |
+
def find_similar_texts(df1, df2, column_name, threshold=0.8):
|
| 20 |
+
# Find rows with similar texts in the specified column
|
| 21 |
+
similar_texts = []
|
| 22 |
+
for index1, row1 in df1.iterrows():
|
| 23 |
+
for index2, row2 in df2.iterrows():
|
| 24 |
+
similarity = SequenceMatcher(None, str(row1[column_name]), str(row2[column_name])).ratio()
|
| 25 |
+
if similarity >= threshold:
|
| 26 |
+
similar_texts.append((index1, index2, row1[column_name], row2[column_name]))
|
| 27 |
+
return similar_texts
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
st.title("Item Comparison App")
|
| 31 |
+
|
| 32 |
+
# Upload files
|
| 33 |
+
st.header("Upload Files")
|
| 34 |
+
warehouse_file = st.file_uploader("Upload Warehouse Item Stocks (CSV or Excel)")
|
| 35 |
+
industry_file = st.file_uploader("Upload Industry Item Stocks (CSV or Excel)")
|
| 36 |
+
|
| 37 |
+
if warehouse_file is not None and industry_file is not None:
|
| 38 |
+
# Read files
|
| 39 |
+
warehouse_df = read_csv_or_excel(warehouse_file)
|
| 40 |
+
industry_df = read_csv_or_excel(industry_file)
|
| 41 |
+
|
| 42 |
+
# Get column names
|
| 43 |
+
warehouse_columns = warehouse_df.columns.tolist()
|
| 44 |
+
industry_columns = industry_df.columns.tolist()
|
| 45 |
+
|
| 46 |
+
# Select columns using dropdowns
|
| 47 |
+
st.header("Select Columns")
|
| 48 |
+
warehouse_column = st.selectbox("Choose column from warehouse item stocks:", warehouse_columns)
|
| 49 |
+
industry_column = st.selectbox("Choose column from industry item stocks:", industry_columns)
|
| 50 |
+
|
| 51 |
+
# Find exact matches
|
| 52 |
+
exact_matches = find_exact_matches(warehouse_df, industry_df, warehouse_column)
|
| 53 |
+
|
| 54 |
+
# Find similar texts
|
| 55 |
+
similar_texts = find_similar_texts(warehouse_df, industry_df, warehouse_column)
|
| 56 |
+
|
| 57 |
+
# Display results
|
| 58 |
+
st.header("Exact Matches")
|
| 59 |
+
st.write(exact_matches)
|
| 60 |
+
|
| 61 |
+
st.header("Similar Texts")
|
| 62 |
+
for text_pair in similar_texts:
|
| 63 |
+
st.write(f"Row {text_pair[0]} in warehouse item stocks is similar to Row {text_pair[1]} in industry item stocks:")
|
| 64 |
+
st.write(f"Warehouse: {text_pair[2]}")
|
| 65 |
+
st.write(f"Industry: {text_pair[3]}")
|
| 66 |
+
st.write("")
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
main()
|