File size: 1,871 Bytes
cf2c908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddbc0b5
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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")

@api.post("/api/transcribe")
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)