sathvikt commited on
Commit
6cfac7c
·
verified ·
1 Parent(s): 7cc9cbf

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +126 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================================================
2
+ # 1) INSTALL DEPENDENCIES
3
+ # =========================================================
4
+ !pip -q install git+https://github.com/huggingface/parler-tts.git
5
+ !pip -q install soundfile transformers accelerate sentencepiece huggingface_hub
6
+
7
+ # =========================================================
8
+ # 2) IMPORTS
9
+ # =========================================================
10
+ import os
11
+ import torch
12
+ import soundfile as sf
13
+ from IPython.display import Audio, display
14
+ from google.colab import files
15
+ from parler_tts import ParlerTTSForConditionalGeneration
16
+ from transformers import AutoTokenizer
17
+ from huggingface_hub import notebook_login, hf_hub_download
18
+
19
+ # =========================================================
20
+ # 3) HUGGING FACE LOGIN
21
+ # =========================================================
22
+ print("🔐 Please login with your Hugging Face READ token")
23
+ notebook_login()
24
+
25
+ # =========================================================
26
+ # 4) VERIFY MODEL ACCESS
27
+ # =========================================================
28
+ MODEL_NAME = "ai4bharat/indic-parler-tts"
29
+
30
+ try:
31
+ hf_hub_download(
32
+ repo_id=MODEL_NAME,
33
+ filename="config.json"
34
+ )
35
+ print("✅ Model access verified")
36
+ except Exception as e:
37
+ print("❌ ACCESS ERROR")
38
+ print("Open this page:")
39
+ print("https://huggingface.co/ai4bharat/indic-parler-tts")
40
+ print("Then click 👉 Agree and access repository")
41
+ raise e
42
+
43
+ # =========================================================
44
+ # 5) DEVICE
45
+ # =========================================================
46
+ device = "cuda" if torch.cuda.is_available() else "cpu"
47
+ print("🚀 Using device:", device)
48
+
49
+ # =========================================================
50
+ # 6) LOAD MODEL
51
+ # =========================================================
52
+ print("⏳ Loading Kannada TTS model...")
53
+ model = ParlerTTSForConditionalGeneration.from_pretrained(
54
+ MODEL_NAME
55
+ ).to(device)
56
+
57
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
58
+
59
+ description_tokenizer = AutoTokenizer.from_pretrained(
60
+ model.config.text_encoder._name_or_path
61
+ )
62
+
63
+ print("✅ Model loaded successfully")
64
+
65
+ # =========================================================
66
+ # 7) SAFE GENERATION FUNCTION
67
+ # =========================================================
68
+ def generate_kannada_tts(prompt_text, output_file="/content/kannada_output.wav"):
69
+ prompt_text = str(prompt_text).strip()
70
+
71
+ if not prompt_text:
72
+ raise ValueError("❌ Kannada input cannot be empty")
73
+
74
+ description = (
75
+ "A calm Kannada male speaker with natural pronunciation, "
76
+ "clear studio quality audio, smooth narration, "
77
+ "and no background noise."
78
+ )
79
+
80
+ description_inputs = description_tokenizer(
81
+ description,
82
+ return_tensors="pt"
83
+ ).to(device)
84
+
85
+ prompt_inputs = tokenizer(
86
+ prompt_text,
87
+ return_tensors="pt"
88
+ ).to(device)
89
+
90
+ with torch.no_grad():
91
+ generation = model.generate(
92
+ input_ids=description_inputs.input_ids,
93
+ prompt_input_ids=prompt_inputs.input_ids
94
+ )
95
+
96
+ audio = generation.cpu().numpy().squeeze()
97
+
98
+ sf.write(
99
+ output_file,
100
+ audio,
101
+ model.config.sampling_rate
102
+ )
103
+
104
+ print(f"✅ Audio saved → {output_file}")
105
+ display(Audio(output_file))
106
+
107
+ return output_file
108
+
109
+ # =========================================================
110
+ # 8) USER INPUT OUTSIDE FUNCTION
111
+ # =========================================================
112
+ user_text = input("Enter Kannada text: ")
113
+
114
+ # Example:
115
+ # ನಮಸ್ಕಾರ, ನನ್ನ ಹೆಸರು ಅಥ್ಮಿಕ
116
+
117
+ try:
118
+ output_path = generate_kannada_tts(user_text)
119
+ except Exception as e:
120
+ print("❌ Error:", e)
121
+
122
+ # =========================================================
123
+ # 9) DOWNLOAD
124
+ # =========================================================
125
+ if os.path.exists("/content/kannada_output.wav"):
126
+ files.download("/content/kannada_output.wav")
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ git+https://github.com/huggingface/parler-tts.git
3
+ soundfile
4
+ transformers
5
+ accelerate
6
+ sentencepiece
7
+ torch