Skip to content

Commit 1cb1e14

Browse files
committed
Fix SQLite DB TaskPriority enum issue -> #3
1 parent 5a5bb83 commit 1cb1e14

7 files changed

Lines changed: 75 additions & 66 deletions

File tree

app/api/models/WhisperTaskRequest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737

3838

3939
class TaskPriority(str, Enum):
40-
HIGH = "high"
41-
NORMAL = "normal"
42-
LOW = "low"
40+
high = "high"
41+
normal = "normal"
42+
low = "low"
4343

4444

4545
class TaskType(str, Enum):
@@ -57,7 +57,7 @@ class WhisperTaskRequest(BaseModel):
5757
description="回调URL,任务完成时通知客户端 / Callback URL to notify the client when the task is completed"
5858
)
5959
priority: TaskPriority = Form(
60-
TaskPriority.NORMAL,
60+
TaskPriority.normal,
6161
description="任务优先级 / Task priority"
6262
)
6363
platform: Optional[str] = Form(

app/api/routers/chatgpt_tasks.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,41 +111,41 @@ async def chatgpt_summary(
111111
status_code=status.HTTP_404_NOT_FOUND,
112112
detail=ErrorResponseModel(
113113
code=status.HTTP_404_NOT_FOUND,
114-
message=TaskStatusHttpMessage.NOT_FOUND.value,
114+
message=TaskStatusHttpMessage.not_found.value,
115115
router=str(request.url),
116116
params=dict(request.query_params),
117117
).model_dump()
118118
)
119119

120120
# 任务处于排队中 - 返回202 | Task is queued - return 202
121-
if task.status == TaskStatus.QUEUED:
121+
if task.status == TaskStatus.queued:
122122
raise HTTPException(
123-
status_code=TaskStatusHttpCode.QUEUED.value,
123+
status_code=TaskStatusHttpCode.queued.value,
124124
detail=ErrorResponseModel(
125-
code=TaskStatusHttpCode.QUEUED.value,
126-
message=TaskStatusHttpMessage.QUEUED.value,
125+
code=TaskStatusHttpCode.queued.value,
126+
message=TaskStatusHttpMessage.queued.value,
127127
router=str(request.url),
128128
params=dict(request.query_params),
129129
).model_dump()
130130
)
131131
# 任务正在处理中 - 返回202 | Task is processing - return 202
132-
elif task.status == TaskStatus.PROCESSING:
132+
elif task.status == TaskStatus.processing:
133133
raise HTTPException(
134-
status_code=TaskStatusHttpCode.PROCESSING.value,
134+
status_code=TaskStatusHttpCode.processing.value,
135135
detail=ErrorResponseModel(
136-
code=TaskStatusHttpCode.PROCESSING.value,
137-
message=TaskStatusHttpMessage.PROCESSING.value,
136+
code=TaskStatusHttpCode.processing.value,
137+
message=TaskStatusHttpMessage.processing.value,
138138
router=str(request.url),
139139
params=dict(request.query_params),
140140
).model_dump()
141141
)
142142
# 任务失败 - 返回500 | Task failed - return 500
143-
elif task.status == TaskStatus.FAILED:
143+
elif task.status == TaskStatus.failed:
144144
raise HTTPException(
145-
status_code=TaskStatusHttpCode.FAILED.value,
145+
status_code=TaskStatusHttpCode.failed.value,
146146
detail=ErrorResponseModel(
147-
code=TaskStatusHttpCode.FAILED.value,
148-
message=TaskStatusHttpMessage.FAILED.value,
147+
code=TaskStatusHttpCode.failed.value,
148+
message=TaskStatusHttpMessage.failed.value,
149149
router=str(request.url),
150150
params=dict(request.query_params),
151151
).model_dump()
@@ -193,7 +193,7 @@ async def chatgpt_summary(
193193
)
194194

195195
return ResponseModel(
196-
code=TaskStatusHttpCode.COMPLETED.value,
196+
code=TaskStatusHttpCode.completed.value,
197197
router=str(request.url),
198198
params=_ChatGPTTaskRequest.model_dump(),
199199
data=chatgpt_data
@@ -206,7 +206,7 @@ async def chatgpt_summary(
206206
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
207207
detail=ErrorResponseModel(
208208
code=status.HTTP_503_SERVICE_UNAVAILABLE,
209-
message=TaskStatusHttpMessage.SERVICE_UNAVAILABLE.value,
209+
message=TaskStatusHttpMessage.service_unavailable.value,
210210
router=str(request.url),
211211
params=_ChatGPTTaskRequest.model_dump()
212212
).model_dump()

app/api/routers/whisper_tasks.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ async def task_create(
9595
- 任务完成后回调程序会发送一个 POST 请求,包含任务数据。
9696
- 你可以参考接口文档中的回调测试接口在控制台查看回调信息。
9797
- 例如:`http://localhost/api/whisper/callback/test`
98-
- `priority` (TaskPriority): 任务优先级,默认为 `TaskPriority.NORMAL`。
98+
- `priority` (TaskPriority): 任务优先级,默认为 `normal`,具体取值如下:
99+
- `low`: 低优先级,使用小写字母。
100+
- `normal`: 正常优先级,使用小写字母。
101+
- `high`: 高优先级,使用小写字母。
99102
- `platform` (Optional[str]): 指定平台,例如 'tiktok' 或 'douyin',用于方便区分不同平台的任务在数据库进行查询和分类,默认为空,可以根据需要自定义。
100103
- `language` (str): 指定输出语言,例如 'en' 或 'zh',留空则自动检测。
101104
- `temperature` (str): 采样温度,可以是单个值或使用英文逗号分隔的多个值,将在后端转换为列表,例如 "0.8,1.0"。
@@ -145,7 +148,10 @@ async def task_create(
145148
- The callback program will send a POST request containing task data after the task is completed.
146149
- You can view the callback information in the console by referring to the callback test interface in the API documentation.
147150
- For example: `http://localhost/api/whisper/callback/test`
148-
- `priority` (TaskPriority): Task priority, default is `TaskPriority.NORMAL`.
151+
- `priority` (TaskPriority): Task priority, default is `normal`, specific values are as follows:
152+
- `low`: Low priority, use lowercase letters.
153+
- `normal`: Normal priority, use lowercase letters.
154+
- `high`: High priority, use lowercase letters.
149155
- `platform` (Optional[str]): Specify the platform, e.g., 'tiktok' or 'douyin', for easy query and classification of tasks from different platforms in the database, default is empty, can be customized according to needs.
150156
- `language` (str): Specify the output language, e.g., 'en' or 'zh', leave empty for auto-detection.
151157
- `temperature` (str): Sampling temperature, can be a single value or multiple values separated by commas, which will be converted to a list on the backend, e.g., "0.8,1.0".
@@ -494,7 +500,7 @@ async def task_delete(
494500
status_code=status.HTTP_404_NOT_FOUND,
495501
detail=ErrorResponseModel(
496502
code=status.HTTP_404_NOT_FOUND,
497-
message=TaskStatusHttpMessage.NOT_FOUND.value,
503+
message=TaskStatusHttpMessage.not_found.value,
498504
router=str(request.url),
499505
params=dict(request.query_params),
500506
).model_dump()
@@ -586,49 +592,49 @@ async def task_result(
586592
status_code=status.HTTP_404_NOT_FOUND,
587593
detail=ErrorResponseModel(
588594
code=status.HTTP_404_NOT_FOUND,
589-
message=TaskStatusHttpMessage.NOT_FOUND.value,
595+
message=TaskStatusHttpMessage.not_found.value,
590596
router=str(request.url),
591597
params=dict(request.query_params),
592598
).model_dump()
593599
)
594600

595601
# 任务处于排队中 - 返回202 | Task is queued - return 202
596-
if task.status == TaskStatus.QUEUED:
602+
if task.status == TaskStatus.queued:
597603
raise HTTPException(
598-
status_code=TaskStatusHttpCode.QUEUED.value,
604+
status_code=TaskStatusHttpCode.queued.value,
599605
detail=ErrorResponseModel(
600-
code=TaskStatusHttpCode.QUEUED.value,
601-
message=TaskStatusHttpMessage.QUEUED.value,
606+
code=TaskStatusHttpCode.queued.value,
607+
message=TaskStatusHttpMessage.queued.value,
602608
router=str(request.url),
603609
params=dict(request.query_params),
604610
).model_dump()
605611
)
606612
# 任务正在处理中 - 返回202 | Task is processing - return 202
607-
elif task.status == TaskStatus.PROCESSING:
613+
elif task.status == TaskStatus.processing:
608614
raise HTTPException(
609-
status_code=TaskStatusHttpCode.PROCESSING.value,
615+
status_code=TaskStatusHttpCode.processing.value,
610616
detail=ErrorResponseModel(
611-
code=TaskStatusHttpCode.PROCESSING.value,
612-
message=TaskStatusHttpMessage.PROCESSING.value,
617+
code=TaskStatusHttpCode.processing.value,
618+
message=TaskStatusHttpMessage.processing.value,
613619
router=str(request.url),
614620
params=dict(request.query_params),
615621
).model_dump()
616622
)
617623
# 任务失败 - 返回500 | Task failed - return 500
618-
elif task.status == TaskStatus.FAILED:
624+
elif task.status == TaskStatus.failed:
619625
raise HTTPException(
620-
status_code=TaskStatusHttpCode.FAILED.value,
626+
status_code=TaskStatusHttpCode.failed.value,
621627
detail=ErrorResponseModel(
622-
code=TaskStatusHttpCode.FAILED.value,
623-
message=TaskStatusHttpMessage.FAILED.value,
628+
code=TaskStatusHttpCode.failed.value,
629+
message=TaskStatusHttpMessage.failed.value,
624630
router=str(request.url),
625631
params=dict(request.query_params),
626632
).model_dump()
627633
)
628634

629635
# 任务已完成 - 返回200 | Task is completed - return 200
630636
return ResponseModel(
631-
code=TaskStatusHttpCode.COMPLETED.value,
637+
code=TaskStatusHttpCode.completed.value,
632638
router=str(request.url),
633639
params=dict(request.query_params),
634640
data=task.to_dict()
@@ -641,7 +647,7 @@ async def task_result(
641647
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
642648
detail=ErrorResponseModel(
643649
code=status.HTTP_503_SERVICE_UNAVAILABLE,
644-
message=TaskStatusHttpMessage.SERVICE_UNAVAILABLE.value,
650+
message=TaskStatusHttpMessage.service_unavailable.value,
645651
router=str(request.url),
646652
params=dict(request.query_params),
647653
).model_dump()
@@ -1366,13 +1372,13 @@ async def generate_subtitles(
13661372
status_code=status.HTTP_404_NOT_FOUND,
13671373
detail=ErrorResponseModel(
13681374
code=status.HTTP_404_NOT_FOUND,
1369-
message=TaskStatusHttpMessage.NOT_FOUND.value,
1375+
message=TaskStatusHttpMessage.not_found.value,
13701376
router=str(request.url),
13711377
params=dict(request.query_params),
13721378
).model_dump()
13731379
)
13741380

1375-
if task.status != TaskStatus.COMPLETED:
1381+
if task.status != TaskStatus.completed:
13761382
raise HTTPException(
13771383
status_code=status.HTTP_400_BAD_REQUEST,
13781384
detail=ErrorResponseModel(

app/database/DatabaseManager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, AsyncEngine
3939
from sqlalchemy.orm import sessionmaker
4040
from sqlalchemy.exc import SQLAlchemyError, OperationalError
41+
from sqlalchemy.sql import func
4142
from contextlib import asynccontextmanager
4243
from app.database.models.TaskModels import TaskBase, Task, QueryTasksOptionalFilter, TaskStatus, TaskPriority
4344
from app.database.models.WorkFlowModels import WorkFlowBase, Workflow, WorkflowTask, WorkflowNotification
@@ -232,12 +233,12 @@ async def get_queued_tasks(self, max_concurrent_tasks: int) -> List[Task]:
232233
try:
233234
result = await session.execute(
234235
select(Task)
235-
.where(Task.status == TaskStatus.QUEUED)
236+
.where(Task.status == TaskStatus.queued)
236237
.order_by(
237238
case(
238-
(Task.priority == TaskPriority.HIGH, 1),
239-
(Task.priority == TaskPriority.NORMAL, 2),
240-
(Task.priority == TaskPriority.LOW, 3),
239+
(Task.priority == TaskPriority.high, 1),
240+
(Task.priority == TaskPriority.normal, 2),
241+
(Task.priority == TaskPriority.low, 3),
241242
)
242243
)
243244
.limit(max_concurrent_tasks)

app/database/models/TaskModels.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
from pydantic import BaseModel, constr, Field, ConfigDict, field_validator
3838
from sqlalchemy import Column, Integer, String, Enum, Text, JSON, Float, DateTime
39+
from sqlalchemy.types import Enum as SQLAlchemyEnum
3940
from sqlalchemy.ext.declarative import declarative_base
4041

4142
# 定义基础类 | Define Base class
@@ -44,47 +45,47 @@
4445

4546
# 定义任务状态的枚举类型 | Define an enum for task status
4647
class TaskStatus(enum.Enum):
47-
QUEUED = 'queued'
48-
PROCESSING = 'processing'
49-
COMPLETED = 'completed'
50-
FAILED = 'failed'
48+
queued = 'queued'
49+
processing = 'processing'
50+
completed = 'completed'
51+
failed = 'failed'
5152

5253

5354
# TaskStatusHttpCode 枚举类,用于映射 TaskStatus 到 HTTP 状态码
5455
# TaskStatusHttpCode enum class, used to map TaskStatus to HTTP status code
5556
class TaskStatusHttpCode(enum.Enum):
5657
# 202 - Accepted (for ongoing processing)
57-
QUEUED = HTTPStatus.ACCEPTED
58+
queued = HTTPStatus.ACCEPTED
5859
# 202 - Accepted (for ongoing processing)
59-
PROCESSING = HTTPStatus.ACCEPTED
60+
processing = HTTPStatus.ACCEPTED
6061
# 200 - OK (for successful completion)
61-
COMPLETED = HTTPStatus.OK
62+
completed = HTTPStatus.OK
6263
# 500 - Internal Server Error (for task failure)
63-
FAILED = HTTPStatus.INTERNAL_SERVER_ERROR
64+
failed = HTTPStatus.INTERNAL_SERVER_ERROR
6465

6566

6667
# TaskStatusHttpMessage 枚举类,用于映射 TaskStatus 到 HTTP 状态消息
6768
# TaskStatusHttpMessage enum class, used to map TaskStatus to HTTP status message
6869
class TaskStatusHttpMessage(enum.Enum):
6970
# 202 - Accepted (for ongoing processing)
70-
QUEUED = "Task is queued and not started yet"
71+
queued = "Task is queued and not started yet"
7172
# 202 - Accepted (for ongoing processing)
72-
PROCESSING = "Task is currently being processed"
73+
processing = "Task is currently being processed"
7374
# 200 - OK (for successful completion)
74-
COMPLETED = "Task has been completed"
75+
completed = "Task has been completed"
7576
# 404 - Not Found (for task not found or invalid task ID)
76-
NOT_FOUND = "Task not found or has been deleted or invalid task ID"
77+
not_found = "Task not found or has been deleted or invalid task ID"
7778
# 500 - Internal Server Error (for task failure)
78-
FAILED = "Task failed during processing"
79+
failed = "Task failed during processing"
7980
# 503 - Service Unavailable (for database error)
80-
SERVICE_UNAVAILABLE = "Database error occurred. Please try again later."
81+
service_unavailable = "Database error occurred. Please try again later."
8182

8283

8384
# 定义任务优先级的枚举类型 | Define an enum for task priority
8485
class TaskPriority(enum.Enum):
85-
LOW = 'low'
86-
NORMAL = 'normal'
87-
HIGH = 'high'
86+
high = "high"
87+
normal = "normal"
88+
low = "low"
8889

8990

9091
class Task(TaskBase):
@@ -103,9 +104,9 @@ class Task(TaskBase):
103104
# 回调时间 | Callback time
104105
callback_time = Column(DateTime, nullable=True)
105106
# 任务优先级 | Task priority
106-
priority = Column(Enum(TaskPriority), default=TaskPriority.NORMAL)
107+
priority = Column(Enum(TaskPriority), default=TaskPriority.normal)
107108
# 任务状态,初始为 QUEUED | Task status, initially QUEUED
108-
status = Column(Enum(TaskStatus), default=TaskStatus.QUEUED)
109+
status = Column(Enum(TaskStatus), default=TaskStatus.queued)
109110
# 检测到的语言 | Detected language
110111
language = Column(String(10), nullable=True)
111112
# 任务对应的平台 | Platform for the task

app/processors/task_processor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ async def fetch_task_worker(self):
194194
tasks = await self.db_manager.get_queued_tasks(self.max_concurrent_tasks)
195195
for task in tasks:
196196
# 将任务状态更新为处理中 | Update task status to processing
197-
await self.db_manager.update_task(task.id, status=TaskStatus.PROCESSING)
197+
await self.db_manager.update_task(task.id, status=TaskStatus.processing)
198198
# 将结果放入 task_result_queue 中 | Put the result into task_result_queue
199199
await self.task_processing_queue.put(tasks)
200200
except Exception as e:
201201
self.logger.error(f"Error fetching tasks from database: {str(e)}")
202+
self.logger.error(traceback.format_exc())
202203
finally:
203204
# 标记查询完成 | Mark the query as completed
204205
self.fetch_queue.task_done()
@@ -464,7 +465,7 @@ def _process_task_sync(self, task: Task) -> dict:
464465

465466
# 更新任务状态和结果 | Update task status and result
466467
task_update = {
467-
"status": TaskStatus.COMPLETED,
468+
"status": TaskStatus.completed,
468469
"file_path": task.file_path,
469470
"file_size_bytes": task.file_size_bytes,
470471
"file_duration": task.file_duration,
@@ -482,7 +483,7 @@ def _process_task_sync(self, task: Task) -> dict:
482483

483484
except Exception as e:
484485
task_update = {
485-
"status": TaskStatus.FAILED,
486+
"status": TaskStatus.failed,
486487
"error_message": str(e)
487488
}
488489
self.update_queue.put_nowait((task.id, task_update))

config/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class FastAPISettings:
4646
# 项目描述 | Project description
4747
description: str = "⚡ A high-performance asynchronous API for Automatic Speech Recognition (ASR) and translation. No need to purchase the Whisper API—perform inference using a locally running Whisper model with support for multi-GPU concurrency and designed for distributed deployment. It also includes built-in crawlers for social media platforms like TikTok and Douyin, enabling seamless media processing from multiple social platforms. This provides a powerful and scalable solution for automated media content data processing."
4848
# 项目版本 | Project version
49-
version: str = "1.0.4"
49+
version: str = "1.0.5"
5050
# Swagger 文档 URL | Swagger docs URL
5151
docs_url: str = "/"
5252
# 是否开启 debug 模式 | Whether to enable debug mode

0 commit comments

Comments
 (0)