Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,11 @@ from huggingface_hub import InferenceClient
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import GPT2Tokenizer
|
| 4 |
import yfinance as yf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 7 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
|
@@ -49,9 +54,37 @@ def format_prompt(message, history):
|
|
| 49 |
|
| 50 |
def get_stock_data(ticker):
|
| 51 |
stock = yf.Ticker(ticker)
|
| 52 |
-
hist = stock.history(period="
|
| 53 |
return hist
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
| 56 |
global total_tokens_used
|
| 57 |
input_tokens = len(tokenizer.encode(prompt))
|
|
@@ -64,82 +97,17 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
|
|
| 64 |
formatted_prompt = format_prompt(prompt, history)
|
| 65 |
output_accumulated = ""
|
| 66 |
try:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
yield
|
| 73 |
-
|
| 74 |
-
# ์ถ๊ฐ์ ์ธ ๋ถ์ ์์ฒญ์ด ์๋ค๋ฉด, yfinance๋ก ๋ฐ์ดํฐ ์์ง ๋ฐ ๋ถ์
|
| 75 |
-
stock_data = get_stock_data(stock_info['ticker']) # ํฐ์ปค๋ฅผ ์ด์ฉํด ์ฃผ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
| 76 |
-
stream = client.text_generation(
|
| 77 |
-
formatted_prompt,
|
| 78 |
-
temperature=temperature,
|
| 79 |
-
max_new_tokens=min(max_new_tokens, available_tokens),
|
| 80 |
-
top_p=top_p,
|
| 81 |
-
repetition_penalty=repetition_penalty,
|
| 82 |
-
do_sample=True,
|
| 83 |
-
seed=42,
|
| 84 |
-
stream=True
|
| 85 |
-
)
|
| 86 |
-
for response in stream:
|
| 87 |
-
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
| 88 |
-
output_accumulated += output_part
|
| 89 |
-
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}\nStock Data: {stock_data}"
|
| 90 |
else:
|
| 91 |
-
|
| 92 |
-
ticker = prompt.upper()
|
| 93 |
-
if ticker in ['AAPL', 'MSFT', 'AMZN', 'GOOGL', 'TSLA']:
|
| 94 |
-
stock_info = get_stock_info_by_ticker(ticker)
|
| 95 |
-
response_msg = f"{stock_info['name']}์(๋) {stock_info['description']} ์ฃผ๋ ฅ์ผ๋ก ์์ฐํ๋ ๊ธฐ์
์
๋๋ค. {stock_info['name']}์ ํฐ์ปค๋ {stock_info['ticker']}์
๋๋ค. ์ํ์๋ ์ข
๋ชฉ์ด ๋ง๋๊ฐ์?"
|
| 96 |
-
output_accumulated += response_msg
|
| 97 |
-
yield output_accumulated
|
| 98 |
-
|
| 99 |
-
# ์ถ๊ฐ์ ์ธ ๋ถ์ ์์ฒญ์ด ์๋ค๋ฉด, yfinance๋ก ๋ฐ์ดํฐ ์์ง ๋ฐ ๋ถ์
|
| 100 |
-
stock_data = get_stock_data(stock_info['ticker']) # ํฐ์ปค๋ฅผ ์ด์ฉํด ์ฃผ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
| 101 |
-
stream = client.text_generation(
|
| 102 |
-
formatted_prompt,
|
| 103 |
-
temperature=temperature,
|
| 104 |
-
max_new_tokens=min(max_new_tokens, available_tokens),
|
| 105 |
-
top_p=top_p,
|
| 106 |
-
repetition_penalty=repetition_penalty,
|
| 107 |
-
do_sample=True,
|
| 108 |
-
seed=42,
|
| 109 |
-
stream=True
|
| 110 |
-
)
|
| 111 |
-
for response in stream:
|
| 112 |
-
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
| 113 |
-
output_accumulated += output_part
|
| 114 |
-
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}\nStock Data: {stock_data}"
|
| 115 |
-
else:
|
| 116 |
-
yield f"์
๋ ฅํ์ '{prompt}'์(๋) ์ง์๋๋ ์ข
๋ชฉ๋ช
๋๋ ํฐ์ปค๊ฐ ์๋๋๋ค. ํ์ฌ ์ง์๋๋ ์ข
๋ชฉ์ ์ ํ(AAPL), ๋ง์ดํฌ๋ก์ํํธ(MSFT), ์๋ง์กด(AMZN), ์ํ๋ฒณ(GOOGL), ํ
์ฌ๋ผ(TSLA) ๋ฑ์
๋๋ค. ์ ํํ ์ข
๋ชฉ๋ช
๋๋ ํฐ์ปค๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 117 |
except Exception as e:
|
| 118 |
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
| 119 |
|
| 120 |
-
# ํฐ์ปค๋ฅผ ํ ๋๋ก ์ข
๋ชฉ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ํจ์
|
| 121 |
-
def get_stock_info_by_ticker(ticker):
|
| 122 |
-
stock_info = {
|
| 123 |
-
"AAPL": {'ticker': 'AAPL', 'name': '์ ํ', 'description': '์์ดํฐ์'},
|
| 124 |
-
"MSFT": {'ticker': 'MSFT', 'name': '๋ง์ดํฌ๋ก์ํํธ', 'description': '์๋์ฐ ์ด์์ฒด์ ์ ์คํผ์ค ์ํํธ์จ์ด๋ฅผ'},
|
| 125 |
-
"AMZN": {'ticker': 'AMZN', 'name': '์๋ง์กด', 'description': '์ ์์๊ฑฐ๋ ๋ฐ ํด๋ผ์ฐ๋ ์๋น์ค๋ฅผ'},
|
| 126 |
-
"GOOGL": {'ticker': 'GOOGL', 'name': '์ํ๋ฒณ', 'description': '๊ฒ์ ์์ง ๋ฐ ์จ๋ผ์ธ ๊ด๊ณ ๋ฅผ'},
|
| 127 |
-
"TSLA": {'ticker': 'TSLA', 'name': 'ํ
์ฌ๋ผ', 'description': '์ ๊ธฐ์๋์ฐจ์ ์๋์ง ์ ์ฅ์ฅ์น๋ฅผ'},
|
| 128 |
-
}
|
| 129 |
-
return stock_info.get(ticker, {'ticker': None, 'name': None, 'description': ''})
|
| 130 |
-
|
| 131 |
-
# ์ข
๋ชฉ๋ช
์ ํ ๋๋ก ํฐ์ปค์ ๊ธฐ์
์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ํจ์
|
| 132 |
-
def get_stock_info(name):
|
| 133 |
-
stock_info = {
|
| 134 |
-
"apple": {'ticker': 'AAPL', 'name': '์ ํ', 'description': '์์ดํฐ์'},
|
| 135 |
-
"microsoft": {'ticker': 'MSFT', 'name': '๋ง์ดํฌ๋ก์ํํธ', 'description': '์๋์ฐ ์ด์์ฒด์ ์ ์คํผ์ค ์ํํธ์จ์ด๋ฅผ'},
|
| 136 |
-
"amazon": {'ticker': 'AMZN', 'name': '์๋ง์กด', 'description': '์ ์์๊ฑฐ๋ ๋ฐ ํด๋ผ์ฐ๋ ์๋น์ค๋ฅผ'},
|
| 137 |
-
"google": {'ticker': 'GOOGL', 'name': '์ํ๋ฒณ (๊ตฌ๊ธ)', 'description': '๊ฒ์ ์์ง ๋ฐ ์จ๋ผ์ธ ๊ด๊ณ ๋ฅผ'},
|
| 138 |
-
"tesla": {'ticker': 'TSLA', 'name': 'ํ
์ฌ๋ผ', 'description': '์ ๊ธฐ์๋์ฐจ์ ์๋์ง ์ ์ฅ์ฅ์น๋ฅผ'},
|
| 139 |
-
# ์ถ๊ฐ์ ์ธ ์ข
๋ชฉ์ ๋ํ ์ ๋ณด๋ฅผ ์ด๊ณณ์ ๊ตฌํํ ์ ์์ต๋๋ค.
|
| 140 |
-
}
|
| 141 |
-
return stock_info.get(name.lower(), {'ticker': None, 'name': name, 'description': ''})
|
| 142 |
-
|
| 143 |
mychatbot = gr.Chatbot(
|
| 144 |
avatar_images=["./user.png", "./botm.png"],
|
| 145 |
bubble_full_width=False,
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import GPT2Tokenizer
|
| 4 |
import yfinance as yf
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import talib
|
| 9 |
+
import tech_indicators as ti
|
| 10 |
|
| 11 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 12 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
|
|
|
| 54 |
|
| 55 |
def get_stock_data(ticker):
|
| 56 |
stock = yf.Ticker(ticker)
|
| 57 |
+
hist = stock.history(period="6mo") # ์ง๋ 6๊ฐ์๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
| 58 |
return hist
|
| 59 |
|
| 60 |
+
def apply_technical_indicators(df):
|
| 61 |
+
df['SMA'] = talib.SMA(df['Close'], timeperiod=20)
|
| 62 |
+
df['EMA'] = talib.EMA(df['Close'], timeperiod=20)
|
| 63 |
+
df['RSI'] = talib.RSI(df['Close'], timeperiod=14)
|
| 64 |
+
macd, macdsignal, macdhist = talib.MACD(df['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
|
| 65 |
+
df['MACD'] = macd
|
| 66 |
+
df['MACD_signal'] = macdsignal
|
| 67 |
+
return df
|
| 68 |
+
|
| 69 |
+
def plot_technical_indicators(df):
|
| 70 |
+
plt.figure(figsize=(14, 7))
|
| 71 |
+
plt.subplot(2, 1, 1)
|
| 72 |
+
plt.plot(df['Close'], label='Close Price')
|
| 73 |
+
plt.plot(df['SMA'], label='SMA 20')
|
| 74 |
+
plt.plot(df['EMA'], label='EMA 20')
|
| 75 |
+
plt.title('Price Chart with SMA and EMA')
|
| 76 |
+
plt.legend()
|
| 77 |
+
|
| 78 |
+
plt.subplot(2, 1, 2)
|
| 79 |
+
plt.plot(df['RSI'], label='RSI')
|
| 80 |
+
plt.title('RSI Chart')
|
| 81 |
+
plt.legend()
|
| 82 |
+
|
| 83 |
+
plt.tight_layout()
|
| 84 |
+
plt.savefig('/mnt/data/Technical_Indicators.png')
|
| 85 |
+
plt.close()
|
| 86 |
+
return '/mnt/data/Technical_Indicators.png'
|
| 87 |
+
|
| 88 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
| 89 |
global total_tokens_used
|
| 90 |
input_tokens = len(tokenizer.encode(prompt))
|
|
|
|
| 97 |
formatted_prompt = format_prompt(prompt, history)
|
| 98 |
output_accumulated = ""
|
| 99 |
try:
|
| 100 |
+
ticker = prompt.upper()
|
| 101 |
+
stock_data = get_stock_data(ticker)
|
| 102 |
+
if not stock_data.empty:
|
| 103 |
+
enhanced_data = apply_technical_indicators(stock_data)
|
| 104 |
+
image_path = plot_technical_indicators(enhanced_data)
|
| 105 |
+
yield f"Technical analysis for {ticker} completed. See the chart here: {image_path}\n\n---\nTotal tokens used: {total_tokens_used}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
else:
|
| 107 |
+
yield f"No data available for {ticker}. Please check the ticker and try again."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
mychatbot = gr.Chatbot(
|
| 112 |
avatar_images=["./user.png", "./botm.png"],
|
| 113 |
bubble_full_width=False,
|