Download app.py from Akdenizz/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 3.47 kB
-
https://huggingface.co/spaces/Akdenizz/chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Akdenizz/chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Akdenizz/chatbot/resolve/main/app.py
3.47 kB
| import torch | |
| import os | |
| import sys | |
| MODEL_PATH = "advanced_chatbot_model.pth" | |
| DEVICE = torch.device('cpu') | |
| def get_model_params(): | |
| print("==========================================================") | |
| print("### MODEL PARAMETRE ANALİZİ BAŞLADI ###") | |
| print("==========================================================") | |
| if not os.path.exists(MODEL_PATH): | |
| print(f"HATA: Model dosyası ({MODEL_PATH}) bulunamadı. Lütfen dosya adını kontrol edin.") | |
| return | |
| try: | |
| # Ağırlıkları yükle | |
| loaded_object = torch.load(MODEL_PATH, map_location=DEVICE) | |
| # State Dictionary'yi bul (model kaydetme şeklinize göre değişebilir) | |
| if isinstance(loaded_object, dict) and 'model_state_dict' in loaded_object: | |
| state_dict = loaded_object['model_state_dict'] | |
| print("Bilgi: 'model_state_dict' anahtarı kullanılarak ağırlıklar yüklendi.") | |
| elif isinstance(loaded_object, dict): | |
| state_dict = loaded_object | |
| print("Bilgi: Yüklenen dosya doğrudan state_dict olarak algılandı.") | |
| else: | |
| print("HATA: Yüklenen dosya bir sözlük (dictionary) veya state_dict içermiyor.") | |
| return | |
| # 1. VOCAB_SIZE ve EMBED_DIM (embedding.weight: [VOCAB_SIZE, EMBED_DIM]) | |
| embed_shape = state_dict['embedding.weight'].shape | |
| VOCAB_SIZE = embed_shape[0] | |
| EMBED_DIM = embed_shape[1] | |
| # 2. HIDDEN_DIM (lstm.weight_hh_l0: [4*H, H] olmalı, H gizli boyut) | |
| # Bidirectional olduğu için ilk katmanın gizli ağırlığının 2. boyutu (index 1) Hidden Dim'i verir. | |
| lstm_hh_shape = state_dict['lstm.weight_hh_l0'].shape | |
| HIDDEN_DIM = lstm_hh_shape[1] | |
| # 3. INTERMEDIATE_DIM (fc1.bias: [INTERMEDIATE_DIM]) | |
| fc1_bias_shape = state_dict['fc1.bias'].shape | |
| INTERMEDIATE_DIM = fc1_bias_shape[0] | |
| # 4. OUTPUT_DIM (fc2.weight: [OUTPUT_DIM, INTERMEDIATE_DIM] olmalı) | |
| fc2_shape = state_dict['fc2.weight'].shape | |
| OUTPUT_DIM = fc2_shape[0] | |
| print("==========================================================") | |
| print("TESPİT EDİLEN GERÇEK MODEL PARAMETRELERİ:") | |
| print(f"1. VOCAB_SIZE (Kelime Dağarcığı Boyutu): {VOCAB_SIZE}") | |
| print(f"2. EMBED_DIM (Gömülü Vektör Boyutu): {EMBED_DIM}") | |
| print(f"3. HIDDEN_DIM (LSTM Gizli Boyutu): {HIDDEN_DIM}") | |
| print(f"4. INTERMEDIATE_DIM (Ara Katman Boyutu): {INTERMEDIATE_DIM}") | |
| print("==========================================================") | |
| print("Modelin başarılı bir şekilde yüklenmesi için bu dört değeri kullanacağız.") | |
| except KeyError as e: | |
| print("==========================================================") | |
| print(f"ANAHTAR HATASI: '{e}' anahtarı bulunamadı.") | |
| print("Bu, modelinizdeki katman adlarının (embedding, lstm, fc1, fc2) kodla uyuşmadığı anlamına gelir.") | |
| print("==========================================================") | |
| except Exception as e: | |
| print("==========================================================") | |
| print("ÇÖZÜMLEME BAŞARISIZ OLDU. DİĞER HATA MESAJI:") | |
| print(e) | |
| print("==========================================================") | |
| # Bu kod Gradio arayüzünü başlatmayı denemeyecektir. | |
| sys.exit(0) | |
| if __name__ == '__main__': | |
| get_model_params() |