Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234564 unmodified lines111213141516171819202122import gradio as grfrom datasets import load_datasetimport tempfiledef combine_dataset_texts(dataset_name, split, text_column):try:4 unmodified linesif text_column not in dataset.column_names:raise gr.Error(f"Column '{text_column}' not found in dataset")# Combine all textscombined_text = "\n\n".join([example[text_column] for example in dataset])# Create a temporary filewith tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:f.write(combined_text)return f.nameexcept Exception as e:12345678910111213141516171819202122232425264 unmodified lines313233343536373839404142434445464748import gradio as grfrom datasets import load_datasetimport tempfileimport refrom langdetect import detectdef is_english(text):"""Check if the text is in English."""try:return detect(text) == 'en'except:return Falsedef clean_text(text):"""Remove non-English text and ** from the text."""# Remove **text = re.sub(r'\*\*', '', text)# Split text into sentences and filter out non-English sentencessentences = re.split(r'(?<=[.!?])\s+', text)cleaned_sentences = [s for s in sentences if is_english(s)]return ' '.join(cleaned_sentences)def combine_dataset_texts(dataset_name, split, text_column):try:4 unmodified linesif text_column not in dataset.column_names:raise gr.Error(f"Column '{text_column}' not found in dataset")# Combine all texts into a single string without separating datapointscombined_text = " ".join([example[text_column] for example in dataset])# Clean the text: remove non-English and **cleaned_text = clean_text(combined_text)# Insert a newline after each period (.) except for ."processed_text = re.sub(r'\.(?!")', '.\n', cleaned_text)# Create a temporary filewith tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:f.write(processed_text)return f.nameexcept Exception as e:
app.py+29-3