juanmartip95 commited on
Commit
251866e
1 Parent(s): 46fc0ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -67
app.py CHANGED
@@ -5,7 +5,6 @@ from ChatResponse import get_completion_from_messages, template
5
  from SendWA import sendWA
6
  from List_Sharepoint import upload_list_sharepoint
7
  import uuid
8
- import json
9
  conversation_id = str(uuid.uuid4())
10
  print(conversation_id)
11
  def res(prompt, historial):
@@ -27,77 +26,19 @@ def res(prompt, historial):
27
 
28
  return respuesta, historial
29
 
30
-
31
  def respond(message, chat_history, history):
32
- response = res(message, history)
33
- bot_message = response[0]
34
- chat_history.append((message, bot_message))
35
- upload_list_sharepoint(conversation_id, "Anonymous", message, bot_message)
36
-
37
- # Check if the conversation is over
38
- if "vista!" in bot_message.lower(): # Check if "vista!" is in the bot's response
39
- #chat_history.append(("System", "The word 'vista!' was found in the bot's response."))
40
- # Get the assistant's last message
41
- last_message = response[1][-1]['content'] # assuming the last message is from the assistant
42
-
43
- # Split the last message by "vista!"
44
- parts = last_message.split("vista!")
45
-
46
- # If there are two parts (text + JSON), process the JSON part
47
- if len(parts) == 2:
48
- json_part = parts[1].strip() # Extract the JSON part and remove leading/trailing spaces
49
- # Now you can pass the JSON output to the extract_info function
50
- info = extract_info(json_part)
51
- # Use the extracted info or append it to chat_history, etc.
52
- chat_history.append(("System", json_part))
53
- chat_history.append(("System", info))# Example: append the extracted JSON to chat history
54
- WA(info)
55
- #print(info) # Example: print the extracted info
56
-
57
- chat_history.append(("System", parts[0])) # Append the text part to chat history
58
-
59
- return "", chat_history, response[1]
60
-
61
-
62
-
63
 
64
  def WA(history):
65
- sendWA("491771675218",history)
66
  return "✅ Validado"
67
 
68
 
69
- def extract_info(json_response):
70
- # Initialize the information to None
71
- info = {
72
- 'nombre_del_usuario': None,
73
- 'numero_de_telefono_del_usuario': None,
74
- 'direccion_del_usuario': None,
75
- 'forma_de_pago': None,
76
- 'pedido': None
77
- }
78
- try:
79
- # Parse the JSON response
80
- data = json.loads(json_response)
81
- # Extract information from the JSON data
82
- for key, value in data.items():
83
- # Check if the key exists in the info dictionary and update the corresponding value
84
- if key in info:
85
- info[key] = value
86
- except json.JSONDecodeError as e:
87
- # Handle JSON decoding error
88
- print(f"Error parsing JSON: {e}")
89
- return None
90
-
91
- # Check if all the information has been extracted
92
- if all(value is not None for value in info.values()):
93
- # Assuming WA(info) does something with the extracted info
94
- WA(info)
95
- return info
96
- else:
97
- # If required fields are missing, return None or handle the error as needed
98
- print("Required fields are missing.")
99
- return None
100
-
101
  css = """.gradio-container-3-47-1 button {font-size: 75%;}
102
  .message.svelte-1pjfiar.svelte-1pjfiar.svelte-1pjfiar {padding: 5px;}
103
  """
@@ -126,7 +67,6 @@ with gr.Blocks(theme=gr.themes.Default(text_size="sm"),css=css) as demo:
126
  btn.click(respond, inputs=[msg, chatbot,history], outputs=[msg, chatbot,history])
127
  msg.submit(respond, inputs=[msg, chatbot,history], outputs=[msg, chatbot,history]) #Press enter to submit
128
  pedido.click(WA, inputs=history,outputs=pedido)
129
-
130
  clear.click(lambda: None, None, chatbot, queue=False)
131
  #history_box = gr.Textbox()
132
  #history_button.click(history, inputs=chatbot, outputs=history_box)
 
5
  from SendWA import sendWA
6
  from List_Sharepoint import upload_list_sharepoint
7
  import uuid
 
8
  conversation_id = str(uuid.uuid4())
9
  print(conversation_id)
10
  def res(prompt, historial):
 
26
 
27
  return respuesta, historial
28
 
 
29
  def respond(message, chat_history, history):
30
+ #print(history,type(history))
31
+ response= res(message,history)
32
+ bot_message = response[0]
33
+ chat_history.append((message, bot_message))
34
+ upload_list_sharepoint(conversation_id,"Anonymous",message,bot_message)
35
+ return "", chat_history,response[1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def WA(history):
38
+ sendWA("573138614084",history)
39
  return "✅ Validado"
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  css = """.gradio-container-3-47-1 button {font-size: 75%;}
43
  .message.svelte-1pjfiar.svelte-1pjfiar.svelte-1pjfiar {padding: 5px;}
44
  """
 
67
  btn.click(respond, inputs=[msg, chatbot,history], outputs=[msg, chatbot,history])
68
  msg.submit(respond, inputs=[msg, chatbot,history], outputs=[msg, chatbot,history]) #Press enter to submit
69
  pedido.click(WA, inputs=history,outputs=pedido)
 
70
  clear.click(lambda: None, None, chatbot, queue=False)
71
  #history_box = gr.Textbox()
72
  #history_button.click(history, inputs=chatbot, outputs=history_box)