Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
def process_audio(audio_file):
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
# Send the audio file to your API with the correct field name
|
| 10 |
with open(audio_file, "rb") as f:
|
| 11 |
-
files = {"audio_file": f}
|
| 12 |
-
response = requests.post(
|
| 13 |
|
| 14 |
if response.status_code == 200:
|
| 15 |
try:
|
| 16 |
data = response.json()
|
| 17 |
transcription = data.get("transcription", "No transcription found")
|
| 18 |
-
|
| 19 |
except ValueError:
|
| 20 |
-
|
| 21 |
else:
|
| 22 |
-
|
| 23 |
|
| 24 |
except Exception as e:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Gradio UI
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
-
gr.Markdown("## π€ Record Audio and Send to
|
| 30 |
|
| 31 |
with gr.Row():
|
| 32 |
audio_input = gr.Audio(sources=["microphone"], type="filepath")
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
submit_btn = gr.Button("Send to
|
| 36 |
-
submit_btn.click(process_audio, inputs=audio_input, outputs=
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# API endpoints with model names
|
| 5 |
+
NVIDIA_CONFORMER_API = "http://8.213.40.255/transcribe" # nvidia-conformer-ctc-large-arabic
|
| 6 |
+
OPENAI_WHISPER_API = "http://8.213.32.123/transcribe?language=ar&task=transcribe&word_timestamps=false" # openai-whisper-large-v3
|
| 7 |
|
| 8 |
def process_audio(audio_file):
|
| 9 |
+
result_conformer = "No response from NVIDIA Conformer API"
|
| 10 |
+
result_whisper = "No response from OpenAI Whisper API"
|
| 11 |
+
|
| 12 |
try:
|
|
|
|
| 13 |
with open(audio_file, "rb") as f:
|
| 14 |
+
files = {"audio_file": f}
|
| 15 |
+
response = requests.post(NVIDIA_CONFORMER_API, files=files)
|
| 16 |
|
| 17 |
if response.status_code == 200:
|
| 18 |
try:
|
| 19 |
data = response.json()
|
| 20 |
transcription = data.get("transcription", "No transcription found")
|
| 21 |
+
result_conformer = f"π NVIDIA Conformer Transcription: {transcription}"
|
| 22 |
except ValueError:
|
| 23 |
+
result_conformer = f"β οΈ NVIDIA Conformer could not parse JSON: {response.text}"
|
| 24 |
else:
|
| 25 |
+
result_conformer = f"β NVIDIA Conformer Error: {response.status_code} - {response.text}"
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
+
result_conformer = f"β οΈ NVIDIA Conformer Exception: {str(e)}"
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
with open(audio_file, "rb") as f:
|
| 32 |
+
files = {"file": (audio_file, f, "audio/wav")}
|
| 33 |
+
headers = {"accept": "application/json"}
|
| 34 |
+
response = requests.post(OPENAI_WHISPER_API, files=files, headers=headers)
|
| 35 |
+
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
try:
|
| 38 |
+
data = response.json()
|
| 39 |
+
text = data.get("text", "No text found")
|
| 40 |
+
result_whisper = f"π OpenAI Whisper Transcription: {text}"
|
| 41 |
+
except ValueError:
|
| 42 |
+
result_whisper = f"β οΈ OpenAI Whisper could not parse JSON: {response.text}"
|
| 43 |
+
else:
|
| 44 |
+
result_whisper = f"β OpenAI Whisper Error: {response.status_code} - {response.text}"
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
result_whisper = f"β οΈ OpenAI Whisper Exception: {str(e)}"
|
| 48 |
+
|
| 49 |
+
return result_conformer, result_whisper
|
| 50 |
|
| 51 |
# Gradio UI
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("## π€ Record Audio and Send to NVIDIA Conformer & OpenAI Whisper APIs")
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
audio_input = gr.Audio(sources=["microphone"], type="filepath")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
output_conformer = gr.Textbox(label="NVIDIA Conformer API Response")
|
| 60 |
+
output_whisper = gr.Textbox(label="OpenAI Whisper API Response")
|
| 61 |
|
| 62 |
+
submit_btn = gr.Button("Send to APIs")
|
| 63 |
+
submit_btn.click(process_audio, inputs=audio_input, outputs=[output_conformer, output_whisper])
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|