Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,44 +2,56 @@ import gradio as gr
|
|
2 |
from transformers import pipeline
|
3 |
import torch
|
4 |
|
|
|
|
|
|
|
5 |
# Carregando o modelo Whisper para transcrição de áudio
|
6 |
transcriber = pipeline(
|
7 |
task="automatic-speech-recognition",
|
8 |
model="openai/whisper-small",
|
9 |
-
device=
|
10 |
)
|
11 |
|
12 |
-
# Carregando o
|
13 |
-
|
14 |
-
"
|
15 |
-
model="
|
|
|
16 |
)
|
17 |
|
18 |
def transcribe_and_analyze(audio_file):
|
19 |
"""
|
20 |
-
Recebe um arquivo de áudio, transcreve e analisa
|
21 |
"""
|
22 |
# Transcrevendo o áudio
|
23 |
transcription = transcriber(audio_file)["text"]
|
24 |
|
25 |
-
#
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
|
31 |
-
return transcription,
|
32 |
|
33 |
# Criando a interface Gradio
|
34 |
interface = gr.Interface(
|
35 |
fn=transcribe_and_analyze,
|
36 |
-
inputs=gr.Audio(type="filepath", label="Faça upload do seu áudio"),
|
37 |
outputs=[
|
38 |
gr.Textbox(label="Transcrição do Áudio"),
|
39 |
-
gr.Textbox(label="
|
40 |
],
|
41 |
-
title="
|
42 |
-
description="Envie um arquivo de áudio de até 1 hora para transcrição e análise de
|
43 |
theme="default"
|
44 |
)
|
45 |
|
|
|
2 |
from transformers import pipeline
|
3 |
import torch
|
4 |
|
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-small",
|
12 |
+
device=device
|
13 |
)
|
14 |
|
15 |
+
# Carregando o pipeline de classificação zero-shot
|
16 |
+
classifier = pipeline(
|
17 |
+
"zero-shot-classification",
|
18 |
+
model="joeddav/xlm-roberta-large-xnli",
|
19 |
+
device=device
|
20 |
)
|
21 |
|
22 |
def transcribe_and_analyze(audio_file):
|
23 |
"""
|
24 |
+
Recebe um arquivo de áudio, transcreve e analisa as emoções presentes.
|
25 |
"""
|
26 |
# Transcrevendo o áudio
|
27 |
transcription = transcriber(audio_file)["text"]
|
28 |
|
29 |
+
# Lista de emoções para a classificação
|
30 |
+
emotions = ["medo", "rancor", "ódio", "raiva", "felicidade", "amor", "tristeza", "ansiedade", "inveja", "sarcasmo", "vergonha"]
|
31 |
+
|
32 |
+
# Realizando a classificação zero-shot na transcrição
|
33 |
+
classification = classifier(transcription, emotions, multi_label=True)
|
34 |
+
|
35 |
+
# Formatando os resultados
|
36 |
+
results = []
|
37 |
+
for label, score in zip(classification["labels"], classification["scores"]):
|
38 |
+
results.append(f"{label}: {score:.2f}")
|
39 |
|
40 |
+
# Unindo os resultados em uma string
|
41 |
+
emotion_output = "\n".join(results)
|
42 |
|
43 |
+
return transcription, emotion_output
|
44 |
|
45 |
# Criando a interface Gradio
|
46 |
interface = gr.Interface(
|
47 |
fn=transcribe_and_analyze,
|
48 |
+
inputs=gr.Audio(type="filepath", label="Faça upload do seu áudio"),
|
49 |
outputs=[
|
50 |
gr.Textbox(label="Transcrição do Áudio"),
|
51 |
+
gr.Textbox(label="Emoções Detectadas")
|
52 |
],
|
53 |
+
title="TranscriSentimento",
|
54 |
+
description="Envie um arquivo de áudio de até 1 hora para transcrição e análise de emoções.",
|
55 |
theme="default"
|
56 |
)
|
57 |
|