File size: 5,612 Bytes
9aaf513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Ported from https://github.com/pavelzbornik/whisperX-FastAPI/blob/main/app/models.py

from enum import Enum
from pydantic import BaseModel
from typing import Optional, List
from uuid import uuid4
from datetime import datetime
from sqlalchemy.types import Enum as SQLAlchemyEnum
from typing import Any
from sqlmodel import SQLModel, Field, JSON, Column


class ResultType(str, Enum):
    JSON = "json"
    FILEPATH = "filepath"


class TaskStatus(str, Enum):
    PENDING = "pending"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELLED = "cancelled"
    QUEUED = "queued"
    PAUSED = "paused"
    RETRYING = "retrying"

    def __str__(self):
        return self.value


class TaskType(str, Enum):
    TRANSCRIPTION = "transcription"
    VAD = "vad"
    BGM_SEPARATION = "bgm_separation"

    def __str__(self):
        return self.value


class TaskStatusResponse(BaseModel):
    """`TaskStatusResponse` is a wrapper class that hides sensitive information from `Task`"""
    identifier: str = Field(..., description="Unique identifier for the queued task that can be used for tracking")
    status: TaskStatus = Field(..., description="Current status of the task")
    task_type: Optional[TaskType] = Field(
        default=None,
        description="Type/category of the task"
    )
    result_type: Optional[ResultType] = Field(
        default=ResultType.JSON,
        description="Result type whether it's a filepath or JSON"
    )
    result: Optional[Any] = Field(
        default=None,
        description="JSON data representing the result of the task"
    )
    task_params: Optional[dict] = Field(
        default=None,
        description="Parameters of the task"
    )
    error: Optional[str] = Field(
        default=None,
        description="Error message, if any, associated with the task"
    )
    duration: Optional[float] = Field(
        default=None,
        description="Duration of the task execution"
    )


class Task(SQLModel, table=True):
    """

    Table to store tasks information.



    Attributes:

    - id: Unique identifier for each task (Primary Key).

    - uuid: Universally unique identifier for each task.

    - status: Current status of the task.

    - result: JSON data representing the result of the task.

    - result_type: Type of the data whether it is normal JSON data or filepath.

    - file_name: Name of the file associated with the task.

    - task_type: Type/category of the task.

    - duration: Duration of the task execution.

    - error: Error message, if any, associated with the task.

    - created_at: Date and time of creation.

    - updated_at: Date and time of last update.

    """

    __tablename__ = "tasks"

    id: Optional[int] = Field(
        default=None,
        primary_key=True,
        description="Unique identifier for each task (Primary Key)"
    )
    uuid: str = Field(
        default_factory=lambda: str(uuid4()),
        description="Universally unique identifier for each task"
    )
    status: Optional[TaskStatus] = Field(
        default=None,
        sa_column=Field(sa_column=SQLAlchemyEnum(TaskStatus)),
        description="Current status of the task",
    )
    result: Optional[dict] = Field(
        default_factory=dict,
        sa_column=Column(JSON),
        description="JSON data representing the result of the task"
    )
    result_type: Optional[ResultType] = Field(
        default=ResultType.JSON,
        sa_column=Field(sa_column=SQLAlchemyEnum(ResultType)),
        description="Result type whether it's a filepath or JSON"
    )
    file_name: Optional[str] = Field(
        default=None,
        description="Name of the file associated with the task"
    )
    url: Optional[str] = Field(
        default=None,
        description="URL of the file associated with the task"
    )
    audio_duration: Optional[float] = Field(
        default=None,
        description="Duration of the audio in seconds"
    )
    language: Optional[str] = Field(
        default=None,
        description="Language of the file associated with the task"
    )
    task_type: Optional[TaskType] = Field(
        default=None,
        sa_column=Field(sa_column=SQLAlchemyEnum(TaskType)),
        description="Type/category of the task"
    )
    task_params: Optional[dict] = Field(
        default_factory=dict,
        sa_column=Column(JSON),
        description="Parameters of the task"
    )
    duration: Optional[float] = Field(
        default=None,
        description="Duration of the task execution"
    )
    error: Optional[str] = Field(
        default=None,
        description="Error message, if any, associated with the task"
    )
    created_at: datetime = Field(
        default_factory=datetime.utcnow,
        description="Date and time of creation"
    )
    updated_at: datetime = Field(
        default_factory=datetime.utcnow,
        sa_column_kwargs={"onupdate": datetime.utcnow},
        description="Date and time of last update"
    )

    def to_response(self) -> "TaskStatusResponse":
        return TaskStatusResponse(
            identifier=self.uuid,
            status=self.status,
            task_type=self.task_type,
            result_type=self.result_type,
            result=self.result,
            task_params=self.task_params,
            error=self.error,
            duration=self.duration
        )


class TasksResult(BaseModel):
    tasks: List[Task]