Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from tqdm import tqdm
|
3 |
+
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
+
from io import StringIO
|
6 |
+
|
7 |
+
|
8 |
+
def correct_text(uploaded_file, column_to_correct):
|
9 |
+
"""
|
10 |
+
Corrects text in the specified column using a text correction model.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
uploaded_file: DataFrame containing the text to correct
|
14 |
+
column_to_correct: Index of the column to correct
|
15 |
+
Returns:
|
16 |
+
DataFrame with corrected text in a new column
|
17 |
+
"""
|
18 |
+
corrector = pipeline("text2text-generation",
|
19 |
+
model="sdadas/byt5-text-correction")
|
20 |
+
|
21 |
+
df = uploaded_file
|
22 |
+
progress_bar = st.progress(0)
|
23 |
+
status_text = st.text("Correcting text 🧠...")
|
24 |
+
|
25 |
+
for index, row in df.iterrows():
|
26 |
+
if pd.notna(row.iloc[column_to_correct]):
|
27 |
+
original_text = str(row.iloc[column_to_correct])
|
28 |
+
corrected_text = corrector(
|
29 |
+
"<es>" + original_text, max_length=1024)[0]['generated_text']
|
30 |
+
|
31 |
+
# Save corrected text only if different from original
|
32 |
+
if corrected_text != original_text:
|
33 |
+
df.loc[index, column_to_correct + 1] = corrected_text
|
34 |
+
|
35 |
+
progress = (index + 1) / len(df)
|
36 |
+
progress_bar.progress(progress)
|
37 |
+
status_text.text(f"Progress: {int(progress * 100)}% completed ")
|
38 |
+
|
39 |
+
return df
|
40 |
+
|
41 |
+
|
42 |
+
def choose_columns(dataframe):
|
43 |
+
"""
|
44 |
+
Lets user select columns to correct and displays preview of data.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
dataframe: Input DataFrame
|
48 |
+
Returns:
|
49 |
+
Selected column index or None if no selection
|
50 |
+
"""
|
51 |
+
st.write("Choose the columns to correct 🔍")
|
52 |
+
column_to_correct = st.selectbox(
|
53 |
+
"Select columns to correct", dataframe.columns)
|
54 |
+
|
55 |
+
if column_to_correct:
|
56 |
+
st.write("Preview of data in selected columns 👀:")
|
57 |
+
non_empty_data = dataframe[dataframe[column_to_correct].notna()]
|
58 |
+
st.dataframe(non_empty_data[column_to_correct].head())
|
59 |
+
|
60 |
+
if st.button("Correct Text"):
|
61 |
+
if column_to_correct is not None:
|
62 |
+
return dataframe.columns.get_loc(column_to_correct)
|
63 |
+
else:
|
64 |
+
st.error("Please select a column before correcting text ❌")
|
65 |
+
return None
|
66 |
+
|
67 |
+
|
68 |
+
def main():
|
69 |
+
"""Main function to run the text correction application"""
|
70 |
+
st.title("CSV text Correction App ✔")
|
71 |
+
uploaded_file = st.file_uploader("Choose a CSV file 📄", type=["csv"])
|
72 |
+
if uploaded_file is not None:
|
73 |
+
try:
|
74 |
+
dataframe = pd.read_csv(uploaded_file, encoding='utf-8')
|
75 |
+
column_index = choose_columns(dataframe)
|
76 |
+
if column_index is not None:
|
77 |
+
st.write(correct_text(dataframe, column_index))
|
78 |
+
except UnicodeDecodeError:
|
79 |
+
st.error(
|
80 |
+
"Error: Unable to decode the file. Please check the file encoding or try another file.")
|
81 |
+
except Exception as e:
|
82 |
+
st.error(f"An unexpected error occurred: {e}")
|
83 |
+
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
main()
|