Adicionado POC do serviço de audio (Adicionado Roberta)
Browse files- app/api.py +2 -1
- app/audio_processor.py +7 -8
- app/sentiment_model.py +15 -22
app/api.py
CHANGED
|
@@ -41,7 +41,8 @@ def analyze(request: Request, data: dict):
|
|
| 41 |
try:
|
| 42 |
result = process_audio(
|
| 43 |
video_base64=video_base64,
|
| 44 |
-
audio_base64=audio_base64
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
log("Finalizado analise de audio", logger=logger)
|
|
|
|
| 41 |
try:
|
| 42 |
result = process_audio(
|
| 43 |
video_base64=video_base64,
|
| 44 |
+
audio_base64=audio_base64,
|
| 45 |
+
logger=logger
|
| 46 |
)
|
| 47 |
|
| 48 |
log("Finalizado analise de audio", logger=logger)
|
app/audio_processor.py
CHANGED
|
@@ -3,6 +3,7 @@ import tempfile
|
|
| 3 |
import os
|
| 4 |
from app.config import WHISPER_MODEL, WHISPER_MODEL_PATH
|
| 5 |
os.environ["XDG_CACHE_HOME"] = str(WHISPER_MODEL_PATH) # "./models"
|
|
|
|
| 6 |
from app.sentiment_model import analyze_sentiment
|
| 7 |
import whisper
|
| 8 |
from moviepy.editor import VideoFileClip
|
|
@@ -41,7 +42,7 @@ def extract_audio_from_video(video_path):
|
|
| 41 |
# -------------------------
|
| 42 |
# 🔥 PROCESSAMENTO PRINCIPAL
|
| 43 |
# -------------------------
|
| 44 |
-
def process_audio(video_base64=None, audio_base64=None):
|
| 45 |
|
| 46 |
video_path = None
|
| 47 |
audio_path = None
|
|
@@ -52,14 +53,14 @@ def process_audio(video_base64=None, audio_base64=None):
|
|
| 52 |
# -------------------------
|
| 53 |
|
| 54 |
if audio_base64:
|
| 55 |
-
|
| 56 |
audio_path = save_base64_to_file(audio_base64, ".wav")
|
| 57 |
|
| 58 |
elif video_base64:
|
| 59 |
-
|
| 60 |
video_path = save_base64_to_file(video_base64, ".mp4")
|
| 61 |
|
| 62 |
-
|
| 63 |
audio_path = extract_audio_from_video(video_path)
|
| 64 |
|
| 65 |
else:
|
|
@@ -69,7 +70,7 @@ def process_audio(video_base64=None, audio_base64=None):
|
|
| 69 |
# 🎯 2. WHISPER
|
| 70 |
# -------------------------
|
| 71 |
|
| 72 |
-
|
| 73 |
|
| 74 |
result = whisper_model.transcribe(
|
| 75 |
audio_path,
|
|
@@ -78,15 +79,13 @@ def process_audio(video_base64=None, audio_base64=None):
|
|
| 78 |
fp16=False # importante para CPU
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# text = result["text"]
|
| 82 |
-
|
| 83 |
text = " ".join([seg["text"] for seg in result["segments"]])
|
| 84 |
|
| 85 |
# -------------------------
|
| 86 |
# 🎯 3. ROBERTA (cardiffnlp/twitter-xlm-roberta)
|
| 87 |
# -------------------------
|
| 88 |
|
| 89 |
-
sentiment, score = analyze_sentiment(text)
|
| 90 |
|
| 91 |
print(f"Sentimento: {sentiment} ({score})")
|
| 92 |
|
|
|
|
| 3 |
import os
|
| 4 |
from app.config import WHISPER_MODEL, WHISPER_MODEL_PATH
|
| 5 |
os.environ["XDG_CACHE_HOME"] = str(WHISPER_MODEL_PATH) # "./models"
|
| 6 |
+
from app.logger import log
|
| 7 |
from app.sentiment_model import analyze_sentiment
|
| 8 |
import whisper
|
| 9 |
from moviepy.editor import VideoFileClip
|
|
|
|
| 42 |
# -------------------------
|
| 43 |
# 🔥 PROCESSAMENTO PRINCIPAL
|
| 44 |
# -------------------------
|
| 45 |
+
def process_audio(video_base64=None, audio_base64=None, logger=None):
|
| 46 |
|
| 47 |
video_path = None
|
| 48 |
audio_path = None
|
|
|
|
| 53 |
# -------------------------
|
| 54 |
|
| 55 |
if audio_base64:
|
| 56 |
+
log("Recebido áudio base64", logger=logger)
|
| 57 |
audio_path = save_base64_to_file(audio_base64, ".wav")
|
| 58 |
|
| 59 |
elif video_base64:
|
| 60 |
+
log("Recebido vídeo base64", logger=logger)
|
| 61 |
video_path = save_base64_to_file(video_base64, ".mp4")
|
| 62 |
|
| 63 |
+
log("Extraindo áudio do vídeo...", logger=logger)
|
| 64 |
audio_path = extract_audio_from_video(video_path)
|
| 65 |
|
| 66 |
else:
|
|
|
|
| 70 |
# 🎯 2. WHISPER
|
| 71 |
# -------------------------
|
| 72 |
|
| 73 |
+
log("Transcrevendo com Whisper...", logger=logger)
|
| 74 |
|
| 75 |
result = whisper_model.transcribe(
|
| 76 |
audio_path,
|
|
|
|
| 79 |
fp16=False # importante para CPU
|
| 80 |
)
|
| 81 |
|
|
|
|
|
|
|
| 82 |
text = " ".join([seg["text"] for seg in result["segments"]])
|
| 83 |
|
| 84 |
# -------------------------
|
| 85 |
# 🎯 3. ROBERTA (cardiffnlp/twitter-xlm-roberta)
|
| 86 |
# -------------------------
|
| 87 |
|
| 88 |
+
sentiment, score = analyze_sentiment(text, logger)
|
| 89 |
|
| 90 |
print(f"Sentimento: {sentiment} ({score})")
|
| 91 |
|
app/sentiment_model.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
|
| 3 |
_pipeline = None
|
|
@@ -30,26 +31,7 @@ def split_text(text, max_length=300):
|
|
| 30 |
|
| 31 |
return chunks
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
model = get_pipeline()
|
| 35 |
-
|
| 36 |
-
#result = model(text[:512])[0]
|
| 37 |
-
|
| 38 |
-
result = model(text)[0]
|
| 39 |
-
|
| 40 |
-
label = result["label"]
|
| 41 |
-
score = result["score"]
|
| 42 |
-
|
| 43 |
-
if label == "LABEL_0":
|
| 44 |
-
sentiment = "negative"
|
| 45 |
-
elif label == "LABEL_1":
|
| 46 |
-
sentiment = "neutral"
|
| 47 |
-
else:
|
| 48 |
-
sentiment = "positive"
|
| 49 |
-
|
| 50 |
-
return sentiment, score
|
| 51 |
-
|
| 52 |
-
def analyze_sentiment(text: str):
|
| 53 |
model = get_pipeline()
|
| 54 |
|
| 55 |
# 🔥 quebra em partes menores
|
|
@@ -58,6 +40,9 @@ def analyze_sentiment(text: str):
|
|
| 58 |
results = []
|
| 59 |
|
| 60 |
for chunk in chunks:
|
|
|
|
|
|
|
|
|
|
| 61 |
r = model(chunk)[0]
|
| 62 |
results.append(r)
|
| 63 |
|
|
@@ -72,11 +57,19 @@ def analyze_sentiment(text: str):
|
|
| 72 |
}
|
| 73 |
|
| 74 |
if not results:
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
total = 0
|
| 78 |
for r in results:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
avg = total / len(results)
|
| 82 |
|
|
|
|
| 1 |
+
from app.logger import log
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
_pipeline = None
|
|
|
|
| 31 |
|
| 32 |
return chunks
|
| 33 |
|
| 34 |
+
def analyze_sentiment(text: str, logger=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
model = get_pipeline()
|
| 36 |
|
| 37 |
# 🔥 quebra em partes menores
|
|
|
|
| 40 |
results = []
|
| 41 |
|
| 42 |
for chunk in chunks:
|
| 43 |
+
if not chunk.strip():
|
| 44 |
+
continue
|
| 45 |
+
|
| 46 |
r = model(chunk)[0]
|
| 47 |
results.append(r)
|
| 48 |
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
if not results:
|
| 60 |
+
log("Não houve resultado", "warning", logger=logger)
|
| 61 |
+
return "neutral", 0
|
| 62 |
|
| 63 |
total = 0
|
| 64 |
for r in results:
|
| 65 |
+
label = r["label"]
|
| 66 |
+
score = r["score"]
|
| 67 |
+
|
| 68 |
+
if label not in score_map:
|
| 69 |
+
continue
|
| 70 |
+
|
| 71 |
+
log(f"Label: {label} | Score: {score}")
|
| 72 |
+
total += score_map[label] * score
|
| 73 |
|
| 74 |
avg = total / len(results)
|
| 75 |
|