Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
|
| 4 |
+
# 1. Load the model into memory (Runs once on startup)
|
| 5 |
+
# "base" is a good balance of speed and accuracy for a free CPU
|
| 6 |
+
print("Loading Whisper model...")
|
| 7 |
+
model = whisper.load_model("base")
|
| 8 |
+
|
| 9 |
+
# 2. Define the inference function
|
| 10 |
+
def transcribe_audio(audio_filepath):
|
| 11 |
+
print(f"Processing audio: {audio_filepath}")
|
| 12 |
+
|
| 13 |
+
# Run inference, forcing Arabic and extracting timestamps
|
| 14 |
+
result = model.transcribe(
|
| 15 |
+
audio_filepath,
|
| 16 |
+
language="ar",
|
| 17 |
+
word_timestamps=True
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Return a clean dictionary
|
| 21 |
+
return {
|
| 22 |
+
"text": result["text"],
|
| 23 |
+
"segments": result["segments"]
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# 3. Create the API routing interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=transcribe_audio,
|
| 29 |
+
inputs=gr.Audio(type="filepath", label="Upload Arabic Audio"),
|
| 30 |
+
outputs=gr.JSON(label="Transcription Data"),
|
| 31 |
+
title="Arabic Speech-to-Text Microservice"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# 4. Launch the server
|
| 35 |
+
demo.launch()
|