marioluciofjr commited on
Commit
8d4e3a2
·
verified ·
1 Parent(s): beacf7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
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=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
  )
11
 
12
- # Carregando o modelo de análise de sentimentos em português
13
- sentiment_analyzer = pipeline(
14
- "sentiment-analysis",
15
- model="nlptown/bert-base-multilingual-uncased-sentiment"
 
16
  )
17
 
18
  def transcribe_and_analyze(audio_file):
19
  """
20
- Recebe um arquivo de áudio, transcreve e analisa o sentimento.
21
  """
22
  # Transcrevendo o áudio
23
  transcription = transcriber(audio_file)["text"]
24
 
25
- # Analisando o sentimento da transcrição
26
- sentiment = sentiment_analyzer(transcription)[0]
 
 
 
 
 
 
 
 
27
 
28
- # Formatando a saída do sentimento
29
- sentiment_output = f"{sentiment['label']} (confiança: {sentiment['score']:.2f})"
30
 
31
- return transcription, sentiment_output
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"), # Removed source argument
37
  outputs=[
38
  gr.Textbox(label="Transcrição do Áudio"),
39
- gr.Textbox(label="Análise de Sentimento")
40
  ],
41
- title="Transcrição e Análise de Sentimentos de Áudio",
42
- description="Envie um arquivo de áudio de até 1 hora para transcrição e análise de sentimentos.",
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