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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import contextlib
|
| 3 |
+
import io
|
| 4 |
+
import random
|
| 5 |
+
import tempfile
|
| 6 |
+
import time
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Optional, Tuple
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import numpy as np
|
| 12 |
+
import soundfile as sf
|
| 13 |
+
import torch
|
| 14 |
+
|
| 15 |
+
from dia.model import Dia
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# --- Global Setup ---
|
| 19 |
+
parser = argparse.ArgumentParser(description="Gradio interface for Nari TTS")
|
| 20 |
+
parser.add_argument("--device", type=str, default=None, help="Force device (e.g., 'cuda', 'mps', 'cpu')")
|
| 21 |
+
parser.add_argument("--share", action="store_true", help="Enable Gradio sharing")
|
| 22 |
+
|
| 23 |
+
args = parser.parse_args()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Determine device
|
| 27 |
+
if args.device:
|
| 28 |
+
device = torch.device(args.device)
|
| 29 |
+
elif torch.cuda.is_available():
|
| 30 |
+
device = torch.device("cuda")
|
| 31 |
+
# Simplified MPS check for broader compatibility
|
| 32 |
+
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
|
| 33 |
+
# Basic check is usually sufficient, detailed check can be problematic
|
| 34 |
+
device = torch.device("mps")
|
| 35 |
+
else:
|
| 36 |
+
device = torch.device("cpu")
|
| 37 |
+
|
| 38 |
+
print(f"Using device: {device}")
|
| 39 |
+
|
| 40 |
+
# Load Nari model and config
|
| 41 |
+
print("Loading Nari model...")
|
| 42 |
+
try:
|
| 43 |
+
dtype_map = {
|
| 44 |
+
"cpu": "float32",
|
| 45 |
+
"mps": "float32", # Apple M series – better with float32
|
| 46 |
+
"cuda": "float16", # NVIDIA – better with float16
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
dtype = dtype_map.get(device.type, "float16")
|
| 50 |
+
print(f"Using device: {device}, attempting to load model with {dtype}")
|
| 51 |
+
model = Dia.from_pretrained("nari-labs/Dia-1.6B-0626", compute_dtype=dtype, device=device)
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"Error loading Nari model: {e}")
|
| 54 |
+
raise
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def set_seed(seed: int):
|
| 58 |
+
"""Sets the random seed for reproducibility."""
|
| 59 |
+
random.seed(seed)
|
| 60 |
+
np.random.seed(seed)
|
| 61 |
+
torch.manual_seed(seed)
|
| 62 |
+
if torch.cuda.is_available():
|
| 63 |
+
torch.cuda.manual_seed(seed)
|
| 64 |
+
torch.cuda.manual_seed_all(seed)
|
| 65 |
+
torch.backends.cudnn.deterministic = True
|
| 66 |
+
torch.backends.cudnn.benchmark = False
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def run_inference(
|
| 70 |
+
text_input: str,
|
| 71 |
+
audio_prompt_text_input: str,
|
| 72 |
+
audio_prompt_input: Optional[Tuple[int, np.ndarray]],
|
| 73 |
+
max_new_tokens: int,
|
| 74 |
+
cfg_scale: float,
|
| 75 |
+
temperature: float,
|
| 76 |
+
top_p: float,
|
| 77 |
+
cfg_filter_top_k: int,
|
| 78 |
+
speed_factor: float,
|
| 79 |
+
seed: Optional[int] = None,
|
| 80 |
+
):
|
| 81 |
+
"""
|
| 82 |
+
Runs Nari inference using the globally loaded model and provided inputs.
|
| 83 |
+
Uses temporary files for text and audio prompt compatibility with inference.generate.
|
| 84 |
+
"""
|
| 85 |
+
global model, device # Access global model, config, device
|
| 86 |
+
console_output_buffer = io.StringIO()
|
| 87 |
+
|
| 88 |
+
with contextlib.redirect_stdout(console_output_buffer):
|
| 89 |
+
# Prepend transcript text if audio_prompt provided
|
| 90 |
+
if audio_prompt_input and audio_prompt_text_input and not audio_prompt_text_input.isspace():
|
| 91 |
+
text_input = audio_prompt_text_input + "\n" + text_input
|
| 92 |
+
text_input = text_input.strip()
|
| 93 |
+
|
| 94 |
+
if audio_prompt_input and (not audio_prompt_text_input or audio_prompt_text_input.isspace()):
|
| 95 |
+
raise gr.Error("Audio Prompt Text input cannot be empty.")
|
| 96 |
+
|
| 97 |
+
if not text_input or text_input.isspace():
|
| 98 |
+
raise gr.Error("Text input cannot be empty.")
|
| 99 |
+
|
| 100 |
+
# Preprocess Audio
|
| 101 |
+
temp_txt_file_path = None
|
| 102 |
+
temp_audio_prompt_path = None
|
| 103 |
+
output_audio = (44100, np.zeros(1, dtype=np.float32))
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
prompt_path_for_generate = None
|
| 107 |
+
if audio_prompt_input is not None:
|
| 108 |
+
sr, audio_data = audio_prompt_input
|
| 109 |
+
# Check if audio_data is valid
|
| 110 |
+
if audio_data is None or audio_data.size == 0 or audio_data.max() == 0: # Check for silence/empty
|
| 111 |
+
gr.Warning("Audio prompt seems empty or silent, ignoring prompt.")
|
| 112 |
+
else:
|
| 113 |
+
# Save prompt audio to a temporary WAV file
|
| 114 |
+
with tempfile.NamedTemporaryFile(mode="wb", suffix=".wav", delete=False) as f_audio:
|
| 115 |
+
temp_audio_prompt_path = f_audio.name # Store path for cleanup
|
| 116 |
+
|
| 117 |
+
# Basic audio preprocessing for consistency
|
| 118 |
+
# Convert to float32 in [-1, 1] range if integer type
|
| 119 |
+
if np.issubdtype(audio_data.dtype, np.integer):
|
| 120 |
+
max_val = np.iinfo(audio_data.dtype).max
|
| 121 |
+
audio_data = audio_data.astype(np.float32) / max_val
|
| 122 |
+
elif not np.issubdtype(audio_data.dtype, np.floating):
|
| 123 |
+
gr.Warning(f"Unsupported audio prompt dtype {audio_data.dtype}, attempting conversion.")
|
| 124 |
+
# Attempt conversion, might fail for complex types
|
| 125 |
+
try:
|
| 126 |
+
audio_data = audio_data.astype(np.float32)
|
| 127 |
+
except Exception as conv_e:
|
| 128 |
+
raise gr.Error(f"Failed to convert audio prompt to float32: {conv_e}")
|
| 129 |
+
|
| 130 |
+
# Ensure mono (average channels if stereo)
|
| 131 |
+
if audio_data.ndim > 1:
|
| 132 |
+
if audio_data.shape[0] == 2: # Assume (2, N)
|
| 133 |
+
audio_data = np.mean(audio_data, axis=0)
|
| 134 |
+
elif audio_data.shape[1] == 2: # Assume (N, 2)
|
| 135 |
+
audio_data = np.mean(audio_data, axis=1)
|
| 136 |
+
else:
|
| 137 |
+
gr.Warning(
|
| 138 |
+
f"Audio prompt has unexpected shape {audio_data.shape}, taking first channel/axis."
|
| 139 |
+
)
|
| 140 |
+
audio_data = (
|
| 141 |
+
audio_data[0] if audio_data.shape[0] < audio_data.shape[1] else audio_data[:, 0]
|
| 142 |
+
)
|
| 143 |
+
audio_data = np.ascontiguousarray(audio_data) # Ensure contiguous after slicing/mean
|
| 144 |
+
|
| 145 |
+
# Write using soundfile
|
| 146 |
+
try:
|
| 147 |
+
sf.write(
|
| 148 |
+
temp_audio_prompt_path, audio_data, sr, subtype="FLOAT"
|
| 149 |
+
) # Explicitly use FLOAT subtype
|
| 150 |
+
prompt_path_for_generate = temp_audio_prompt_path
|
| 151 |
+
print(f"Created temporary audio prompt file: {temp_audio_prompt_path} (orig sr: {sr})")
|
| 152 |
+
except Exception as write_e:
|
| 153 |
+
print(f"Error writing temporary audio file: {write_e}")
|
| 154 |
+
raise gr.Error(f"Failed to save audio prompt: {write_e}")
|
| 155 |
+
|
| 156 |
+
# Set and Display Generation Seed
|
| 157 |
+
if seed is None or seed < 0:
|
| 158 |
+
seed = random.randint(0, 2**32 - 1)
|
| 159 |
+
print(f"\nNo seed provided, generated random seed: {seed}\n")
|
| 160 |
+
else:
|
| 161 |
+
print(f"\nUsing user-selected seed: {seed}\n")
|
| 162 |
+
set_seed(seed)
|
| 163 |
+
|
| 164 |
+
# Run Generation
|
| 165 |
+
print(f'Generating speech: \n"{text_input}"\n')
|
| 166 |
+
|
| 167 |
+
start_time = time.time()
|
| 168 |
+
|
| 169 |
+
# Use torch.inference_mode() context manager for the generation call
|
| 170 |
+
with torch.inference_mode():
|
| 171 |
+
output_audio_np = model.generate(
|
| 172 |
+
text_input,
|
| 173 |
+
max_tokens=max_new_tokens,
|
| 174 |
+
cfg_scale=cfg_scale,
|
| 175 |
+
temperature=temperature,
|
| 176 |
+
top_p=top_p,
|
| 177 |
+
cfg_filter_top_k=cfg_filter_top_k, # Pass the value here
|
| 178 |
+
use_torch_compile=False, # Keep False for Gradio stability
|
| 179 |
+
audio_prompt=prompt_path_for_generate,
|
| 180 |
+
verbose=True,
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
end_time = time.time()
|
| 184 |
+
print(f"Generation finished in {end_time - start_time:.2f} seconds.\n")
|
| 185 |
+
|
| 186 |
+
# 4. Convert Codes to Audio
|
| 187 |
+
if output_audio_np is not None:
|
| 188 |
+
# Get sample rate from the loaded DAC model
|
| 189 |
+
output_sr = 44100
|
| 190 |
+
|
| 191 |
+
# --- Slow down audio ---
|
| 192 |
+
original_len = len(output_audio_np)
|
| 193 |
+
# Ensure speed_factor is positive and not excessively small/large to avoid issues
|
| 194 |
+
speed_factor = max(0.1, min(speed_factor, 5.0))
|
| 195 |
+
target_len = int(original_len / speed_factor) # Target length based on speed_factor
|
| 196 |
+
if target_len != original_len and target_len > 0: # Only interpolate if length changes and is valid
|
| 197 |
+
x_original = np.arange(original_len)
|
| 198 |
+
x_resampled = np.linspace(0, original_len - 1, target_len)
|
| 199 |
+
resampled_audio_np = np.interp(x_resampled, x_original, output_audio_np)
|
| 200 |
+
output_audio = (
|
| 201 |
+
output_sr,
|
| 202 |
+
resampled_audio_np.astype(np.float32),
|
| 203 |
+
) # Use resampled audio
|
| 204 |
+
print(
|
| 205 |
+
f"Resampled audio from {original_len} to {target_len} samples for {speed_factor:.2f}x speed."
|
| 206 |
+
)
|
| 207 |
+
else:
|
| 208 |
+
output_audio = (
|
| 209 |
+
output_sr,
|
| 210 |
+
output_audio_np,
|
| 211 |
+
) # Keep original if calculation fails or no change
|
| 212 |
+
print(f"Skipping audio speed adjustment (factor: {speed_factor:.2f}).")
|
| 213 |
+
# --- End slowdown ---
|
| 214 |
+
|
| 215 |
+
print(f"Audio conversion successful. Final shape: {output_audio[1].shape}, Sample Rate: {output_sr}")
|
| 216 |
+
|
| 217 |
+
# Explicitly convert to int16 to prevent Gradio warning
|
| 218 |
+
if output_audio[1].dtype == np.float32 or output_audio[1].dtype == np.float64:
|
| 219 |
+
audio_for_gradio = np.clip(output_audio[1], -1.0, 1.0)
|
| 220 |
+
audio_for_gradio = (audio_for_gradio * 32767).astype(np.int16)
|
| 221 |
+
output_audio = (output_sr, audio_for_gradio)
|
| 222 |
+
print("Converted audio to int16 for Gradio output.")
|
| 223 |
+
|
| 224 |
+
else:
|
| 225 |
+
print("\nGeneration finished, but no valid tokens were produced.")
|
| 226 |
+
# Return default silence
|
| 227 |
+
gr.Warning("Generation produced no output.")
|
| 228 |
+
|
| 229 |
+
except Exception as e:
|
| 230 |
+
print(f"Error during inference: {e}")
|
| 231 |
+
import traceback
|
| 232 |
+
|
| 233 |
+
traceback.print_exc()
|
| 234 |
+
# Re-raise as Gradio error to display nicely in the UI
|
| 235 |
+
raise gr.Error(f"Inference failed: {e}")
|
| 236 |
+
|
| 237 |
+
finally:
|
| 238 |
+
# Cleanup Temporary Files defensively
|
| 239 |
+
if temp_txt_file_path and Path(temp_txt_file_path).exists():
|
| 240 |
+
try:
|
| 241 |
+
Path(temp_txt_file_path).unlink()
|
| 242 |
+
print(f"Deleted temporary text file: {temp_txt_file_path}")
|
| 243 |
+
except OSError as e:
|
| 244 |
+
print(f"Warning: Error deleting temporary text file {temp_txt_file_path}: {e}")
|
| 245 |
+
if temp_audio_prompt_path and Path(temp_audio_prompt_path).exists():
|
| 246 |
+
try:
|
| 247 |
+
Path(temp_audio_prompt_path).unlink()
|
| 248 |
+
print(f"Deleted temporary audio prompt file: {temp_audio_prompt_path}")
|
| 249 |
+
except OSError as e:
|
| 250 |
+
print(f"Warning: Error deleting temporary audio prompt file {temp_audio_prompt_path}: {e}")
|
| 251 |
+
|
| 252 |
+
# After generation, capture the printed output
|
| 253 |
+
console_output = console_output_buffer.getvalue()
|
| 254 |
+
|
| 255 |
+
return output_audio, seed, console_output
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
# --- Create Gradio Interface ---
|
| 259 |
+
css = """
|
| 260 |
+
#col-container {max-width: 90%; margin-left: auto; margin-right: auto;}
|
| 261 |
+
"""
|
| 262 |
+
# Attempt to load default text from example.txt
|
| 263 |
+
default_text = "[S1] Dia is an open weights text to dialogue model. \n[S2] You get full control over scripts and voices. \n[S1] Wow. Amazing. (laughs) \n[S2] Try it now on Git hub or Hugging Face."
|
| 264 |
+
example_txt_path = Path("./example.txt")
|
| 265 |
+
if example_txt_path.exists():
|
| 266 |
+
try:
|
| 267 |
+
default_text = example_txt_path.read_text(encoding="utf-8").strip()
|
| 268 |
+
if not default_text: # Handle empty example file
|
| 269 |
+
default_text = "Example text file was empty."
|
| 270 |
+
except Exception as e:
|
| 271 |
+
print(f"Warning: Could not read example.txt: {e}")
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
# Build Gradio UI
|
| 275 |
+
with gr.Blocks(css=css, theme="gradio/dark") as demo:
|
| 276 |
+
gr.Markdown("# Nari Text-to-Speech Synthesis")
|
| 277 |
+
|
| 278 |
+
with gr.Row(equal_height=False):
|
| 279 |
+
with gr.Column(scale=1):
|
| 280 |
+
with gr.Accordion("Audio Reference Prompt (Optional)", open=False):
|
| 281 |
+
audio_prompt_input = gr.Audio(
|
| 282 |
+
label="Audio Prompt (Optional)",
|
| 283 |
+
show_label=True,
|
| 284 |
+
sources=["upload", "microphone"],
|
| 285 |
+
type="numpy",
|
| 286 |
+
)
|
| 287 |
+
audio_prompt_text_input = gr.Textbox(
|
| 288 |
+
label="Transcript of Audio Prompt (Required if using Audio Prompt)",
|
| 289 |
+
placeholder="Enter text here...",
|
| 290 |
+
value="",
|
| 291 |
+
lines=5, # Increased lines
|
| 292 |
+
)
|
| 293 |
+
text_input = gr.Textbox(
|
| 294 |
+
label="Text To Generate",
|
| 295 |
+
placeholder="Enter text here...",
|
| 296 |
+
value=default_text,
|
| 297 |
+
lines=5, # Increased lines
|
| 298 |
+
)
|
| 299 |
+
with gr.Accordion("Generation Parameters", open=False):
|
| 300 |
+
max_new_tokens = gr.Slider(
|
| 301 |
+
label="Max New Tokens (Audio Length)",
|
| 302 |
+
minimum=860,
|
| 303 |
+
maximum=3072,
|
| 304 |
+
value=model.config.decoder_config.max_position_embeddings, # Use config default if available, else fallback
|
| 305 |
+
step=50,
|
| 306 |
+
info="Controls the maximum length of the generated audio (more tokens = longer audio).",
|
| 307 |
+
)
|
| 308 |
+
cfg_scale = gr.Slider(
|
| 309 |
+
label="CFG Scale (Guidance Strength)",
|
| 310 |
+
minimum=1.0,
|
| 311 |
+
maximum=5.0,
|
| 312 |
+
value=3.0, # Default from inference.py
|
| 313 |
+
step=0.1,
|
| 314 |
+
info="Higher values increase adherence to the text prompt.",
|
| 315 |
+
)
|
| 316 |
+
temperature = gr.Slider(
|
| 317 |
+
label="Temperature (Randomness)",
|
| 318 |
+
minimum=1.0,
|
| 319 |
+
maximum=2.5,
|
| 320 |
+
value=1.8, # Default from inference.py
|
| 321 |
+
step=0.05,
|
| 322 |
+
info="Lower values make the output more deterministic, higher values increase randomness.",
|
| 323 |
+
)
|
| 324 |
+
top_p = gr.Slider(
|
| 325 |
+
label="Top P (Nucleus Sampling)",
|
| 326 |
+
minimum=0.70,
|
| 327 |
+
maximum=1.0,
|
| 328 |
+
value=0.95, # Default from inference.py
|
| 329 |
+
step=0.01,
|
| 330 |
+
info="Filters vocabulary to the most likely tokens cumulatively reaching probability P.",
|
| 331 |
+
)
|
| 332 |
+
cfg_filter_top_k = gr.Slider(
|
| 333 |
+
label="CFG Filter Top K",
|
| 334 |
+
minimum=15,
|
| 335 |
+
maximum=100,
|
| 336 |
+
value=45,
|
| 337 |
+
step=1,
|
| 338 |
+
info="Top k filter for CFG guidance.",
|
| 339 |
+
)
|
| 340 |
+
speed_factor_slider = gr.Slider(
|
| 341 |
+
label="Speed Factor",
|
| 342 |
+
minimum=0.8,
|
| 343 |
+
maximum=1.0,
|
| 344 |
+
value=1.0,
|
| 345 |
+
step=0.02,
|
| 346 |
+
info="Adjusts the speed of the generated audio (1.0 = original speed).",
|
| 347 |
+
)
|
| 348 |
+
seed_input = gr.Number(
|
| 349 |
+
label="Generation Seed (Optional)",
|
| 350 |
+
value=-1,
|
| 351 |
+
precision=0, # No decimal points
|
| 352 |
+
step=1,
|
| 353 |
+
interactive=True,
|
| 354 |
+
info="Set a generation seed for reproducible outputs. Leave empty or -1 for random seed.",
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
run_button = gr.Button("Generate Audio", variant="primary")
|
| 358 |
+
|
| 359 |
+
with gr.Column(scale=1):
|
| 360 |
+
audio_output = gr.Audio(
|
| 361 |
+
label="Generated Audio",
|
| 362 |
+
type="numpy",
|
| 363 |
+
autoplay=False,
|
| 364 |
+
)
|
| 365 |
+
seed_output = gr.Textbox(label="Generation Seed", interactive=False)
|
| 366 |
+
console_output = gr.Textbox(label="Console Output Log", lines=10, interactive=False)
|
| 367 |
+
|
| 368 |
+
# Link button click to function
|
| 369 |
+
run_button.click(
|
| 370 |
+
fn=run_inference,
|
| 371 |
+
inputs=[
|
| 372 |
+
text_input,
|
| 373 |
+
audio_prompt_text_input,
|
| 374 |
+
audio_prompt_input,
|
| 375 |
+
max_new_tokens,
|
| 376 |
+
cfg_scale,
|
| 377 |
+
temperature,
|
| 378 |
+
top_p,
|
| 379 |
+
cfg_filter_top_k,
|
| 380 |
+
speed_factor_slider,
|
| 381 |
+
seed_input,
|
| 382 |
+
],
|
| 383 |
+
outputs=[
|
| 384 |
+
audio_output,
|
| 385 |
+
seed_output,
|
| 386 |
+
console_output,
|
| 387 |
+
], # Add status_output here if using it
|
| 388 |
+
api_name="generate_audio",
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
+
# Add examples (ensure the prompt path is correct or remove it if example file doesn't exist)
|
| 392 |
+
example_prompt_path = "./example_prompt.mp3" # Adjust if needed
|
| 393 |
+
examples_list = [
|
| 394 |
+
[
|
| 395 |
+
"[S1] Oh fire! Oh my goodness! What's the procedure? What to we do people? The smoke could be coming through an air duct! \n[S2] Oh my god! Okay.. it's happening. Everybody stay calm! \n[S1] What's the procedure... \n[S2] Everybody stay fucking calm!!!... Everybody fucking calm down!!!!! \n[S1] No! No! If you touch the handle, if its hot there might be a fire down the hallway! ",
|
| 396 |
+
None,
|
| 397 |
+
3072,
|
| 398 |
+
3.0,
|
| 399 |
+
1.8,
|
| 400 |
+
0.95,
|
| 401 |
+
45,
|
| 402 |
+
1.0,
|
| 403 |
+
],
|
| 404 |
+
[
|
| 405 |
+
"[S1] Open weights text to dialogue model. \n[S2] You get full control over scripts and voices. \n[S1] I'm biased, but I think we clearly won. \n[S2] Hard to disagree. (laughs) \n[S1] Thanks for listening to this demo. \n[S2] Try it now on Git hub and Hugging Face. \n[S1] If you liked our model, please give us a star and share to your friends. \n[S2] This was Nari Labs.",
|
| 406 |
+
example_prompt_path if Path(example_prompt_path).exists() else None,
|
| 407 |
+
3072,
|
| 408 |
+
3.0,
|
| 409 |
+
1.8,
|
| 410 |
+
0.95,
|
| 411 |
+
45,
|
| 412 |
+
1.0,
|
| 413 |
+
],
|
| 414 |
+
]
|
| 415 |
+
|
| 416 |
+
if examples_list:
|
| 417 |
+
gr.Examples(
|
| 418 |
+
examples=examples_list,
|
| 419 |
+
inputs=[
|
| 420 |
+
text_input,
|
| 421 |
+
audio_prompt_input,
|
| 422 |
+
max_new_tokens,
|
| 423 |
+
cfg_scale,
|
| 424 |
+
temperature,
|
| 425 |
+
top_p,
|
| 426 |
+
cfg_filter_top_k,
|
| 427 |
+
speed_factor_slider,
|
| 428 |
+
seed_input,
|
| 429 |
+
],
|
| 430 |
+
outputs=[audio_output],
|
| 431 |
+
fn=run_inference,
|
| 432 |
+
cache_examples=False,
|
| 433 |
+
label="Examples (Click to Run)",
|
| 434 |
+
)
|
| 435 |
+
else:
|
| 436 |
+
gr.Markdown("_(No examples configured or example prompt file missing)_")
|
| 437 |
+
|
| 438 |
+
# --- Launch the App ---
|
| 439 |
+
if __name__ == "__main__":
|
| 440 |
+
print("Launching Gradio interface...")
|
| 441 |
+
|
| 442 |
+
# set `GRADIO_SERVER_NAME`, `GRADIO_SERVER_PORT` env vars to override default values
|
| 443 |
+
# use `GRADIO_SERVER_NAME=0.0.0.0` for Docker
|
| 444 |
+
demo.launch(share=args.share)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.40.0
|
| 2 |
+
torch
|
| 3 |
+
gradio==4.26.0
|
| 4 |
+
soundfile
|