Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from TTS.api import TTS
|
| 5 |
+
import noisereduce as nr
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
import numpy as np
|
| 8 |
+
import time
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
# Agreements
|
| 12 |
+
os.environ['COQUI_TOS_AGREED'] = '1'
|
| 13 |
+
|
| 14 |
+
# Load Model
|
| 15 |
+
print("⏳ Loading Engine...")
|
| 16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
+
# Using a faster model for the first run
|
| 18 |
+
tts = TTS("tts_models/multilingual/multi-dataset/your_tts").to(device)
|
| 19 |
+
print("✅ Engine Loaded!")
|
| 20 |
+
|
| 21 |
+
def generate_api_voice(text, reference_audio):
|
| 22 |
+
if not text or not reference_audio:
|
| 23 |
+
return None, "Error: Text aur Reference Audio dono zaroori hain!"
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# 1. Noise Reduction
|
| 27 |
+
data, rate = sf.read(reference_audio)
|
| 28 |
+
if len(data.shape) > 1: data = data.mean(axis=1)
|
| 29 |
+
clean_data = nr.reduce_noise(y=data, sr=rate)
|
| 30 |
+
ref_path = "clean_ref.wav"
|
| 31 |
+
sf.write(ref_path, clean_data, rate)
|
| 32 |
+
|
| 33 |
+
# 2. Chunking Text
|
| 34 |
+
output_file = "output_voice.wav"
|
| 35 |
+
tts.tts_to_file(text=text, speaker_wav=ref_path, language="en", file_path=output_file)
|
| 36 |
+
|
| 37 |
+
return output_file, "✅ Audio Generated Successfully!"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return None, f"⚠️ Error: {str(e)}"
|
| 40 |
+
|
| 41 |
+
# Interface
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=generate_api_voice,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Textbox(label="Script daalo", placeholder="Yahan apna text likho..."),
|
| 46 |
+
gr.Audio(type="filepath", label="Voice Sample upload karo")
|
| 47 |
+
],
|
| 48 |
+
outputs=[
|
| 49 |
+
gr.Audio(label="Glowmation AI Voice"),
|
| 50 |
+
gr.Textbox(label="Status")
|
| 51 |
+
],
|
| 52 |
+
title="🎙️ Glowmation AI Engine v1",
|
| 53 |
+
description="Permanent API for Glowmation App"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
iface.launch()
|