Spaces:
Sleeping

falapet-api / app.py
netolima0210's picture
Update app.py
6a72007 verified
Raw History Blame Contribute Delete
3.61 kB
import gradio as gr
from transformers import pipeline
import numpy as np
model = pipeline("audio-classification",
model="ardneebwar/wav2vec2-animal-sounds-finetuned-hubert-finetuned-animals")
frases_pt = {
"dog": {
"happy": "Quero brincar! 🐾",
"alert": "Tem alguém aí? 👀",
"anxious": "Fica comigo! 🥺",
"sad": "Tô com saudade... 😢",
"crow": "Tô com fome! 🍖",
"cat": "Quero carinho! 🐾",
"dog": "Tô feliz aqui! 😄",
"rooster": "Acorda! Vamos passear! 🦮",
"pig": "Tô entediado... 😴",
"frog": "Tem visita chegando! 🔔",
"cow": "Preciso de você agora! 🥺",
"sheep": "Estou com medo! 😨",
"hen": "Me leva pra fora! 🌳",
"duck": "Quero meu brinquedo! 🧸",
"horse": "Vamos correr! 🏃",
},
"cat": {
"happy": "Me faz carinho! 😸",
"alert": "Vi algo estranho! 🐱",
"anxious": "Não me deixa sozinho! 😿",
"sad": "Quero comida! 🍽️",
"crow": "Tô com fome! 🍖",
"cat": "Me ignora não! 😾",
"dog": "Quero meu cantinho! 🛋️",
"rooster": "Me acorda não! 😤",
"pig": "Tô sonolento... 😴",
"frog": "Tem algo me incomodando! 🙀",
"cow": "Quero carinho agora! 🥺",
"sheep": "Estou com frio! 🥶",
"hen": "Me deixa em paz! 😼",
"duck": "Tô entediado! 🐾",
"horse": "Quero brincar! 🐱",
}
}
frases_en = {
"dog": {
"happy": "Let's play! 🐾",
"alert": "Someone's there! 👀",
"anxious": "Stay with me! 🥺",
"sad": "I miss you... 😢",
"crow": "I'm hungry! 🍖",
"cat": "Pet me please! 🐾",
"dog": "I'm so happy! 😄",
"rooster": "Wake up! Let's walk! 🦮",
"pig": "I'm bored... 😴",
"frog": "Someone's coming! 🔔",
"cow": "I need you now! 🥺",
"sheep": "I'm scared! 😨",
"hen": "Take me outside! 🌳",
"duck": "I want my toy! 🧸",
"horse": "Let's run! 🏃",
},
"cat": {
"happy": "Pet me! 😸",
"alert": "I saw something! 🐱",
"anxious": "Don't leave me! 😿",
"sad": "Feed me! 🍽️",
"crow": "I'm hungry! 🍖",
"cat": "Don't ignore me! 😾",
"dog": "I need my space! 🛋️",
"rooster": "Don't wake me! 😤",
"pig": "I'm sleepy... 😴",
"frog": "Something's bothering me! 🙀",
"cow": "I want cuddles! 🥺",
"sheep": "I'm cold! 🥶",
"hen": "Leave me alone! 😼",
"duck": "I'm bored! 🐾",
"horse": "Let's play! 🐱",
}
}
def traduzir(audio, animal, idioma):
try:
sr, data = audio
if data.dtype != np.float32:
data = data.astype(np.float32) / np.iinfo(data.dtype).max
resultado = model({"sampling_rate": sr, "raw": data})
emocao = resultado[0]["label"].lower()
frases = frases_pt if idioma == "pt" else frases_en
tipo = "dog" if animal == "cao" else "cat"
frase = frases[tipo].get(emocao, "Hmm... 🤔")
return {"frase": frase, "emocao": emocao}
except Exception as e:
return {"frase": "Erro ao processar áudio", "emocao": str(e)}
demo = gr.Interface(
fn=traduzir,
inputs=[
gr.Audio(type="numpy"),
gr.Radio(["cao", "gato"], value="cao", label="Animal"),
gr.Radio(["pt", "en"], value="pt", label="Idioma")
],
outputs="json"
)
demo.launch()