This commit is contained in:
HuangHai
2026-01-20 19:16:55 +08:00
parent f2f7a38210
commit 66cb0faeff
9 changed files with 52 additions and 110 deletions

View File

@@ -3,7 +3,6 @@ import os
import asyncio
import logging
import sys
from dashscope import Files
# Ensure project root is in path
sys.path.append(r"d:\dsWork\aiData")
@@ -32,104 +31,33 @@ async def transcribe_all():
txt_path = os.path.join(transcript_dir, txt_filename)
if os.path.exists(txt_path):
logger.info(f"Skipping (already exists): {txt_filename}")
continue
# Check if file is empty
if os.path.getsize(txt_path) > 0:
logger.info(f"Skipping (already exists): {txt_filename}")
continue
else:
logger.info(f"Re-processing empty file: {txt_filename}")
logger.info(f"Processing: {filename}")
uploaded_file = None
try:
# 1. Upload file to DashScope
logger.info(f"Uploading {filename} to DashScope...")
# Use purpose='assistants' to bypass jsonl check
upload_resp = Files.upload(audio_path, purpose='assistants', description=filename)
# Direct local file transcription using Recognition API
text = await client.transcribe_file(audio_path)
if upload_resp.status_code == 200:
# Handle output structure (dict or object)
output_data = upload_resp.output
uploaded_files = None
if hasattr(output_data, 'uploaded_files'):
uploaded_files = output_data.uploaded_files
elif isinstance(output_data, dict):
uploaded_files = output_data.get('uploaded_files')
if not uploaded_files:
logger.error(f"No uploaded_files in response: {output_data}")
continue
uploaded_file = uploaded_files[0]
logger.info(f"Uploaded file info: {uploaded_file}")
# Handle uploaded_file structure
file_id = None
if hasattr(uploaded_file, 'file_id'):
file_id = uploaded_file.file_id
elif isinstance(uploaded_file, dict):
file_id = uploaded_file.get('file_id')
if not file_id:
logger.error(f"No file_id in uploaded file: {uploaded_file}")
continue
logger.info(f"Uploaded successfully. File ID: {file_id}")
# Try passing file_id. If that fails, we might need another approach.
# According to some docs, file_urls=["file-xxx"] works.
target_url = file_id
if text:
with open(txt_path, 'w', encoding='utf-8') as f:
f.write(text)
logger.info(f"Saved transcript to: {txt_filename}")
else:
logger.error(f"Upload failed: {upload_resp}")
continue
# 2. Transcribe
logger.info(f"Transcribing {file_id}...")
output = await client.transcribe_audio(file_urls=[target_url])
if output and output.task_status == 'SUCCEEDED':
# Parse results
results = output.results
if results:
for res in results:
transcription_url = res.get('transcription_url')
if transcription_url:
# Download result
trans_data = await client.download_transcription_result(transcription_url)
if trans_data:
# Extract text
# text_with_ts = await client.extract_transcript_with_timestamp(trans_data)
text_clean = await client.extract_transcript_without_timestamp(trans_data)
# Save to file
with open(txt_path, 'w', encoding='utf-8') as f:
f.write(text_clean)
logger.info(f"Saved transcript to: {txt_filename}")
else:
logger.error(f"Failed to download transcript for {filename}")
else:
logger.error(f"No transcription_url in result for {filename}")
else:
logger.error(f"No results in output for {filename}")
else:
logger.error(f"Transcription failed for {filename}")
logger.error(f"Failed to transcribe: {filename}")
except Exception as e:
logger.error(f"Error processing {filename}: {e}")
finally:
# 3. Cleanup: Delete uploaded file
if uploaded_file:
try:
fid = None
if hasattr(uploaded_file, 'file_id'):
fid = uploaded_file.file_id
elif isinstance(uploaded_file, dict):
fid = uploaded_file.get('file_id')
if fid:
Files.delete(fid)
logger.info(f"Deleted remote file {fid}")
except Exception as e:
logger.warning(f"Failed to delete remote file: {e}")
logger.error(f"Error processing {filename}: {str(e)}", exc_info=True)
if __name__ == "__main__":
asyncio.run(transcribe_all())
try:
asyncio.run(transcribe_all())
except KeyboardInterrupt:
logger.info("Stopped by user")
except Exception as e:
logger.error(f"Fatal error: {str(e)}", exc_info=True)