bugfix: function
Browse files
app.py
CHANGED
@@ -1,11 +1,40 @@
|
|
1 |
import time
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
yield message[: i+1]
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import time
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
|
6 |
+
# Función que hace la solicitud a la API y muestra la respuesta progresivamente
|
7 |
+
def slow_api_response(message, history):
|
8 |
+
url = "https://data-monopolio.dev.dd360.mx/ai-assistant/v1"
|
|
|
9 |
|
10 |
+
# Crea el payload con el mensaje proporcionado
|
11 |
+
payload = json.dumps({
|
12 |
+
"query": message,
|
13 |
+
"sessionId": "12345",
|
14 |
+
"numMessages": "1",
|
15 |
+
"userId": "user-01",
|
16 |
+
"property_id": "c0ced2220b87fc23762facf617157a4f"
|
17 |
+
})
|
18 |
+
|
19 |
+
headers = {
|
20 |
+
'Content-Type': 'application/json'
|
21 |
+
}
|
22 |
|
23 |
+
try:
|
24 |
+
# Realiza la solicitud POST a la API
|
25 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
26 |
+
api_response = response.text
|
27 |
+
|
28 |
+
# Simula una respuesta progresiva
|
29 |
+
for i in range(len(api_response)):
|
30 |
+
time.sleep(0.05) # Simula un retraso
|
31 |
+
yield api_response[:i + 1] # Devuelve la respuesta progresiva
|
32 |
|
33 |
+
except requests.RequestException as e:
|
34 |
+
yield f"Error: {str(e)}"
|
35 |
+
|
36 |
+
# Interfaz Gradio usando la función slow_api_response
|
37 |
+
demo = gr.ChatInterface(fn=slow_api_response)
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
demo.launch()
|