This commit is contained in:
@@ -192,166 +192,8 @@ class TranscriptionTaskViewSet(viewsets.ModelViewSet):
|
||||
logger.error(f"Unexpected response format: {type(data_obj)} - {data_obj}")
|
||||
return Response({'error': f"Unexpected response format: {type(data_obj)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
task_status = data_obj.get('TaskStatus')
|
||||
|
||||
# 兼容其他状态字段名
|
||||
if not task_status:
|
||||
task_status = data_obj.get('Status')
|
||||
|
||||
if task_status == 'COMPLETE' or task_status == 'COMPLETED' or task_status == 'SUCCEEDED':
|
||||
task.status = TranscriptionTask.Status.SUCCEEDED
|
||||
|
||||
# 解析结果
|
||||
task_result = data_obj.get('Result', {})
|
||||
logger.info(f"Task result keys: {task_result.keys()}")
|
||||
|
||||
# 提取逐字稿
|
||||
transcription_data = task_result.get('Transcription', {})
|
||||
logger.info(f"Raw transcription data type: {type(transcription_data)}")
|
||||
|
||||
# 如果是 URL (字符串),尝试下载内容
|
||||
if isinstance(transcription_data, str) and transcription_data.startswith('http'):
|
||||
try:
|
||||
import requests
|
||||
logger.info(f"Downloading transcription from {transcription_data}")
|
||||
t_resp = requests.get(transcription_data)
|
||||
if t_resp.status_code == 200:
|
||||
transcription_data = t_resp.json()
|
||||
logger.info(f"Downloaded transcription keys: {transcription_data.keys() if isinstance(transcription_data, dict) else 'Not a dict'}")
|
||||
# 保存原始数据
|
||||
task.transcription_data = transcription_data
|
||||
else:
|
||||
logger.warning(f"Failed to download transcription: {t_resp.status_code}")
|
||||
transcription_data = {}
|
||||
except Exception as e:
|
||||
logger.error(f"Error downloading transcription: {e}")
|
||||
transcription_data = {}
|
||||
elif isinstance(transcription_data, dict) and 'TranscriptionUrl' in transcription_data:
|
||||
# 有些情况 Transcription 还是对象,但内容在 Url 字段
|
||||
try:
|
||||
import requests
|
||||
url = transcription_data['TranscriptionUrl']
|
||||
logger.info(f"Downloading transcription from {url}")
|
||||
t_resp = requests.get(url)
|
||||
if t_resp.status_code == 200:
|
||||
transcription_data = t_resp.json()
|
||||
logger.info(f"Downloaded transcription keys: {transcription_data.keys() if isinstance(transcription_data, dict) else 'Not a dict'}")
|
||||
# 保存原始数据
|
||||
task.transcription_data = transcription_data
|
||||
except Exception as e:
|
||||
logger.error(f"Error downloading transcription nested url: {e}")
|
||||
|
||||
if isinstance(transcription_data, dict):
|
||||
# 确定包含实际内容的字典源
|
||||
content_source = transcription_data
|
||||
|
||||
# 关键修复:
|
||||
# 阿里云返回的 JSON 可能是 {"Transcription": {"Sentences": ...}} 也可能是 {"Sentences": ...}
|
||||
# 之前的逻辑虽然尝试了 content_source = transcription_data['Transcription'],但如果 key 不存在会报错
|
||||
# 且如果是 {"TaskId": "...", "Transcription": {"Sentences": ...}} 这种结构,需要先剥离外层
|
||||
|
||||
# 尝试找到真正的 sentences/paragraphs 所在的字典
|
||||
# 优先查找 'Transcription' 键,如果它对应的是字典,那么数据很可能在里面
|
||||
if 'Transcription' in content_source and isinstance(content_source['Transcription'], dict):
|
||||
content_source = content_source['Transcription']
|
||||
logger.info(f"Drilled down to nested 'Transcription' key. Keys: {content_source.keys()}")
|
||||
|
||||
# 尝试提取 Sentences
|
||||
sentences = content_source.get('Sentences', [])
|
||||
|
||||
# 尝试提取 Paragraphs
|
||||
paragraphs_data = content_source.get('Paragraphs', [])
|
||||
|
||||
if sentences:
|
||||
full_text = " ".join([s.get('Text', '') for s in sentences])
|
||||
task.transcription = full_text
|
||||
elif paragraphs_data:
|
||||
# 处理 Paragraphs
|
||||
para_list = []
|
||||
if isinstance(paragraphs_data, dict):
|
||||
# 有时结构是 {"Paragraphs": {"Paragraphs": [...]}} 或者 {"Paragraphs": [...]}
|
||||
para_list = paragraphs_data.get('Paragraphs', [])
|
||||
if not para_list and isinstance(paragraphs_data, list):
|
||||
para_list = paragraphs_data
|
||||
elif isinstance(paragraphs_data, list):
|
||||
para_list = paragraphs_data
|
||||
|
||||
if para_list:
|
||||
texts = []
|
||||
for p in para_list:
|
||||
if 'Text' in p:
|
||||
texts.append(p['Text'])
|
||||
elif 'Sentences' in p:
|
||||
for s in p['Sentences']:
|
||||
if 'Text' in s:
|
||||
texts.append(s['Text'])
|
||||
task.transcription = "\n".join(texts)
|
||||
logger.info(f"Extracted {len(texts)} paragraphs")
|
||||
else:
|
||||
logger.warning(f"Paragraphs found but failed to extract list. Type: {type(paragraphs_data)}")
|
||||
else:
|
||||
logger.warning(f"Could not find Sentences or Paragraphs in content source. Keys: {content_source.keys()}")
|
||||
|
||||
# 提取总结
|
||||
# 总结结果结构可能因配置不同而异,这里尝试获取摘要
|
||||
summarization = task_result.get('Summarization', {})
|
||||
|
||||
# 如果是 URL (字符串),尝试下载内容
|
||||
if isinstance(summarization, str) and summarization.startswith('http'):
|
||||
try:
|
||||
import requests
|
||||
logger.info(f"Downloading summarization from {summarization}")
|
||||
s_resp = requests.get(summarization)
|
||||
if s_resp.status_code == 200:
|
||||
summarization = s_resp.json()
|
||||
# 保存原始数据
|
||||
task.summary_data = summarization
|
||||
else:
|
||||
logger.warning(f"Failed to download summarization: {s_resp.status_code}")
|
||||
summarization = {}
|
||||
except Exception as e:
|
||||
logger.error(f"Error downloading summarization: {e}")
|
||||
summarization = {}
|
||||
|
||||
# 听悟的总结通常在 Summarization.Text 或类似字段
|
||||
# 如果是章节摘要,可能在 Chapters 中
|
||||
# 假设是全文摘要
|
||||
if 'Text' in summarization:
|
||||
task.summary = summarization['Text']
|
||||
elif 'Headline' in summarization:
|
||||
task.summary = summarization['Headline']
|
||||
else:
|
||||
# 尝试从章节摘要中提取
|
||||
chapters = task_result.get('Chapters', [])
|
||||
# 处理 AutoChapters
|
||||
auto_chapters = task_result.get('AutoChapters', {})
|
||||
if isinstance(auto_chapters, str) and auto_chapters.startswith('http'):
|
||||
try:
|
||||
import requests
|
||||
logger.info(f"Downloading auto chapters from {auto_chapters}")
|
||||
ac_resp = requests.get(auto_chapters)
|
||||
if ac_resp.status_code == 200:
|
||||
auto_chapters = ac_resp.json()
|
||||
task.auto_chapters_data = auto_chapters
|
||||
except Exception as e:
|
||||
logger.error(f"Error downloading auto chapters: {e}")
|
||||
|
||||
summary_parts = []
|
||||
for chapter in chapters:
|
||||
if 'Headline' in chapter:
|
||||
summary_parts.append(chapter['Headline'])
|
||||
if 'Summary' in chapter:
|
||||
summary_parts.append(chapter['Summary'])
|
||||
task.summary = "\n".join(summary_parts)
|
||||
|
||||
task.save()
|
||||
|
||||
elif task_status == 'FAILED':
|
||||
task.status = TranscriptionTask.Status.FAILED
|
||||
task.error_message = data_obj.get('TaskStatusText', result.get('Message', 'Unknown error'))
|
||||
task.save()
|
||||
|
||||
# 其他状态 (PENDING, RUNNING) 不做更改
|
||||
# 调用 Service 进行解析和更新
|
||||
service.parse_and_update_task(task, result)
|
||||
|
||||
serializer = self.get_serializer(task)
|
||||
return Response(serializer.data)
|
||||
|
||||
Reference in New Issue
Block a user