farid678 commited on
Commit
c210c3a
·
verified ·
1 Parent(s): d87efde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -1,20 +1,18 @@
1
- import os
 
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
4
  import numpy as np
5
- import io
6
  import soundfile as sf
 
7
 
8
  # -----------------------------
9
- # HUGGING FACE INFERENCE CLIENT
10
  # -----------------------------
11
- HF_TOKEN = os.getenv("HF_TOKEN")
12
- if HF_TOKEN is None:
13
- raise ValueError("Environment variable HF_TOKEN not found.")
14
-
15
- client = InferenceClient(
16
- provider="auto", # می‌توانید 'fal-ai' یا 'auto' استفاده کنید
17
- api_key=HF_TOKEN,
18
  )
19
 
20
  # -----------------------------
@@ -25,24 +23,25 @@ def tts_generate(text):
25
  return None
26
 
27
  try:
28
- # دریافت صوت به صورت bytes
29
- audio_bytes = client.text_to_speech(
30
- text,
31
- model="canopylabs/orpheus-3b-0.1-ft",
32
- )
33
  except Exception as e:
34
- return f"Error: {str(e)}"
 
 
 
 
35
 
36
- # تبدیل bytes به numpy array و نمونه‌برداری
37
- buffer = io.BytesIO(audio_bytes)
38
- audio, sr = sf.read(buffer, dtype="float32")
 
39
 
40
- return (sr, audio)
41
 
42
  # -----------------------------
43
- # ENGLISH SAMPLE TEXTS
44
  # -----------------------------
45
- ENGLISH_SAMPLES = [
46
  "[neutral] Hello! This is a neutral English voice generated by Orpheus.",
47
  "[expressive] I'm really excited to show you how natural this voice sounds!",
48
  "[calm] Please relax and enjoy this calm and smooth narration.",
@@ -57,13 +56,13 @@ demo = gr.Interface(
57
  fn=tts_generate,
58
  inputs=gr.Textbox(
59
  label="Enter text (English supported with tags)",
60
- placeholder=ENGLISH_SAMPLES[0],
61
- lines=4,
62
  ),
63
- outputs=gr.Audio(label="Generated Audio"),
64
- title="Orpheus 3B Text-to-Speech (Inference API)",
65
  description=(
66
- "English TTS using **canopylabs/orpheus-3b-0.1-ft** via Hugging Face Inference API.\n\n"
67
  "Supported style tags examples:\n"
68
  "- `[neutral]`\n"
69
  "- `[expressive]`\n"
@@ -71,9 +70,9 @@ demo = gr.Interface(
71
  "- `[narration]`\n"
72
  "- `[conversation]`\n\n"
73
  "Example:\n"
74
- "`[expressive] I'm very happy to see you today!.`"
75
  ),
76
- examples=[[s] for s in ENGLISH_SAMPLES],
77
  )
78
 
79
  if __name__ == "__main__":
 
1
+ import torch
2
+ from transformers import pipeline
3
  import gradio as gr
 
4
  import numpy as np
 
5
  import soundfile as sf
6
+ import io
7
 
8
  # -----------------------------
9
+ # LOAD PIPELINE
10
  # -----------------------------
11
+ device = 0 if torch.cuda.is_available() else -1
12
+ tts_pipe = pipeline(
13
+ task="text-to-speech",
14
+ model="canopylabs/orpheus-3b-0.1-ft",
15
+ device=device
 
 
16
  )
17
 
18
  # -----------------------------
 
23
  return None
24
 
25
  try:
26
+ output = tts_pipe(text)
 
 
 
 
27
  except Exception as e:
28
+ print("Error:", e)
29
+ return None
30
+
31
+ audio = np.asarray(output["audio"], dtype=np.float32)
32
+ sr = output["sampling_rate"]
33
 
34
+ # Convert to bytes for Gradio Audio
35
+ buffer = io.BytesIO()
36
+ sf.write(buffer, audio, sr, format="WAV")
37
+ buffer.seek(0)
38
 
39
+ return buffer
40
 
41
  # -----------------------------
42
+ # SAMPLE TEXTS WITH STYLE TAGS
43
  # -----------------------------
44
+ SAMPLES = [
45
  "[neutral] Hello! This is a neutral English voice generated by Orpheus.",
46
  "[expressive] I'm really excited to show you how natural this voice sounds!",
47
  "[calm] Please relax and enjoy this calm and smooth narration.",
 
56
  fn=tts_generate,
57
  inputs=gr.Textbox(
58
  label="Enter text (English supported with tags)",
59
+ placeholder=SAMPLES[0],
60
+ lines=4
61
  ),
62
+ outputs=gr.Audio(type="file", label="Generated Audio"),
63
+ title="Orpheus3B TTS",
64
  description=(
65
+ "English TTS using **canopylabs/orpheus-3b-0.1-ft** via Transformers pipeline.\n\n"
66
  "Supported style tags examples:\n"
67
  "- `[neutral]`\n"
68
  "- `[expressive]`\n"
 
70
  "- `[narration]`\n"
71
  "- `[conversation]`\n\n"
72
  "Example:\n"
73
+ "`[expressive] I'm very happy to see you today!`"
74
  ),
75
+ examples=[[s] for s in SAMPLES],
76
  )
77
 
78
  if __name__ == "__main__":