Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import os # Import os module to handle file paths
|
| 4 |
+
|
| 5 |
+
# Load the transcription model
|
| 6 |
+
# Using a smaller model that might fit in memory, or consider running on CPU if memory is still an issue
|
| 7 |
+
# For demonstration, let's try 'openai/whisper-small'
|
| 8 |
+
print("🎤 Loading transcription pipeline...")
|
| 9 |
+
try:
|
| 10 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cuda:0") # Explicitly use GPU if available
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"Could not load model on GPU: {e}. Trying on CPU.")
|
| 13 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cpu") # Fallback to CPU
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def transcribe_audio(audio_file_path):
|
| 17 |
+
"""
|
| 18 |
+
Transcribes the audio file using the loaded pipeline.
|
| 19 |
+
"""
|
| 20 |
+
if audio_file_path is None:
|
| 21 |
+
return "Please upload an audio file."
|
| 22 |
+
|
| 23 |
+
# Ensure the file path is accessible
|
| 24 |
+
if not os.path.exists(audio_file_path):
|
| 25 |
+
return f"Error: Audio file not found at {audio_file_path}"
|
| 26 |
+
|
| 27 |
+
print(f" transcribe the audio file: {audio_file_path}")
|
| 28 |
+
# Run transcription on the audio file with chunking
|
| 29 |
+
# Adjust chunk_length_s and return_timestamps as needed for your audio
|
| 30 |
+
try:
|
| 31 |
+
transcription_result = transcriber(audio_file_path, chunk_length_s=30, return_timestamps=True)
|
| 32 |
+
return transcription_result["text"]
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error during transcription: {e}"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Create the Gradio interface
|
| 38 |
+
print("🚀 Creating Gradio interface for Transcription...")
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=transcribe_audio,
|
| 41 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"), # Use type="filepath" to get the path to the temporary file
|
| 42 |
+
outputs=gr.Textbox(label="Transcription"),
|
| 43 |
+
title="Audio Transcription Pipeline",
|
| 44 |
+
description="Upload an audio file (e.g., MP3, WAV) to get a transcription."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Launch the interface
|
| 48 |
+
print("✨ Launching Gradio interface...")
|
| 49 |
+
# Set share=True to get a public link for sharing (optional)
|
| 50 |
+
iface.launch(debug=True, share=True)
|
| 51 |
+
print("\n✅ Gradio interface launched.")
|