Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from kokoro_onnx import Kokoro
|
| 3 |
import soundfile as sf
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
sf.write(
|
| 18 |
-
return
|
| 19 |
|
| 20 |
-
# Interface
|
| 21 |
-
|
| 22 |
fn=narrar_biblia,
|
| 23 |
-
inputs=gr.JSON(label="
|
| 24 |
-
outputs=gr.Audio(label="
|
| 25 |
-
api_name="
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from kokoro_onnx import Kokoro
|
| 3 |
import soundfile as sf
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
+
# URLs dos arquivos oficiais (Kokoro 82M - O melhor para CPU em 2026)
|
| 8 |
+
MODEL_URL = "https://huggingface.co"
|
| 9 |
+
VOICES_URL = "https://huggingface.co"
|
| 10 |
|
| 11 |
+
def download_file(url, filename):
|
| 12 |
+
if not os.path.exists(filename):
|
| 13 |
+
print(f"Baixando {filename}...")
|
| 14 |
+
response = requests.get(url)
|
| 15 |
+
with open(filename, "wb") as f:
|
| 16 |
+
f.write(response.content)
|
| 17 |
+
print(f"{filename} baixado com sucesso.")
|
| 18 |
+
|
| 19 |
+
# Baixa os arquivos necessários para a memória do Space
|
| 20 |
+
download_file(MODEL_URL, "kokoro-v0_19.onnx")
|
| 21 |
+
download_file(VOICES_URL, "voices.bin")
|
| 22 |
+
|
| 23 |
+
# Inicializa o modelo
|
| 24 |
+
model = Kokoro("kokoro-v0_19.onnx", "voices.bin")
|
| 25 |
+
|
| 26 |
+
def narrar_biblia(data):
|
| 27 |
+
# O JSON deve vir no formato {"texto": "Sua historia aqui"}
|
| 28 |
+
texto = data.get("texto", "")
|
| 29 |
+
if not texto:
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
# 'am_michael' é a voz masculina ideal (estilo narração épica/bíblica)
|
| 33 |
+
samples, sample_rate = model.create(texto, voice="am_michael", speed=1.0)
|
| 34 |
|
| 35 |
+
output_path = "narracao.wav"
|
| 36 |
+
sf.write(output_path, samples, sample_rate)
|
| 37 |
+
return output_path
|
| 38 |
|
| 39 |
+
# Interface configurada como API JSON
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
fn=narrar_biblia,
|
| 42 |
+
inputs=gr.JSON(label="Input JSON"),
|
| 43 |
+
outputs=gr.Audio(label="Audio Gerado"),
|
| 44 |
+
api_name="predict"
|
| 45 |
)
|
| 46 |
|
| 47 |
+
demo.launch()
|