Spaces:
Sleeping
Sleeping
File size: 4,943 Bytes
fc133fb 58e4b18 fc133fb 399084d fc133fb 58e4b18 fc133fb 399084d ccba23d 58e4b18 399084d 58e4b18 fc133fb 58e4b18 fc133fb 399084d fc133fb 399084d fc133fb 399084d fc133fb 399084d fc133fb ccba23d 58e4b18 fc133fb 58e4b18 ccba23d fc133fb ccba23d fc133fb ccba23d 399084d ccba23d fc133fb ccba23d fc133fb 58e4b18 |
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 |
import os
import random
import re
from string import Template
import gradio as gr
import pandas as pd
from datasets import Dataset
from huggingface_hub import HfApi
from pypdf import PdfReader
to_be_removed = ["ͳ", "•", "→", "□", "▪", "►", "�", "", "", "", ""]
to_be_replaced = {
"½": "1/2",
"–": "-",
"‘": "'",
"’": "'",
"…": "...",
"₋": "-",
"−": "-",
"⓫": "11.",
"⓬": "12.",
"⓭": "13.",
"⓮": "14.",
"◦": "°",
"❶": "1.",
"❷": "2.",
"❸": "3.",
"❹": "4.",
"❺": "5.",
"❻": "6.",
"❼": "7.",
"❽": "8.",
"❾": "9.",
"❿": "10.",
"\n": " ",
}
def clean(text):
# Remove all the unwanted characters
for char in to_be_removed:
text = text.replace(char, "")
# Replace all the characters that need to be replaced
for char, replacement in to_be_replaced.items():
text = text.replace(char, replacement)
# For all \n, if the next line doesn't start with a capital letter, remove the \n
# text = re.sub(r"\n([^A-ZÀ-ÖØ-Þ])", r" \1", text)
# Make sure that every "." is followed by a space
text = re.sub(r"\.([^ ])", r". \1", text)
# Add a space between a lowercase followed by an uppercase "aA" -> "a A" (include accents)
text = re.sub(r"([a-zà-öø-ÿ])([A-ZÀ-ÖØ-Þ])", r"\1 \2", text)
# Make sure that there is no space before a comma, a period, or a hyphen
text = text.replace(" ,", ",")
text = text.replace(" .", ".")
text = text.replace(" -", "-")
text = text.replace("- ", "-")
while " " in text:
text = text.replace(" ", " ")
return text
def pdf2dataset(file, progress=gr.Progress()):
progress(0, desc="Starting...")
reader = PdfReader(file)
num_pages = len(reader.pages)
dataset_name = f"{random.getrandbits(128):x}"
page_texts = []
for page in progress.tqdm(reader.pages, total=num_pages, desc="Converting pages"):
page_text = page.extract_text()
page_text = clean(page_text)
page_texts.append(page_text)
progress(0, desc="Uploading to Hugging Face...")
dataset = Dataset.from_dict({"text": page_texts})
dataset.push_to_hub(f"pdf2dataset/{dataset_name}", token=os.getenv("TOKEN"))
progress(1, desc="Done!")
instrctions = instructions_template.substitute(dataset_name=dataset_name)
preview = dataset["text"][:10]
preview = pd.DataFrame(preview, columns=["text"])
print(f"Dataset {dataset_name} uploaded successfully.")
return instrctions, preview, dataset_name
def delete_dataset(dataset_name):
api = HfApi()
if "/" in dataset_name:
user_id, dataset_name = dataset_name.split("/")
else:
user_id = "pdf2dataset"
if not user_id == "pdf2dataset":
print(f"Invalid namespace deteced in {dataset_name}.")
return f"❌ Invalid namespace deteced: {user_id}"
repo_id = f"{user_id}/{dataset_name}"
try:
api.delete_repo(repo_id, repo_type="dataset")
print(f"Dataset {dataset_name} deleted successfully.")
return "✅ Dataset deleted successfully."
except Exception as e:
print(f"Error deleting dataset{dataset_name}: {e}")
return f"❌ Error deleting dataset: {e}"
caution_text = """⚠️ Caution:
- This process will upload your data to a public Hugging Face repository. Do not upload sensitive information.
- Anyone (including you) will be able to delete the dataset once it is uploaded.
"""
instructions_template = Template(
"""
🔗: https://huggingface.co/datasets/pdf2dataset/$dataset_name.
```python
from datasets import load_dataset
dataset = load_dataset("pdf2dataset/$dataset_name")
```
"""
)
with gr.Blocks() as demo:
gr.Markdown("# PDF to 🤗 Dataset")
gr.Markdown("## 1️⃣ Upload a PDF")
file = gr.File(file_types=["pdf"], height=50)
gr.Markdown(caution_text)
gr.Markdown("## 2️⃣ Convert the PDF and upload")
convert_button = gr.Button("🔄 Convert and upload")
preview = gr.Dataframe(
label="Preview (first 10 rows)", headers=["text"], datatype=["str"], row_count=10, wrap=True, height=200
)
gr.Markdown("## 3️⃣ Use the dataset in your code")
instructions = gr.Markdown(instructions_template.substitute(dataset_name="generated_dataset_name"))
gr.Markdown("## 4️⃣ Delete the (optional)")
dataset_name_to_delete = gr.Textbox("", placeholder="Enter dataset name to delete", label="Dataset to delete")
delete_button = gr.Button("🗑️ Delete dataset")
# Define the actions
convert_button.click(pdf2dataset, inputs=[file], outputs=[instructions, preview, dataset_name_to_delete])
delete_button.click(delete_dataset, inputs=[dataset_name_to_delete], outputs=[delete_button])
dataset_name_to_delete.input(lambda: "🗑️ Delete dataset", outputs=[delete_button])
demo.launch()
|