File size: 1,603 Bytes
88dfbed
bfbdff8
d40cd6c
 
0dd4627
d40cd6c
 
 
 
9ef2fec
bfbdff8
 
 
 
 
f030eea
d40cd6c
 
 
eb1b78e
d40cd6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
from llama_index.core import VectorStoreIndex, Settings
from llama_index.readers.file import PDFReader
import gradio as gr
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

# Disable the default LLM
Settings.llm = None

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)

def create_index_from_pdf(pdf_path):
    pdf_reader = PDFReader()
    documents = pdf_reader.load_data(file=pdf_path)
    
    embed_model = HuggingFaceEmbedding(model_name='BAAI/bge-large-es')
    
    index = VectorStoreIndex.from_documents(
        documents,
        embed_model=embed_model
    )
    
    query_engine = index.as_query_engine(
        similarity_top_k=3,  # Increased to find more relevant context
        response_mode="compact"
    )
    
    return query_engine

pdf_url = 'https://www.boe.es/buscar/pdf/1995/BOE-A-1995-25444-consolidado.pdf'
pdf_path = './BOE-A-1995-25444-consolidado.pdf'

download_pdf(pdf_url, pdf_path)
query_engine = create_index_from_pdf(pdf_path)

def search_pdf(query):
    # Modificar la consulta para buscar específicamente penas
    modified_query = f"Pena para el delito de {query}"
    response = query_engine.query(modified_query)
    return response.response

gr.Interface(
    fn=search_pdf, 
    inputs="text", 
    outputs="text",
    title="Buscador de Penas en Código Penal",
    description="Introduce un tipo de delito para encontrar su pena correspondiente"
).launch()