Bouaziz-bad commited on
Commit
8a6c8ee
·
1 Parent(s): 86f8c42

Add audio transcription with Whisper

Browse files
Files changed (3) hide show
  1. README.md +15 -9
  2. app.py +41 -7
  3. example.wav +0 -0
README.md CHANGED
@@ -1,11 +1,17 @@
1
  ---
2
- title: Kab Hsf Test
3
- emoji: 👀
4
- colorFrom: yellow
5
- colorTo: red
6
- sdk: docker
7
- app_port: 7860
8
- pinned: false
9
- license: apache-2.0
10
- short_description: Testing the HSF environment
 
 
 
 
 
 
11
  ---
 
1
  ---
2
+ title: Audio Transcriber
3
+ emoji: 🎙️
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: gradio
7
+ app_file: app.py
8
+ python_version: "3.10"
9
+ short_description: Upload an audio file and get a transcription using Whisper.
10
+ tags:
11
+ - audio
12
+ - speech-to-text
13
+ - whisper
14
+ - transcription
15
+ models:
16
+ - openai/whisper-small
17
  ---
app.py CHANGED
@@ -1,9 +1,43 @@
1
- from flask import Flask
2
- app = Flask(__name__)
 
3
 
4
- @app.route("/")
5
- def hello():
6
- return "<h1>Hello, World from Hugging Face Space!</h1>"
 
 
 
 
 
7
 
8
- if __name__ == "__main__":
9
- app.run(host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
4
 
5
+ # Load the Whisper model for speech recognition
6
+ device = 0 if torch.cuda.is_available() else -1
7
+ transcriber = pipeline(
8
+ "automatic-speech-recognition",
9
+ model="openai/whisper-small",
10
+ chunk_length_s=30,
11
+ device=device
12
+ )
13
 
14
+ def transcribe_audio(audio_file):
15
+ """
16
+ Transcribe uploaded audio file.
17
+ Args:
18
+ audio_file (str): Path to the audio file
19
+ Returns:
20
+ str: Transcribed text
21
+ """
22
+ if audio_file is None:
23
+ return "Please upload an audio file."
24
+
25
+ # Run transcription
26
+ result = transcriber(audio_file)
27
+ return result["text"]
28
+
29
+ # Define the Gradio interface
30
+ demo = gr.Interface(
31
+ fn=transcribe_audio,
32
+ inputs=gr.Audio(sources=["upload", "microphone"], type="filepath"),
33
+ outputs=gr.Textbox(label="Transcription", lines=8),
34
+ title="🎙️ Audio Transcription with Whisper",
35
+ description="Upload an audio file or record directly to transcribe it using OpenAI's Whisper model.",
36
+ examples=[
37
+ ["example.wav"] # Optional: add a sample audio file named example.wav
38
+ ],
39
+ allow_flagging="never"
40
+ )
41
+
42
+ # Launch the app
43
+ demo.launch()
example.wav ADDED
Binary file (84.7 kB). View file