Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,40 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from llama_index.core import VectorStoreIndex
|
4 |
+
from llama_index.readers.file import PDFReader
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Funci贸n para descargar el archivo PDF desde una URL
|
8 |
+
def download_pdf(url, destination):
|
9 |
+
os.makedirs(os.path.dirname(destination), exist_ok=True)
|
10 |
+
response = requests.get(url)
|
11 |
+
with open(destination, 'wb') as f:
|
12 |
+
f.write(response.content)
|
13 |
+
|
14 |
+
# Funci贸n para crear el 铆ndice a partir del PDF
|
15 |
+
def create_index_from_pdf(pdf_path):
|
16 |
+
pdf_reader = PDFReader() # Inicializar el PDFReader sin la ruta del archivo
|
17 |
+
with open(pdf_path, 'rb') as pdf_file: # Abrir el archivo en modo binario
|
18 |
+
documents = pdf_reader.load_data(file=pdf_file) # Pasar el archivo abierto
|
19 |
+
|
20 |
+
# Crear el 铆ndice
|
21 |
+
index = VectorStoreIndex.from_documents(documents)
|
22 |
+
return index
|
23 |
+
|
24 |
+
# Ruta del archivo PDF a descargar
|
25 |
+
pdf_url = 'https://www.boe.es/buscar/pdf/1995/BOE-A-1995-25444-consolidado.pdf'
|
26 |
+
pdf_path = './BOE-A-1995-25444-consolidado.pdf'
|
27 |
+
|
28 |
+
# Descargar el PDF
|
29 |
+
download_pdf(pdf_url, pdf_path)
|
30 |
+
|
31 |
+
# Crear el 铆ndice a partir del PDF
|
32 |
+
index = create_index_from_pdf(pdf_path)
|
33 |
+
|
34 |
+
# Funci贸n de b煤squeda en el 铆ndice
|
35 |
+
def search_pdf(query):
|
36 |
+
response = index.query(query)
|
37 |
+
return response
|
38 |
+
|
39 |
+
# Interfaz Gradio
|
40 |
+
gr.Interface(fn=search_pdf, inputs="text", outputs="text").launch()
|