File size: 13,848 Bytes
9a33833
8ae36cb
9a33833
9be1c64
 
a6e838a
1ce93ee
9be1c64
 
 
 
521ae0b
 
 
 
 
 
 
 
 
 
 
 
a6e838a
521ae0b
 
 
7e8e126
 
9d8a1d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e838a
9d8a1d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e838a
9d8a1d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import os
os.system('pip install curl_cffi tqdm bitsandbytes tiktoken g4f pinecone-client pandas datasets sentence-transformers')

# Setup and load your keys
import os
from g4f import ChatCompletion
#from google.colab import userdata
from pinecone import Pinecone
import pandas as pd
from datasets import Dataset
from sentence_transformers import SentenceTransformer
import gradio as gr

model_name = "BAAI/bge-m3"

# APIs personales
#PINECONE_ENVIRONMENT = us-east-1
#PINECONE_API_KEY = 3a3e9022-381d-436e-84cb-ba93464d283e

os.environ["PINECONE_ENVIRONMENT"] = "us-east-1"
os.environ["PINECONE_API_KEY"] = "3a3e9022-381d-436e-84cb-ba93464d283e"

# Retrieve the Pinecone API key from the user
PINECONE_API_KEY = "3a3e9022-381d-436e-84cb-ba93464d283e"  # Use the key you set in the secrets
PINECONE_ENVIRONMENT = "us-east-1"  # Use the environment you set in the secrets

# Initialize Pinecone with the API key
pc = Pinecone(api_key=PINECONE_API_KEY)

# Global variables to store the selected model and dimensions
EMBED_MODEL = 'BGE_M3-1024'
DIMENSIONS = 1024

# Confirm selection automatically
print(f"Model selected: {EMBED_MODEL}")
print(f"Dimensions set as: {DIMENSIONS}")

# Function to print current selection (can be used in other cells)
def print_current_selection():
    print(f"Currently selected model: {EMBED_MODEL}")
    print(f"Dimensions: {DIMENSIONS}")

# Establecer el nombre del 铆ndice autom谩ticamente
INDEX_NAME = 'vestidos'

# Obtener la clave API de Pinecone
#PINECONE_API_KEY = userdata.get('PINECONE_API_KEY')

def connect_to_pinecone(index_name):
    global INDEX_NAME
    try:
        pc = Pinecone(api_key=PINECONE_API_KEY)
        index = pc.Index(index_name)

        # Asegurarse de que la conexi贸n se establezca
        index_stats = index.describe_index_stats()
        print(f"Successfully connected to Pinecone index '{index_name}'!")
        print("Index Stats:", index_stats)

        # Actualizar la variable global INDEX_NAME
        INDEX_NAME = index_name
        print(f"Global INDEX_NAME updated to: {INDEX_NAME}")

    except Exception as e:
        print(f"Failed to connect to Pinecone index '{index_name}':", str(e))

# Conectar autom谩ticamente al 铆ndice "vestidos"
connect_to_pinecone(INDEX_NAME)

# Funci贸n para imprimir el nombre del 铆ndice actual (puede ser usada en otras celdas)
def print_current_index():
    print(f"Current index name: {INDEX_NAME}")

# Verificar si las variables globales necesarias est谩n configuradas
if 'INDEX_NAME' not in globals() or INDEX_NAME is None:
    raise ValueError("INDEX_NAME is not set. Please set the index name first.")

if 'EMBED_MODEL' not in globals() or EMBED_MODEL is None:
    raise ValueError("EMBED_MODEL is not set. Please select an embedding model first.")

# Inicializar cliente de Pinecone
#PINECONE_API_KEY = userdata.get('PINECONE_API_KEY')
pc = Pinecone(api_key=PINECONE_API_KEY)

# Inicializar el 铆ndice de Pinecone
index = pc.Index(INDEX_NAME)

# Obtener la dimensi贸n del 铆ndice
index_stats = index.describe_index_stats()
vector_dim = index_stats['dimension']
print(f"Index dimension: {vector_dim}")

# Definir manualmente los campos de contexto y enlace
CONTEXT_FIELDS = ['Etiqueta', 'Pregunta 1', 'Pregunta 2', 'Pregunta 3', 'Respuesta Combinada']
LINK_FIELDS = ['Etiqueta', 'Respuesta Combinada']

# Imprimir confirmaci贸n de campos seleccionados
print(f"Context fields set to: {CONTEXT_FIELDS}")
print(f"Link fields set to: {LINK_FIELDS}")

# Funci贸n para obtener las selecciones actuales de campos (puede ser usada en otras celdas)
def get_field_selections():
    return {
        "CONTEXT_FIELDS": CONTEXT_FIELDS,
        "LINK_FIELDS": LINK_FIELDS
    }

#####################################

# Check if required global variables are set
if 'EMBED_MODEL' not in globals() or EMBED_MODEL is None:
    raise ValueError("EMBED_MODEL is not set. Please select an embedding model first.")
if 'INDEX_NAME' not in globals() or INDEX_NAME is None:
    raise ValueError("INDEX_NAME is not set. Please create or select an index first.")
if 'CONTEXT_FIELDS' not in globals() or 'LINK_FIELDS' not in globals():
    raise ValueError("CONTEXT_FIELDS and LINK_FIELDS are not set. Please run the field selection cell first.")

# Initialize the Sentence-Transformer model
embedding_model = SentenceTransformer(model_name)

# Initialize Pinecone with the API key and connect to the index
pinecone_client = Pinecone(api_key=PINECONE_API_KEY)
index = pinecone_client.Index(INDEX_NAME)

# Constants
LIMIT = 3750

def vector_search(query):
    # Generate embedding using Sentence-Transformer model
    xq = embedding_model.encode(query)

    # Perform vector search on Pinecone index
    res = index.query(vector=xq.tolist(), top_k=3, include_metadata=True)
    if res['matches']:
        return [
            {
                'content': ' '.join(f"{k}: {v}" for k, v in match['metadata'].items() if k in CONTEXT_FIELDS and k != 'Etiqueta'),
                'metadata': match['metadata']
            }
            for match in res['matches']
            if 'metadata' in match
        ]
    return []

def create_prompt(query, contexts):
    prompt_start = "\n\nContexto:\n"
    prompt_end = f"\n\nPregunta: {query}\nRespuesta:"

    current_contexts = "\n\n---\n\n".join([context['content'] for context in contexts])
    if len(prompt_start + current_contexts + prompt_end) >= LIMIT:
        # Truncate contexts if they exceed the limit
        available_space = LIMIT - len(prompt_start) - len(prompt_end)
        truncated_contexts = current_contexts[:available_space]
        return prompt_start + truncated_contexts + prompt_end
    else:
        return prompt_start + current_contexts + prompt_end

def complete(prompt):
    return [f"Hola"]

def check_image_exists(filepath):
    return os.path.exists(filepath)

def chat_function(message, history):
    # Perform vector search
    search_results = vector_search(message)

    # Create prompt with relevant contexts
    query_with_contexts = create_prompt(message, search_results)

    # Generate response
    response = complete(query_with_contexts)

    partial_message = response[0].split("\n")[0]  # Solo tomar la primera l铆nea de la respuesta

    # Handle the logic for processing tags and images internally
    relevant_links = [result['metadata'].get(field) for result in search_results for field in LINK_FIELDS if field in result['metadata']]
    full_response = partial_message
    image_url = None
    tags_detected = []

    filtered_links = []
    if relevant_links:
        for link in relevant_links:
            if any(tag in link for tag in ["lila_61", "lila_63", "lila_62", "lila_64", "fuxia_70", "fuxia_71", "fuxia_72", "fuxia_73", "fuxia_74", "melon_68", "melon_66", "melon_67", "melon_65", "vino_19", "vino_20", "barney_69", "loro_27", "lacre_02", "amarillo_03", "amarillo_04", "azulino_11", "azulino_14", "azulino_12", "azulino_13", "beigs_09", "beigs_10", "beigs_07", "beigs_06", "beigs_08", "beigs_05", "marina_32", "marina_29", "marina_28", "marina_31", "marina_30", "rojo_26", "rojo_23", "rojo_21", "rojo_22", "rojo_25", "rojo_24", "celeste_40", "celeste_38", "celeste_39", "celeste_33", "celeste_35", "celeste_37", "celeste_41", "celeste_42", "celeste_34", "celeste_36", "sirenita_01", "marino_18", "marino_17", "marino_16", "marino_15", "rosa_87", "rosa_86", "rosa_79", "rosa_82", "rosa_83", "rosa_78", "rosa_84", "rosa_85", "rosa_75", "rosa_80", "rosa_81", "rosa_77", "rosa_76", "blanco_55", "blanco_56", "blanco_53", "blanco_52", "blanco_57", "blanco_49", "blanco_51", "blanco_60", "blanco_47", "blanco_44", "blanco_50", "blanco_48", "blanco_59", "blanco_43", "blanco_58", "blanco_46", "blanco_45", "blanco_54"]):
                tags_detected.append(link)  # Save the tag but don't display it
            else:
                filtered_links.append(link)

        # Add the first relevant link under a single "Respuestas relevantes" section
        if filtered_links:
            full_response += f".\n\nTe detallamos nuestro contenido a continuaci贸n:\n" + filtered_links[0]

        # Now handle the images based on the detected tags
        tags_to_images = {
            "lila_61": "/content/lila_61.jpeg",
            "lila_63": "/content/lila_63.jpeg",
            "lila_62": "/content/lila_62.jpeg",
            "lila_64": "/content/lila_64.jpeg",
            "fuxia_70": "/content/fuxia_70.jpeg",
            "fuxia_71": "/content/fuxia_71.jpeg",
            "fuxia_72": "/content/fuxia_72.jpeg",
            "fuxia_73": "/content/fuxia_73.jpeg",
            "fuxia_74": "/content/fuxia_74.jpeg",
            "melon_68": "/content/melon_68.jpeg",
            "melon_66": "/content/melon_66.jpeg",
            "melon_67": "/content/melon_67.jpeg",
            "melon_65": "/content/melon_65.jpeg",
            "vino_19": "/content/vino_19.jpeg",
            "vino_20": "/content/vino_20.jpeg",
            "barney_69": "/content/barney_69.jpeg",
            "loro_27": "/content/loro_27.png",
            "lacre_02": "/content/lacre_02.jpeg",
            "amarillo_03": "/content/amarillo_03.jpeg",
            "amarillo_04": "/content/amarillo_04.jpeg",
            "azulino_11": "/content/azulino_11.jpeg",
            "azulino_14": "/content/azulino_14.jpeg",
            "azulino_12": "/content/azulino_12.jpeg",
            "azulino_13": "/content/azulino_13.jpeg",
            "beigs_09": "/content/beigs_09.jpeg",
            "beigs_10": "/content/beigs_10.jpeg",
            "beigs_07": "/content/beigs_07.jpeg",
            "beigs_06": "/content/beigs_06.jpeg",
            "beigs_08": "/content/beigs_08.jpeg",
            "beigs_05": "/content/beigs_05.jpeg",
            "marina_32": "/content/marina_32.jpeg",
            "marina_29": "/content/marina_29.jpeg",
            "marina_28": "/content/marina_28.jpeg",
            "marina_31": "/content/marina_31.jpeg",
            "marina_30": "/content/marina_30.jpeg",
            "rojo_26": "/content/rojo_26.jpeg",
            "rojo_23": "/content/rojo_23.jpeg",
            "rojo_21": "/content/rojo_21.jpeg",
            "rojo_22": "/content/rojo_22.jpeg",
            "rojo_25": "/content/rojo_25.jpeg",
            "rojo_24": "/content/rojo_24.jpeg",
            "celeste_40": "/content/celeste_40.jpeg",
            "celeste_38": "/content/celeste_38.jpeg",
            "celeste_39": "/content/celeste_39.jpeg",
            "celeste_33": "/content/celeste_33.jpeg",
            "celeste_35": "/content/celeste_35.jpeg",
            "celeste_37": "/content/celeste_37.jpeg",
            "celeste_41": "/content/celeste_41.jpeg",
            "celeste_42": "/content/celeste_42.jpeg",
            "celeste_34": "/content/celeste_34.jpeg",
            "celeste_36": "/content/celeste_36.jpeg",
            "sirenita_01": "/content/sirenita_01.png",
            "marino_18": "/content/marino_18.jpeg",
            "marino_17": "/content/marino_17.jpeg",
            "marino_16": "/content/marino_16.jpeg",
            "marino_15": "/content/marino_15.jpeg",
            "rosa_87": "/content/rosa_87.jpeg",
            "rosa_86": "/content/rosa_86.png",
            "rosa_79": "/content/rosa_79.jpeg",
            "rosa_82": "/content/rosa_82.png",
            "rosa_83": "/content/rosa_83.jpeg",
            "rosa_78": "/content/rosa_78.jpeg",
            "rosa_84": "/content/rosa_84.jpeg",
            "rosa_85": "/content/rosa_85.jpeg",
            "rosa_75": "/content/rosa_75.jpeg",
            "rosa_80": "/content/rosa_80.png",
            "rosa_81": "/content/rosa_81.png",
            "rosa_77": "/content/rosa_77.jpeg",
            "rosa_76": "/content/rosa_76.png",
            "blanco_55": "/content/blanco_55.jpeg",
            "blanco_56": "/content/blanco_56.jpeg",
            "blanco_53": "/content/blanco_53.jpeg",
            "blanco_52": "/content/blanco_52.jpeg",
            "blanco_57": "/content/blanco_57.jpeg",
            "blanco_49": "/content/blanco_49.jpeg",
            "blanco_51": "/content/blanco_51.jpeg",
            "blanco_60": "/content/blanco_60.jpeg",
            "blanco_47": "/content/blanco_47.jpeg",
            "blanco_44": "/content/blanco_44.jpeg",
            "blanco_50": "/content/blanco_50.jpeg",
            "blanco_48": "/content/blanco_48.jpeg",
            "blanco_59": "/content/blanco_59.jpeg",
            "blanco_43": "/content/blanco_43.jpeg",
            "blanco_58": "/content/blanco_58.png",
            "blanco_46": "/content/blanco_46.jpeg",
            "blanco_45": "/content/blanco_45.jpeg",
            "blanco_54": "/content/blanco_54.jpeg",
        }


        for tag in tags_detected:
            for key, path in tags_to_images.items():
                if key in tag and check_image_exists(path):
                    image_url = path
                    break

    return full_response, image_url


def update_image(image_url):
    if image_url:
        return image_url
    else:
        return None

# Gradio layout setup
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column(scale=1):
            chatbot_input = gr.Textbox(label="Tu mensaje")
            chatbot_output = gr.Chatbot(label="ChatBot")
            chatbot_history = gr.State(value=[])
            image_url = gr.State(value=None)
            submit_button = gr.Button("Enviar")
        with gr.Column(scale=1):
            image_output = gr.Image(label="Imagen asociada")

    def process_input(message, history):
        full_response, image = chat_function(message, history)
        history.append((message, full_response))
        return history, history, image

    submit_button.click(process_input, inputs=[chatbot_input, chatbot_history], outputs=[chatbot_output, chatbot_history, image_url])
    image_url.change(fn=update_image, inputs=image_url, outputs=image_output)

# Launch the interface
demo.launch(debug=True)