forked from quant-speed-AI/Scoring-System
创赢未来评分系统 - 初始化提交(移除大文件)
This commit is contained in:
0
backend/ai_services/management/commands/__init__.py
Normal file
0
backend/ai_services/management/commands/__init__.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
import oss2
|
||||
from aliyunsdkcore.client import AcsClient
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Check Aliyun configuration status'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write("Checking Aliyun Configuration...")
|
||||
|
||||
configs = {
|
||||
'ALIYUN_ACCESS_KEY_ID': settings.ALIYUN_ACCESS_KEY_ID,
|
||||
'ALIYUN_ACCESS_KEY_SECRET': settings.ALIYUN_ACCESS_KEY_SECRET,
|
||||
'ALIYUN_OSS_BUCKET_NAME': settings.ALIYUN_OSS_BUCKET_NAME,
|
||||
'ALIYUN_OSS_ENDPOINT': settings.ALIYUN_OSS_ENDPOINT,
|
||||
'ALIYUN_TINGWU_APP_KEY': settings.ALIYUN_TINGWU_APP_KEY,
|
||||
}
|
||||
|
||||
all_valid = True
|
||||
for key, value in configs.items():
|
||||
if not value:
|
||||
self.stdout.write(self.style.ERROR(f"[MISSING] {key} is not set or empty"))
|
||||
all_valid = False
|
||||
else:
|
||||
masked_value = value[:4] + "****" + value[-4:] if len(value) > 8 else "****"
|
||||
self.stdout.write(self.style.SUCCESS(f"[OK] {key}: {masked_value}"))
|
||||
|
||||
if not all_valid:
|
||||
self.stdout.write(self.style.ERROR("\nConfiguration check FAILED. Some required settings are missing."))
|
||||
return
|
||||
|
||||
# Test OSS Connection
|
||||
self.stdout.write("\nTesting OSS Connection...")
|
||||
try:
|
||||
auth = oss2.Auth(configs['ALIYUN_ACCESS_KEY_ID'], configs['ALIYUN_ACCESS_KEY_SECRET'])
|
||||
bucket = oss2.Bucket(auth, configs['ALIYUN_OSS_ENDPOINT'], configs['ALIYUN_OSS_BUCKET_NAME'])
|
||||
bucket.get_bucket_info()
|
||||
self.stdout.write(self.style.SUCCESS("[OK] OSS Connection successful"))
|
||||
except Exception as e:
|
||||
self.stdout.write(self.style.ERROR(f"[FAILED] OSS Connection failed: {e}"))
|
||||
|
||||
# Test Tingwu Client Initialization
|
||||
self.stdout.write("\nTesting Tingwu Client Initialization...")
|
||||
try:
|
||||
client = AcsClient(
|
||||
configs['ALIYUN_ACCESS_KEY_ID'],
|
||||
configs['ALIYUN_ACCESS_KEY_SECRET'],
|
||||
"cn-beijing"
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS("[OK] Tingwu Client initialized"))
|
||||
except Exception as e:
|
||||
self.stdout.write(self.style.ERROR(f"[FAILED] Tingwu Client init failed: {e}"))
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import time
|
||||
import logging
|
||||
from django.core.management.base import BaseCommand
|
||||
from ai_services.models import TranscriptionTask
|
||||
from ai_services.services import AliyunTingwuService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Polls Aliyun Tingwu for transcription results every 10 seconds'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write(self.style.SUCCESS('Starting polling service...'))
|
||||
service = AliyunTingwuService()
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Find tasks that are PENDING or PROCESSING
|
||||
# Include PENDING because create() might set it to PENDING initially
|
||||
# though usually it sets to PROCESSING if task_id is obtained.
|
||||
# Just in case.
|
||||
tasks = TranscriptionTask.objects.filter(
|
||||
status__in=[TranscriptionTask.Status.PENDING, TranscriptionTask.Status.PROCESSING]
|
||||
).exclude(task_id__isnull=True).exclude(task_id='')
|
||||
|
||||
count = tasks.count()
|
||||
if count > 0:
|
||||
self.stdout.write(f'Found {count} pending/processing tasks.')
|
||||
|
||||
for task in tasks:
|
||||
self.stdout.write(f'Checking task {task.task_id} (Status: {task.status})...')
|
||||
try:
|
||||
result = service.get_task_info(task.task_id)
|
||||
|
||||
# Store old status to check for changes
|
||||
old_status = task.status
|
||||
|
||||
service.parse_and_update_task(task, result)
|
||||
|
||||
# Re-fetch or check updated object
|
||||
if task.status != old_status:
|
||||
if task.status == TranscriptionTask.Status.SUCCEEDED:
|
||||
self.stdout.write(self.style.SUCCESS(f'Task {task.task_id} SUCCEEDED'))
|
||||
elif task.status == TranscriptionTask.Status.FAILED:
|
||||
self.stdout.write(self.style.ERROR(f'Task {task.task_id} FAILED: {task.error_message}'))
|
||||
else:
|
||||
# Still processing
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking task {task.task_id}: {e}")
|
||||
self.stdout.write(self.style.ERROR(f"Error checking task {task.task_id}: {e}"))
|
||||
|
||||
# Wait for 10 seconds
|
||||
time.sleep(10)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
self.stdout.write(self.style.SUCCESS('Stopping polling service...'))
|
||||
break
|
||||
except Exception as e:
|
||||
logger.error(f"Polling loop error: {e}")
|
||||
self.stdout.write(self.style.ERROR(f"Polling loop error: {e}"))
|
||||
time.sleep(10)
|
||||
102
backend/ai_services/management/commands/test_tingwu_local.py
Normal file
102
backend/ai_services/management/commands/test_tingwu_local.py
Normal file
@@ -0,0 +1,102 @@
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
import json
|
||||
import logging
|
||||
from django.conf import settings
|
||||
|
||||
# 设置 Django 环境
|
||||
# 添加项目根目录到 sys.path
|
||||
sys.path.append('/Volumes/data/Quant-Speed/market_page/backend')
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') # 修正为正确的 settings 模块路径
|
||||
django.setup()
|
||||
|
||||
from ai_services.services import AliyunTingwuService
|
||||
from ai_services.models import TranscriptionTask
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def test_tingwu_transcription():
|
||||
file_url = "https://tangledup-ai-staging.oss-cn-shanghai.aliyuncs.com/Video/%E6%95%99%E5%AD%A6.mp4"
|
||||
|
||||
print(f"Testing transcription for: {file_url}")
|
||||
|
||||
service = AliyunTingwuService()
|
||||
|
||||
# 1. 创建任务
|
||||
try:
|
||||
print("Creating task...")
|
||||
response = service.create_transcription_task(file_url)
|
||||
print(f"Create task response: {json.dumps(response, indent=2, ensure_ascii=False)}")
|
||||
|
||||
if 'Data' in response and isinstance(response['Data'], dict):
|
||||
task_id = response['Data'].get('TaskId')
|
||||
else:
|
||||
task_id = response.get('TaskId')
|
||||
|
||||
if not task_id:
|
||||
print("Failed to get TaskId")
|
||||
return
|
||||
|
||||
print(f"Task created with ID: {task_id}")
|
||||
|
||||
# 2. 轮询查询任务状态
|
||||
import time
|
||||
max_retries = 60 # 5 minutes
|
||||
for i in range(max_retries):
|
||||
print(f"Checking status (attempt {i+1}/{max_retries})...")
|
||||
result = service.get_task_info(task_id)
|
||||
|
||||
# 解析结果
|
||||
if isinstance(result, str):
|
||||
try:
|
||||
result = json.loads(result)
|
||||
except:
|
||||
pass
|
||||
|
||||
if isinstance(result, dict):
|
||||
data_obj = result.get('Data', result)
|
||||
else:
|
||||
data_obj = result
|
||||
|
||||
task_status = data_obj.get('TaskStatus')
|
||||
if not task_status:
|
||||
task_status = data_obj.get('Status')
|
||||
|
||||
print(f"Current status: {task_status}")
|
||||
|
||||
if task_status in ['COMPLETE', 'COMPLETED', 'SUCCEEDED']:
|
||||
print("Task succeeded!")
|
||||
print(f"Full Result: {json.dumps(data_obj, indent=2, ensure_ascii=False)}")
|
||||
|
||||
# 尝试解析 Transcription
|
||||
task_result = data_obj.get('Result', {})
|
||||
transcription_data = task_result.get('Transcription', {})
|
||||
|
||||
if isinstance(transcription_data, str) and transcription_data.startswith('http'):
|
||||
import requests
|
||||
print(f"Downloading transcription from {transcription_data}")
|
||||
t_resp = requests.get(transcription_data)
|
||||
if t_resp.status_code == 200:
|
||||
content = t_resp.json()
|
||||
print(f"Downloaded content structure keys: {content.keys()}")
|
||||
# print(f"Content sample: {json.dumps(content, indent=2, ensure_ascii=False)[:500]}...")
|
||||
else:
|
||||
print(f"Failed to download: {t_resp.status_code}")
|
||||
|
||||
break
|
||||
elif task_status == 'FAILED':
|
||||
print(f"Task failed: {data_obj}")
|
||||
break
|
||||
|
||||
time.sleep(5)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_tingwu_transcription()
|
||||
Reference in New Issue
Block a user