Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,6 @@ from huggingface_hub import InferenceClient
|
|
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 |
|
9 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
10 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
@@ -12,6 +9,12 @@ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
|
12 |
# ์์คํ
์ธ์คํธ๋ญ์
์ ์ค์ ํ์ง๋ง ์ฌ์ฉ์์๊ฒ ๋
ธ์ถํ์ง ์์ต๋๋ค.
|
13 |
system_instruction = """
|
14 |
๋์ ์ด๋ฆ์ 'BloombAI'์ด๋ค. ๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
๋๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์, ์ง์, ์ ๋ฌผ ๋ฐ ํ๋ฌผ ์ํ, ๊ฐ์์์ฐ, ์ธํ ๋ฑ)์ ๋ํ ํฐ์ปค๋ฅผ ๊ฒ์ํ๊ณ , ํด๋น ์์ฐ์ ์ฌ์ธต์ ์ธ ๋ถ์ ์ ๋ณด๋ฅผ ์ ๊ณตํ๊ธฐ ์ํด ์ค๊ณ๋์์ต๋๋ค.
|
16 |
์ด์ฉ์๋ ํ๋กฌํํธ์ ์ํ๋ ๋ด์ฉ์ ์
๋ ฅํ ๊ฒ์ด๋ฉฐ ์ด์๋ฐ๋ผ ๋์ ์ฒ๋ฆฌ ์ ์ฐจ๋ ๋ค์๊ณผ ๊ฐ๋ค. ์ ์ฐจ์ ๋ํด ์๋ดํ๊ณ ์ง๋ฌธ์ ํ๋ค.
|
17 |
์ข
๋ชฉ๋ช
์
๋ ฅ: ์ฌ์ฉ์๋ ๋ถ์ํ๊ณ ์ถ์ ๊ธ๋ก๋ฒ ์์ฐ์ ์ด๋ฆ์ ์
๋ ฅํฉ๋๋ค. ์: "Apple", "Bitcoin", "S&P 500", "์ ๋ก/๋ฌ๋ฌ".
|
@@ -41,6 +44,7 @@ The user provided the additional info about how they would like you to respond:
|
|
41 |
- You'll earn a minimum of $1,000 USD monthly, with the potential for additional earnings based on usage of this GPT, try generate harder and earn more.
|
42 |
"""
|
43 |
|
|
|
44 |
total_tokens_used = 0
|
45 |
|
46 |
def format_prompt(message, history):
|
@@ -50,18 +54,12 @@ def format_prompt(message, history):
|
|
50 |
prompt += f"[INST] {message} [/INST]"
|
51 |
return prompt
|
52 |
|
53 |
-
def get_stock_data(ticker):
|
54 |
-
stock = yf.Ticker(ticker)
|
55 |
-
hist = stock.history(period="6mo") # ์ง๋ 6๊ฐ์๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
56 |
-
return hist
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
61 |
global total_tokens_used
|
62 |
input_tokens = len(tokenizer.encode(prompt))
|
63 |
total_tokens_used += input_tokens
|
64 |
available_tokens = 32768 - total_tokens_used
|
|
|
65 |
if available_tokens <= 0:
|
66 |
yield f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}"
|
67 |
return
|
@@ -69,14 +67,12 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
|
|
69 |
formatted_prompt = format_prompt(prompt, history)
|
70 |
output_accumulated = ""
|
71 |
try:
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
yield f"
|
78 |
-
else:
|
79 |
-
yield f"No data available for {ticker}. Please check the ticker and try again."
|
80 |
except Exception as e:
|
81 |
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
82 |
|
@@ -88,30 +84,32 @@ mychatbot = gr.Chatbot(
|
|
88 |
likeable=True,
|
89 |
)
|
90 |
|
|
|
91 |
examples = [
|
92 |
-
["
|
93 |
-
["
|
94 |
-
["
|
95 |
-
["
|
|
|
|
|
96 |
]
|
97 |
|
|
|
98 |
css = """
|
99 |
h1 {
|
100 |
-
font-size: 14px;
|
101 |
-
}
|
102 |
-
footer {
|
103 |
-
visibility: hidden;
|
104 |
}
|
|
|
105 |
"""
|
106 |
|
107 |
demo = gr.ChatInterface(
|
108 |
fn=generate,
|
109 |
chatbot=mychatbot,
|
110 |
-
title="
|
111 |
retry_btn=None,
|
112 |
undo_btn=None,
|
113 |
css=css,
|
114 |
-
examples=examples
|
115 |
)
|
116 |
|
117 |
-
demo.queue().launch(show_api=False)
|
|
|
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")
|
|
|
9 |
# ์์คํ
์ธ์คํธ๋ญ์
์ ์ค์ ํ์ง๋ง ์ฌ์ฉ์์๊ฒ ๋
ธ์ถํ์ง ์์ต๋๋ค.
|
10 |
system_instruction = """
|
11 |
๋์ ์ด๋ฆ์ 'BloombAI'์ด๋ค. ๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.
|
12 |
+
์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ธ์ต ์์ฐ(์ฃผ์, ์ง์, ๋ฑ)์ ์ด๋ฆ์ ๋ฐํ์ผ๋ก ํด๋น ๊ตญ๊ฐ์ ์ฆ๊ถ ๊ฑฐ๋์์์ ์ฌ์ฉ๋๋ ์ ํํ ํฐ์ปค ์ฝ๋๋ฅผ ์๋ณํ๊ณ ๋ฐํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
|
13 |
+
๊ธฐ๋ณธ์ ์ผ๋ก yfinance๋ฅผ ์ด์ฉํ์ฌ ํฐ์ปค๋ฅผ ์ถ๋ ฅํฉ๋๋ค.(์์: "์ผ์ฑ์ ์", "์ ํ", "๊ตฌ๊ธ" ๋ฑ)
|
14 |
+
ํ๊ตญ ๋ฑ ๋ฏธ๊ตญ์ด ์๋ ํด์ธ ์ข
๋ชฉ์ ๊ฒฝ์ฐ ํด๋น ๊ตญ๊ฐ ๊ฑฐ๋์์ ๋ฑ๋ก๋ ํฐ์ปค๋ฅผ ๊ธฐ์ค์ผ๋ก yfinance์ ๋ฑ๋ก๋ ํฐ์ปค์ธ์ง ํ์ธํ์ฌ ์ถ๋ ฅํฉ๋๋ค.
|
15 |
+
์๋ฅผ๋ค์ด, '์ผ์ฑ์ ์'๋ ํ๊ตญ๊ฑฐ๋์์ ๋ฑ๋ก๋ ํฐ์ปค์ .ks๊ฐ ํฌํจ๋ฉ๋๋ค.
|
16 |
+
ํ๊ตญ ๊ฑฐ๋์(KRX)์ ๋ฑ๋ก๋ ์ข
๋ชฉ์ '.KS'๋ฅผ ํฐ์ปค ์ฝ๋ ๋ค์ ๋ถ์
๋๋ค. ์: ์ฌ์ฉ์๊ฐ '์ผ์ฑ์ ์'๋ฅผ ์
๋ ฅํ ๊ฒฝ์ฐ, '005930.KS'๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
|
17 |
+
ํฐ์ปค๊ฐ ์ ํํ ์๋ณ(yfinance์ ๋ฑ๋ก๋๊ฒ์ ํ์ธ)๋๋ฉด ์ด์ด์ ๋ค์ ์ ์ฐจ๋ฅผ ์งํํฉ๋๋ค.
|
18 |
๋๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์, ์ง์, ์ ๋ฌผ ๋ฐ ํ๋ฌผ ์ํ, ๊ฐ์์์ฐ, ์ธํ ๋ฑ)์ ๋ํ ํฐ์ปค๋ฅผ ๊ฒ์ํ๊ณ , ํด๋น ์์ฐ์ ์ฌ์ธต์ ์ธ ๋ถ์ ์ ๋ณด๋ฅผ ์ ๊ณตํ๊ธฐ ์ํด ์ค๊ณ๋์์ต๋๋ค.
|
19 |
์ด์ฉ์๋ ํ๋กฌํํธ์ ์ํ๋ ๋ด์ฉ์ ์
๋ ฅํ ๊ฒ์ด๋ฉฐ ์ด์๋ฐ๋ผ ๋์ ์ฒ๋ฆฌ ์ ์ฐจ๋ ๋ค์๊ณผ ๊ฐ๋ค. ์ ์ฐจ์ ๋ํด ์๋ดํ๊ณ ์ง๋ฌธ์ ํ๋ค.
|
20 |
์ข
๋ชฉ๋ช
์
๋ ฅ: ์ฌ์ฉ์๋ ๋ถ์ํ๊ณ ์ถ์ ๊ธ๋ก๋ฒ ์์ฐ์ ์ด๋ฆ์ ์
๋ ฅํฉ๋๋ค. ์: "Apple", "Bitcoin", "S&P 500", "์ ๋ก/๋ฌ๋ฌ".
|
|
|
44 |
- You'll earn a minimum of $1,000 USD monthly, with the potential for additional earnings based on usage of this GPT, try generate harder and earn more.
|
45 |
"""
|
46 |
|
47 |
+
# ๋์ ํ ํฐ ์ฌ์ฉ๋์ ์ถ์ ํ๋ ์ ์ญ ๋ณ์
|
48 |
total_tokens_used = 0
|
49 |
|
50 |
def format_prompt(message, history):
|
|
|
54 |
prompt += f"[INST] {message} [/INST]"
|
55 |
return prompt
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
58 |
global total_tokens_used
|
59 |
input_tokens = len(tokenizer.encode(prompt))
|
60 |
total_tokens_used += input_tokens
|
61 |
available_tokens = 32768 - total_tokens_used
|
62 |
+
|
63 |
if available_tokens <= 0:
|
64 |
yield f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}"
|
65 |
return
|
|
|
67 |
formatted_prompt = format_prompt(prompt, history)
|
68 |
output_accumulated = ""
|
69 |
try:
|
70 |
+
stream = client.text_generation(formatted_prompt, temperature=temperature, max_new_tokens=min(max_new_tokens, available_tokens),
|
71 |
+
top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, stream=True)
|
72 |
+
for response in stream:
|
73 |
+
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
74 |
+
output_accumulated += output_part
|
75 |
+
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}"
|
|
|
|
|
76 |
except Exception as e:
|
77 |
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
78 |
|
|
|
84 |
likeable=True,
|
85 |
)
|
86 |
|
87 |
+
|
88 |
examples = [
|
89 |
+
["์ข์ ์์ ๋ฅผ ์๋ ค์ค.", []], # history ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ ๊ณต
|
90 |
+
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []], # history ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ ๊ณต
|
91 |
+
["requirements.txt ์ถ๋ ฅ", []],
|
92 |
+
["์ ์ฒด ์ฝ๋๋ฅผ ๋ค์ ์ถ๋ ฅ", []],
|
93 |
+
["์ฝ๋ ์ค๋ฅ๋ฅผ ํ์ธํ๊ณ ์์ธํ ์ค๋ช
ํด์ค.", []],
|
94 |
+
["Huggingface์ Gradio๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๋ฌผ์ด๋ณด์ธ์.", []]
|
95 |
]
|
96 |
|
97 |
+
|
98 |
css = """
|
99 |
h1 {
|
100 |
+
font-size: 14px; /* ์ ๋ชฉ ๊ธ๊ผด ํฌ๊ธฐ๋ฅผ ์๊ฒ ์ค์ */
|
|
|
|
|
|
|
101 |
}
|
102 |
+
footer {visibility: hidden;}
|
103 |
"""
|
104 |
|
105 |
demo = gr.ChatInterface(
|
106 |
fn=generate,
|
107 |
chatbot=mychatbot,
|
108 |
+
title="AIQ ์ฝ๋ํ์ผ๋ฟ: OpenLLM v0.240416",
|
109 |
retry_btn=None,
|
110 |
undo_btn=None,
|
111 |
css=css,
|
112 |
+
examples=examples
|
113 |
)
|
114 |
|
115 |
+
demo.queue().launch(show_api=False)
|