Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Recupera il token
|
| 6 |
API_TOKEN = os.environ.get("htoken")
|
| 7 |
|
| 8 |
-
# Dizionario dei migliori modelli gratuiti e Open Source disponibili per Text-to-Speech
|
| 9 |
MODELLI_DISPONIBILI = {
|
| 10 |
"Suno Bark Small (Multilingua, Espressivo ma lento)": "suno/bark-small",
|
| 11 |
"Microsoft SpeechT5 (Inglese, Veloce e stabilissimo)": "microsoft/speecht5_tts",
|
|
@@ -15,47 +14,54 @@ MODELLI_DISPONIBILI = {
|
|
| 15 |
}
|
| 16 |
|
| 17 |
def genera_audio(testo, nome_modello):
|
| 18 |
-
# 1. Controllo hardware: Verifichiamo che il container abbia letto il token
|
| 19 |
if not API_TOKEN:
|
| 20 |
-
return "ERRORE
|
| 21 |
-
|
| 22 |
if not testo.strip():
|
| 23 |
-
return "Errore: Inserisci del testo.", None
|
| 24 |
|
| 25 |
-
# 2. Recupera l'ID esatto del modello in base alla scelta dell'utente
|
| 26 |
model_id = MODELLI_DISPONIBILI.get(nome_modello)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Chiamata API ufficiale per l'audio
|
| 33 |
-
audio_bytes = client.text_to_speech(testo, model=model_id)
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
-
|
| 39 |
-
messaggio_errore = f"Errore durante l'inferenza: {errore_str}\n\n"
|
| 40 |
-
|
| 41 |
-
# Analisi degli errori più comuni per aiutarti nel debug
|
| 42 |
-
if "401" in errore_str or "Invalid username" in errore_str:
|
| 43 |
-
messaggio_errore += "💡 SOLUZIONE: Il tuo Token non ha i permessi corretti o lo Space non è stato riavviato. Controlla che il token sia 'Finegrained' e fai Restart."
|
| 44 |
-
elif "503" in errore_str or "loading" in errore_str.lower():
|
| 45 |
-
messaggio_errore += "💡 SOLUZIONE: Questo specifico modello è in letargo sui server. Riprova tra 30 secondi o scegli un altro modello dal menu."
|
| 46 |
-
|
| 47 |
-
return messaggio_errore, None
|
| 48 |
|
| 49 |
-
#
|
| 50 |
with gr.Blocks(theme=gr.themes.Soft()) as interfaccia:
|
| 51 |
-
gr.Markdown("# Generatore Audio IA
|
| 52 |
-
gr.Markdown("
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
testo_input = gr.Textbox(label="Testo da generare", placeholder="Scrivi qui il tuo prompt...", lines=3)
|
| 56 |
modello_dropdown = gr.Dropdown(
|
| 57 |
choices=list(MODELLI_DISPONIBILI.keys()),
|
| 58 |
-
value="
|
| 59 |
label="Scegli il Modello IA"
|
| 60 |
)
|
| 61 |
|
|
@@ -64,12 +70,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as interfaccia:
|
|
| 64 |
status_output = gr.Textbox(label="Stato Console", lines=4)
|
| 65 |
audio_output = gr.Audio(label="Player Audio", autoplay=True)
|
| 66 |
|
| 67 |
-
# Collegamento dell'interfaccia alla logica Python
|
| 68 |
pulsante.click(
|
| 69 |
fn=genera_audio,
|
| 70 |
inputs=[testo_input, modello_dropdown],
|
| 71 |
outputs=[status_output, audio_output]
|
| 72 |
)
|
| 73 |
|
| 74 |
-
# Avvio dell'app web
|
| 75 |
interfaccia.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Recupera il token dal Secret creato precedentemente
|
| 6 |
API_TOKEN = os.environ.get("htoken")
|
| 7 |
|
|
|
|
| 8 |
MODELLI_DISPONIBILI = {
|
| 9 |
"Suno Bark Small (Multilingua, Espressivo ma lento)": "suno/bark-small",
|
| 10 |
"Microsoft SpeechT5 (Inglese, Veloce e stabilissimo)": "microsoft/speecht5_tts",
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def genera_audio(testo, nome_modello):
|
|
|
|
| 17 |
if not API_TOKEN:
|
| 18 |
+
return "ERRORE: Token mancante. Assicurati di aver salvato il Secret 'htoken'.", None
|
| 19 |
+
|
| 20 |
if not testo.strip():
|
| 21 |
+
return "Errore: Inserisci del testo da pronunciare.", None
|
| 22 |
|
|
|
|
| 23 |
model_id = MODELLI_DISPONIBILI.get(nome_modello)
|
| 24 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
| 25 |
+
|
| 26 |
+
# Intestazioni per la richiesta HTTP diretta, aggirando l'InferenceClient
|
| 27 |
+
headers = {
|
| 28 |
+
"Authorization": f"Bearer {API_TOKEN}",
|
| 29 |
+
"Content-Type": "application/json"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
try:
|
| 33 |
+
# Effettua la richiesta POST forzando l'inferenza
|
| 34 |
+
response = requests.post(api_url, headers=headers, json={"inputs": testo}, timeout=60)
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Gestione degli errori del server
|
| 37 |
+
if response.status_code == 503:
|
| 38 |
+
dati = response.json()
|
| 39 |
+
tempo = round(dati.get('estimated_time', 20))
|
| 40 |
+
return f"ATTESA: Il modello si sta accendendo (Errore 503). Riprova tra {tempo} secondi.", None
|
| 41 |
+
|
| 42 |
+
if response.status_code != 200:
|
| 43 |
+
return f"ERRORE SERVER {response.status_code}: {response.text}", None
|
| 44 |
+
|
| 45 |
+
# Salva i byte ricevuti in un file audio fisico temporaneo (metodo più stabile per Gradio)
|
| 46 |
+
file_path = "output.wav"
|
| 47 |
+
with open(file_path, "wb") as f:
|
| 48 |
+
f.write(response.content)
|
| 49 |
+
|
| 50 |
+
return f"Successo! Audio generato con il modello: {model_id}", file_path
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
+
return f"Errore di rete/connessione: {str(e)}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Interfaccia Utente
|
| 56 |
with gr.Blocks(theme=gr.themes.Soft()) as interfaccia:
|
| 57 |
+
gr.Markdown("# Generatore Audio IA (Metodo HTTP Diretto)")
|
| 58 |
+
gr.Markdown("Questa versione utilizza richieste HTTP pure per bypassare i blocchi della libreria ufficiale.")
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
testo_input = gr.Textbox(label="Testo da generare", placeholder="Scrivi qui il tuo prompt...", lines=3)
|
| 62 |
modello_dropdown = gr.Dropdown(
|
| 63 |
choices=list(MODELLI_DISPONIBILI.keys()),
|
| 64 |
+
value="Microsoft SpeechT5 (Inglese, Veloce e stabilissimo)",
|
| 65 |
label="Scegli il Modello IA"
|
| 66 |
)
|
| 67 |
|
|
|
|
| 70 |
status_output = gr.Textbox(label="Stato Console", lines=4)
|
| 71 |
audio_output = gr.Audio(label="Player Audio", autoplay=True)
|
| 72 |
|
|
|
|
| 73 |
pulsante.click(
|
| 74 |
fn=genera_audio,
|
| 75 |
inputs=[testo_input, modello_dropdown],
|
| 76 |
outputs=[status_output, audio_output]
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
interfaccia.launch()
|