Tala2025 commited on
Commit
d3201b5
·
verified ·
1 Parent(s): a1cba75

Create whisper2_asr.py

Browse files
Files changed (1) hide show
  1. whisper2_asr.py +35 -0
whisper2_asr.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # Set the model ID for Whisper small English model
5
+ model_id = "openai/whisper-small.en"
6
+
7
+ # Function to send audio to Hugging Face Inference API and get transcription
8
+ def transcribe(audio):
9
+ if audio is None:
10
+ return "No audio provided."
11
+
12
+ API_URL = f"https://api-inference.huggingface.co/models/{model_id}"
13
+ headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"}
14
+
15
+ # Read and send the audio file
16
+ with open(audio, "rb") as f:
17
+ data = f.read()
18
+ response = requests.post(API_URL, headers=headers, data=data)
19
+
20
+ # Return the transcription or error
21
+ if response.status_code == 200:
22
+ return response.json().get("text", "No text returned.")
23
+ else:
24
+ return f"Error: {response.status_code} - {response.text}"
25
+
26
+ # Gradio Interface
27
+ interface = gr.Interface(
28
+ fn=transcribe,
29
+ inputs=gr.Audio(source="upload", type="filepath", label="Upload Audio"),
30
+ outputs=gr.Textbox(label="Transcribed Text"),
31
+ title="Speech Recognition with Whisper",
32
+ description="Upload an audio file and get the transcribed text using OpenAI Whisper (small.en)."
33
+ )
34
+
35
+ interface.launch()