PuppetLover commited on
Commit
ee1c5d3
·
verified ·
1 Parent(s): 0657cac

Update src/report_generator.py

Browse files
Files changed (1) hide show
  1. src/report_generator.py +29 -17
src/report_generator.py CHANGED
@@ -12,40 +12,52 @@ import streamlit as st
12
 
13
  # download from Hugging Face dataset
14
  def ensure_db():
15
- import tempfile
16
- db_dir = "database"
17
- if os.path.exists(db_dir) and not os.path.isdir(db_dir):
18
- raise RuntimeError(f"Đã tồn tại file tên '{db_dir}', không thể tạo thư mục. Hãy xóa hoặc đổi tên file này.")
 
 
 
 
 
 
 
 
 
19
  try:
20
- os.makedirs(db_dir, exist_ok=True)
21
- db_path = os.path.join(db_dir, "stock_insights.db")
22
- local_dir = db_dir
23
- except Exception:
24
- temp_dir = os.path.join(tempfile.gettempdir(), "stockinsights_db")
25
- os.makedirs(temp_dir, exist_ok=True)
26
- db_path = os.path.join(temp_dir, "stock_insights.db")
27
- local_dir = temp_dir
28
- if not os.path.exists(db_path):
29
  from huggingface_hub import hf_hub_download
30
- hf_hub_download(
 
 
31
  repo_id="PuppetLover/stock_insights",
32
  filename="stock_insights.db",
33
  repo_type="dataset",
34
- local_dir=local_dir,
35
  local_dir_use_symlinks=False,
36
  )
37
- return db_path
 
 
 
 
 
38
 
39
- DB_PATH = ensure_db()
 
40
 
41
  def generate_stock_report(stock_code, time_period):
 
42
  start_date, end_date = time_period
43
  today = datetime.now().date()
 
44
  db_path = DB_PATH
 
45
  report = {
46
  "stock_code": stock_code,
47
  "report_period": f"{start_date} to {end_date}"
48
  }
 
49
  with sqlite3.connect(db_path) as conn:
50
  # Tạo bảng tạm relevant_articles
51
  conn.execute("DROP TABLE IF EXISTS relevant_articles;")
 
12
 
13
  # download from Hugging Face dataset
14
  def ensure_db():
15
+ repo_path = os.path.join(os.getcwd(), "database", "stock_insights.db")
16
+ if os.path.exists(repo_path):
17
+ return repo_path
18
+
19
+ candidates = [
20
+ os.path.join("/app", "database", "stock_insights.db"),
21
+ os.path.join("/tmp", "database", "stock_insights.db"),
22
+ os.path.join("/tmp", "stock_insights.db"),
23
+ ]
24
+ for p in candidates:
25
+ if os.path.exists(p):
26
+ return p
27
+
28
  try:
 
 
 
 
 
 
 
 
 
29
  from huggingface_hub import hf_hub_download
30
+ tmp_dir = os.path.join("/tmp", "database")
31
+ os.makedirs(tmp_dir, exist_ok=True)
32
+ local_file = hf_hub_download(
33
  repo_id="PuppetLover/stock_insights",
34
  filename="stock_insights.db",
35
  repo_type="dataset",
36
+ local_dir=tmp_dir,
37
  local_dir_use_symlinks=False,
38
  )
39
+ return local_file
40
+ except Exception as e:
41
+ local_rel = os.path.join("database", "stock_insights.db")
42
+ if os.path.exists(local_rel):
43
+ return local_rel
44
+ raise RuntimeError(f"Cannot access or download database file: {e}")
45
 
46
+ # gọi và gán hằng DB_PATH dùng trong module
47
+ DB_PATH = ensure_db()
48
 
49
  def generate_stock_report(stock_code, time_period):
50
+
51
  start_date, end_date = time_period
52
  today = datetime.now().date()
53
+ # db_path = os.path.join("database", "stock_insights.db")
54
  db_path = DB_PATH
55
+
56
  report = {
57
  "stock_code": stock_code,
58
  "report_period": f"{start_date} to {end_date}"
59
  }
60
+
61
  with sqlite3.connect(db_path) as conn:
62
  # Tạo bảng tạm relevant_articles
63
  conn.execute("DROP TABLE IF EXISTS relevant_articles;")