Update app.py
Browse files
app.py
CHANGED
|
@@ -2,34 +2,28 @@ import gradio as gr
|
|
| 2 |
import joblib
|
| 3 |
import whisper
|
| 4 |
import re
|
| 5 |
-
import nltk
|
| 6 |
-
from nltk.corpus import stopwords
|
| 7 |
|
| 8 |
-
#
|
| 9 |
# 1. Carregar modelo de sentimento + TF-IDF
|
| 10 |
-
#
|
|
|
|
| 11 |
clf = joblib.load("sentiment_model_logreg.pkl")
|
| 12 |
vect = joblib.load("tfidf_vect.pkl")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
nltk.download("stopwords", quiet=True)
|
| 16 |
-
stop_words = set(stopwords.words("portuguese"))
|
| 17 |
-
|
| 18 |
-
def clean_text(text):
|
| 19 |
text = text.lower()
|
| 20 |
-
text = re.sub(r"[^a-
|
| 21 |
-
|
| 22 |
-
return " ".join(words)
|
| 23 |
|
| 24 |
def predict_sentiment(text):
|
| 25 |
-
text_clean =
|
| 26 |
X = vect.transform([text_clean])
|
| 27 |
pred = clf.predict(X)[0]
|
| 28 |
return "POSITIVO" if pred == 1 else "NEGATIVO"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
# 2. Carregar modelo Whisper
|
| 32 |
-
#
|
| 33 |
|
| 34 |
whisper_model = whisper.load_model("small")
|
| 35 |
|
|
@@ -37,37 +31,39 @@ def audio_to_text(audio_path):
|
|
| 37 |
result = whisper_model.transcribe(audio_path, fp16=False)
|
| 38 |
return result["text"]
|
| 39 |
|
| 40 |
-
#
|
| 41 |
# 3. Função final do pipeline
|
| 42 |
-
#
|
| 43 |
|
| 44 |
def pipeline(lista_audios):
|
| 45 |
if not lista_audios:
|
| 46 |
return []
|
| 47 |
-
|
| 48 |
-
resultados = []
|
| 49 |
|
|
|
|
| 50 |
for audio in lista_audios:
|
| 51 |
texto = audio_to_text(audio)
|
| 52 |
sentimento = predict_sentiment(texto)
|
| 53 |
resultados.append([audio, texto, sentimento])
|
| 54 |
-
|
| 55 |
-
return resultados
|
| 56 |
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
# 4. Interface Gradio
|
| 60 |
-
#
|
| 61 |
|
| 62 |
app = gr.Interface(
|
| 63 |
fn=pipeline,
|
| 64 |
-
inputs=gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
outputs=gr.Dataframe(
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
title="Análise de Sentimento por Áudio",
|
| 70 |
-
description="
|
| 71 |
)
|
| 72 |
|
| 73 |
-
app.launch()
|
|
|
|
| 2 |
import joblib
|
| 3 |
import whisper
|
| 4 |
import re
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# ------------------------------
|
| 7 |
# 1. Carregar modelo de sentimento + TF-IDF
|
| 8 |
+
# ------------------------------
|
| 9 |
+
|
| 10 |
clf = joblib.load("sentiment_model_logreg.pkl")
|
| 11 |
vect = joblib.load("tfidf_vect.pkl")
|
| 12 |
|
| 13 |
+
def preprocess(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
text = text.lower()
|
| 15 |
+
text = re.sub(r"[^a-zà-ú0-9 ]", "", text)
|
| 16 |
+
return text
|
|
|
|
| 17 |
|
| 18 |
def predict_sentiment(text):
|
| 19 |
+
text_clean = preprocess(text)
|
| 20 |
X = vect.transform([text_clean])
|
| 21 |
pred = clf.predict(X)[0]
|
| 22 |
return "POSITIVO" if pred == 1 else "NEGATIVO"
|
| 23 |
|
| 24 |
+
# ------------------------------
|
| 25 |
# 2. Carregar modelo Whisper
|
| 26 |
+
# ------------------------------
|
| 27 |
|
| 28 |
whisper_model = whisper.load_model("small")
|
| 29 |
|
|
|
|
| 31 |
result = whisper_model.transcribe(audio_path, fp16=False)
|
| 32 |
return result["text"]
|
| 33 |
|
| 34 |
+
# ------------------------------
|
| 35 |
# 3. Função final do pipeline
|
| 36 |
+
# ------------------------------
|
| 37 |
|
| 38 |
def pipeline(lista_audios):
|
| 39 |
if not lista_audios:
|
| 40 |
return []
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
resultados = []
|
| 43 |
for audio in lista_audios:
|
| 44 |
texto = audio_to_text(audio)
|
| 45 |
sentimento = predict_sentiment(texto)
|
| 46 |
resultados.append([audio, texto, sentimento])
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
return resultados
|
| 49 |
|
| 50 |
+
# ------------------------------
|
| 51 |
# 4. Interface Gradio
|
| 52 |
+
# ------------------------------
|
| 53 |
|
| 54 |
app = gr.Interface(
|
| 55 |
fn=pipeline,
|
| 56 |
+
inputs=gr.Audio(
|
| 57 |
+
type="filepath",
|
| 58 |
+
label="Envie seus áudios WAV",
|
| 59 |
+
multiple=True
|
| 60 |
+
),
|
| 61 |
outputs=gr.Dataframe(
|
| 62 |
+
headers=["Arquivo", "Texto reconhecido", "Sentimento"],
|
| 63 |
+
label="Resultados"
|
| 64 |
+
),
|
| 65 |
title="Análise de Sentimento por Áudio",
|
| 66 |
+
description="Envie vários áudios WAV. O sistema transcreve cada um deles com Whisper e analisa o sentimento."
|
| 67 |
)
|
| 68 |
|
| 69 |
+
app.launch()
|