File size: 1,263 Bytes
88dfbed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c88d9c
88dfbed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()