You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.3 KiB
68 lines
2.3 KiB
import requests |
|
import os |
|
|
|
|
|
def call_asr_api(): |
|
api_url = "http://localhost:7777/transcriptions" |
|
audio_file = r"G:\Work_data\workstation\Forwork-voice-txt\FireRedASR\叠词检测\可以.mp3" |
|
|
|
try: |
|
with open(audio_file, 'rb') as f: |
|
files = {'file': f} |
|
response = requests.post(api_url, files=files) |
|
|
|
result = response.json() |
|
print("完整响应:", result) |
|
|
|
# 检查响应状态 |
|
if result.get('code') == 200: |
|
transcription_data = result['data']['transcription'] |
|
statistics = result['data']['statistics'] |
|
|
|
print(f"\n转录结果 ({len(transcription_data)} 个片段):") |
|
print("-" * 50) |
|
|
|
# 收集所有文本内容 |
|
text_lines = [] |
|
|
|
for segment in transcription_data: |
|
start_sec = segment['start'] / 1000 # 转换为秒 |
|
end_sec = segment['end'] / 1000 |
|
content = segment['content'] |
|
seg_type = segment.get('segment_type', 'unknown') |
|
confidence = segment.get('confidence', 0) |
|
|
|
print(f"[{start_sec:.1f}s - {end_sec:.1f}s] ({seg_type}) 置信度:{confidence:.3f}") |
|
print(f" {content}") |
|
print() |
|
|
|
# 添加到文本列表 |
|
text_lines.append(content) |
|
|
|
print("处理统计:") |
|
print(f" 总片段数: {statistics['total_segments']}") |
|
print(f" 处理时间: {statistics['processing_time']}秒") |
|
print(f" 分段类型: {statistics['segmentation_types']}") |
|
|
|
# 保存为txt文件 |
|
# 生成txt文件路径(与音频文件同目录,同文件名) |
|
txt_file = os.path.splitext(audio_file)[0] + '.txt' |
|
|
|
with open(txt_file, 'w', encoding='utf-8') as f: |
|
f.write('\n'.join(text_lines)) |
|
|
|
print(f"\n✓ 转录文本已保存到: {txt_file}") |
|
|
|
else: |
|
print(f"API调用失败: {result.get('message')}") |
|
|
|
except FileNotFoundError: |
|
print(f"音频文件不存在: {audio_file}") |
|
except requests.exceptions.RequestException as e: |
|
print(f"网络请求失败: {e}") |
|
except Exception as e: |
|
print(f"发生错误: {e}") |
|
|
|
|
|
if __name__ == "__main__": |
|
call_asr_api()
|
|
|