Spaces:
Running
Running
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| import gradio as gr | |
| from gradio_ui import create_ui | |
| from app.asr_model import transcribe_audio | |
| from app.language_detection import detect_language_from_text | |
| from app.history import save_to_history | |
| import os | |
| import tempfile | |
| import shutil | |
| # Initialize FastAPI app | |
| api = FastAPI(title="Multilingual ASR API", description="REST API for audio transcription", version="1.0.0") | |
| async def api_transcribe(audio_file: UploadFile = File(...)): | |
| """ | |
| REST endpoint to upload an audio file and get its transcription. | |
| """ | |
| if not audio_file.filename: | |
| raise HTTPException(status_code=400, detail="No file provided") | |
| try: | |
| # Save uploaded file to a temporary file | |
| fd, temp_path = tempfile.mkstemp(suffix=os.path.splitext(audio_file.filename)[1]) | |
| with os.fdopen(fd, "wb") as f: | |
| shutil.copyfileobj(audio_file.file, f) | |
| # Run inference | |
| transcript = transcribe_audio(temp_path) | |
| lang = detect_language_from_text(transcript) | |
| # Save to history | |
| save_to_history(audio_file.filename, transcript, lang) | |
| # Cleanup temp file | |
| os.remove(temp_path) | |
| return { | |
| "filename": audio_file.filename, | |
| "language": lang, | |
| "transcript": transcript | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Mount Gradio app on root | |
| demo = create_ui() | |
| app = gr.mount_gradio_app(api, demo, path="/") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| # Note: reload=True is disabled intentionally. | |
| # It causes subprocess workers that cannot find the app modules, | |
| # resulting in a blank Gradio page and hanging model loads. | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |