Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
import random
|
| 5 |
import time
|
| 6 |
import pandas as pd
|
| 7 |
from datetime import date
|
| 8 |
import plotly.express as px # 그래프 라이브러리 추가
|
|
|
|
| 9 |
|
| 10 |
# --- Streamlit 설정 ---
|
| 11 |
st.set_page_config(
|
|
@@ -150,30 +150,17 @@ div[data-testid="stToast"] {
|
|
| 150 |
|
| 151 |
|
| 152 |
# --- API 키 설정 (Hugging Face Secrets에서 관리 권장) ---
|
| 153 |
-
if "
|
| 154 |
st.error(
|
| 155 |
-
"
|
| 156 |
)
|
| 157 |
st.stop()
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
# --- Gemini 모델 설정 ---
|
| 162 |
-
generation_config = {
|
| 163 |
-
"temperature": 0.7,
|
| 164 |
-
"top_p": 0.95,
|
| 165 |
-
"top_k": 64,
|
| 166 |
-
"max_output_tokens": 25000,
|
| 167 |
-
"response_mime_type": "text/plain",
|
| 168 |
-
}
|
| 169 |
-
model = genai.GenerativeModel(
|
| 170 |
-
model_name="gemini-2.0-flash-exp", # 또는 "gemini-pro"
|
| 171 |
-
generation_config=generation_config,
|
| 172 |
-
)
|
| 173 |
|
| 174 |
# --- 세션 상태 초기화 (Streamlit 앱 상태 관리) ---
|
| 175 |
if "chat_session" not in st.session_state:
|
| 176 |
-
st.session_state["chat_session"] =
|
| 177 |
if "portfolio" not in st.session_state:
|
| 178 |
st.session_state["portfolio"] = {"cash": 10000000, "stocks": {}}
|
| 179 |
if "stocks" not in st.session_state:
|
|
@@ -441,8 +428,21 @@ def generate_news():
|
|
| 441 |
**생성된 뉴스 기사:**
|
| 442 |
"""
|
| 443 |
chat_session = st.session_state["chat_session"]
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
news_articles = []
|
| 448 |
if news_text:
|
|
@@ -470,9 +470,21 @@ def explain_daily_news_meanings(daily_news):
|
|
| 470 |
뉴스 의미 해설:
|
| 471 |
"""
|
| 472 |
chat_session = st.session_state["chat_session"]
|
|
|
|
| 473 |
try:
|
| 474 |
-
response =
|
| 475 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
|
| 477 |
explanation = ""
|
| 478 |
related_sectors = []
|
|
@@ -495,7 +507,7 @@ def explain_daily_news_meanings(daily_news):
|
|
| 495 |
meanings[str(i + 1)] = {"explanation": explanation, "sectors": related_sectors}
|
| 496 |
|
| 497 |
|
| 498 |
-
except google.api_core.exceptions.ResourceExhausted
|
| 499 |
st.error(
|
| 500 |
f"API 할당량 초과 오류가 발생했습니다. 잠시 후 다시 시도해주세요. 오류 메시지: {e}"
|
| 501 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
import pandas as pd
|
| 6 |
from datetime import date
|
| 7 |
import plotly.express as px # 그래프 라이브러리 추가
|
| 8 |
+
from openai import OpenAI
|
| 9 |
|
| 10 |
# --- Streamlit 설정 ---
|
| 11 |
st.set_page_config(
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
# --- API 키 설정 (Hugging Face Secrets에서 관리 권장) ---
|
| 153 |
+
if "OPENAI_API_KEY" not in os.environ: # 환경 변수 이름 변경
|
| 154 |
st.error(
|
| 155 |
+
"OPENAI_API_KEY 환경 변수가 설정되지 않았습니다. Hugging Face Secrets 또는 환경 변수에 API 키를 설정해주세요." # 에러 메시지 변경
|
| 156 |
)
|
| 157 |
st.stop()
|
| 158 |
|
| 159 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) # OpenAI 클라이언트 초기화
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# --- 세션 상태 초기화 (Streamlit 앱 상태 관리) ---
|
| 162 |
if "chat_session" not in st.session_state:
|
| 163 |
+
st.session_state["chat_session"] = [] # OpenAI는 채팅 세션 관리가 다름. 메시지 리스트로 관리
|
| 164 |
if "portfolio" not in st.session_state:
|
| 165 |
st.session_state["portfolio"] = {"cash": 10000000, "stocks": {}}
|
| 166 |
if "stocks" not in st.session_state:
|
|
|
|
| 428 |
**생성된 뉴스 기사:**
|
| 429 |
"""
|
| 430 |
chat_session = st.session_state["chat_session"]
|
| 431 |
+
messages = [{"role": "user", "content": prompt}] # messages 리스트 생성
|
| 432 |
+
|
| 433 |
+
response = client.chat.completions.create( # OpenAI ChatCompletions API 호출
|
| 434 |
+
model="gpt-4o-mini",
|
| 435 |
+
messages=messages, # messages 리스트 전달
|
| 436 |
+
response_format={
|
| 437 |
+
"type": "text"
|
| 438 |
+
},
|
| 439 |
+
temperature=0.7,
|
| 440 |
+
max_completion_tokens=25000,
|
| 441 |
+
top_p=0.95,
|
| 442 |
+
frequency_penalty=0,
|
| 443 |
+
presence_penalty=0
|
| 444 |
+
)
|
| 445 |
+
news_text = response.choices[0].message.content.strip() # 응답 텍스트 추출
|
| 446 |
|
| 447 |
news_articles = []
|
| 448 |
if news_text:
|
|
|
|
| 470 |
뉴스 의미 해설:
|
| 471 |
"""
|
| 472 |
chat_session = st.session_state["chat_session"]
|
| 473 |
+
messages = [{"role": "user", "content": prompt}] # messages 리스트 생성
|
| 474 |
try:
|
| 475 |
+
response = client.chat.completions.create( # OpenAI ChatCompletions API 호출
|
| 476 |
+
model="gpt-4o-mini",
|
| 477 |
+
messages=messages, # messages 리스트 전달
|
| 478 |
+
response_format={
|
| 479 |
+
"type": "text"
|
| 480 |
+
},
|
| 481 |
+
temperature=0.7,
|
| 482 |
+
max_completion_tokens=25000,
|
| 483 |
+
top_p=0.95,
|
| 484 |
+
frequency_penalty=0,
|
| 485 |
+
presence_penalty=0
|
| 486 |
+
)
|
| 487 |
+
meaning_text = response.choices[0].message.content.strip() # 응답 텍스트 추출
|
| 488 |
|
| 489 |
explanation = ""
|
| 490 |
related_sectors = []
|
|
|
|
| 507 |
meanings[str(i + 1)] = {"explanation": explanation, "sectors": related_sectors}
|
| 508 |
|
| 509 |
|
| 510 |
+
except Exception as e: # google.api_core.exceptions.ResourceExhausted -> Exception으로 변경
|
| 511 |
st.error(
|
| 512 |
f"API 할당량 초과 오류가 발생했습니다. 잠시 후 다시 시도해주세요. 오류 메시지: {e}"
|
| 513 |
)
|