Spaces:
Sleeping
Sleeping
Update src/pages/page_report.py
Browse files- src/pages/page_report.py +35 -30
src/pages/page_report.py
CHANGED
|
@@ -5,6 +5,7 @@ import pandas as pd
|
|
| 5 |
from datetime import datetime
|
| 6 |
from report_generator import generate_stock_report, show_report
|
| 7 |
from utils import load_css, call_genai_summary
|
|
|
|
| 8 |
|
| 9 |
def main():
|
| 10 |
mongo_uri = os.getenv("MONGO_URI")
|
|
@@ -84,34 +85,38 @@ def main():
|
|
| 84 |
submitted = st.form_submit_button("Tạo báo cáo", use_container_width=True)
|
| 85 |
|
| 86 |
if submitted and stock_code_input:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
st.rerun()
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
from report_generator import generate_stock_report, show_report
|
| 7 |
from utils import load_css, call_genai_summary
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
def main():
|
| 11 |
mongo_uri = os.getenv("MONGO_URI")
|
|
|
|
| 85 |
submitted = st.form_submit_button("Tạo báo cáo", use_container_width=True)
|
| 86 |
|
| 87 |
if submitted and stock_code_input:
|
| 88 |
+
temp = stock_code_input.strip().upper()
|
| 89 |
+
if not re.fullmatch(r"[A-Z0-9]{1,6}", temp):
|
| 90 |
+
st.error("Mã cổ phiếu không hợp lệ. Vui lòng nhập 1-6 ký tự chữ hoa hoặc số (ví dụ: HPG, VIC).")
|
| 91 |
+
else:
|
| 92 |
+
with st.spinner(f'Đang tổng hợp và phân tích dữ liệu cho mã {stock_code_input}...'):
|
| 93 |
+
report_data = generate_stock_report(
|
| 94 |
+
stock_code_input, (str(start_date), str(end_date)))
|
| 95 |
+
|
| 96 |
+
if report_data and (report_data["overall_sentiment"]["positive_mentions"] > 0 or report_data["overall_sentiment"]["negative_mentions"] > 0):
|
| 97 |
+
summary = call_genai_summary(
|
| 98 |
+
report_data, stock_code_input, (str(start_date), str(end_date)))
|
| 99 |
+
else:
|
| 100 |
+
summary = f"Không tìm thấy đủ dữ liệu nổi bật cho mã **{stock_code_input}** trong khoảng thời gian đã chọn để tạo tóm tắt AI."
|
| 101 |
+
|
| 102 |
+
# Save to MongoDB
|
| 103 |
+
inserted_id = reports_history.insert_one({
|
| 104 |
+
"uid": st.session_state["uid"],
|
| 105 |
+
"report_data": report_data,
|
| 106 |
+
"summary": summary,
|
| 107 |
+
"created_at": datetime.utcnow()
|
| 108 |
+
}).inserted_id
|
| 109 |
+
|
| 110 |
+
# Add new report to top of history
|
| 111 |
+
new_report = {
|
| 112 |
+
"_id": inserted_id,
|
| 113 |
+
"report_data": report_data,
|
| 114 |
+
"summary": summary,
|
| 115 |
+
"created_at": datetime.utcnow()
|
| 116 |
+
}
|
| 117 |
+
st.session_state["reports_history_list"].insert(0, new_report)
|
| 118 |
+
|
| 119 |
+
# Show new report
|
| 120 |
+
st.session_state["selected_report"] = new_report
|
| 121 |
+
st.session_state["show_form"] = False
|
| 122 |
st.rerun()
|