Spaces:
Build error
Build error
fix
Browse files
app.py
CHANGED
|
@@ -1,42 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
|
| 5 |
# Función para manejar el flujo de la conversación
|
| 6 |
-
def model(text, conversation):
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Payload (datos a enviar)
|
| 10 |
data = {
|
| 11 |
-
"patient_data": {
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"message": {"text": text},
|
| 15 |
"step": "chat"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
# Encabezados (puedes agregar autorización si es necesario)
|
| 19 |
headers = {
|
| 20 |
"Content-Type": "application/json"
|
| 21 |
}
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
response = requests.post(url, json=data, headers=headers)
|
| 25 |
|
| 26 |
-
# Mostrar respuesta
|
| 27 |
-
print("Status code:", response.status_code)
|
| 28 |
-
print("Response JSON:", response.json()['output']['response'])
|
| 29 |
output = response.json()['output']['response']
|
| 30 |
-
return output
|
| 31 |
|
| 32 |
-
def interact_with_chatbot(text, conversation):
|
| 33 |
-
|
| 34 |
-
response
|
| 35 |
-
|
| 36 |
-
# Devolver la respuesta generada y el estado de la conversación (como lista)
|
| 37 |
-
#conversation.append((text, response)) # Agregar interacción a la lista
|
| 38 |
-
return response
|
| 39 |
|
| 40 |
-
#
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import uuid
|
| 4 |
|
| 5 |
# Función para manejar el flujo de la conversación
|
| 6 |
+
def model(text, conversation, name, previous_prescriptions, obra_social, plan, nro_afiliado, ids):
|
| 7 |
+
# Si es la primera vez, generamos los IDs
|
| 8 |
+
if ids is None:
|
| 9 |
+
ids = {
|
| 10 |
+
"unique_id": str(uuid.uuid4()),
|
| 11 |
+
"user_id": str(uuid.uuid4())
|
| 12 |
+
}
|
| 13 |
|
|
|
|
| 14 |
data = {
|
| 15 |
+
"patient_data": {
|
| 16 |
+
"name": name,
|
| 17 |
+
"previous_prescriptions": previous_prescriptions,
|
| 18 |
+
"obra_social": obra_social,
|
| 19 |
+
"plan": plan,
|
| 20 |
+
"nro_afiliado": nro_afiliado
|
| 21 |
+
},
|
| 22 |
+
"unique_id": ids["unique_id"],
|
| 23 |
+
"user_id": ids["user_id"],
|
| 24 |
"message": {"text": text},
|
| 25 |
"step": "chat"
|
| 26 |
}
|
| 27 |
|
|
|
|
| 28 |
headers = {
|
| 29 |
"Content-Type": "application/json"
|
| 30 |
}
|
| 31 |
+
print("DATA",data)
|
| 32 |
|
| 33 |
+
response = requests.post(url="https://dev.apis.umasalud.com/ai/predoc", json=data, headers=headers)
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
output = response.json()['output']['response']
|
| 36 |
+
return output, ids # devolvemos los ids actualizados para mantenerlos
|
| 37 |
|
| 38 |
+
def interact_with_chatbot(text, conversation, name, previous_prescriptions, obra_social, plan, nro_afiliado, ids):
|
| 39 |
+
response, ids = model(text, conversation, name, previous_prescriptions, obra_social, plan, nro_afiliado, ids)
|
| 40 |
+
return response, conversation + [(text, response)], ids
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Interfaz
|
| 43 |
+
with gr.Blocks():
|
| 44 |
+
gr.Markdown("## 👨⚕️ PreDoc Chatbot 📋💊")
|
| 45 |
|
| 46 |
+
with gr.Accordion("📝 Datos del paciente", open=True):
|
| 47 |
+
name = gr.Textbox(label="Nombre del paciente", value="Roberto")
|
| 48 |
+
previous_prescriptions = gr.Textbox(label="Preescripciones previas", placeholder="Ej: losartán 50mg...")
|
| 49 |
+
obra_social = gr.Textbox(label="Obra social", placeholder="Ej: OSDE")
|
| 50 |
+
plan = gr.Textbox(label="Plan", placeholder="Ej: 210")
|
| 51 |
+
nro_afiliado = gr.Textbox(label="Número de afiliado", placeholder="Ej: 12345678")
|
| 52 |
+
|
| 53 |
+
chatbot = gr.Chatbot()
|
| 54 |
+
msg = gr.Textbox(label="Tu mensaje")
|
| 55 |
+
|
| 56 |
+
state_chat = gr.State([]) # historial de la conversación
|
| 57 |
+
state_ids = gr.State(None) # IDs generados solo una vez
|
| 58 |
+
|
| 59 |
+
def user_send(message, chat_history, name, previous_prescriptions, obra_social, plan, nro_afiliado, ids):
|
| 60 |
+
response, new_history, new_ids = interact_with_chatbot(
|
| 61 |
+
message, chat_history, name, previous_prescriptions, obra_social, plan, nro_afiliado, ids
|
| 62 |
+
)
|
| 63 |
+
return "", new_history, new_ids
|
| 64 |
+
|
| 65 |
+
msg.submit(
|
| 66 |
+
user_send,
|
| 67 |
+
[msg, state_chat, name, previous_prescriptions, obra_social, plan, nro_afiliado, state_ids],
|
| 68 |
+
[msg, chatbot, state_ids]
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
#demo.launch(debug=True)
|