import os import requests from llama_index.core import VectorStoreIndex from llama_index.readers.file import PDFReader import gradio as gr # Función para descargar el archivo PDF desde una URL def download_pdf(url, destination): os.makedirs(os.path.dirname(destination), exist_ok=True) response = requests.get(url) with open(destination, 'wb') as f: f.write(response.content) # Función para crear el índice a partir del PDF def create_index_from_pdf(pdf_path): pdf_reader = PDFReader() # Inicializar el PDFReader sin la ruta del archivo documents = pdf_reader.load_data(file=pdf_path) # Pasar la ruta del archivo, no el archivo abierto # Crear el índice index = VectorStoreIndex.from_documents(documents) return index # Ruta del archivo PDF a descargar pdf_url = 'https://www.boe.es/buscar/pdf/1995/BOE-A-1995-25444-consolidado.pdf' pdf_path = './BOE-A-1995-25444-consolidado.pdf' # Descargar el PDF download_pdf(pdf_url, pdf_path) # Crear el índice a partir del PDF index = create_index_from_pdf(pdf_path) # Función de búsqueda en el índice def search_pdf(query): response = index.query(query) return response # Interfaz Gradio gr.Interface(fn=search_pdf, inputs="text", outputs="text").launch()