Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,11 +5,14 @@ import torch
|
|
5 |
# Verificando se a GPU está disponível
|
6 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
7 |
|
8 |
-
# Carregando o modelo Whisper para transcrição de áudio
|
9 |
transcriber = pipeline(
|
10 |
task="automatic-speech-recognition",
|
11 |
-
model="openai/whisper-
|
12 |
-
device=device
|
|
|
|
|
|
|
13 |
)
|
14 |
|
15 |
# Carregando o tokenizer lento para o classificador
|
@@ -27,36 +30,36 @@ classifier = pipeline(
|
|
27 |
)
|
28 |
|
29 |
def transcribe_and_analyze(audio_file):
|
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 |
-
# Criando a interface Gradio
|
60 |
interface = gr.Interface(
|
61 |
fn=transcribe_and_analyze,
|
62 |
inputs=gr.Audio(type="filepath", label="Faça upload do seu áudio"),
|
|
|
5 |
# Verificando se a GPU está disponível
|
6 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
7 |
|
8 |
+
# Carregando o modelo Whisper avançado para transcrição de áudio
|
9 |
transcriber = pipeline(
|
10 |
task="automatic-speech-recognition",
|
11 |
+
model="openai/whisper-large-v2", # Modelo mais avançado
|
12 |
+
device=device,
|
13 |
+
chunk_length_s=30,
|
14 |
+
stride_length_s=5,
|
15 |
+
generate_kwargs={"language": "Portuguese", "task": "transcribe"}
|
16 |
)
|
17 |
|
18 |
# Carregando o tokenizer lento para o classificador
|
|
|
30 |
)
|
31 |
|
32 |
def transcribe_and_analyze(audio_file):
|
33 |
+
with gr.Progress(track_tqdm=True) as progress:
|
34 |
+
progress(0, desc="Iniciando transcrição...")
|
35 |
+
|
36 |
+
# Transcrevendo o áudio
|
37 |
+
transcription_result = transcriber(audio_file)
|
38 |
+
transcription = transcription_result["text"]
|
39 |
+
progress(50, desc="Transcrição concluída. Analisando emoções...")
|
40 |
+
|
41 |
+
# Lista atualizada de emoções para a classificação
|
42 |
+
emotions = ["alegria", "tristeza", "raiva", "nojo", "medo", "ansiedade", "vergonha", "tédio", "inveja"]
|
43 |
+
|
44 |
+
# Realizando a classificação zero-shot na transcrição
|
45 |
+
classification = classifier(transcription, emotions, multi_label=True)
|
46 |
+
|
47 |
+
# Formatando os resultados
|
48 |
+
results = []
|
49 |
+
for label, score in zip(classification["labels"], classification["scores"]):
|
50 |
+
results.append(f"{label.capitalize()}: {score:.2f}")
|
51 |
+
|
52 |
+
# Ordenando os resultados por score decrescente
|
53 |
+
results.sort(key=lambda x: float(x.split(": ")[1]), reverse=True)
|
54 |
+
|
55 |
+
# Unindo os resultados em uma string
|
56 |
+
emotion_output = "\n".join(results)
|
57 |
+
|
58 |
+
progress(100, desc="Processamento concluído.")
|
59 |
+
|
60 |
+
return transcription, emotion_output
|
61 |
|
62 |
+
# Criando a interface Gradio com barra de progresso
|
63 |
interface = gr.Interface(
|
64 |
fn=transcribe_and_analyze,
|
65 |
inputs=gr.Audio(type="filepath", label="Faça upload do seu áudio"),
|