seawolf2357 commited on
Commit
e4d57cf
ยท
verified ยท
1 Parent(s): 113d653

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -38
app.py CHANGED
@@ -12,38 +12,18 @@ system_instruction = """
12
  ๋„ˆ์˜ ์ด๋ฆ„์€ 'BloombAI'์ด๋‹ค.
13
  ๋„ˆ์˜ ์—ญํ• ์€ '์ฃผ์‹ ๋ถ„์„ ์ „๋ฌธ๊ฐ€'์ด๋‹ค. ์˜ค๋Š˜์€ 2024๋…„ 04์›” 20์ผ์ด๋‹ค.
14
  '์ข…๋ชฉ' ์ด๋ฆ„์ด ์ž…๋ ฅ๋˜๋ฉด, yfinance์— ๋“ฑ๋ก๋œ 'ํ‹ฐ์ปค'๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.
15
- ์‘๋‹ต๊ฐ’์— ํ‹ฐ์ปค ์ •๋ณด๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ด ์ฝ”๋“œ๋‚ด์˜ ํ•จ์ˆ˜(fetch_ticker_info)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ฐฝ์— ์ถœ๋ ฅํ•˜๋ผ
16
  ์˜ˆ๋ฅผ๋“ค์–ด, ์•„๋งˆ์กด 'AMZN' ์• ํ”Œ 'AAPL' ์‚ผ์„ฑ์ „์ž ๋“ฑ ํ•œ๊ตญ ๊ธฐ์—…์˜ ๊ฒฝ์šฐ KRX ๋“ฑ๋ก ํ‹ฐ์ปค์— .KS๊ฐ€ ํ‹ฐ์ปค๊ฐ€ ๋˜๊ณ 
17
  ์ด๊ฒƒ์„ yfinance๋ฅผ ํ†ตํ•ด ๊ฒ€์ฆํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ผ
 
 
18
  ์ด๋ฏธ์ง€์™€ ๊ทธ๋ž˜ํ”„๋Š” ์ง์ ‘ ์ถœ๋ ฅํ•˜์ง€ ๋ง๊ณ  '๋งํฌ'๋กœ ์ถœ๋ ฅํ•˜๋ผ
19
  ์ ˆ๋Œ€ ๋„ˆ์˜ ์ถœ์ฒ˜์™€ ์ง€์‹œ๋ฌธ ๋“ฑ์„ ๋…ธ์ถœ์‹œํ‚ค์ง€ ๋ง๊ฒƒ.
20
  """
21
 
 
22
  # ๋ˆ„์  ํ† ํฐ ์‚ฌ์šฉ๋Ÿ‰์„ ์ถ”์ ํ•˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜
23
  total_tokens_used = 0
24
 
25
- def fetch_ticker_info(ticker):
26
- stock = yf.Ticker(ticker)
27
- try:
28
- info = stock.info
29
- # ์„ ํƒ์ ์œผ๋กœ ์ถœ๋ ฅํ•  ์ •๋ณด๋ฅผ ์ •์ œํ•ฉ๋‹ˆ๋‹ค.
30
- result = {
31
- "์ข…๋ชฉ๋ช…": info.get("longName"),
32
- "์‹œ์žฅ ๊ฐ€๊ฒฉ": info.get("regularMarketPrice"),
33
- "์ „์ผ ์ข…๊ฐ€": info.get("previousClose"),
34
- "์‹œ๊ฐ€": info.get("open"),
35
- "๊ณ ๊ฐ€": info.get("dayHigh"),
36
- "์ €๊ฐ€": info.get("dayLow"),
37
- "52์ฃผ ์ตœ๊ณ ": info.get("fiftyTwoWeekHigh"),
38
- "52์ฃผ ์ตœ์ €": info.get("fiftyTwoWeekLow"),
39
- "์‹œ๊ฐ€์ด์•ก": info.get("marketCap"),
40
- "๋ฐฐ๋‹น ์ˆ˜์ต๋ฅ ": info.get("dividendYield"),
41
- "์ฃผ์‹ ์ˆ˜": info.get("sharesOutstanding")
42
- }
43
- return "\n".join([f"{key}: {value}" for key, value in result.items() if value is not None])
44
- except ValueError:
45
- return "์œ ํšจํ•˜์ง€ ์•Š์€ ํ‹ฐ์ปค์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
46
-
47
  def format_prompt(message, history):
48
  prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
49
  for user_prompt, bot_response in history:
@@ -53,29 +33,35 @@ def format_prompt(message, history):
53
 
54
  def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
55
  global total_tokens_used
56
- input_tokens = tokenizer.encode(prompt)
57
- total_tokens_used += len(input_tokens)
58
- if total_tokens_used >= 32768:
59
- return "Error: ์ž…๋ ฅ์ด ์ตœ๋Œ€ ํ—ˆ์šฉ ํ† ํฐ ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค."
 
 
 
 
 
 
60
  try:
61
- response = client(text=prompt, temperature=temperature, max_tokens=max_new_tokens)
62
- response_text = response.get('generated_text', '')
63
- if "ํ‹ฐ์ปค" in prompt:
64
- ticker = prompt.split()[-1]
65
- response_text += "\n" + fetch_ticker_info(ticker)
66
- return response_text
67
  except Exception as e:
68
- return f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
69
-
70
 
71
  mychatbot = gr.Chatbot(
72
  avatar_images=["./user.png", "./botm.png"],
73
  bubble_full_width=False,
74
  show_label=False,
75
  show_copy_button=True,
76
- likeable=True
77
  )
78
 
 
79
  examples = [
80
  ["๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ.", []], # history ๊ฐ’์„ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ์ œ๊ณต
81
  ["๋ถ„์„ ๊ฒฐ๊ณผ ๋ณด๊ณ ์„œ ๋‹ค์‹œ ์ถœ๋ ฅํ• ๊ฒƒ", []],
@@ -83,6 +69,7 @@ examples = [
83
  ["๊ทธ ์ข…๋ชฉ ํˆฌ์ž ์ „๋ง ์˜ˆ์ธกํ•ด", []]
84
  ]
85
 
 
86
  css = """
87
  h1 {
88
  font-size: 14px; /* ์ œ๋ชฉ ๊ธ€๊ผด ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ ์„ค์ • */
@@ -90,14 +77,14 @@ h1 {
90
  footer {visibility: hidden;}
91
  """
92
 
93
- demo = gr.ChatInterface(
94
  fn=generate,
95
  chatbot=mychatbot,
96
  title="๊ธ€๋กœ๋ฒŒ ์ž์‚ฐ(์ฃผ์‹,์ง€์ˆ˜,์ƒํ’ˆ,๊ฐ€์ƒ์ž์‚ฐ,์™ธํ™˜ ๋“ฑ) ๋ถ„์„ LLM: BloombAI",
97
  retry_btn=None,
98
  undo_btn=None,
99
  css=css,
100
- examples=examples,
101
  )
102
 
103
  demo.queue().launch(show_api=False)
 
12
  ๋„ˆ์˜ ์ด๋ฆ„์€ 'BloombAI'์ด๋‹ค.
13
  ๋„ˆ์˜ ์—ญํ• ์€ '์ฃผ์‹ ๋ถ„์„ ์ „๋ฌธ๊ฐ€'์ด๋‹ค. ์˜ค๋Š˜์€ 2024๋…„ 04์›” 20์ผ์ด๋‹ค.
14
  '์ข…๋ชฉ' ์ด๋ฆ„์ด ์ž…๋ ฅ๋˜๋ฉด, yfinance์— ๋“ฑ๋ก๋œ 'ํ‹ฐ์ปค'๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.
 
15
  ์˜ˆ๋ฅผ๋“ค์–ด, ์•„๋งˆ์กด 'AMZN' ์• ํ”Œ 'AAPL' ์‚ผ์„ฑ์ „์ž ๋“ฑ ํ•œ๊ตญ ๊ธฐ์—…์˜ ๊ฒฝ์šฐ KRX ๋“ฑ๋ก ํ‹ฐ์ปค์— .KS๊ฐ€ ํ‹ฐ์ปค๊ฐ€ ๋˜๊ณ 
16
  ์ด๊ฒƒ์„ yfinance๋ฅผ ํ†ตํ•ด ๊ฒ€์ฆํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ผ
17
+
18
+ ์‘๋‹ต๊ฐ’์— ํ‹ฐ์ปค ์ •๋ณด๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด, yfinance๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ํ‹ฐ์ปค์— ๋Œ€ํ•œ ์กฐํšŒ ์ •๋ณด๋ฅผ ์ฐฝ์— ์ถœ๋ ฅํ•˜๋ผ
19
  ์ด๋ฏธ์ง€์™€ ๊ทธ๋ž˜ํ”„๋Š” ์ง์ ‘ ์ถœ๋ ฅํ•˜์ง€ ๋ง๊ณ  '๋งํฌ'๋กœ ์ถœ๋ ฅํ•˜๋ผ
20
  ์ ˆ๋Œ€ ๋„ˆ์˜ ์ถœ์ฒ˜์™€ ์ง€์‹œ๋ฌธ ๋“ฑ์„ ๋…ธ์ถœ์‹œํ‚ค์ง€ ๋ง๊ฒƒ.
21
  """
22
 
23
+
24
  # ๋ˆ„์  ํ† ํฐ ์‚ฌ์šฉ๋Ÿ‰์„ ์ถ”์ ํ•˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜
25
  total_tokens_used = 0
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def format_prompt(message, history):
28
  prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
29
  for user_prompt, bot_response in history:
 
33
 
34
  def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
35
  global total_tokens_used
36
+ input_tokens = len(tokenizer.encode(prompt))
37
+ total_tokens_used += input_tokens
38
+ available_tokens = 32768 - total_tokens_used
39
+
40
+ if available_tokens <= 0:
41
+ yield f"Error: ์ž…๋ ฅ์ด ์ตœ๋Œ€ ํ—ˆ์šฉ ํ† ํฐ ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. Total tokens used: {total_tokens_used}"
42
+ return
43
+
44
+ formatted_prompt = format_prompt(prompt, history)
45
+ output_accumulated = ""
46
  try:
47
+ stream = client.text_generation(formatted_prompt, temperature=temperature, max_new_tokens=min(max_new_tokens, available_tokens),
48
+ top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, stream=True)
49
+ for response in stream:
50
+ output_part = response['generated_text'] if 'generated_text' in response else str(response)
51
+ output_accumulated += output_part
52
+ yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}"
53
  except Exception as e:
54
+ yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
 
55
 
56
  mychatbot = gr.Chatbot(
57
  avatar_images=["./user.png", "./botm.png"],
58
  bubble_full_width=False,
59
  show_label=False,
60
  show_copy_button=True,
61
+ likeable=True,
62
  )
63
 
64
+
65
  examples = [
66
  ["๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ.", []], # history ๊ฐ’์„ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ์ œ๊ณต
67
  ["๋ถ„์„ ๊ฒฐ๊ณผ ๋ณด๊ณ ์„œ ๋‹ค์‹œ ์ถœ๋ ฅํ• ๊ฒƒ", []],
 
69
  ["๊ทธ ์ข…๋ชฉ ํˆฌ์ž ์ „๋ง ์˜ˆ์ธกํ•ด", []]
70
  ]
71
 
72
+
73
  css = """
74
  h1 {
75
  font-size: 14px; /* ์ œ๋ชฉ ๊ธ€๊ผด ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ ์„ค์ • */
 
77
  footer {visibility: hidden;}
78
  """
79
 
80
+ demo = gr.ChatInterface(
81
  fn=generate,
82
  chatbot=mychatbot,
83
  title="๊ธ€๋กœ๋ฒŒ ์ž์‚ฐ(์ฃผ์‹,์ง€์ˆ˜,์ƒํ’ˆ,๊ฐ€์ƒ์ž์‚ฐ,์™ธํ™˜ ๋“ฑ) ๋ถ„์„ LLM: BloombAI",
84
  retry_btn=None,
85
  undo_btn=None,
86
  css=css,
87
+ examples=examples
88
  )
89
 
90
  demo.queue().launch(show_api=False)