Spaces:
Runtime error
Runtime error
fcernafukuzaki
commited on
Commit
•
aa7fdfd
1
Parent(s):
2235554
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ import gradio as gr
|
|
3 |
from pathlib import Path
|
4 |
import os
|
5 |
import pandas as pd
|
|
|
|
|
|
|
6 |
import openai
|
7 |
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext
|
8 |
from langchain.chat_models import ChatOpenAI
|
@@ -21,6 +24,13 @@ from openai.embeddings_utils import cosine_similarity
|
|
21 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
22 |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
class ChatBotInmobiliaria():
|
26 |
def __init__(self):
|
@@ -59,27 +69,10 @@ class ChatBotInmobiliaria():
|
|
59 |
print(e)
|
60 |
return "Hubo un error."
|
61 |
|
62 |
-
def ask(
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
print(f"Nombre del archivo: {path_file}")
|
67 |
-
extension = os.path.splitext(path_file)[1]
|
68 |
-
print(f"Extensión el archivo: {extension}")
|
69 |
-
dir_name = str(Path(path_file).parent)
|
70 |
-
print(f"Carpeta donde está ubicado el archivo: {dir_name}")
|
71 |
-
|
72 |
-
if extension.lower() == ".pdf":
|
73 |
-
chatbot = ChatBotInmobiliaria()
|
74 |
-
DATASET_JSON = "dataset_file.json"
|
75 |
-
chatbot.create_dataset(dir_name, DATASET_JSON)
|
76 |
-
chatbot.load_dataset(DATASET_JSON)
|
77 |
-
return chatbot.ask(question=pregunta)
|
78 |
-
elif extension.lower() == ".json":
|
79 |
-
chatbot = ChatBotInmobiliaria()
|
80 |
-
chatbot.load_dataset(path_file)
|
81 |
-
return chatbot.ask(question=pregunta)
|
82 |
-
|
83 |
|
84 |
|
85 |
# Gradio
|
@@ -94,16 +87,24 @@ Demo Inmobiliaria, el objetivo es responder preguntas a través de OpenAI previa
|
|
94 |
"""
|
95 |
|
96 |
article = "<p style='text-align: center'><a href='http://allaideas.com/index.html' target='_blank'>Demo Inmobiliaria: Link para más info</a> </p>"
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
101 |
|
102 |
-
|
|
|
103 |
|
104 |
demo = gr.Interface(
|
105 |
-
fn=
|
106 |
-
inputs=
|
107 |
outputs=out1,
|
108 |
title="Demo Inmobiliaria",
|
109 |
description=description,
|
|
|
3 |
from pathlib import Path
|
4 |
import os
|
5 |
import pandas as pd
|
6 |
+
import time
|
7 |
+
import random
|
8 |
+
|
9 |
import openai
|
10 |
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext
|
11 |
from langchain.chat_models import ChatOpenAI
|
|
|
24 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
25 |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
26 |
|
27 |
+
# CONSTANTES
|
28 |
+
DATASET_JSON = "demo-inmobiliaria.json"
|
29 |
+
|
30 |
+
# Ubicación dataset
|
31 |
+
carpeta_actual = os.getcwd()
|
32 |
+
print(f"Nombre de la carpeta actual: {carpeta_actual}")
|
33 |
+
PATH_FILE = f"{os.getcwd()}/{DATASET_JSON}"
|
34 |
|
35 |
class ChatBotInmobiliaria():
|
36 |
def __init__(self):
|
|
|
69 |
print(e)
|
70 |
return "Hubo un error."
|
71 |
|
72 |
+
def ask(pregunta):
|
73 |
+
gpt_bot = ChatBotInmobiliaria()
|
74 |
+
gpt_bot.load_dataset(PATH_FILE)
|
75 |
+
return gpt_bot.ask(question=pregunta)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
|
78 |
# Gradio
|
|
|
87 |
"""
|
88 |
|
89 |
article = "<p style='text-align: center'><a href='http://allaideas.com/index.html' target='_blank'>Demo Inmobiliaria: Link para más info</a> </p>"
|
90 |
+
examples = [["¿Cuánto está una casa en San Isidro?"],["Hay precios más baratos?"],["A dónde llamo?"]]
|
91 |
+
|
92 |
+
gpt_bot = ChatBotInmobiliaria()
|
93 |
+
gpt_bot.load_dataset(PATH_FILE)
|
94 |
+
chat_history = []
|
95 |
|
96 |
+
def chat(pregunta):
|
97 |
+
bot_message = str(gpt_bot.ask(question=pregunta))
|
98 |
+
chat_history.append((pregunta, bot_message))
|
99 |
+
time.sleep(1)
|
100 |
+
return chat_history
|
101 |
|
102 |
+
in1 = gr.inputs.Textbox(label="Pregunta")
|
103 |
+
out1 = gr.outputs.Chatbot(label="Respuesta").style(height=450)
|
104 |
|
105 |
demo = gr.Interface(
|
106 |
+
fn=chat,
|
107 |
+
inputs=in1,
|
108 |
outputs=out1,
|
109 |
title="Demo Inmobiliaria",
|
110 |
description=description,
|