Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit Β·
93ec99f
1
Parent(s): 645a5e6
sync from f0b3ebb
Browse files- app.py +123 -34
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import gc
|
|
|
|
| 6 |
import os
|
| 7 |
import re
|
| 8 |
from collections import OrderedDict
|
|
@@ -194,35 +195,131 @@ def _resolveCode(query: str) -> tuple[str | None, str | None]:
|
|
| 194 |
return None, f"κ²μ μ€ν¨: {e}"
|
| 195 |
|
| 196 |
|
| 197 |
-
def
|
| 198 |
-
"""μ«μ 컬λΌ
|
| 199 |
-
config = {}
|
| 200 |
if df is None or df.empty:
|
| 201 |
-
return
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
| 206 |
)
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
|
| 214 |
-
def _showDataFrame(df: pd.DataFrame, key: str = ""):
|
| 215 |
-
"""DataFrame
|
| 216 |
if df is None or df.empty:
|
| 217 |
st.markdown('<p class="dl-empty">λ°μ΄ν° μμ</p>', unsafe_allow_html=True)
|
| 218 |
return
|
|
|
|
| 219 |
st.dataframe(
|
| 220 |
-
df,
|
| 221 |
-
column_config=_buildColumnConfig(df),
|
| 222 |
use_container_width=True,
|
| 223 |
hide_index=True,
|
| 224 |
key=key or None,
|
| 225 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
|
| 228 |
# ββ ν리λ‘λ ββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -328,7 +425,7 @@ if code:
|
|
| 328 |
except Exception:
|
| 329 |
pass
|
| 330 |
|
| 331 |
-
_showDataFrame(finDf, key="finance")
|
| 332 |
|
| 333 |
# ββ Sections ββ
|
| 334 |
topics = []
|
|
@@ -361,17 +458,14 @@ if code:
|
|
| 361 |
secText = f"μ‘°ν μ€ν¨: {e}"
|
| 362 |
|
| 363 |
if secDf is not None:
|
| 364 |
-
_showDataFrame(secDf, key="section")
|
| 365 |
elif secText:
|
| 366 |
st.markdown(secText)
|
| 367 |
|
| 368 |
# ββ AI Chat ββ
|
| 369 |
-
with st.expander("π€ AI λΆμ (
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
if not hasAi:
|
| 373 |
-
st.info("AI λΆμμ μ¬μ©νλ €λ©΄ HuggingFace Spaces Settings β Variables and secretsμμ `OPENAI_API_KEY`λ₯Ό μ€μ νμΈμ.")
|
| 374 |
-
else:
|
| 375 |
if "messages" not in st.session_state:
|
| 376 |
st.session_state.messages = []
|
| 377 |
|
|
@@ -385,15 +479,10 @@ if code:
|
|
| 385 |
st.markdown(prompt)
|
| 386 |
|
| 387 |
with st.chat_message("assistant"):
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
st.session_state.messages.append({"role": "assistant", "content": answer or "μλ΅ μμ"})
|
| 393 |
-
except Exception as e:
|
| 394 |
-
errMsg = f"λΆμ μ€ν¨: {e}"
|
| 395 |
-
st.markdown(errMsg)
|
| 396 |
-
st.session_state.messages.append({"role": "assistant", "content": errMsg})
|
| 397 |
|
| 398 |
else:
|
| 399 |
# λ―Έμ
λ ₯ μν
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import gc
|
| 6 |
+
import io
|
| 7 |
import os
|
| 8 |
import re
|
| 9 |
from collections import OrderedDict
|
|
|
|
| 195 |
return None, f"κ²μ μ€ν¨: {e}"
|
| 196 |
|
| 197 |
|
| 198 |
+
def _formatDf(df: pd.DataFrame) -> pd.DataFrame:
|
| 199 |
+
"""μ«μ 컬λΌμ μ²λ¨μ μ½€λ§ λ¬Έμμ΄λ‘ λ³ν (μμμ μ κ±°)."""
|
|
|
|
| 200 |
if df is None or df.empty:
|
| 201 |
+
return df
|
| 202 |
+
result = df.copy()
|
| 203 |
+
for col in result.columns:
|
| 204 |
+
if pd.api.types.is_numeric_dtype(result[col]):
|
| 205 |
+
result[col] = result[col].apply(
|
| 206 |
+
lambda x: f"{int(x):,}" if pd.notna(x) and x == x else ""
|
| 207 |
)
|
| 208 |
+
return result
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def _toExcel(df: pd.DataFrame) -> bytes:
|
| 212 |
+
"""DataFrame β Excel bytes."""
|
| 213 |
+
buf = io.BytesIO()
|
| 214 |
+
df.to_excel(buf, index=False, engine="openpyxl")
|
| 215 |
+
return buf.getvalue()
|
| 216 |
|
| 217 |
|
| 218 |
+
def _showDataFrame(df: pd.DataFrame, key: str = "", downloadName: str = ""):
|
| 219 |
+
"""DataFrame νμ + μμ
λ€μ΄λ‘λ λ²νΌ."""
|
| 220 |
if df is None or df.empty:
|
| 221 |
st.markdown('<p class="dl-empty">λ°μ΄ν° μμ</p>', unsafe_allow_html=True)
|
| 222 |
return
|
| 223 |
+
# ν¬λ§·ν
λ λ²μ μΌλ‘ νμ
|
| 224 |
st.dataframe(
|
| 225 |
+
_formatDf(df),
|
|
|
|
| 226 |
use_container_width=True,
|
| 227 |
hide_index=True,
|
| 228 |
key=key or None,
|
| 229 |
)
|
| 230 |
+
# μμ
λ€μ΄λ‘λ (μλ³Έ μ«μ μ μ§)
|
| 231 |
+
if downloadName:
|
| 232 |
+
st.download_button(
|
| 233 |
+
label="π₯ Excel λ€μ΄λ‘λ",
|
| 234 |
+
data=_toExcel(df),
|
| 235 |
+
file_name=f"{downloadName}.xlsx",
|
| 236 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
| 237 |
+
key=f"dl_{key}" if key else None,
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
# ββ AI ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 242 |
+
|
| 243 |
+
_HAS_OPENAI = bool(os.environ.get("OPENAI_API_KEY"))
|
| 244 |
+
|
| 245 |
+
if _HAS_OPENAI:
|
| 246 |
+
dartlab.llm.configure(provider="openai", api_key=os.environ["OPENAI_API_KEY"])
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def _askAi(stockCode: str, question: str) -> str:
|
| 250 |
+
"""AI μ§λ¬Έ μ²λ¦¬. OpenAI μ°μ , μμΌλ©΄ HF λ¬΄λ£ Inference API."""
|
| 251 |
+
# OpenAIκ° μ€μ λμ΄ μμΌλ©΄ dartlab.ask μ¬μ©
|
| 252 |
+
if _HAS_OPENAI:
|
| 253 |
+
try:
|
| 254 |
+
q = f"{stockCode} {question}" if stockCode else question
|
| 255 |
+
answer = dartlab.ask(q, stream=False, raw=False)
|
| 256 |
+
return answer or "μλ΅ μμ"
|
| 257 |
+
except Exception as e:
|
| 258 |
+
return f"λΆμ μ€ν¨: {e}"
|
| 259 |
+
|
| 260 |
+
# HF Inference API (ν ν° μμ΄λ λ¬΄λ£ νΈμΆ κ°λ₯)
|
| 261 |
+
try:
|
| 262 |
+
from huggingface_hub import InferenceClient
|
| 263 |
+
token = os.environ.get("HF_TOKEN") # μμΌλ©΄ rate limit λμμ§
|
| 264 |
+
client = InferenceClient(
|
| 265 |
+
model="meta-llama/Llama-3.1-8B-Instruct",
|
| 266 |
+
token=token if token else None,
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
context = _buildAiContext(stockCode)
|
| 270 |
+
systemMsg = (
|
| 271 |
+
"λΉμ μ νκ΅ κΈ°μ
μ¬λ¬΄ λΆμ μ λ¬Έκ°μ
λλ€. "
|
| 272 |
+
"μλ μ¬λ¬΄ λ°μ΄ν°λ₯Ό λ°νμΌλ‘ μ¬μ©μμ μ§λ¬Έμ νκ΅μ΄λ‘ λ΅λ³νμΈμ. "
|
| 273 |
+
"μ«μλ μ²λ¨μ μ½€λ§λ₯Ό μ¬μ©νκ³ , κ·Όκ±°λ₯Ό λͺ
νν μ μνμΈμ.\n\n"
|
| 274 |
+
f"{context}"
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
response = client.chat_completion(
|
| 278 |
+
messages=[
|
| 279 |
+
{"role": "system", "content": systemMsg},
|
| 280 |
+
{"role": "user", "content": question},
|
| 281 |
+
],
|
| 282 |
+
max_tokens=1024,
|
| 283 |
+
)
|
| 284 |
+
return response.choices[0].message.content or "μλ΅ μμ"
|
| 285 |
+
except Exception as e:
|
| 286 |
+
return f"AI λΆμ μ€ν¨: {e}"
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
def _buildAiContext(stockCode: str) -> str:
|
| 290 |
+
"""AIμ μ λ¬ν κΈ°μ
μ¬λ¬΄ 컨ν
μ€νΈ ꡬμ±."""
|
| 291 |
+
try:
|
| 292 |
+
c = _getCompany(stockCode)
|
| 293 |
+
except Exception:
|
| 294 |
+
return f"μ’
λͺ©μ½λ: {stockCode}"
|
| 295 |
+
|
| 296 |
+
parts = [f"κΈ°μ
: {c.corpName} ({c.stockCode}), μμ₯: {c.market}"]
|
| 297 |
+
|
| 298 |
+
# IS μμ½
|
| 299 |
+
try:
|
| 300 |
+
isDf = _toPandas(c.IS)
|
| 301 |
+
if isDf is not None and not isDf.empty:
|
| 302 |
+
parts.append(f"\n[μμ΅κ³μ°μ μμ½]\n{isDf.head(15).to_string()}")
|
| 303 |
+
except Exception:
|
| 304 |
+
pass
|
| 305 |
+
|
| 306 |
+
# BS μμ½
|
| 307 |
+
try:
|
| 308 |
+
bsDf = _toPandas(c.BS)
|
| 309 |
+
if bsDf is not None and not bsDf.empty:
|
| 310 |
+
parts.append(f"\n[μ¬λ¬΄μνν μμ½]\n{bsDf.head(15).to_string()}")
|
| 311 |
+
except Exception:
|
| 312 |
+
pass
|
| 313 |
+
|
| 314 |
+
# ratios μμ½
|
| 315 |
+
try:
|
| 316 |
+
ratDf = _toPandas(c.ratios)
|
| 317 |
+
if ratDf is not None and not ratDf.empty:
|
| 318 |
+
parts.append(f"\n[μ¬λ¬΄λΉμ¨]\n{ratDf.head(15).to_string()}")
|
| 319 |
+
except Exception:
|
| 320 |
+
pass
|
| 321 |
+
|
| 322 |
+
return "\n".join(parts)
|
| 323 |
|
| 324 |
|
| 325 |
# ββ ν리λ‘λ ββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 425 |
except Exception:
|
| 426 |
pass
|
| 427 |
|
| 428 |
+
_showDataFrame(finDf, key="finance", downloadName=f"{code}_{sheetTab}")
|
| 429 |
|
| 430 |
# ββ Sections ββ
|
| 431 |
topics = []
|
|
|
|
| 458 |
secText = f"μ‘°ν μ€ν¨: {e}"
|
| 459 |
|
| 460 |
if secDf is not None:
|
| 461 |
+
_showDataFrame(secDf, key="section", downloadName=f"{code}_{selectedTopic}")
|
| 462 |
elif secText:
|
| 463 |
st.markdown(secText)
|
| 464 |
|
| 465 |
# ββ AI Chat ββ
|
| 466 |
+
with st.expander("π€ AI λΆμ (무λ£)", expanded=False):
|
| 467 |
+
st.caption("Llama 3.1 8B κΈ°λ° λ¬΄λ£ AI λΆμ Β· 볡μ‘ν μ§λ¬Έμ OpenAI API μ€μ μ λ μ νν©λλ€")
|
| 468 |
+
if True:
|
|
|
|
|
|
|
|
|
|
| 469 |
if "messages" not in st.session_state:
|
| 470 |
st.session_state.messages = []
|
| 471 |
|
|
|
|
| 479 |
st.markdown(prompt)
|
| 480 |
|
| 481 |
with st.chat_message("assistant"):
|
| 482 |
+
with st.spinner("λΆμ μ€..."):
|
| 483 |
+
answer = _askAi(code, prompt)
|
| 484 |
+
st.markdown(answer)
|
| 485 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
|
| 487 |
else:
|
| 488 |
# λ―Έμ
λ ₯ μν
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
dartlab>=0.7.8
|
| 2 |
streamlit>=1.45,<2
|
|
|
|
|
|
|
|
|
| 1 |
dartlab>=0.7.8
|
| 2 |
streamlit>=1.45,<2
|
| 3 |
+
openpyxl>=3.1
|
| 4 |
+
huggingface_hub>=0.25
|