basyx commited on
Commit
7775283
·
verified ·
1 Parent(s): 7b8c0bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -18
app.py CHANGED
@@ -1,42 +1,108 @@
 
1
  import os
2
  import gradio as gr
3
  import tempfile
4
  import soundfile as sf
5
  from models import Tokenizer, Kokoro
6
- from fastapi import FastAPI, Request
7
- from fastapi.responses import FileResponse
8
- import uvicorn
9
 
10
- # Initialize Kokoro components
 
 
11
  def get_style_vector_choices(directory="voices"):
12
- if not os.path.exists(directory): return []
13
  return [file for file in os.listdir(directory) if file.endswith(".pt")]
14
 
 
15
  def get_onnx_models(directory="weights"):
16
- if not os.path.exists(directory): return []
17
  return [file for file in os.listdir(directory) if file.endswith(".onnx")]
18
 
19
- def local_tts(text: str, model_path: str, style_vector: str, output_file_format: str = "wav", speed: float = 1.0):
 
 
 
 
 
 
 
 
 
20
  if len(text) > 0:
21
  try:
22
  tokenizer = Tokenizer()
23
  style_vector_path = os.path.join("voices", style_vector)
24
- model_full_path = os.path.join("weights", model_path)
 
 
25
 
26
- inference = Kokoro(model_full_path, style_vector_path, tokenizer=tokenizer, lang='en-us')
27
  audio, sample_rate = inference.generate_audio(text, speed=speed)
28
 
29
  with tempfile.NamedTemporaryFile(suffix=f".{output_file_format}", delete=False) as temp_file:
30
  sf.write(temp_file.name, audio, sample_rate)
31
- return temp_file.name
 
 
 
32
  except Exception as e:
33
- print(f"Inference Error: {e}")
34
- return None
35
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # --- FASTAPI SETUP ---
38
- app = FastAPI(title="Basyx TTS Hub")
39
 
40
- @app.post("/v1/audio/speech")
41
- async def api_tts(request: Request):
42
- data = await request.json()
 
1
+
2
  import os
3
  import gradio as gr
4
  import tempfile
5
  import soundfile as sf
6
  from models import Tokenizer, Kokoro
 
 
 
7
 
8
+ # Function to fetch available style vectors dynamically
9
+
10
+
11
  def get_style_vector_choices(directory="voices"):
 
12
  return [file for file in os.listdir(directory) if file.endswith(".pt")]
13
 
14
+
15
  def get_onnx_models(directory="weights"):
 
16
  return [file for file in os.listdir(directory) if file.endswith(".onnx")]
17
 
18
+ # Function to perform TTS using your local model
19
+
20
+
21
+ def local_tts(
22
+ text: str,
23
+ model_path: str,
24
+ style_vector: str,
25
+ output_file_format: str = "wav",
26
+ speed: float = 1.0
27
+ ):
28
  if len(text) > 0:
29
  try:
30
  tokenizer = Tokenizer()
31
  style_vector_path = os.path.join("voices", style_vector)
32
+ model_path = os.path.join("weights", model_path)
33
+
34
+ inference = Kokoro(model_path, style_vector_path, tokenizer=tokenizer, lang='en-us')
35
 
 
36
  audio, sample_rate = inference.generate_audio(text, speed=speed)
37
 
38
  with tempfile.NamedTemporaryFile(suffix=f".{output_file_format}", delete=False) as temp_file:
39
  sf.write(temp_file.name, audio, sample_rate)
40
+ temp_file_path = temp_file.name
41
+
42
+ return temp_file_path
43
+
44
  except Exception as e:
45
+ raise gr.Error(f"An error occurred during TTS inference: {str(e)}")
46
+ else:
47
+ raise gr.Error("Input text cannot be empty.")
48
+
49
+
50
+ # Get the list of available style vectors
51
+ style_vector_choices = get_style_vector_choices()
52
+ onnx_models_choices = get_onnx_models()
53
+
54
+ # sample texts and their corresponding audio
55
+ sample_outputs = [
56
+ ("Educational Note", "Machine learning models rely on large datasets and complex algorithms to identify patterns and make predictions.", "assets/edu_note.wav"),
57
+ ("Fun Fact", "Did you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible!", "assets/fun_fact.wav"),
58
+ ("Thanks", "Thank you for listening to this audio. It was generated by the Kokoro TTS model.", "assets/thanks.wav")
59
+ ]
60
+
61
+ example_texts = [
62
+ ["Machine learning models rely on large datasets and complex algorithms to identify patterns and make predictions."],
63
+ ["Did you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible!"],
64
+ ["Thank you for listening to this audio. It was generated by the Kokoro TTS model."]
65
+ ]
66
+
67
+ # Gradio Interface
68
+ with gr.Blocks() as demo:
69
+ gr.Markdown("## <center> Kokoro TTS ONNX Inference | [GitHub Link](https://github.com/yakhyo/kokoro-onnx) </center>")
70
+
71
+ # Model-specific inputs
72
+ with gr.Row(variant="panel"):
73
+ model_path = gr.Dropdown(choices=onnx_models_choices, label="ONNX Model Path", value=onnx_models_choices[0])
74
+ style_vector = gr.Dropdown(choices=style_vector_choices, label="Style Vector", value=style_vector_choices[0])
75
+ output_file_format = gr.Dropdown(choices=["wav", "mp3"], label="Output Format", value="wav")
76
+ speed = gr.Slider(minimum=0.5, maximum=2.0, value=1.0, step=0.1, label="Speed")
77
+
78
+ # Text input and output
79
+ text = gr.Textbox(
80
+ label="Input Text",
81
+ placeholder="Enter text to convert to speech."
82
+ )
83
+ btn = gr.Button("Generate Speech")
84
+ output_audio = gr.Audio(label="Generated Audio", type="filepath")
85
+
86
+ # Link inputs and outputs
87
+ btn.click(
88
+ fn=local_tts,
89
+ inputs=[text, model_path, style_vector, output_file_format, speed],
90
+ outputs=output_audio
91
+ )
92
+
93
+ # Add example texts
94
+ gr.Examples(
95
+ examples=example_texts,
96
+ inputs=[text],
97
+ label="Click an example to populate the input text"
98
+ )
99
+
100
+ # Add example texts and audios
101
+ gr.Markdown("### Sample Texts and Audio")
102
+ for topic, sample_text, sample_audio in sample_outputs:
103
+ with gr.Row():
104
+ gr.Textbox(value=sample_text, label=topic, interactive=False)
105
+ gr.Audio(value=sample_audio, label="Example Audio", type="filepath", interactive=False)
106
 
107
+ demo.launch(server_name="0.0.0.0")
 
108