Spaces:
Running
Running
| import os | |
| import time | |
| import asyncio | |
| import onnx_asr | |
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from concurrent.futures import ThreadPoolExecutor | |
| app = FastAPI(title="GigaAM v3 Speech Recognition API") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Пул потоков для блокирующих операций ASR | |
| executor = ThreadPoolExecutor(max_workers=1) | |
| print("🚀 Загрузка модели GigaAM v3...") | |
| start_init = time.time() | |
| try: | |
| onnx_model = onnx_asr.load_model("gigaam-v3-e2e-ctc") | |
| init_time = time.time() - start_init | |
| print(f"✅ Модель загружена за {init_time:.2f}с") | |
| except Exception as e: | |
| print(f"❌ Ошибка загрузки модели: {e}") | |
| exit(1) | |
| async def root(): | |
| return { | |
| "status": "ready", | |
| "version": "3.1.0", | |
| "model": "harvi-audio" | |
| } | |
| async def health(): | |
| return {"status": "healthy", "model_loaded": True} | |
| async def transcribe_audio(audio: UploadFile = File(...)): | |
| start_time = time.time() | |
| try: | |
| # Асинхронное чтение файла | |
| audio_bytes = await audio.read() | |
| temp_path = f"/tmp/{int(time.time())}_{audio.filename}" | |
| with open(temp_path, "wb") as f: | |
| f.write(audio_bytes) | |
| try: | |
| # КРИТИЧНО: Выносим блокирующую операцию в отдельный поток | |
| # Это освобождает Event Loop для обработки других запросов (/health, etc.) | |
| loop = asyncio.get_running_loop() | |
| text = await loop.run_in_executor( | |
| executor, | |
| lambda: onnx_model.recognize(temp_path) | |
| ) | |
| if not text or not text.strip(): | |
| text = "[Речь не распознана]" | |
| duration = time.time() - start_time | |
| return { | |
| "text": text, | |
| "duration": round(duration, 2) | |
| } | |
| finally: | |
| if os.path.exists(temp_path): | |
| os.remove(temp_path) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Ошибка: {str(e)}") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |