Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
import yfinance as yf
|
6 |
+
from PIL import Image
|
7 |
+
import ast
|
8 |
+
from datetime import datetime, timedelta
|
9 |
+
|
10 |
+
# Cargar el modelo
|
11 |
+
modelo = tf.keras.models.load_model('binary_classification_model.keras')
|
12 |
+
|
13 |
+
# Definir constantes
|
14 |
+
img_height, img_width = 224, 224
|
15 |
+
I_min, I_max = -3.5, 3.5
|
16 |
+
|
17 |
+
# Definir listas de acciones
|
18 |
+
mineria = ['BHP', 'RIO', 'VALE', 'NEM', 'FCX', 'GOLD', 'SCCO', 'TECK', 'AA', 'AEM']
|
19 |
+
entretenimiento = ['DIS', 'NFLX', 'CMCSA', 'EA', 'TTWO', 'SONY', 'LYV', 'CNK', 'IMAX', 'AMC']
|
20 |
+
tecnologia = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'META', 'INTC', 'NVDA', 'CSCO', 'ORCL', 'IBM']
|
21 |
+
alimentacion = ['KO', 'PEP', 'MDLZ', 'TSN', 'GIS', 'K', 'CAG', 'HSY', 'SYY', 'HRL']
|
22 |
+
otros = ['JNJ', 'PG', 'WMT', 'XOM', 'JPM', 'V', 'PFE', 'HD', 'BA', 'MCD']
|
23 |
+
acciones = mineria + entretenimiento + tecnologia + alimentacion + otros
|
24 |
+
|
25 |
+
# Funci贸n para calcular fechas
|
26 |
+
def calcular_fechas(fecha_fin_str):
|
27 |
+
fecha_fin = datetime.strptime(fecha_fin_str, '%Y-%m-%d')
|
28 |
+
if fecha_fin.weekday() == 0: # Lunes es 0
|
29 |
+
fecha_inicio = fecha_fin - timedelta(days=3)
|
30 |
+
else:
|
31 |
+
fecha_inicio = fecha_fin - timedelta(days=1) # Resta un d铆a en otros casos
|
32 |
+
fecha_fin_mas_uno = fecha_fin + timedelta(days=1)
|
33 |
+
return fecha_inicio.strftime('%Y-%m-%d'), fecha_fin_mas_uno.strftime('%Y-%m-%d')
|
34 |
+
|
35 |
+
# Funci贸n para escalar la matriz a RGB
|
36 |
+
def escalar_matriz_rgb(matriz, I_min, I_max):
|
37 |
+
matriz_normalizada = (matriz - I_min) / (I_max - I_min)
|
38 |
+
rojo = (1 - matriz_normalizada.clip(max=0)) * 255
|
39 |
+
verde = matriz_normalizada.clip(min=0) * 255
|
40 |
+
azul = (1 - abs(matriz_normalizada)) * 255
|
41 |
+
imagen_rgb = np.stack([rojo, verde, azul], axis=-1).astype('uint8')
|
42 |
+
return imagen_rgb
|
43 |
+
|
44 |
+
# Funci贸n para preparar la imagen
|
45 |
+
def preparar_imagen(imagen):
|
46 |
+
imagen = imagen.resize((img_height, img_width))
|
47 |
+
imagen_array = np.array(imagen) / 255.0
|
48 |
+
imagen_array = np.expand_dims(imagen_array, axis=0)
|
49 |
+
return imagen_array
|
50 |
+
|
51 |
+
# Funci贸n para combinar las puntuaciones en una matriz de intensidades
|
52 |
+
def combinar_puntuaciones(z_vector):
|
53 |
+
z_columna = z_vector.reshape(-1, 1)
|
54 |
+
z_fila = z_vector.reshape(1, -1)
|
55 |
+
matriz_intensidad = (z_columna + z_fila) / 2
|
56 |
+
return matriz_intensidad
|
57 |
+
|
58 |
+
# Funci贸n principal para procesar y predecir
|
59 |
+
def procesar_y_predecir(fecha_fin_str):
|
60 |
+
# Calcular fechas de inicio y fin
|
61 |
+
fecha_inicio, fecha_fin_mas_uno = calcular_fechas(fecha_fin_str)
|
62 |
+
|
63 |
+
# Descargar datos y calcular los retornos
|
64 |
+
datos_acciones = yf.download(acciones, start=fecha_inicio, end=fecha_fin_mas_uno)['Adj Close']
|
65 |
+
valores = datos_acciones.pct_change().dropna().values[0]
|
66 |
+
|
67 |
+
# Valores fijos de desviaci贸n est谩ndar y media
|
68 |
+
desviacion_retornos = pd.Series({
|
69 |
+
'AA': 0.033874, 'AAPL': 0.018052, 'AEM': 0.024548, 'AMC': 0.088641, 'AMZN': 0.020669,
|
70 |
+
'BA': 0.025481, 'BHP': 0.021584, 'CAG': 0.016168, 'CMCSA': 0.015756, 'CNK': 0.034773,
|
71 |
+
'CSCO': 0.015871, 'DIS': 0.017589, 'EA': 0.018129, 'FCX': 0.034673, 'GIS': 0.013253,
|
72 |
+
'GOLD': 0.024993, 'GOOGL': 0.017890, 'HD': 0.015374, 'HRL': 0.014172, 'HSY': 0.013699,
|
73 |
+
'IBM': 0.014975, 'IMAX': 0.029040, 'INTC': 0.022295, 'JNJ': 0.011410, 'JPM': 0.017179,
|
74 |
+
'K': 0.013803, 'KO': 0.011238, 'LYV': 0.023748, 'MCD': 0.012875, 'MDLZ': 0.013368,
|
75 |
+
'META': 0.023727, 'MSFT': 0.017148, 'NEM': 0.022646, 'NFLX': 0.027685, 'NVDA': 0.030759,
|
76 |
+
'ORCL': 0.017283, 'PEP': 0.011764, 'PFE': 0.014615, 'PG': 0.011667, 'RIO': 0.020612,
|
77 |
+
'SCCO': 0.022382, 'SONY': 0.018923, 'SYY': 0.018709, 'TECK': 0.035790, 'TSN': 0.017928,
|
78 |
+
'TTWO': 0.021477, 'V': 0.015461, 'VALE': 0.030546, 'WMT': 0.013344, 'XOM': 0.017617
|
79 |
+
}) # Coloca aqu铆 las desviaciones
|
80 |
+
media_retornos = pd.Series({
|
81 |
+
'AA': 0.000632, 'AAPL': 0.001083, 'AEM': 0.000843, 'AMC': 0.001134, 'AMZN': 0.001229,
|
82 |
+
'BA': 0.000451, 'BHP': 0.000543, 'CAG': 0.000287, 'CMCSA': 0.000366, 'CNK': 0.000569,
|
83 |
+
'CSCO': 0.000543, 'DIS': 0.000201, 'EA': 0.000629, 'FCX': 0.000933, 'GIS': 0.000337,
|
84 |
+
'GOLD': 0.000649, 'GOOGL': 0.000899, 'HD': 0.000776, 'HRL': 0.000279, 'HSY': 0.000421,
|
85 |
+
'IBM': 0.000449, 'IMAX': 0.000262, 'INTC': 0.000170, 'JNJ': 0.000359, 'JPM': 0.000777,
|
86 |
+
'K': 0.000341, 'KO': 0.000398, 'LYV': 0.000882, 'MCD': 0.000679, 'MDLZ': 0.000443,
|
87 |
+
'META': 0.001095, 'MSFT': 0.001097, 'NEM': 0.000790, 'NFLX': 0.001496, 'NVDA': 0.002764,
|
88 |
+
'ORCL': 0.000768, 'PEP': 0.000434, 'PFE': 0.000253, 'PG': 0.000441, 'RIO': 0.000662,
|
89 |
+
'SCCO': 0.000949, 'SONY': 0.000867, 'SYY': 0.000538, 'TECK': 0.001210, 'TSN': 0.000411,
|
90 |
+
'TTWO': 0.000924, 'V': 0.000747, 'VALE': 0.000822, 'WMT': 0.000593, 'XOM': 0.000433
|
91 |
+
}) # Coloca aqu铆 las medias
|
92 |
+
|
93 |
+
# Calcular puntuaciones z y generar la imagen
|
94 |
+
puntuaciones_z = (valores - media_retornos) / desviacion_retornos
|
95 |
+
puntuaciones_z_limited = puntuaciones_z.clip(lower=-3.5, upper=3.5)
|
96 |
+
matriz_intensidad = combinar_puntuaciones(puntuaciones_z_limited.values)
|
97 |
+
matriz_rgb = escalar_matriz_rgb(matriz_intensidad, I_min, I_max)
|
98 |
+
imagen = Image.fromarray(matriz_rgb, mode='RGB')
|
99 |
+
imagen_resized = imagen.resize((224, 224))
|
100 |
+
imagen_procesada = preparar_imagen(imagen_resized)
|
101 |
+
|
102 |
+
# Predecir con el modelo
|
103 |
+
prediccion = modelo.predict(imagen_procesada)
|
104 |
+
predicted_class = int((prediccion > 0.5).astype(int).flatten()[0])
|
105 |
+
mensaje = "Ma帽ana va a ganar la NYSE" if predicted_class == 1 else "Ma帽ana va a perder la NYSE"
|
106 |
+
|
107 |
+
return imagen_resized, mensaje
|
108 |
+
|
109 |
+
# Interfaz de Gradio
|
110 |
+
iface = gr.Interface(
|
111 |
+
fn=procesar_y_predecir,
|
112 |
+
inputs=gr.Textbox(label="Fecha", placeholder="Ingresa la fecha en formato aaaa-mm-dd"),
|
113 |
+
outputs=[gr.Image(type="pil"), gr.Textbox(label="Predicci贸n")]
|
114 |
+
)
|
115 |
+
|
116 |
+
iface.launch()
|