import streamlit as st import pyrebase import os import base64 import json import requests from dotenv import load_dotenv load_dotenv() # ==== Firebase Config & Initialization ==== def initialize_firebase(): """Khởi tạo và trả về các đối tượng Firebase. Sử dụng singleton pattern.""" if "firebase_app" not in st.session_state: firebase_config = { "apiKey": os.getenv("FIREBASE_API_KEY"), "authDomain": os.getenv("FIREBASE_AUTH_DOMAIN"), "projectId": os.getenv("FIREBASE_PROJECT_ID"), "storageBucket": os.getenv("FIREBASE_STORAGE_BUCKET"), "messagingSenderId": os.getenv("FIREBASE_MESSAGING_SENDER_ID"), "appId": os.getenv("FIREBASE_APP_ID"), "databaseURL": os.getenv("FIREBASE_DATABASE_URL", ""), } st.session_state.firebase_app = pyrebase.initialize_app(firebase_config) auth_fb = st.session_state.firebase_app.auth() return auth_fb # ==== CSS Dùng chung ==== def load_css(): """Tải CSS theme Cyberpunk Neon.""" st.markdown(""" """, unsafe_allow_html=True) # ==== Hàm Render Avatar ==== def render_avatar(uid, container, get_avatar_blob_func): avatar_bytes = get_avatar_blob_func(uid) if avatar_bytes: img_base64 = base64.b64encode(avatar_bytes).decode() avatar_html = f'' else: avatar_html = """
👤
Chưa có avatar
""" container.html(f"
{avatar_html}
") # ==== Hàm gọi Gemini API ==== def call_genai_summary(report_data, stock_code, time_period): api_key = os.getenv("GEMINI_API_KEY") if not api_key: st.error("Vui lòng cung cấp GEMINI_API_KEY trong file .env") return "Lỗi: Chưa cấu hình API Key." url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={api_key}" prompt = f""" Hãy tóm tắt ngắn gọn, chuyên nghiệp về mã cổ phiếu {stock_code} trong giai đoạn {time_period[0]} đến {time_period[1]} dựa trên dữ liệu JSON sau: {json.dumps(report_data, ensure_ascii=False, indent=2)} """ payload = {"contents": [{"parts": [{"text": prompt}]}]} try: response = requests.post(url, json=payload, timeout=45) response.raise_for_status() data = response.json() return data["candidates"][0]["content"]["parts"][0]["text"] except Exception as e: return f"Lỗi khi gọi Gemini API: {e}"