Paul720810 commited on
Commit
5c29663
·
verified ·
1 Parent(s): d2b45cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -15
app.py CHANGED
@@ -68,7 +68,7 @@ class TextToSQLSystem:
68
  self._log("初始化系統...")
69
  self.query_cache = {}
70
 
71
- # 1. 載入嵌入模型(使用 transformers)
72
  self._log(f"載入嵌入模型: {embed_model_name}")
73
  self.embed_tokenizer = AutoTokenizer.from_pretrained(embed_model_name)
74
  self.embed_model = AutoModel.from_pretrained(embed_model_name)
@@ -81,21 +81,67 @@ class TextToSQLSystem:
81
  # 3. 載入數據集並建立索引
82
  self.dataset, self.faiss_index = self._load_and_index_dataset()
83
 
84
- # 4. 載入 GGUF 模型
85
- self._log("載入 GGUF 模型...")
86
- model_path = hf_hub_download(
87
- repo_id=GGUF_REPO_ID,
88
- filename=GGUF_FILENAME,
89
- repo_type="dataset"
90
- )
91
- self.llm = Llama(
92
- model_path=model_path,
93
- n_ctx=1024,
94
- n_threads=os.cpu_count(),
95
- n_batch=512,
96
- verbose=False
97
- )
98
  self._log("✅ 系統初始化完成")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  def _log(self, message: str, level: str = "INFO"):
101
  self.log_history.append(format_log(message, level))
 
68
  self._log("初始化系統...")
69
  self.query_cache = {}
70
 
71
+ # 1. 載入嵌入模型
72
  self._log(f"載入嵌入模型: {embed_model_name}")
73
  self.embed_tokenizer = AutoTokenizer.from_pretrained(embed_model_name)
74
  self.embed_model = AutoModel.from_pretrained(embed_model_name)
 
81
  # 3. 載入數據集並建立索引
82
  self.dataset, self.faiss_index = self._load_and_index_dataset()
83
 
84
+ # 4. 載入 GGUF 模型(添加錯誤處理)
85
+ self._load_gguf_model()
86
+
 
 
 
 
 
 
 
 
 
 
 
87
  self._log("✅ 系統初始化完成")
88
+ def _load_gguf_model(self):
89
+ """載入 GGUF 模型並處理錯誤"""
90
+ try:
91
+ self._log("載入 GGUF 模型...")
92
+ model_path = hf_hub_download(
93
+ repo_id=GGUF_REPO_ID,
94
+ filename=GGUF_FILENAME,
95
+ repo_type="dataset"
96
+ )
97
+
98
+ # 檢查文件完整性
99
+ file_size = os.path.getsize(model_path)
100
+ expected_size = 986 * 1024 * 1024 # 986MB
101
+ if file_size != expected_size:
102
+ self._log(f"⚠️ 文件大小不匹配: {file_size} != {expected_size}", "WARNING")
103
+ # 重新下載
104
+ os.remove(model_path)
105
+ model_path = hf_hub_download(
106
+ repo_id=GGUF_REPO_ID,
107
+ filename=GGUF_FILENAME,
108
+ repo_type="dataset",
109
+ force_download=True
110
+ )
111
+
112
+ # 使用更兼容的參數
113
+ self.llm = Llama(
114
+ model_path=model_path,
115
+ n_ctx=1024,
116
+ n_threads=max(2, os.cpu_count() - 1), # 留一個核心給系統
117
+ n_batch=256,
118
+ verbose=True, # 開啟詳細日誌
119
+ n_gpu_layers=0 # 強制使用CPU
120
+ )
121
+ self._log("✅ GGUF 模型載入成功")
122
+
123
+ except Exception as e:
124
+ self._log(f"❌ GGUF 模型載入失敗: {e}", "ERROR")
125
+ self._log("嘗試使用備用載入方式...")
126
+ self._load_gguf_model_fallback(model_path)
127
+ def _load_gguf_model_fallback(self, model_path):
128
+ """備用載入方式"""
129
+ try:
130
+ # 嘗試不同的參數組合
131
+ self.llm = Llama(
132
+ model_path=model_path,
133
+ n_ctx=512, # 更小的上下文
134
+ n_threads=4,
135
+ n_batch=128,
136
+ vocab_only=False,
137
+ use_mmap=True,
138
+ use_mlock=False,
139
+ verbose=True
140
+ )
141
+ self._log("✅ 備用方式載入成功")
142
+ except Exception as e:
143
+ self._log(f"❌ 備用方式也失敗: {e}", "ERROR")
144
+ self.llm = None
145
 
146
  def _log(self, message: str, level: str = "INFO"):
147
  self.log_history.append(format_log(message, level))