File size: 1,162 Bytes
d3201b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests

# Set the model ID for Whisper small English model
model_id = "openai/whisper-small.en"

# Function to send audio to Hugging Face Inference API and get transcription
def transcribe(audio):
    if audio is None:
        return "No audio provided."

    API_URL = f"https://api-inference.huggingface.co/models/{model_id}"
    headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"}

    # Read and send the audio file
    with open(audio, "rb") as f:
        data = f.read()
    response = requests.post(API_URL, headers=headers, data=data)

    # Return the transcription or error
    if response.status_code == 200:
        return response.json().get("text", "No text returned.")
    else:
        return f"Error: {response.status_code} - {response.text}"

# Gradio Interface
interface = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(source="upload", type="filepath", label="Upload Audio"),
    outputs=gr.Textbox(label="Transcribed Text"),
    title="Speech Recognition with Whisper",
    description="Upload an audio file and get the transcribed text using OpenAI Whisper (small.en)."
)

interface.launch()