| import gradio as gr |
| from transformers import pipeline |
| import soundfile as sf |
| import librosa |
| import numpy as np |
| from fastapi import FastAPI |
| from fastapi.middleware.cors import CORSMiddleware |
|
|
| |
| MODEL_ID = "svu22/arabic_model" |
|
|
| |
|
|
| try: |
| pipe = pipeline("automatic-speech-recognition", model=MODEL_ID) |
| print("Pipeline loaded successfully!") |
| except Exception as e: |
| print(f"Error loading pipeline: {e}") |
| pipe = None |
|
|
| def transcribe_audio(audio_input): |
| if pipe is None: |
| return "Error: Model pipeline could not be loaded. Please check the logs." |
| if audio_input is None: |
| return "Please upload an audio file." |
|
|
| filepath = audio_input |
| try: |
| audio_array, sampling_rate = sf.read(filepath) |
| if audio_array.ndim > 1: |
| audio_array = audio_array.mean(axis=1) |
| if sampling_rate != 16000: |
| audio_array = librosa.resample(y=audio_array, orig_sr=sampling_rate, target_sr=16000) |
| |
| result = pipe(audio_array.copy()) |
| transcription = result["text"] |
| return transcription |
| except Exception as e: |
| print(f"An error occurred during transcription: {e}") |
| return f"An error occurred: {str(e)}" |
|
|
| |
| |
| |
| with gr.Blocks() as interface: |
| gr.Markdown("## Automatic Speech Recognition for Arabic") |
| gr.Markdown("This is a demo for the fine-tuned Wav2Vec2 model for Arabic ASR. Upload an audio file or record your voice to see the transcription.") |
| |
| with gr.Row(): |
| audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio File") |
| transcribed_text = gr.Textbox(label="Result", interactive=False) |
|
|
| submit_btn = gr.Button("Transcribe") |
|
|
| submit_btn.click( |
| fn=transcribe_audio, |
| inputs=audio_input, |
| outputs=transcribed_text, |
| api_name="predict" |
| ) |
| |
| |
|
|
| |
| |
| |
| app = FastAPI() |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| app = gr.mount_gradio_app(app, interface, path="/") |
|
|
|
|