File size: 4,522 Bytes
bb55334
 
 
 
 
aa7fdfd
 
 
bb55334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa7fdfd
 
 
 
 
 
 
bb55334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82dca84
 
 
 
 
 
 
bb55334
 
 
51a6be5
bb55334
 
 
 
 
 
 
 
 
 
 
 
 
bd2dc88
aa7fdfd
 
 
 
bb55334
aa7fdfd
 
 
 
 
bb55334
82dca84
 
f3ceecb
 
82dca84
 
 
f3ceecb
 
 
 
82dca84
 
 
 
 
 
 
 
 
 
 
f3ceecb
82dca84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb55334
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import json
import gradio as gr
from pathlib import Path
import os
import pandas as pd
import time
import random

import openai
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext
from langchain.chat_models import ChatOpenAI
import textwrap

# Procesar datos de PDF
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import CharacterTextSplitter

#import gradio as gr
from openai.embeddings_utils import get_embedding
from openai.embeddings_utils import cosine_similarity


# API KEY OPENAI
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY

# CONSTANTES
DATASET_JSON = "demo-inmobiliaria.json"

# Ubicación dataset
carpeta_actual = os.getcwd()
print(f"Nombre de la carpeta actual: {carpeta_actual}")
PATH_FILE = f"{os.getcwd()}/{DATASET_JSON}"

class ChatBotInmobiliaria():
    def __init__(self):
        self.embedding_engine = "text-embedding-ada-002"
        self.model_name = "gpt-3.5-turbo"
        self.index = None
    
    def create_dataset(self, directory_path, filepath_dataset):
        # directory_path: Directorio donde se ubican los archivos PDF.
        # filepath_dataset: Nombre del archivo JSON vectorizado.
        if directory_path != None:
            #Leer los PDFs
            pdf = SimpleDirectoryReader(directory_path).load_data()
            #Definir e instanciar el modelo
            modelo = LLMPredictor(llm=ChatOpenAI(temperature=0, model_name=self.model_name))
            #Indexar el contenido de los PDFs
            service_context = ServiceContext.from_defaults(llm_predictor=modelo)
            self.index = GPTSimpleVectorIndex.from_documents(pdf, service_context = service_context)
            self.__save_model(filepath_dataset)

    def __save_model(self, filepath):
        #Guardar el índice a disco para no tener que repetir cada vez
        #Recordar que necesistaríamos persistir el drive para que lo mantenga
        self.index.save_to_disk(filepath)
    
    def load_dataset(self, filepath):
        #Cargar el índice del disco
        self.index = GPTSimpleVectorIndex.load_from_disk(filepath)

    def ask(self, question=""):
        if len(question) == 0:
            print("Debe de ingresar una pregunta.")
        try:
            return self.index.query(f"""
            Actúa como un representante de ventas de una inmobiliaria.
            
            {question} 
            
            Responde en español
            """)
        except Exception as e:
            print(e)
            return "Hubo un error."
    

# Gradio

description ="""
<p>
<center>
Demo Inmobiliaria, el objetivo es responder preguntas a través de OpenAI previamente entrenado con un archivo PDF.
<img src="https://raw.githubusercontent.com/All-Aideas/sea_apirest/main/logo.png" alt="logo" width="250"/>
</center>
</p>
"""

article = "<p style='text-align: center'><a href='http://allaideas.com/index.html' target='_blank'>Demo Inmobiliaria: Link para más info</a> </p>"
examples = [["¿Cuánto está una casa en San Isidro?"],["Hay precios más baratos?"],["A dónde llamo?"],["Qué leyes existen?"]]

gpt_bot = ChatBotInmobiliaria()
gpt_bot.load_dataset(PATH_FILE)
chat_history = []

def chat(pregunta):
    bot_message = str(gpt_bot.ask(question=pregunta))
    chat_history.append((pregunta, bot_message))
    time.sleep(1)
    return chat_history


with gr.Blocks() as demo:
    gr.Markdown(f"""
    <p><center><h1>Demo Inmobiliaria</h1></p></center>
    {description}
    """)

    out1 = gr.Chatbot(label="Respuesta").style(height=300)
    with gr.Row():
        in2 = gr.Textbox(label="Pregunta")
        enter = gr.Button("Enviar mensaje")
    
    clear = gr.Button("Nuevo chat")
    
    gr.Markdown(article)

    def respond(message, chat_history):
        bot_message = str(gpt_bot.ask(question=message))
        chat_history.append((message, bot_message))
        time.sleep(1)
        return "", chat_history

    enter.click(fn=respond, inputs=[in2, out1], outputs=[in2, out1])
    in2.submit(respond, [in2, out1], [in2, out1])
    clear.click(lambda: None, None, out1, queue=False)
    
# in1 = gr.inputs.Textbox(label="Pregunta")
# out1 = gr.outputs.Chatbot(label="Respuesta").style(height=350)

# demo = gr.Interface(
#     fn=chat,
#     inputs=in1,
#     outputs=out1,
#     title="Demo Inmobiliaria",
#     description=description,
#     article=article,
#     enable_queue=True,
#     examples=examples,
#     )

demo.launch(debug=True)