Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install required packages
|
| 2 |
+
!pip install transformers datasets torchaudio TTS huggingface_hub
|
| 3 |
+
|
| 4 |
+
# Import libraries
|
| 5 |
+
from datasets import load_dataset, DatasetDict
|
| 6 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# Load existing TTS model (you can choose other models too)
|
| 10 |
+
model_id = "microsoft/speecht5_tts" # Example model
|
| 11 |
+
processor = SpeechT5Processor.from_pretrained(model_id)
|
| 12 |
+
model = SpeechT5ForTextToSpeech.from_pretrained(model_id)
|
| 13 |
+
|
| 14 |
+
# Load your conlang dataset
|
| 15 |
+
dataset = load_dataset("csv", data_files={"train": "./dataset/train.csv"}, delimiter=",")
|
| 16 |
+
|
| 17 |
+
# Preprocessing: convert text to tokens and load audio
|
| 18 |
+
# You can define your own tokenizer for your conlang here
|
| 19 |
+
def preprocess(example):
|
| 20 |
+
input_ids = processor.tokenizer(example["text"], return_tensors="pt").input_ids[0]
|
| 21 |
+
return {"input_ids": input_ids}
|
| 22 |
+
|
| 23 |
+
dataset = dataset.map(preprocess)
|
| 24 |
+
|
| 25 |
+
# Prepare DataLoader
|
| 26 |
+
from torch.utils.data import DataLoader
|
| 27 |
+
|
| 28 |
+
def collate_fn(batch):
|
| 29 |
+
input_ids = torch.nn.utils.rnn.pad_sequence([b["input_ids"] for b in batch], batch_first=True)
|
| 30 |
+
return {"input_ids": input_ids}
|
| 31 |
+
|
| 32 |
+
train_loader = DataLoader(dataset["train"], batch_size=4, shuffle=True, collate_fn=collate_fn)
|
| 33 |
+
|
| 34 |
+
# Fine-tune the model
|
| 35 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=5e-5)
|
| 36 |
+
model.train()
|
| 37 |
+
|
| 38 |
+
for epoch in range(10): # example: 10 epochs
|
| 39 |
+
for batch in train_loader:
|
| 40 |
+
outputs = model(input_ids=batch["input_ids"], labels=batch["input_ids"])
|
| 41 |
+
loss = outputs.loss
|
| 42 |
+
loss.backward()
|
| 43 |
+
optimizer.step()
|
| 44 |
+
optimizer.zero_grad()
|
| 45 |
+
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
|
| 46 |
+
|
| 47 |
+
# Save model to Hugging Face Hub (optional)
|
| 48 |
+
from huggingface_hub import HfApi, HfFolder, Repository
|
| 49 |
+
|
| 50 |
+
repo = Repository(local_dir="./conlang-tts", clone_from="your-username/conlang-tts")
|
| 51 |
+
model.save_pretrained("./conlang-tts")
|
| 52 |
+
processor.save_pretrained("./conlang-tts")
|
| 53 |
+
repo.push_to_hub()
|
| 54 |
+
|