Spaces:
Build error
Build error
Download back.py from vchandraiitk/iisc: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/spaces/vchandraiitk/iisc/resolve/main/back.py
- Command line
-
hf download hf://spaces/vchandraiitk/iisc/back.py
-
curl -L -o back.py https://huggingface.co/spaces/vchandraiitk/iisc/resolve/main/back.py
3.14 kB
| import pandas as pd | |
| import gradio as gr | |
| import shutil | |
| import os | |
| # Define relative paths for your files (for HuggingFace deployment) | |
| FORECAST = './artifacts/1_gnn_forecast_confidence_final.csv' | |
| # Load your forecast data | |
| try: | |
| df = pd.read_csv(FORECAST) | |
| print("✅ Forecast data loaded") | |
| except Exception as e: | |
| raise FileNotFoundError(f"❌ Failed to load forecast CSV: {e}") | |
| # Prepare ticker list | |
| tickers = sorted(df['Ticker'].unique()) | |
| # Function to fetch forecast | |
| def get_forecast(ticker): | |
| filtered = df[df['Ticker'] == ticker] | |
| output = {} | |
| confidences = {} | |
| for horizon in [30, 180, 365]: | |
| row = filtered[filtered['Forecast_Horizon_Days'] == horizon] | |
| if not row.empty: | |
| price = row.iloc[0]['Predicted_Close_Price'] | |
| confidence = row.iloc[0]['Confidence_%'] | |
| output[horizon] = f"Price: {price:.2f}" | |
| confidences[horizon] = confidence | |
| else: | |
| output[horizon] = "No data available" | |
| confidences[horizon] = 0 | |
| return output[30], confidences[30], output[180], confidences[180], output[365], confidences[365] | |
| # Function to fetch related stocks | |
| def get_related_stocks(ticker): | |
| filtered = df[df['Ticker'] == ticker] | |
| if not filtered.empty: | |
| return filtered.iloc[0]['Suggested_Related_Stocks'] | |
| else: | |
| return "No related stocks found." | |
| # Gradio UI with only Forecast Viewer Tab | |
| with gr.Blocks(css=""" | |
| #forecast30 {background-color: #ffcccc} | |
| #confidence30 .wrap.svelte-13f8w9x {background: linear-gradient(to right, #ff6666, #ffcccc);} | |
| #forecast180 {background-color: #ccffcc} | |
| #confidence180 .wrap.svelte-13f8w9x {background: linear-gradient(to right, #66ff66, #ccffcc);} | |
| #forecast365 {background-color: #ccccff} | |
| #confidence365 .wrap.svelte-13f8w9x {background: linear-gradient(to right, #6666ff, #ccccff);} | |
| """) as demo: | |
| with gr.Tab("Forecast Viewer"): | |
| gr.Markdown("## 📈 Stock Forecast Viewer") | |
| ticker_dropdown = gr.Dropdown(label="Select Ticker", choices=tickers) | |
| forecast_30 = gr.Textbox(label="30 Days Forecast", elem_id="forecast30") | |
| confidence_30 = gr.Slider(label="30 Days Confidence", minimum=0, maximum=100, elem_id="confidence30") | |
| forecast_180 = gr.Textbox(label="180 Days Forecast", elem_id="forecast180") | |
| confidence_180 = gr.Slider(label="180 Days Confidence", minimum=0, maximum=100, elem_id="confidence180") | |
| forecast_365 = gr.Textbox(label="365 Days Forecast", elem_id="forecast365") | |
| confidence_365 = gr.Slider(label="365 Days Confidence", minimum=0, maximum=100, elem_id="confidence365") | |
| related_button = gr.Button("Show Related Stocks") | |
| related_output = gr.Textbox(label="Suggested Related Stocks") | |
| ticker_dropdown.change( | |
| get_forecast, | |
| inputs=[ticker_dropdown], | |
| outputs=[forecast_30, confidence_30, forecast_180, confidence_180, forecast_365, confidence_365] | |
| ) | |
| related_button.click( | |
| get_related_stocks, | |
| inputs=[ticker_dropdown], | |
| outputs=[related_output] | |
| ) | |
| # Launch app | |
| demo.launch() | |