Download app.py from elelabc/gxsp2: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/spaces/elelabc/gxsp2/resolve/main/app.py
- Command line
-
hf download hf://spaces/elelabc/gxsp2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/elelabc/gxsp2/resolve/main/app.py
3.56 kB
| import gradio as gr | |
| import requests | |
| import os | |
| import time | |
| # ================= 配置区域 ================= | |
| API_KEY = "sk-DHQ9p46YjdM86SxSGJCL6MmRpVLRYBnPBRH7dNTtYpmyuW47" | |
| API_URL = "https://yunwu.ai/v1/audio/transcriptions" | |
| # =========================================== | |
| def test_whisper_api(audio_path): | |
| if not audio_path: | |
| return {"error": "请先上传或录制音频"} | |
| # 1. 检查文件大小 | |
| file_size = os.path.getsize(audio_path) | |
| if file_size == 0: | |
| return {"错误": "上传的音频文件大小为 0,无效文件。"} | |
| print(f"📂 准备上传: {audio_path} (大小: {file_size/1024:.2f} KB)") | |
| # 2. 伪装 Header(防止被判定为爬虫攻击) | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" | |
| } | |
| try: | |
| # 3. 读取文件到内存 | |
| with open(audio_path, "rb") as f: | |
| file_content = f.read() | |
| # 🔥🔥🔥 核心修改:将所有参数都放入 files 列表 🔥🔥🔥 | |
| # 这种写法强制 requests 将所有字段(包括文本参数)都打在一个 multipart 包里 | |
| # 且保证了顺序,极大提高了对 Go/Java 后端的兼容性 | |
| multipart_payload = [ | |
| ('model', (None, 'whisper-1')), # 文本字段,文件名设为 None | |
| ('response_format', (None, 'verbose_json')), | |
| ('language', (None, 'zh')), | |
| ('file', ('test_audio.mp3', file_content, 'audio/mp3')) # 文件字段 | |
| ] | |
| print("⏳ 正在发送强兼容请求...") | |
| # 注意:这里只传 files=multipart_payload,不传 data=... | |
| response = requests.post(API_URL, headers=headers, files=multipart_payload, timeout=60) | |
| # 4. 结果处理 | |
| if response.status_code == 200: | |
| result = response.json() | |
| summary = "✅ 请求成功!\n" | |
| if 'segments' in result: | |
| count = len(result['segments']) | |
| summary += f"🎉 获取到 {count} 条时间轴数据\n" | |
| for i, seg in enumerate(result['segments'][:3]): | |
| summary += f"[{seg['start']:.2f}s -> {seg['end']:.2f}s]: {seg['text']}\n" | |
| return {"摘要": summary, "原始数据": result} | |
| else: | |
| # 如果还是 429,说明 IP 暂时被风控,建议等待 | |
| err_msg = f"❌ 失败 [{response.status_code}]" | |
| if response.status_code == 429: | |
| err_msg += "\n⚠️ 触发频率限制,请暂停测试 1 分钟后再试。" | |
| try: | |
| err_msg += f"\n服务端返回: {response.text}" | |
| except: pass | |
| return {"状态": "Error", "详细信息": err_msg} | |
| except Exception as e: | |
| return {"错误": f"程序异常: {str(e)}"} | |
| # ================= 启动 ================= | |
| with gr.Blocks(title="Whisper API 终极兼容版") as demo: | |
| gr.Markdown("## 🛠️ Whisper API 终极兼容测试") | |
| gr.Markdown("采用 `All-in-Files` 封装策略,修复 `NextPart: EOF` 错误。") | |
| with gr.Row(): | |
| input_audio = gr.Audio(label="上传音频", type="filepath") | |
| btn_run = gr.Button("🚀 运行测试", variant="primary") | |
| output_json = gr.JSON(label="测试结果") | |
| btn_run.click(test_whisper_api, inputs=input_audio, outputs=output_json) | |
| if __name__ == "__main__": | |
| demo.launch() |