Download mis_pdfs.json from alphasoft91/llama3: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/alphasoft91/llama3/resolve/main/mis_pdfs.json
- Command line
-
hf download hf://alphasoft91/llama3/mis_pdfs.json
-
curl -L -o mis_pdfs.json https://huggingface.co/alphasoft91/llama3/resolve/main/mis_pdfs.json
1.26 kB
Invalid JSON:Unexpected token 'i', "import os
"... is not valid JSON
| import os | |
| import json | |
| from pathlib import Path | |
| from pypdf import PdfReader | |
| def extract_text_from_pdf(pdf_path): | |
| """Extrae el texto de un PDF usando pypdf.""" | |
| reader = PdfReader(pdf_path) | |
| text = "" | |
| for page in reader.pages: | |
| page_text = page.extract_text() | |
| if page_text: | |
| text += page_text + "\n" | |
| return text | |
| def create_dataset_from_pdfs(pdf_dir, output_json="mis_pdfs.json"): | |
| """ | |
| Recorre la carpeta pdf_dir, extrae el texto de cada PDF y lo guarda en un archivo JSON. | |
| Cada documento se guarda como un diccionario con 'filename' y 'text'. | |
| """ | |
| documentos = [] | |
| for archivo in os.listdir(pdf_dir): | |
| if archivo.lower().endswith(".pdf"): | |
| ruta_pdf = Path(pdf_dir) / archivo | |
| texto = extract_text_from_pdf(ruta_pdf) | |
| documentos.append({"filename": archivo, "text": texto}) | |
| with open(output_json, "w", encoding="utf-8") as f: | |
| json.dump(documentos, f, ensure_ascii=False, indent=2) | |
| print(f"Se ha creado el archivo {output_json} con {len(documentos)} documentos.") | |
| if __name__ == "__main__": | |
| pdf_directory = "ruta_a_tus_pdfs" # <-- Ajusta esta ruta con tu carpeta de PDFs | |
| create_dataset_from_pdfs(pdf_directory, "mis_pdfs.json") | |