Spaces:
Build error
Build error
"second commit"
Browse filesFiles changed (21)
- __pycache__/dataset.cpython-39.pycAdded
- __pycache__/model.cpython-39.pycAdded
- __pycache__/utils.cpython-39.pycAdded
- app.py+124-4
- cache/intent/embeddings.ptAdded+3
- cache/intent/intent2idx.jsonAdded+152
- cache/intent/vocab.pklAdded+3
- cache/slot/embeddings.ptAdded+3
- cache/slot/tag2idx.jsonAdded+11
- cache/slot/vocab.pklAdded+3
- ckpt/intent/model_checkpoint.pthAdded+3
- data/intent/eval.jsonAdded
- data/intent/test.jsonAdded
- data/intent/train.jsonAdded
- data/slot/eval.jsonAdded
- data/slot/test.jsonAdded
- data/slot/train.jsonAdded
- dataset.pyAdded+74
- environment.ymlAdded+10
- model.pyAdded+76
- utils.pyAdded+42
1234567import gradio as grdef greet(name):return "Hello " + name + "!!"iface = gr.Interface(fn=greet, inputs="text", outputs="text")iface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127import gradio as grfrom typing import Dict, Listimport torchimport torch.nn as nnimport torch.optim as optimfrom torch.utils.data import DataLoaderimport jsonimport picklefrom pathlib import Pathfrom dataset import SeqClsDatasetfrom utils import Vocabfrom model import SeqClassifierimport ipdbmax_len = 128hidden_size = 256num_layers = 2dropout = 0.1bidirectional = Truelr = 1e-3batch_size = 64num_epoch = 5TRAIN = "train"DEV = "eval"TEST = "test"SPLITS = [TRAIN, DEV, TEST]device = "cpu"data_dir = Path("./data/intent/")ckpt_dir = Path("./ckpt/intent/")cache_dir = Path("./cache/intent/")# Before executing, place intent2idx.json, embeddings.pt, vocab.pkl, and utils.py in /contentwith open(cache_dir / "vocab.pkl", "rb") as f:vocab: Vocab = pickle.load(f)intent_idx_path = cache_dir / "intent2idx.json"intent2idx: Dict[str, int] = json.loads(intent_idx_path.read_text())data_paths = {split: data_dir / f"{split}.json" for split in SPLITS}data = {split: json.loads(path.read_text()) for split, path in data_paths.items()}datasets: Dict[str, SeqClsDataset] = {split: SeqClsDataset(split_data, vocab, intent2idx, max_len)for split, split_data in data.items()}#ipdb.set_trace()test_loader = DataLoader(datasets['test'], batch_size=batch_size, shuffle=False)embeddings = torch.load(cache_dir / "embeddings.pt")embeddings.to(device)# Load the best model after training# Initialize a new model with the same architecturebest_model = SeqClassifier(embeddings=embeddings,hidden_size=hidden_size,num_layers=num_layers,dropout=dropout,bidirectional=bidirectional,num_class=len(intent2idx)).to(device)# Define the path to the checkpoint fileckpt_path = ckpt_dir / "model_checkpoint.pth"# Load the model's state_dict and optimizer's state_dict from the checkpointcheckpoint = torch.load(ckpt_path, map_location=torch.device('cpu'))# Load the model's weightsbest_model.load_state_dict(checkpoint['model_state_dict']).to(device)# Reinitialize the optimizer with the model's parameters and load its state'''weight_decay = 1e-5optimizer = optim.Adam(best_model.parameters(), lr=lr, weight_decay=weight_decay)optimizer.load_state_dict(checkpoint['optimizer_state_dict'])'''# Retrieve the epoch number from the checkpointepoch = checkpoint['epoch']# Set the best model to evaluation modebest_model.eval()dic_intent2idx: Dict[str, int] = json.loads(intent_idx_path.read_text())dic_idx2label = {idx: intent for intent, idx in dic_intent2idx.items()}def Tidx2label(idx: int):return dic_idx2label[idx]with open(cache_dir / "vocab.pkl", "rb") as f:vocab: Vocab = pickle.load(f)# 把句子做成embeddings的索引def collate_fn(texts: str) -> torch.tensor:# 提取所有樣本的文本數據和標籤數據texts = texts.split()# 使用 vocab 將文本數據轉換為整數索引序列,並指定最大長度encoded_texts = vocab.encode_batch([[text for text in texts]], to_len=max_len)# 將整數索引序列轉換為 PyTorch 張量encoded_text = torch.tensor(encoded_texts)return encoded_textdef classify(text):encoded_text = collate_fn(text).to(device)output = best_model(encoded_text[0])Predicted_class = torch.argmax(output).item()prediction = Tidx2label(Predicted_class)return predictiondemo = gr.Interface(fn=classify,inputs=gr.Textbox(placeholder="請輸入一段文字..."),outputs="label",interpretation="default",examples=[["Take me to church"],["tell me what to call you"],["could you be a person"]])demo.launch()
123version https://git-lfs.github.com/spec/v1oid sha256:f48c2a4bb711ddd28a95f849b676ab6c76a4aeff3ba01976ccea97a4808ce790size 7789931123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152{ "last_maintenance": 0, "car_rental": 1, "transactions": 2, "user_name": 3, "credit_limit": 4, "date": 5, "greeting": 6, "international_fees": 7, "gas": 8, "calculator": 9, "redeem_rewards": 10, "change_ai_name": 11, "alarm": 12, "pin_change": 13, "update_playlist": 14, "what_can_i_ask_you": 15, "translate": 16, "change_accent": 17, "text": 18, "thank_you": 19, "where_are_you_from": 20, "goodbye": 21, "recipe": 22, "interest_rate": 23, "ingredients_list": 24, "tire_pressure": 25, "definition": 26, "who_do_you_work_for": 27, "todo_list": 28, "improve_credit_score": 29, "meaning_of_life": 30, "change_speed": 31, "exchange_rate": 32, "next_holiday": 33, "make_call": 34, "insurance_change": 35, "spending_history": 36, "meal_suggestion": 37, "fun_fact": 38, "restaurant_suggestion": 39, "tire_change": 40, "calendar_update": 41, "confirm_reservation": 42, "next_song": 43, "are_you_a_bot": 44, "yes": 45, "find_phone": 46, "cancel_reservation": 47, "what_is_your_name": 48, "bill_balance": 49, "direct_deposit": 50, "flight_status": 51, "order_status": 52, "maybe": 53, "transfer": 54, "freeze_account": 55, "cancel": 56, "shopping_list": 57, "measurement_conversion": 58, "jump_start": 59, "international_visa": 60, "travel_alert": 61, "oil_change_when": 62, "accept_reservations": 63, "report_lost_card": 64, "pto_request_status": 65, "repeat": 66, "directions": 67, "payday": 68, "smart_home": 69, "damaged_card": 70, "lost_luggage": 71, "carry_on": 72, "insurance": 73, "what_song": 74, "current_location": 75, "ingredient_substitution": 76, "order": 77, "todo_list_update": 78, "reset_settings": 79, "replacement_card_duration": 80, "order_checks": 81, "roll_dice": 82, "new_card": 83, "vaccines": 84, "pto_used": 85, "time": 86, "how_old_are_you": 87, "account_blocked": 88, "card_declined": 89, "who_made_you": 90, "shopping_list_update": 91, "rewards_balance": 92, "restaurant_reviews": 93, "change_user_name": 94, "spelling": 95, "nutrition_info": 96, "restaurant_reservation": 97, "timer": 98, "cook_time": 99, "whisper_mode": 100, "travel_notification": 101, "routing": 102, "book_hotel": 103, "apr": 104, "w2": 105, "gas_type": 106, "schedule_meeting": 107, "meeting_schedule": 108, "reminder": 109, "income": 110, "plug_type": 111, "what_are_your_hobbies": 112, "schedule_maintenance": 113, "report_fraud": 114, "food_last": 115, "traffic": 116, "no": 117, "reminder_update": 118, "book_flight": 119, "mpg": 120, "pto_balance": 121, "tell_joke": 122, "calories": 123, "balance": 124, "rollover_401k": 125, "weather": 126, "change_language": 127, "distance": 128, "play_music": 129, "min_payment": 130, "sync_device": 131, "pay_bill": 132, "taxes": 133, "share_location": 134, "bill_due": 135, "pto_request": 136, "calendar": 137, "uber": 138, "do_you_have_pets": 139, "change_volume": 140, "timezone": 141, "application_status": 142, "flip_coin": 143, "credit_score": 144, "oil_change_how": 145, "expiration_date": 146, "credit_limit_change": 147, "how_busy": 148, "travel_suggestion": 149}123version https://git-lfs.github.com/spec/v1oid sha256:9d4fa520420cf60655dd67114826cef0f8be23bc7ca07cdb3c072f2a400e242bsize 78973123version https://git-lfs.github.com/spec/v1oid sha256:faba49b73dfdd2a98dbbfe7b53eed50b8edd9df716169e8f837558c5e24c42bfsize 49410991234567891011{ "O": 0, "B-date": 1, "I-time": 2, "B-time": 3, "B-last_name": 4, "I-people": 5, "B-people": 6, "I-date": 7, "B-first_name": 8}123version https://git-lfs.github.com/spec/v1oid sha256:c711af8ba9cba928df00a20913b2bcdd0738ab3b9210b4b9f10d0ff9dcf27f16size 49861123version https://git-lfs.github.com/spec/v1oid sha256:65fdb8e191b37fc6866acd1699f8978736bfb975b176d5ee0464f43301d928e8size 569473011234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374from typing import List, Dict
import torch
from torch.utils.data import Dataset
from utils import Vocab
class SeqClsDataset(Dataset): def __init__( self, data: List[Dict], vocab: Vocab, label_mapping: Dict[str, int], max_len: int, ): self.data = data self.vocab = vocab self.label_mapping = label_mapping self._idx2label = {idx: intent for intent, idx in self.label_mapping.items()} self.max_len = max_len
def __len__(self) -> int: return len(self.data)
def __getitem__(self, index) -> Dict: instance = self.data[index] return instance
@property def num_classes(self) -> int: return len(self.label_mapping)
def collate_fn(self, samples: List[Dict]) -> Dict: # sample就是batch data # collate_fn幫你把batch data編碼成詞彙的索引 # batch[0] = {'text': '~', 'intent': '~', 'id': 'train-0'}
# 提取所有樣本的文本數據和標籤數據 texts = samples["text"] labels = samples["intent"]
# 使用 vocab 將文本數據轉換為整數索引序列,並指定最大長度 encoded_texts = self.vocab.encode_batch([text.split() for text in texts], to_len=self.max_len)
# 將標籤數據轉換為整數索引序列 encoded_labels = [self.label_mapping[label] for label in labels]
# 將整數索引序列轉換為 PyTorch 張量 encoded_text = torch.tensor(encoded_texts) encoded_label = torch.tensor(encoded_labels)
# 創建批次數據字典 batch_data = { "encoded_text": encoded_text, "encoded_label": encoded_label }
return batch_data
def label2idx(self, label: str): return self.label_mapping[label]
def idx2label(self, idx: int): return self._idx2label[idx]
class SeqTaggingClsDataset(SeqClsDataset): ignore_idx = -100
def collate_fn(self, samples): # TODO: implement collate_fn raise NotImplementedError12345678910name: adl-hw1channels: - defaultsdependencies: - python=3.9 - cudatoolkit=10.2 - cudnn=7.6 - pip - pip: - pip-tools12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576from typing import Dict
import torchimport torch.nn as nn
# Set devicedevice = "cuda" if torch.cuda.is_available() else "cpu"
class SeqClassifier(nn.Module): def __init__( self, embeddings: torch.tensor, hidden_size: int, num_layers: int, dropout: float, bidirectional: bool, num_class: int, ) -> None: super(SeqClassifier, self).__init__() self.embed = nn.Embedding.from_pretrained(embeddings, freeze=False) self.hidden_size=hidden_size self.num_layers=num_layers self.dropout=dropout self.bidirectional=bidirectional self.num_class=num_class
# model architecture self.rnn = nn.LSTM( input_size=embeddings.size(1), hidden_size=hidden_size, num_layers=num_layers, dropout=dropout, bidirectional=bidirectional, batch_first=True ) self.dropout_layer = nn.Dropout(p=self.dropout) self.fc = nn.Linear(self.encoder_output_size, num_class)
@property def encoder_output_size(self) -> int: # calculate the output dimension of rnn if self.bidirectional: return self.hidden_size * 2 else: return self.hidden_size
def forward(self, batch) -> torch.Tensor: # 將輸入嵌入到詞嵌入空間,就是把詞索引換成詞向量 embedded = self.embed(batch)
# 過 LSTM 層 rnn_output, _ = self.rnn(embedded) rnn_output = self.dropout_layer(rnn_output)
if not self.training: last_hidden_state_forward = rnn_output[ -1, :self.hidden_size] # 正向方向的隐藏状态 last_hidden_state_backward = rnn_output[ 0, self.hidden_size:] # 反向方向的隐藏状态 combined_hidden_state = torch.cat((last_hidden_state_forward, last_hidden_state_backward), dim=0)
# 通過全連接層 logits = self.fc(combined_hidden_state) return logits # 返回預測結果
last_hidden_state_forward = rnn_output[:, -1, :self.hidden_size] # 正向方向的隐藏状态 last_hidden_state_backward = rnn_output[:, 0, self.hidden_size:] # 反向方向的隐藏状态 combined_hidden_state = torch.cat((last_hidden_state_forward, last_hidden_state_backward), dim=1)
# 通過全連接層 logits = self.fc(combined_hidden_state) return logits # 返回預測結果
class SeqTagger(SeqClassifier): def forward(self, batch) -> Dict[str, torch.Tensor]: # TODO: implement model forward raise NotImplementedError123456789101112131415161718192021222324252627282930313233343536373839404142from typing import Iterable, List
class Vocab: PAD = "[PAD]" UNK = "[UNK]"
def __init__(self, vocab: Iterable[str]) -> None: self.token2idx = { Vocab.PAD: 0, Vocab.UNK: 1, **{token: i for i, token in enumerate(vocab, 2)}, }
@property def pad_id(self) -> int: return self.token2idx[Vocab.PAD]
@property def unk_id(self) -> int: return self.token2idx[Vocab.UNK]
@property def tokens(self) -> List[str]: return list(self.token2idx.keys())
def token_to_id(self, token: str) -> int: return self.token2idx.get(token, self.unk_id)
def encode(self, tokens: List[str]) -> List[int]: return [self.token_to_id(token) for token in tokens]
def encode_batch( self, batch_tokens: List[List[str]], to_len: int = None ) -> List[List[int]]: batch_ids = [self.encode(tokens) for tokens in batch_tokens] to_len = max(len(ids) for ids in batch_ids) if to_len is None else to_len padded_ids = pad_to_len(batch_ids, to_len, self.pad_id) return padded_ids
def pad_to_len(seqs: List[List[int]], to_len: int, padding: int) -> List[List[int]]: paddeds = [seq[:to_len] + [padding] * max(0, to_len - len(seq)) for seq in seqs] return paddeds__pycache__/dataset.cpython-39.pycAdded
__pycache__/model.cpython-39.pycAdded
__pycache__/utils.cpython-39.pycAdded
app.py+124-4
cache/intent/embeddings.ptAdded+3
cache/intent/intent2idx.jsonAdded+152
cache/intent/vocab.pklAdded+3
cache/slot/embeddings.ptAdded+3
cache/slot/tag2idx.jsonAdded+11
cache/slot/vocab.pklAdded+3
ckpt/intent/model_checkpoint.pthAdded+3
data/intent/eval.jsonAdded
data/intent/test.jsonAdded
data/intent/train.jsonAdded
data/slot/eval.jsonAdded
data/slot/test.jsonAdded
data/slot/train.jsonAdded
dataset.pyAdded+74
environment.ymlAdded+10
model.pyAdded+76
utils.pyAdded+42