File size: 2,387 Bytes
e3003c7
 
 
 
 
 
 
 
 
c3585da
 
e3003c7
 
 
 
 
 
d460df7
e3003c7
 
 
 
 
 
 
 
d460df7
e3003c7
 
 
 
c3585da
e3003c7
 
 
 
 
 
 
 
 
 
 
 
c3585da
e3003c7
 
 
 
 
d460df7
 
 
 
 
 
 
 
 
 
 
 
e3003c7
 
 
 
 
 
 
 
 
 
 
 
 
d460df7
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
import gradio as gr
import torch
from chronos import ChronosPipeline
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from sklearn.metrics import mean_absolute_error, mean_squared_error
import tempfile

def get_popular_tickers():
    return [
        "AAPL", "MSFT", "GOOGL", "AMZN", "META", "TSLA", "NVDA", "JPM",
        "JNJ", "V", "PG", "WMT", "BAC", "DIS", "NFLX", "INTC"
    ]

# Resto del c贸digo se mantiene igual hasta la secci贸n de la interfaz Gradio

with gr.Blocks() as demo:
    gr.Markdown("# Aplicaci贸n de Predicci贸n de Precios de Acciones")
    
    with gr.Row():
        with gr.Column(scale=1):
            ticker = gr.Dropdown(
                choices=get_popular_tickers(),
                value="AAPL",  # A帽adido valor por defecto
                label="Selecciona el S铆mbolo de la Acci贸n"
            )
            train_data_points = gr.Slider(
                minimum=50,
                maximum=5000,
                value=1000,
                step=1,
                label="N煤mero de Datos para Entrenamiento"
            )
            prediction_days = gr.Slider(
                minimum=1,
                maximum=60,
                value=5,
                step=1,
                label="N煤mero de D铆as a Predecir"
            )
            predict_btn = gr.Button("Predecir")
        
    with gr.Column():
        plot_output = gr.Plot(label="Gr谩fico de Predicci贸n")
        download_btn = gr.File(label="Descargar Predicciones")
    
    def update_train_data_points(ticker):
        try:
            stock = yf.Ticker(ticker)
            hist = stock.history(period="max")
            total_points = len(hist)
            return gr.Slider.update(
                maximum=total_points,
                value=min(1000, total_points),
                visible=True
            )
        except Exception as e:
            print(f"Error updating slider: {str(e)}")
            return gr.Slider.update(visible=True)  # Mantener slider visible en caso de error
    
    ticker.change(
        fn=update_train_data_points,
        inputs=[ticker],
        outputs=[train_data_points]
    )
    
    predict_btn.click(
        fn=predict_stock,
        inputs=[ticker, train_data_points, prediction_days],
        outputs=[plot_output, download_btn]
    )

demo.launch()