Myloiose commited on
Commit
a4cc79a
·
verified ·
1 Parent(s): cd2e052

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -1,15 +1,34 @@
1
  import gradio as gr
 
2
 
3
- app = gr.Blocks()
 
 
 
 
 
 
 
 
4
 
5
- def crecimiento_ventas(ventas_str):
6
- ventas = list(map(float, ventas_str.split(',')))
7
- if len(ventas) < 2:
8
- return "Datos insuficientes"
9
- return "Bien" if ventas[-1] > ventas[-2] else "Mal"
10
 
11
- @app.api(input=gr.Textbox(), output=gr.Textbox())
12
- def api_crecimiento(ventas_str):
13
- return crecimiento_ventas(ventas_str)
14
 
15
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import json
3
 
4
+ def crecimiento_ventas_dict(input_json):
5
+ # input_json es un string JSON porque gradio recibe texto
6
+ try:
7
+ data = json.loads(input_json)
8
+ if "ventas" not in data:
9
+ return json.dumps({"error": "Invalid input, missing 'ventas' key"})
10
+
11
+ ventas = data["ventas"]
12
+ # Ejemplo: ventas = [{"marca":"A", "talla":40, "precio":100, "numpares":2}, {...}, ...]
13
 
14
+ # Para simplificar, vamos a sumar "precio * numpares" para el último y penúltimo
15
+ if len(ventas) < 2:
16
+ return json.dumps({"error": "Datos insuficientes"})
 
 
17
 
18
+ total_ultimo = ventas[-1]["precio"] * ventas[-1]["numpares"]
19
+ total_penultimo = ventas[-2]["precio"] * ventas[-2]["numpares"]
 
20
 
21
+ resultado = "Bien" if total_ultimo > total_penultimo else "Mal"
22
+
23
+ return json.dumps({"result": resultado})
24
+
25
+ except Exception as e:
26
+ return json.dumps({"error": str(e)})
27
+
28
+ iface = gr.Interface(
29
+ fn=crecimiento_ventas_dict,
30
+ inputs=gr.Textbox(lines=10, placeholder='{"ventas":[{"marca":"A","talla":40,"precio":100,"numpares":2}, ...]}'),
31
+ outputs="text"
32
+ )
33
+
34
+ iface.launch(server_name="0.0.0.0", server_port=7860)