Views
No views yet
model.pth, config.json, and vocab.json directly from this repository, initializes the model, and safely processes long Arabic text by splitting it into smaller sentence chunks.1sudo apt update
2sudo apt install ffmpeg libavcodec-dev libavutil-dev libavformat-dev1pip install git+[https://github.com/coqui-ai/TTS](https://github.com/coqui-ai/TTS)
2pip install transformers==4.33.0
3pip install torchcodec torch torchaudio huggingface_hub1import os
2import torch
3import torchaudio
4import re
5from TTS.tts.configs.xtts_config import XttsConfig
6from TTS.tts.models.xtts import Xtts
7from huggingface_hub import hf_hub_download
8
9# --- Configuration ---
10
11REPO_ID = "AsmaaAbdelkader/CodeSwitching_TTS_finetuned"
12
13print("Step 1: Downloading model files from Hugging Face...")
14config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
15vocab_path = hf_hub_download(repo_id=REPO_ID, filename="vocab.json")
16checkpoint_path = hf_hub_download(repo_id=REPO_ID, filename="model.pth")
17
18# Users can replace this with their own local file path if they want to clone a different voice
19reference_audio = hf_hub_download(repo_id=REPO_ID, filename="reference_audio.wav")
20
21print("Step 2: Loading the fine-tuned model...")
22config = XttsConfig()
23config.load_json(config_path)
24model = Xtts.init_from_config(config)
25
26model.load_checkpoint(
27 config,
28 checkpoint_path=checkpoint_path,
29 vocab_path=vocab_path,
30 use_deepspeed=False
31)
32model.cuda()
33
34print("Step 3: Computing speaker latents...")
35gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(audio_path=[reference_audio])
36
37# --- Text to Synthesize ---
38full_text = "المستخدم بيرد بتخمين، ووكيل الذكاء الاصطناعي بيبعت إشارة تانية، وبعدين في مرحلة ما، المستخدم ممكن يكسب اللعبة لو خمن صح. كنا مهتمين جداً ببحث اتجاه التواصل، لأن فيه سيناريوهات بيبقى لازم فيها الإنسان هو اللي يقدم الإشارات لوكيل الذكاء الاصطناعي."
39
40print("Step 4: Processing text and generating audio chunks...")
41# Split text into sentences using common Arabic/English punctuation
42sentences = re.split(r'(?<=[.؟!])\s+', full_text)
43wav_chunks = []
44
45print(f"Total sentences to process: {len(sentences)}")
46
47for i, sentence in enumerate(sentences):
48 if len(sentence.strip()) <= 1: # Skip empty strings
49 continue
50
51 print(f" Generating chunk {i+1}...")
52
53 out = model.inference(
54 text=sentence.strip(),
55 language="ar",
56 gpt_cond_latent=gpt_cond_latent,
57 speaker_embedding=speaker_embedding,
58 temperature=0.7,
59 )
60
61 wav_chunks.append(torch.tensor(out["wav"]))
62
63print("Step 5: Saving final audio...")
64if wav_chunks:
65 final_wav = torch.cat(wav_chunks, dim=0)
66 # Sample rate for XTTS is typically 24000
67 output_filename = "output_full.wav"
68 torchaudio.save(output_filename, final_wav.unsqueeze(0), 24000)
69 print(f"Success! Audio saved locally as {output_filename}")