farid678 commited on
Commit
ef52462
·
verified ·
1 Parent(s): 4655264

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -1,36 +1,38 @@
1
  import torch
2
  import numpy as np
3
  import gradio as gr
4
- from transformers import AutoTokenizer, AutoModelForSpeechSeq2Seq
5
  import logging
6
  from scipy.io.wavfile import write
7
  import uuid
8
  import os
 
9
 
10
  # -----------------------------
11
- # Reduce Transformers warnings
12
  # -----------------------------
 
13
  logging.getLogger("transformers").setLevel(logging.ERROR)
14
 
15
  # -----------------------------
16
- # LOAD LOCAL MODEL AND TOKENIZER
17
  # -----------------------------
18
  device = 0 if torch.cuda.is_available() else -1
19
- model_dir = "./" # مسیر محلی در Space، همان‌جایی که adapter_model.safetensors قرار دارد
20
 
21
- # بارگذاری tokenizer
22
- tokenizer = AutoTokenizer.from_pretrained(model_dir)
 
 
23
 
24
- # بارگذاری مدل
25
- model = AutoModelForSpeechSeq2Seq.from_pretrained(
26
- model_dir,
27
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
28
- low_cpu_mem_usage=True,
 
 
29
  )
30
 
31
- if device >= 0:
32
- model = model.to(f"cuda:{device}")
33
-
34
  # -----------------------------
35
  # INFERENCE FUNCTION
36
  # -----------------------------
@@ -38,17 +40,16 @@ def tts_generate(text):
38
  if not text.strip():
39
  return None
40
 
41
- # تبدیل متن به توکن
42
- inputs = tokenizer(text, return_tensors="pt").to(model.device)
43
 
44
- # تولید صوت
45
- with torch.no_grad():
46
- audio_out = model.generate_speech(**inputs)
47
 
48
- audio = np.array(audio_out.cpu().numpy(), dtype=np.float32)
49
 
50
  # sampling rate پیش‌فرض
51
- sr = 22050
52
 
53
  # تبدیل float32 به int16 برای scipy
54
  audio_int16 = (audio * 32767).astype(np.int16)
@@ -82,10 +83,13 @@ demo = gr.Interface(
82
  placeholder=SAMPLES[0],
83
  ),
84
  outputs=gr.Audio(type="filepath", label="Generated Audio"),
85
- title="Custom TTS with Adapter Model",
86
  examples=[[s] for s in SAMPLES],
87
  )
88
 
 
 
 
89
  if __name__ == "__main__":
90
- demo.launch()
91
 
 
1
  import torch
2
  import numpy as np
3
  import gradio as gr
4
+ from transformers import pipeline
5
  import logging
6
  from scipy.io.wavfile import write
7
  import uuid
8
  import os
9
+ import warnings
10
 
11
  # -----------------------------
12
+ # SUPPRESS WARNINGS
13
  # -----------------------------
14
+ warnings.filterwarnings("ignore", category=FutureWarning)
15
  logging.getLogger("transformers").setLevel(logging.ERROR)
16
 
17
  # -----------------------------
18
+ # DEVICE SETUP
19
  # -----------------------------
20
  device = 0 if torch.cuda.is_available() else -1
 
21
 
22
+ # -----------------------------
23
+ # PATH TO FINE-TUNED MODEL
24
+ # -----------------------------
25
+ model_dir = "./" # مسیر فایل‌های fine-tuned در Space
26
 
27
+ # -----------------------------
28
+ # LOAD TTS PIPELINE
29
+ # -----------------------------
30
+ tts_pipe = pipeline(
31
+ task="text-to-speech",
32
+ model=model_dir,
33
+ device=device
34
  )
35
 
 
 
 
36
  # -----------------------------
37
  # INFERENCE FUNCTION
38
  # -----------------------------
 
40
  if not text.strip():
41
  return None
42
 
43
+ # اجرای مدل TTS
44
+ output = tts_pipe(text)
45
 
46
+ if "audio" not in output:
47
+ raise ValueError("TTS pipeline did not return audio")
 
48
 
49
+ audio = np.array(output["audio"], dtype=np.float32)
50
 
51
  # sampling rate پیش‌فرض
52
+ sr = output.get("sampling_rate", 22050)
53
 
54
  # تبدیل float32 به int16 برای scipy
55
  audio_int16 = (audio * 32767).astype(np.int16)
 
83
  placeholder=SAMPLES[0],
84
  ),
85
  outputs=gr.Audio(type="filepath", label="Generated Audio"),
86
+ title="Fine-tuned Orpheus-3B Expressive TTS",
87
  examples=[[s] for s in SAMPLES],
88
  )
89
 
90
+ # -----------------------------
91
+ # CLEAN RUN
92
+ # -----------------------------
93
  if __name__ == "__main__":
94
+ demo.launch(ssr_mode=False) # ssr_mode=False برای کاهش خطاهای asyncio
95