seawolf2357 commited on
Commit
f8f486f
ยท
verified ยท
1 Parent(s): 98f9624

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -41
app.py CHANGED
@@ -21,7 +21,6 @@ system_instruction = """
21
  # ๋ˆ„์  ํ† ํฐ ์‚ฌ์šฉ๋Ÿ‰์„ ์ถ”์ ํ•˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜
22
  total_tokens_used = 0
23
 
24
-
25
  def fetch_ticker_info(ticker):
26
  stock = yf.Ticker(ticker)
27
  try:
@@ -43,7 +42,7 @@ def fetch_ticker_info(ticker):
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:
@@ -56,10 +55,8 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
56
  input_tokens = len(tokenizer.encode(prompt))
57
  total_tokens_used += input_tokens
58
  available_tokens = 32768 - total_tokens_used
59
-
60
  if available_tokens <= 0:
61
- yield f"Error: ์ž…๋ ฅ์ด ์ตœ๋Œ€ ํ—ˆ์šฉ ํ† ํฐ ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. Total tokens used: {total_tokens_used}"
62
- return
63
 
64
  formatted_prompt = format_prompt(prompt, history)
65
  output_accumulated = ""
@@ -69,44 +66,53 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
69
  for response in stream:
70
  output_part = response['generated_text'] if 'generated_text' in response else str(response)
71
  output_accumulated += output_part
72
- yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}"
73
  except Exception as e:
74
- yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
75
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- def setup_interface():
78
- with gr.Blocks() as demo:
79
- gr.Markdown("### ๊ธ€๋กœ๋ฒŒ ์ž์‚ฐ(์ฃผ์‹,์ง€์ˆ˜,์ƒํ’ˆ,๊ฐ€์ƒ์ž์‚ฐ,์™ธํ™˜ ๋“ฑ) ๋ถ„์„ LLM: BloombAI")
80
-
81
- with gr.Row():
82
- ticker_input = gr.Textbox(label="ํ‹ฐ์ปค ์ž…๋ ฅ", placeholder="์˜ˆ: AAPL")
83
- submit_button = gr.Button("์กฐํšŒ")
84
-
85
- chatbot = gr.Chatbot(
86
- avatar_images=["./user.png", "./botm.png"],
87
- bubble_full_width=False,
88
- show_label=False,
89
- show_copy_button=True,
90
- likeable=True
91
- )
92
-
93
- # ์ฑ„ํŒ…๋ด‡ ์ดˆ๊ธฐ ๋ฉ”์‹œ์ง€ ์„ค์ •
94
- chatbot.messages = [("bot", "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค! ์–ด๋–ค ์ฃผ์‹ ์ •๋ณด๊ฐ€ ๊ถ๊ธˆํ•˜์‹ ๊ฐ€์š”?")]
95
-
96
- # ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด ํ‹ฐ์ปค ์ •๋ณด๋ฅผ ์ฑ„ํŒ… ์ฐฝ์— ์ง์ ‘ ์ถœ๋ ฅ
97
- def query_and_show(ticker):
98
- info = fetch_ticker_info(ticker)
99
- return [("bot", f"ํ‹ฐ์ปค '{ticker}'์˜ ์ •๋ณด ์กฐํšŒ ๊ฒฐ๊ณผ:\n\n{info}")]
100
-
101
- submit_button.click(
102
- fn=query_and_show,
103
- inputs=ticker_input,
104
- outputs=chatbot
105
- )
106
-
107
- gr.Markdown("### ์ฑ„ํŒ…")
108
 
109
- return demo
 
 
 
 
 
 
 
 
 
110
 
111
- app = setup_interface()
112
- app.launch()
 
21
  # ๋ˆ„์  ํ† ํฐ ์‚ฌ์šฉ๋Ÿ‰์„ ์ถ”์ ํ•˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜
22
  total_tokens_used = 0
23
 
 
24
  def fetch_ticker_info(ticker):
25
  stock = yf.Ticker(ticker)
26
  try:
 
42
  return "\n".join([f"{key}: {value}" for key, value in result.items() if value is not None])
43
  except ValueError:
44
  return "์œ ํšจํ•˜์ง€ ์•Š์€ ํ‹ฐ์ปค์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
45
+
46
  def format_prompt(message, history):
47
  prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
48
  for user_prompt, bot_response in history:
 
55
  input_tokens = len(tokenizer.encode(prompt))
56
  total_tokens_used += input_tokens
57
  available_tokens = 32768 - total_tokens_used
 
58
  if available_tokens <= 0:
59
+ return f"Error: ์ž…๋ ฅ์ด ์ตœ๋Œ€ ํ—ˆ์šฉ ํ† ํฐ ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. Total tokens used: {total_tokens_used}"
 
60
 
61
  formatted_prompt = format_prompt(prompt, history)
62
  output_accumulated = ""
 
66
  for response in stream:
67
  output_part = response['generated_text'] if 'generated_text' in response else str(response)
68
  output_accumulated += output_part
69
+ return output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}"
70
  except Exception as e:
71
+ return f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
72
 
73
+ def postprocess(history):
74
+ user_prompt = history[-1][0]
75
+ bot_response = history[-1][1]
76
+
77
+ # ํ‹ฐ์ปค ์ •๋ณด ์ถœ๋ ฅ
78
+ if "ํ‹ฐ์ปค" in bot_response:
79
+ ticker = bot_response.split("ํ‹ฐ์ปค")[1].strip()
80
+ ticker_info = fetch_ticker_info(ticker)
81
+ bot_response += f"\n\nํ‹ฐ์ปค ์ •๋ณด:\n{ticker_info}"
82
+
83
+ return [(user_prompt, bot_response)]
84
 
85
+ mychatbot = gr.Chatbot(
86
+ avatar_images=["./user.png", "./botm.png"],
87
+ bubble_full_width=False,
88
+ show_label=False,
89
+ show_copy_button=True,
90
+ likeable=True
91
+ )
92
+
93
+ examples = [
94
+ ["๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ.", []], # history ๊ฐ’์„ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ์ œ๊ณต
95
+ ["๋ถ„์„ ๊ฒฐ๊ณผ ๋ณด๊ณ ์„œ ๋‹ค์‹œ ์ถœ๋ ฅํ• ๊ฒƒ", []],
96
+ ["์ถ”์ฒœ ์ข…๋ชฉ ์•Œ๋ ค์ค˜", []],
97
+ ["๊ทธ ์ข…๋ชฉ ํˆฌ์ž ์ „๋ง ์˜ˆ์ธกํ•ด", []]
98
+ ]
99
+
100
+ css = """
101
+ h1 {
102
+ font-size: 14px; /* ์ œ๋ชฉ ๊ธ€๊ผด ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ ์„ค์ • */
103
+ }
104
+ footer {visibility: hidden;}
105
+ """
 
 
 
 
 
 
 
 
 
 
106
 
107
+ demo = gr.ChatInterface(
108
+ fn=generate,
109
+ chatbot=mychatbot,
110
+ title="๊ธ€๋กœ๋ฒŒ ์ž์‚ฐ(์ฃผ์‹,์ง€์ˆ˜,์ƒํ’ˆ,๊ฐ€์ƒ์ž์‚ฐ,์™ธํ™˜ ๋“ฑ) ๋ถ„์„ LLM: BloombAI",
111
+ retry_btn=None,
112
+ undo_btn=None,
113
+ css=css,
114
+ examples=examples,
115
+ postprocess=postprocess
116
+ )
117
 
118
+ demo.queue().launch(show_api=False)