Spaces:
Runtime error
Runtime error
Download app.py from hwang1/chart: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/spaces/hwang1/chart/resolve/main/app.py
- Command line
-
hf download hf://spaces/hwang1/chart/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hwang1/chart/resolve/main/app.py
2.89 kB
| import yfinance as yf | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| from datetime import datetime, timedelta | |
| import gradio as gr | |
| from flask import Flask, request, jsonify, send_file | |
| from flask_cors import CORS | |
| app = Flask(__name__) | |
| CORS(app, supports_credentials=True, resources={r"/*": {"origins": "*"}}) | |
| # ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ | |
| def fetch_data(): | |
| end_date = datetime.now() | |
| start_date = end_date - timedelta(days=5 * 365) | |
| # ๋ฏธ๊ตญ์ 10๋ ๋ฌผ ๊ตญ์ฑ ์์ต๋ฅ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ | |
| us_interest_rate_data = yf.download('^TNX', start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d')) | |
| us_interest_rate_daily = us_interest_rate_data['Close'] | |
| # ๋นํธ์ฝ์ธ ๊ฐ๊ฒฉ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ | |
| btc_data = yf.download('BTC-USD', start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d')) | |
| btc_daily = btc_data['Close'] | |
| return us_interest_rate_daily, btc_daily | |
| # ๋ฐ์ดํฐ ์ ๊ทํ | |
| def normalize_data(data): | |
| return (data - data.min()) / (data.max() - data.min()) | |
| # ๋ฐ์ดํฐ ๋ถ์ ๋ฐ ๊ทธ๋ํ ์์ฑ | |
| def plot_data(interest_rate_data, btc_prices): | |
| # ๋ฐ์ดํฐ ์ ๊ทํ | |
| interest_rate_normalized = normalize_data(interest_rate_data) | |
| btc_normalized = normalize_data(btc_prices) | |
| # ๋ ๋ฐ์ดํฐ์ ์ฐจ์ด ๊ณ์ฐ ๋ฐ ์ ๊ทํ | |
| diff_normalized = normalize_data(btc_normalized - interest_rate_normalized) | |
| # ๊ทธ๋ํ ์์ฑ | |
| plt.figure(figsize=(14, 7)) | |
| plt.plot(diff_normalized, color='green', label='Normalized Difference (Bitcoin Price - US 10-Year Treasury Yield)') | |
| plt.axhline(y=0.5, color='black', linestyle='--', label='Normalized Value: 0.5') | |
| # ๋ฐฐ๊ฒฝ์ ์ค์ | |
| plt.axhspan(0.0, 0.2, color='blue', alpha=0.3) | |
| plt.axhspan(0.2, 0.4, color='lightblue', alpha=0.3) | |
| plt.axhspan(0.4, 0.5, color='skyblue', alpha=0.3) | |
| plt.axhspan(0.5, 0.6, color='yellow', alpha=0.3) | |
| plt.axhspan(0.6, 0.8, color='orange', alpha=0.3) | |
| plt.axhspan(0.8, 1.0, color='red', alpha=0.3) | |
| # ํ์ฌ ์ ๊ทํ๋ ๊ฐ ํ์ | |
| current_value = diff_normalized.iloc[-1] | |
| plt.text(len(diff_normalized) - 1, current_value, f'Normalized Value: {current_value:.3f}', verticalalignment='bottom', horizontalalignment='right') | |
| plt.title('Difference between Bitcoin Price and US 10-Year Treasury Yield') | |
| plt.xlabel('Date') | |
| plt.ylabel('Normalized Value') | |
| plt.legend() | |
| plt.grid(True) | |
| # ํ์ฌ Figure ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ ํ์ํ์ง ์์ | |
| fig = plt.gcf() # Get the current figure before plt.show() | |
| plt.close() # Close the plot to free up memory | |
| return fig | |
| def run_app(): | |
| interest_rate_data, btc_prices = fetch_data() | |
| return plot_data(interest_rate_data, btc_prices) | |
| iface = gr.Interface(fn=run_app, inputs=[], outputs="plot", title="Financial Data Comparison", live=True) | |
| if __name__ == "__main__": | |
| iface.launch(share=True) | |