File size: 3,617 Bytes
47fd67f
db2cb65
 
 
710f828
369a119
2b64383
e6aea65
2b64383
4085b87
cdd1ca4
 
6ac216c
db2cb65
 
 
 
cdd1ca4
db2cb65
 
69177af
5e6178f
 
69177af
5e6178f
db2cb65
6ac216c
69177af
 
0ff22d2
db5d6fb
69177af
 
 
 
 
 
 
 
6cc9da5
69177af
 
 
47fd67f
05778fb
1189425
6ac216c
 
 
7205827
6ac216c
47fd67f
7487a64
6ac216c
 
d4143db
dcfaf95
604cb00
e916647
d4143db
e916647
 
d4143db
 
 
 
 
 
 
9dd7bdc
d4143db
e51f312
 
 
 
 
2783501
 
 
688dd54
d4143db
05778fb
 
25bbf66
e51f312
d4143db
 
 
378ccc6
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
import gradio as gr
from SysPrompt import sysPrompt
from index_query import get_docs
from ChatResponse import get_completion_from_messages, template
from SendWA import sendWA 
from List_Sharepoint import upload_list_sharepoint
import uuid
import json
conversation_id = str(uuid.uuid4())
print(conversation_id)
import copy


def res(prompt, historial):
    # Preparar historial
    #historial=json.loads(historial)
    if historial==[]:
        historial=copy.deepcopy(sysPrompt)
    historial.append({'role':'user', 'content':prompt})
    
    # Compleción 
    context=get_docs(prompt)
    historial.append({'role':'system', 'content':template(context)})
    response_choices0 = get_completion_from_messages(historial)
    historial.pop() # delete the context prompt

    # Tratamos extraer la respuesta
    respuesta = response_choices0.message.content
    if respuesta == None:
        final=json.loads(response_choices0.message.tool_calls[0].function.arguments)["final_pedido"]
        print(final)
        if final=='S':
            print("¡¡¡Nos vamos a redirección a un asesor!!!")
            respuesta="Hemos registrado un pedido. ¿Qué quieres hacer ahora?"
            historial.append(bot_response(respuesta))
            historial.append({'role':'system', 'content': "El pedido ha finalizado, pero puedes continuar con la conversación normalmente."})
    else:
        historial.append(bot_response(respuesta))
    
    return respuesta, historial

def bot_response(respuesta):
    return {'role':'assistant', 'content':f"{respuesta}"}

def respond(message, chat_history, history):
    print("historial:", history, type(history))
    response=res(message,history)
    bot_message= response[0]
    chat_history.append((message, bot_message))
    #upload_list_sharepoint(conversation_id,"Anonymous",message,bot_message)
    return "", chat_history,response[1]

def WA(history):
    sendWA("573138614084",history)
    return "✅ Validado"

css = """.gradio-container-3-47-1 button {font-size: 75%;}
.message.svelte-1pjfiar.svelte-1pjfiar.svelte-1pjfiar {padding: 5px;}
"""
botImg='https://lagunaai-my.sharepoint.com/personal/juanariasv_lagunaai_onmicrosoft_com/Documents/output-onlinepngtools.png'
with gr.Blocks(theme=gr.themes.Default(text_size="sm"),css=css) as demo:
    chatbot = gr.Chatbot(height=150,avatar_images=(None,botImg), bubble_full_width=False) #just to fit the notebook
    with gr.Row():
      with gr.Column():
        msg = gr.Textbox(show_label=False)
      with gr.Column():
        with gr.Row():
          btn = gr.Button("➤")
          btn.size="sm"
          clear = gr.ClearButton(components=[msg, chatbot], value="🔄")
          clear.size="sm"
        # with gr.Row():
        #   upload=gr.UploadButton("Cargar comprobante 📁", file_types=["image"])
        #   upload.size="sm"
        #   pedido=gr.Button("Validar pedido")
        #   pedido.size="sm"
        with gr.Row():
          humano = gr.Button("Asesor humano 🙋‍♂️🙋‍♀️",link='https://wa.me')
          humano.size="sm"    
    history= gr.State([])#gr.JSON(value=[],visible=True)
    #history_button = gr.Button("Show history")
    btn.click(respond, inputs=[msg, chatbot,history], outputs=[msg, chatbot,history])
    msg.submit(respond, inputs=[msg, chatbot,history], outputs=[msg, chatbot,history]) #Press enter to submit
    clear.click(lambda: [None,[]], None, [chatbot,history], queue=False)
    #pedido.click(WA, inputs=history,outputs=pedido)
    #history_box = gr.Textbox()
    #history_button.click(history, inputs=chatbot, outputs=history_box)
gr.close_all()
demo.launch()