Donlagon007 commited on
Commit
813be6a
·
verified ·
1 Parent(s): 08be483

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +36 -5
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,3 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os, warnings
2
  import streamlit as st
3
  import matplotlib
@@ -11,15 +37,20 @@ st.set_page_config(page_title="CJK Font Debug", layout="wide")
11
  def inspect_fonts():
12
  info = {}
13
 
14
- # อ่apt.txt (น commit ผิด)
 
 
 
 
15
  try:
16
  with open("apt.txt", "r", encoding="utf-8") as f:
17
  info["apt.txt"] = f.read().strip()
18
  except FileNotFoundError:
19
  info["apt.txt"] = "(no apt.txt)"
20
 
21
- # path ที่มักจะมีใน Ubuntu/HF
22
  candidates = [
 
23
  "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
24
  "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
25
  "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
@@ -29,13 +60,13 @@ def inspect_fonts():
29
  ]
30
  info["exists"] = {p: os.path.exists(p) for p in candidates}
31
 
32
- # สแกน directory ทั่วไป
33
  sysfonts = fm.findSystemFonts(fontpaths=["/usr/share/fonts", "/usr/local/share/fonts"])
34
  hits = [p for p in sysfonts if any(k in p.lower() for k in ["wqy", "noto", "cjk", "droid"])]
35
  info["scan_count"] = len(hits)
36
  info["scan_sample"] = hits[:15]
37
 
38
- # เลือกฟอนต์ตัวแรกที่พบ (candidates ก่อน, จากนั้น hits)
39
  chosen = None
40
  for p in candidates + hits:
41
  if os.path.exists(p):
@@ -52,7 +83,6 @@ def inspect_fonts():
52
  matplotlib.rcParams["font.sans-serif"] = ["DejaVu Sans"]
53
  matplotlib.rcParams["axes.unicode_minus"] = False
54
 
55
- # rebuild cache เผื่อเคยมี cache เก่า
56
  try:
57
  fm._rebuild()
58
  except Exception:
@@ -64,6 +94,7 @@ def inspect_fonts():
64
 
65
  return info, chosen
66
 
 
67
  info, chosen = inspect_fonts()
68
 
69
  st.sidebar.subheader("Font debug")
 
1
+ from pathlib import Path
2
+ import requests
3
+
4
+ # ดาวน์โหลดฟอนต์ Noto Sans CJK (ครั้งแรกเท่านั้น) แล้วใช้ทันที
5
+ def ensure_local_cjk_font(font_filename="NotoSansCJKsc-Regular.otf"):
6
+ here = Path(__file__).resolve().parent
7
+ fonts_dir = here / "fonts"
8
+ fonts_dir.mkdir(exist_ok=True)
9
+ dest = fonts_dir / font_filename
10
+
11
+ if not dest.exists():
12
+ url = "https://github.com/googlefonts/noto-cjk/raw/main/Sans/OTF/SimplifiedChinese/NotoSansCJKsc-Regular.otf"
13
+ r = requests.get(url, timeout=60)
14
+ r.raise_for_status()
15
+ dest.write_bytes(r.content)
16
+ print("⬇️ Downloaded font to", dest)
17
+
18
+ fm.fontManager.addfont(str(dest))
19
+ prop = fm.FontProperties(fname=str(dest))
20
+ family = prop.get_name()
21
+ matplotlib.rcParams["font.sans-serif"] = [family, "DejaVu Sans"]
22
+ matplotlib.rcParams["axes.unicode_minus"] = False
23
+ print("✅ Using CJK font (local):", dest.name, "->", family)
24
+ return str(dest), family
25
+
26
+
27
  import os, warnings
28
  import streamlit as st
29
  import matplotlib
 
37
  def inspect_fonts():
38
  info = {}
39
 
40
+ # 0) พยายามให้มีฟนต์ในโปรเจ็กต์กน (สำค) <<<
41
+ local_path, local_family = ensure_local_cjk_font() # <<< โหลด/ลงทะเบียนฟอนต์
42
+ info["local_font"] = {"path": local_path, "family": local_family} # <<<
43
+
44
+ # 1) อ่าน apt.txt (กัน commit ผิด)
45
  try:
46
  with open("apt.txt", "r", encoding="utf-8") as f:
47
  info["apt.txt"] = f.read().strip()
48
  except FileNotFoundError:
49
  info["apt.txt"] = "(no apt.txt)"
50
 
51
+ # 2) path ที่มักจะมีใน Ubuntu/HF + ใส่ path โลคัลไว้หัวแถว <<<
52
  candidates = [
53
+ local_path, # <<< ใช้ไฟล์ที่เราเพิ่งดาวน์โหลดเป็นตัวเลือกแรก
54
  "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
55
  "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
56
  "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
 
60
  ]
61
  info["exists"] = {p: os.path.exists(p) for p in candidates}
62
 
63
+ # 3) สแกน directory ทั่วไป
64
  sysfonts = fm.findSystemFonts(fontpaths=["/usr/share/fonts", "/usr/local/share/fonts"])
65
  hits = [p for p in sysfonts if any(k in p.lower() for k in ["wqy", "noto", "cjk", "droid"])]
66
  info["scan_count"] = len(hits)
67
  info["scan_sample"] = hits[:15]
68
 
69
+ # 4) เลือกฟอนต์ตัวแรกที่พบ
70
  chosen = None
71
  for p in candidates + hits:
72
  if os.path.exists(p):
 
83
  matplotlib.rcParams["font.sans-serif"] = ["DejaVu Sans"]
84
  matplotlib.rcParams["axes.unicode_minus"] = False
85
 
 
86
  try:
87
  fm._rebuild()
88
  except Exception:
 
94
 
95
  return info, chosen
96
 
97
+
98
  info, chosen = inspect_fonts()
99
 
100
  st.sidebar.subheader("Font debug")
requirements.txt CHANGED
@@ -2,4 +2,5 @@ streamlit
2
  matplotlib
3
  seaborn
4
  pandas
5
- numpy
 
 
2
  matplotlib
3
  seaborn
4
  pandas
5
+ numpy
6
+ requests