File size: 2,688 Bytes
223e341
 
 
5f2dcb0
 
 
223e341
 
5f2dcb0
 
 
223e341
 
5f2dcb0
 
223e341
 
2f4a269
 
 
5f2dcb0
2f4a269
5f2dcb0
223e341
5f2dcb0
223e341
 
5f2dcb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223e341
 
 
5f2dcb0
223e341
 
 
5f2dcb0
 
 
 
223e341
5f2dcb0
 
223e341
 
 
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
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import requests

# API endpoints with model names
NVIDIA_CONFORMER_API = "http://8.213.40.255/transcribe"  # nvidia-conformer-ctc-large-arabic
OPENAI_WHISPER_API = "http://8.213.32.123/transcribe?language=ar&task=transcribe&word_timestamps=false"  # openai-whisper-large-v3

def process_audio(audio_file):
    result_conformer = "No response from NVIDIA Conformer API"
    result_whisper = "No response from OpenAI Whisper API"
    
    try:
        with open(audio_file, "rb") as f:
            files = {"audio_file": f}
            response = requests.post(NVIDIA_CONFORMER_API, files=files)

        if response.status_code == 200:
            try:
                data = response.json()
                transcription = data.get("transcription", "No transcription found")
                result_conformer = f"πŸ“ NVIDIA Conformer Transcription: {transcription}"
            except ValueError:
                result_conformer = f"⚠️ NVIDIA Conformer could not parse JSON: {response.text}"
        else:
            result_conformer = f"❌ NVIDIA Conformer Error: {response.status_code} - {response.text}"

    except Exception as e:
        result_conformer = f"⚠️ NVIDIA Conformer Exception: {str(e)}"

    try:
        with open(audio_file, "rb") as f:
            files = {"file": (audio_file, f, "audio/wav")}
            headers = {"accept": "application/json"}
            response = requests.post(OPENAI_WHISPER_API, files=files, headers=headers)

        if response.status_code == 200:
            try:
                data = response.json()
                text = data.get("text", "No text found")
                result_whisper = f"πŸ“ OpenAI Whisper Transcription: {text}"
            except ValueError:
                result_whisper = f"⚠️ OpenAI Whisper could not parse JSON: {response.text}"
        else:
            result_whisper = f"❌ OpenAI Whisper Error: {response.status_code} - {response.text}"

    except Exception as e:
        result_whisper = f"⚠️ OpenAI Whisper Exception: {str(e)}"

    return result_conformer, result_whisper

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🎀 Record Audio and Send to NVIDIA Conformer & OpenAI Whisper APIs")

    with gr.Row():
        audio_input = gr.Audio(sources=["microphone"], type="filepath")
    
    with gr.Row():
        output_conformer = gr.Textbox(label="NVIDIA Conformer API Response")
        output_whisper = gr.Textbox(label="OpenAI Whisper API Response")

    submit_btn = gr.Button("Send to APIs")
    submit_btn.click(process_audio, inputs=audio_input, outputs=[output_conformer, output_whisper])

if __name__ == "__main__":
    demo.launch()