cryman38 commited on
Commit
7da3a84
ยท
verified ยท
1 Parent(s): 8443387

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -28
app.py CHANGED
@@ -24,8 +24,8 @@ class StockCodeNotFoundError(Exception):
24
  class CurrencyConversionError(Exception):
25
  pass
26
 
27
- def parse_input(text, cash_amount, cash_ratio):
28
- logger.info(f"Parsing input: {text} with cash amount: {cash_amount} and cash ratio: {cash_ratio}")
29
  try:
30
  lines = text.strip().split(',')
31
  stock_inputs = []
@@ -42,6 +42,11 @@ def parse_input(text, cash_amount, cash_ratio):
42
  else:
43
  raise ValueError("Invalid input format.")
44
 
 
 
 
 
 
45
  krw_cash = {'amount': cash_amount, 'target_weight': cash_ratio / 100.0}
46
 
47
  cash_ratio = krw_cash['target_weight']
@@ -168,10 +173,9 @@ def get_portfolio_rebalancing_info(portfolio, target_weights, krw_cash):
168
  logger.info("Portfolio rebalancing information calculated")
169
  return result_message
170
 
171
- def rebalance_portfolio(input_text, cash_amount, cash_ratio):
172
  try:
173
- cash_amount = int(cash_amount)
174
- stock_inputs, krw_cash = parse_input(input_text, cash_amount, cash_ratio)
175
  portfolio, target_weights, krw_cash = build_portfolio(stock_inputs, krw_cash)
176
  result_message = get_portfolio_rebalancing_info(portfolio, target_weights, krw_cash)
177
  return result_message
@@ -187,29 +191,7 @@ interface = gr.Interface(
187
  gr.Slider(minimum=0, maximum=100, step=1, value=33, label="ํ˜„๊ธˆ ๋น„์œจ (%)")
188
  ],
189
  outputs="text",
190
- title=f"Re-Balancing Analysis | Your Portfolio Holdings as of {current_date}",
191
- # description=(
192
- # "๊ธฐ๋ณธ ํ†ตํ™”: KRW\n\n"
193
- # "๋ฆฌ๋ฐธ๋Ÿฐ์‹ฑ์€ 1์ฃผ ๋‹จ์œ„๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค.\n\n"
194
- # "ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅํ•˜์„ธ์š”:\n"
195
- # " - ์ฃผ์‹์˜ ๊ฒฝ์šฐ: ํ†ตํ™” ํ‹ฐ์ปค ๋ณด์œ ์ˆ˜๋Ÿ‰ ๋ชฉํ‘œ๋น„์ค‘\n"
196
- # " - ์ถ”๊ฐ€ ํˆฌ์ž๊ธˆ์„ ์ž…๋ ฅํ•˜์„ธ์š”\n"
197
- # " - ์Šฌ๋ผ์ด๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชฉํ‘œํ˜„๊ธˆ๋น„์œจ (%)์„ ์ž…๋ ฅํ•˜์„ธ์š”\n\n"
198
- # " - ์—ฌ๋Ÿฌ ์ข…๋ชฉ์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n"
199
- # " - ์ˆ˜๋Ÿ‰๊ณผ ๋น„์ค‘์— ๋Œ€ํ•ด ์‚ฌ์น™ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n\n"
200
- # "์˜ˆ์‹œ:\n"
201
- # " - usd schd 545 8, usd qqq 22 2\n\n"
202
- # " - ๋ณด์œ ์ฃผ์‹: SCHD 545์ฃผ, QQQ 22์ฃผ\n"
203
- # " - ๋ชฉํ‘œ๋น„์ค‘: SCHD:QQQ = 8:2\n\n"
204
- # "์‚ฌ์น™ ์—ฐ์‚ฐ ์˜ˆ์‹œ:\n"
205
- # " - krw 458730 530+50 8/2, krw 368590 79*2 2+1\n\n"
206
- # "์ถœ๋ ฅ ํ˜•์‹:\n\n"
207
- # " - ์ข…๋ชฉ์ฝ”๋“œ @ํ˜„์žฌ๊ฐ€\n\n"
208
- # " - ํ‰๊ฐ€๊ธˆ์•ก [ํ˜„์žฌ๋น„์ค‘%]\n\n"
209
- # " - [๋ชฉํ‘œ๋น„์ค‘%] ์‹ ๊ทœ์ˆ˜๋Ÿ‰\n\n"
210
- # " - ์ „์ฒด ํ‰๊ฐ€๊ธˆ์•ก"
211
- # )
212
-
213
  )
214
 
215
  if __name__ == "__main__":
 
24
  class CurrencyConversionError(Exception):
25
  pass
26
 
27
+ def parse_input(text, cash_amount_expr, cash_ratio):
28
+ logger.info(f"Parsing input: {text} with cash amount expression: {cash_amount_expr} and cash ratio: {cash_ratio}")
29
  try:
30
  lines = text.strip().split(',')
31
  stock_inputs = []
 
42
  else:
43
  raise ValueError("Invalid input format.")
44
 
45
+ if cash_amount_expr.strip():
46
+ cash_amount = math.floor(eval(cash_amount_expr.replace(' ', '')))
47
+ else:
48
+ cash_amount = 0
49
+
50
  krw_cash = {'amount': cash_amount, 'target_weight': cash_ratio / 100.0}
51
 
52
  cash_ratio = krw_cash['target_weight']
 
173
  logger.info("Portfolio rebalancing information calculated")
174
  return result_message
175
 
176
+ def rebalance_portfolio(input_text, cash_amount_expr, cash_ratio):
177
  try:
178
+ stock_inputs, krw_cash = parse_input(input_text, cash_amount_expr, cash_ratio)
 
179
  portfolio, target_weights, krw_cash = build_portfolio(stock_inputs, krw_cash)
180
  result_message = get_portfolio_rebalancing_info(portfolio, target_weights, krw_cash)
181
  return result_message
 
191
  gr.Slider(minimum=0, maximum=100, step=1, value=33, label="ํ˜„๊ธˆ ๋น„์œจ (%)")
192
  ],
193
  outputs="text",
194
+ title=f"Re-Balancing Analysis | Your Portfolio Holdings as of {current_date}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  )
196
 
197
  if __name__ == "__main__":