Spaces:
Sleeping
Sleeping
File size: 2,585 Bytes
784afc6 bccef6e 784afc6 8d4e3a2 d865f27 784afc6 1e35c36 cd73846 1e35c36 cd73846 784afc6 bccef6e 2a78fb3 bccef6e 8d4e3a2 bccef6e 8d4e3a2 784afc6 d865f27 1e35c36 d865f27 2a78fb3 d865f27 1e35c36 2a78fb3 d865f27 2a78fb3 d865f27 2a78fb3 d865f27 1e35c36 2a78fb3 d865f27 3e8d071 784afc6 cd73846 784afc6 8d4e3a2 784afc6 8d4e3a2 784afc6 5c4b08e db43810 784afc6 1e35c36 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import gradio as gr
from transformers import pipeline, AutoTokenizer
import torch
# Verificando se a GPU está disponível
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Carregando o modelo Whisper para transcrição de áudio
transcriber = pipeline(
task="automatic-speech-recognition",
model="openai/whisper-medium",
device=device,
chunk_length_s=30, # Definindo chunk_length_s para 30 segundos
stride_length_s=5,
generate_kwargs={"language": "Portuguese", "task": "transcribe"}
)
# Carregando o tokenizer lento para o classificador
tokenizer = AutoTokenizer.from_pretrained(
"joeddav/xlm-roberta-large-xnli",
use_fast=False
)
# Carregando o pipeline de classificação zero-shot com o tokenizer lento
classifier = pipeline(
"zero-shot-classification",
model="joeddav/xlm-roberta-large-xnli",
tokenizer=tokenizer,
device=device
)
def transcribe_and_analyze(audio_file):
progress = gr.Progress(track_tqdm=True)
progress(0, desc="Iniciando transcrição...")
# Transcrevendo o áudio com return_timestamps=True
transcription_result = transcriber(audio_file, return_timestamps=True)
transcription = transcription_result["text"]
progress(50, desc="Transcrição concluída. Analisando emoções...")
# Lista de emoções para a classificação
emotions = ["alegria", "tristeza", "raiva", "nojo", "medo", "ansiedade", "vergonha", "tédio", "inveja"]
# Realizando a classificação zero-shot
classification = classifier(transcription, emotions, multi_label=True)
# Formatando os resultados
results = []
for label, score in zip(classification["labels"], classification["scores"]):
results.append(f"{label.capitalize()}: {score:.2f}")
# Ordenando os resultados por score decrescente
results.sort(key=lambda x: float(x.split(": ")[1]), reverse=True)
# Unindo os resultados em uma string
emotion_output = "\n".join(results)
progress(100, desc="Processamento concluído.")
return transcription, emotion_output
# Criando a interface Gradio com barra de progresso
interface = gr.Interface(
fn=transcribe_and_analyze,
inputs=gr.Audio(type="filepath", label="Faça upload do seu áudio"),
outputs=[
gr.Textbox(label="Transcrição do Áudio"),
gr.Textbox(label="Emoções Detectadas")
],
title="Voxsense 🗣️❣️",
description="Envie um arquivo de áudio para transcrição e análise de emoções.",
theme="default"
)
if __name__ == "__main__":
interface.queue().launch() |