File size: 3,246 Bytes
aeda09b
 
 
 
 
 
0dcf03b
aeda09b
0dcf03b
aeda09b
 
 
 
0dcf03b
 
 
aeda09b
0dcf03b
 
 
 
 
aeda09b
 
 
 
 
 
 
 
0dcf03b
aeda09b
 
 
 
 
0dcf03b
aeda09b
0dcf03b
aeda09b
 
 
0dcf03b
aeda09b
 
 
 
 
0dcf03b
aeda09b
 
 
 
0dcf03b
aeda09b
 
 
0dcf03b
aeda09b
0dcf03b
aeda09b
 
 
 
0dcf03b
aeda09b
 
 
 
0dcf03b
aeda09b
 
 
 
0dcf03b
aeda09b
 
 
0dcf03b
aeda09b
0dcf03b
aeda09b
 
 
 
 
0dcf03b
aeda09b
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import pandas as pd
import numpy as np
from datetime import datetime
import joblib
from tensorflow.keras.models import load_model
from huggingface_hub import hf_hub_download

# Configuraci贸n
HF_USER = "DomiDelgado2314"
REPO_NAME = "ProyectoFIN"
CSV_EJEMPLO_URL = f"https://huggingface.co/{HF_USER}/{REPO_NAME}/resolve/main/ejemplo_para_usuario.csv"

# Descargar archivos del repositorio Hugging Face
scaler_path = hf_hub_download(repo_id=f"{HF_USER}/{REPO_NAME}", filename="scaler.save")
model_path = hf_hub_download(repo_id=f"{HF_USER}/{REPO_NAME}", filename="modelo_bitcoin.keras")

# Cargar scaler y modelo
scaler = joblib.load(scaler_path)
model = load_model(model_path)

# Columnas esperadas por el modelo
model_columns = [
    'Volumen_Bitcoin', 'Tasa_Deposito_BCE', 'Indice_Materias_Pri_BCOM',
    'IPC_China', 'Indice_Dolar_DXY', 'IPC_Zona_Euro', 'Tasa_Desempleo_Zona_Euro',
    'Tasa_Fondos_FED', 'Precio_Oro_USD', 'Cierre_Nasdaq', 'Direcciones_Activas_Bitcoin',
    'Cierre_SP500', 'Cierre_Stoxx50', 'IPC_EEUU', 'Tasa_Desempleo_EEUU',
    'Indice_Volatilidad_VIX', 'Volumen_Comercio_Global'
]

# Validar formato de fecha
def validar_fecha(fecha_str):
    try:
        fecha = datetime.strptime(fecha_str, "%Y-%m-%d")
    except:
        return False, "Formato de fecha inv谩lido. Usa YYYY-MM-DD."
    if fecha <= datetime(2025, 5, 16):
        return False, "Fecha debe ser posterior al 2025-05-16."
    if fecha > datetime(2025, 12, 31):
        return False, "Fecha no puede ser posterior al 2025-12-31."
    return True, fecha

# Funci贸n de predicci贸n
def predecir_precio(csv_file, fecha_str):
    valido, res = validar_fecha(fecha_str)
    if not valido:
        return res
    fecha = res

    try:
        df_user = pd.read_csv(csv_file.name)
    except Exception as e:
        return f"Error al leer CSV: {e}"

    faltantes = [col for col in model_columns if col not in df_user.columns]
    if faltantes:
        return f"Faltan columnas obligatorias en CSV: {faltantes}"

    df_vars = df_user[model_columns].copy()

    try:
        X_scaled = scaler.transform(df_vars)
    except Exception as e:
        return f"Error al escalar variables: {e}"

    a帽o, mes, dia = fecha.year, fecha.month, fecha.day
    X_df = pd.DataFrame(X_scaled, columns=model_columns)
    X_df["A帽o"], X_df["Mes"], X_df["D铆a"] = a帽o, mes, dia
    X_input = X_df.to_numpy()

    try:
        pred_scaled = model.predict(X_input)
    except Exception as e:
        return f"Error en la predicci贸n: {e}"

    precio_pred = pred_scaled.flatten()[0]
    return f"Predicci贸n de Precio de Bitcoin para {fecha_str}: {precio_pred:,.2f} USD"

# Interfaz Gradio
with gr.Blocks() as demo:
    gr.Markdown("## 馃獧 Predicci贸n Precio Bitcoin")
    gr.Markdown(f"馃摜 [Descarga aqu铆 el CSV de ejemplo]({CSV_EJEMPLO_URL})")
    csv_input = gr.File(label="Sube tu archivo CSV con las variables")
    fecha_input = gr.Textbox(label="Fecha a predecir (YYYY-MM-DD)", value="2025-05-17")
    btn_predict = gr.Button("Predecir Precio")
    output_msg = gr.Textbox(label="Resultado", interactive=False)

    btn_predict.click(
        fn=predecir_precio,
        inputs=[csv_input, fecha_input],
        outputs=output_msg
    )

demo.launch()