Spaces:
Sleeping
Sleeping
update def upload_and_analyze
Browse files
src/expon/presentation/application/internal/commandservices/audio_upload_service.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
from uuid import UUID, uuid4
|
2 |
from datetime import datetime, timezone
|
3 |
from fastapi import UploadFile
|
4 |
-
|
5 |
|
6 |
from src.expon.presentation.domain.model.aggregates.presentation import Presentation
|
7 |
from src.expon.presentation.domain.model.valueobjects.audio_metadata import AudioMetadata
|
@@ -10,8 +10,6 @@ from src.expon.presentation.domain.services.sentiment_analysis_service import Se
|
|
10 |
from src.expon.presentation.infrastructure.persistence.jpa.repositories.presentation_repository import PresentationRepository
|
11 |
from src.expon.presentation.infrastructure.services.storage.local_storage_service import LocalStorageService
|
12 |
|
13 |
-
import os
|
14 |
-
|
15 |
class AudioUploadService:
|
16 |
def __init__(
|
17 |
self,
|
@@ -27,30 +25,31 @@ class AudioUploadService:
|
|
27 |
|
28 |
def upload_and_analyze(self, file: UploadFile, user_id: UUID = UUID("00000000-0000-0000-0000-000000000000")):
|
29 |
# 1. Guardar archivo original
|
30 |
-
|
31 |
-
|
|
|
32 |
# 2. Transcribir directamente con AssemblyAI
|
33 |
-
result = self.transcription_service.transcribe(
|
34 |
-
|
35 |
transcript = result["text"]
|
36 |
confidence = result.get("confidence", 1.0)
|
37 |
-
|
38 |
-
# 3. Simular metadata b谩sica
|
39 |
metadata = AudioMetadata(
|
40 |
-
duration=0.0,
|
41 |
-
sample_rate=16000,
|
42 |
language="es"
|
43 |
)
|
44 |
-
|
45 |
# 4. Analizar emoci贸n
|
46 |
emotion_data = self.sentiment_service.analyze(transcript)
|
47 |
print("[DEBUG] Transcripci贸n exitosa. Texto:", transcript[:50])
|
48 |
-
|
49 |
# 5. Crear entidad Presentation
|
50 |
presentation = Presentation(
|
51 |
id=uuid4(),
|
52 |
user_id=user_id,
|
53 |
-
filename=
|
54 |
transcript=transcript,
|
55 |
dominant_emotion=emotion_data["dominant_emotion"],
|
56 |
emotion_probabilities=emotion_data["emotion_probabilities"],
|
@@ -58,14 +57,14 @@ class AudioUploadService:
|
|
58 |
metadata=metadata,
|
59 |
created_at=datetime.now(timezone.utc)
|
60 |
)
|
61 |
-
|
62 |
# 6. Guardar en base de datos
|
63 |
self.repository.save(presentation)
|
64 |
-
|
65 |
# 7. Eliminar archivo temporal
|
66 |
try:
|
67 |
-
os.remove(
|
68 |
-
except Exception:
|
69 |
-
|
70 |
-
|
71 |
return presentation
|
|
|
1 |
from uuid import UUID, uuid4
|
2 |
from datetime import datetime, timezone
|
3 |
from fastapi import UploadFile
|
4 |
+
import os
|
5 |
|
6 |
from src.expon.presentation.domain.model.aggregates.presentation import Presentation
|
7 |
from src.expon.presentation.domain.model.valueobjects.audio_metadata import AudioMetadata
|
|
|
10 |
from src.expon.presentation.infrastructure.persistence.jpa.repositories.presentation_repository import PresentationRepository
|
11 |
from src.expon.presentation.infrastructure.services.storage.local_storage_service import LocalStorageService
|
12 |
|
|
|
|
|
13 |
class AudioUploadService:
|
14 |
def __init__(
|
15 |
self,
|
|
|
25 |
|
26 |
def upload_and_analyze(self, file: UploadFile, user_id: UUID = UUID("00000000-0000-0000-0000-000000000000")):
|
27 |
# 1. Guardar archivo original
|
28 |
+
filename = self.storage_service.save(file)
|
29 |
+
full_path = os.path.join(self.storage_service.base_path, filename)
|
30 |
+
|
31 |
# 2. Transcribir directamente con AssemblyAI
|
32 |
+
result = self.transcription_service.transcribe(full_path)
|
33 |
+
|
34 |
transcript = result["text"]
|
35 |
confidence = result.get("confidence", 1.0)
|
36 |
+
|
37 |
+
# 3. Simular metadata b谩sica
|
38 |
metadata = AudioMetadata(
|
39 |
+
duration=0.0,
|
40 |
+
sample_rate=16000,
|
41 |
language="es"
|
42 |
)
|
43 |
+
|
44 |
# 4. Analizar emoci贸n
|
45 |
emotion_data = self.sentiment_service.analyze(transcript)
|
46 |
print("[DEBUG] Transcripci贸n exitosa. Texto:", transcript[:50])
|
47 |
+
|
48 |
# 5. Crear entidad Presentation
|
49 |
presentation = Presentation(
|
50 |
id=uuid4(),
|
51 |
user_id=user_id,
|
52 |
+
filename=filename,
|
53 |
transcript=transcript,
|
54 |
dominant_emotion=emotion_data["dominant_emotion"],
|
55 |
emotion_probabilities=emotion_data["emotion_probabilities"],
|
|
|
57 |
metadata=metadata,
|
58 |
created_at=datetime.now(timezone.utc)
|
59 |
)
|
60 |
+
|
61 |
# 6. Guardar en base de datos
|
62 |
self.repository.save(presentation)
|
63 |
+
|
64 |
# 7. Eliminar archivo temporal
|
65 |
try:
|
66 |
+
os.remove(full_path)
|
67 |
+
except Exception as e:
|
68 |
+
print(f"[WARNING] No se pudo eliminar el archivo temporal: {e}")
|
69 |
+
|
70 |
return presentation
|