101Frost commited on
Commit
5f2dcb0
Β·
verified Β·
1 Parent(s): 2f4a269

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -1,39 +1,66 @@
1
  import gradio as gr
2
  import requests
3
 
4
- # Your API endpoint (replace with your own)
5
- API_URL = "http://8.213.40.255/transcribe"
 
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} # <-- match API spec
12
- response = requests.post(API_URL, files=files)
13
 
14
  if response.status_code == 200:
15
  try:
16
  data = response.json()
17
  transcription = data.get("transcription", "No transcription found")
18
- return f"πŸ“ Transcription: {transcription}"
19
  except ValueError:
20
- return f"⚠️ Could not parse JSON: {response.text}"
21
  else:
22
- return f"❌ Error: {response.status_code} - {response.text}"
23
 
24
  except Exception as e:
25
- return f"⚠️ Exception: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # Gradio UI
28
  with gr.Blocks() as demo:
29
- gr.Markdown("## 🎀 Record Audio and Send to API")
30
 
31
  with gr.Row():
32
  audio_input = gr.Audio(sources=["microphone"], type="filepath")
33
- output_text = gr.Textbox(label="API Response")
 
 
 
34
 
35
- submit_btn = gr.Button("Send to API")
36
- submit_btn.click(process_audio, inputs=audio_input, outputs=output_text)
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()