Delete app.py
Browse files
app.py
DELETED
|
@@ -1,153 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import os
|
| 4 |
-
import numpy as np
|
| 5 |
-
import soundfile as sf
|
| 6 |
-
import tempfile
|
| 7 |
-
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 8 |
-
from peft import PeftModel
|
| 9 |
-
from huggingface_hub import snapshot_download
|
| 10 |
-
|
| 11 |
-
# ── Config ────────────────────────────────────────────────────────
|
| 12 |
-
BASE_MODEL = "openai/whisper-large-v3-turbo"
|
| 13 |
-
ADAPTER_REPO = "mozed54/whisper-finetuned"
|
| 14 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
-
|
| 16 |
-
# ── Chargement ────────────────────────────────────────────────────
|
| 17 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
-
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 19 |
-
|
| 20 |
-
print(f"Device : {device.upper()}")
|
| 21 |
-
print("Chargement du modele de base...")
|
| 22 |
-
|
| 23 |
-
base_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 24 |
-
BASE_MODEL,
|
| 25 |
-
torch_dtype=torch_dtype,
|
| 26 |
-
use_safetensors=True,
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
print("Chargement adapter LoRA...")
|
| 30 |
-
adapter_path = snapshot_download(
|
| 31 |
-
repo_id=ADAPTER_REPO,
|
| 32 |
-
repo_type="dataset",
|
| 33 |
-
token=HF_TOKEN,
|
| 34 |
-
)
|
| 35 |
-
model = PeftModel.from_pretrained(base_model, adapter_path)
|
| 36 |
-
model = model.merge_and_unload()
|
| 37 |
-
model.to(device)
|
| 38 |
-
|
| 39 |
-
processor = AutoProcessor.from_pretrained(BASE_MODEL)
|
| 40 |
-
|
| 41 |
-
pipe = pipeline(
|
| 42 |
-
"automatic-speech-recognition",
|
| 43 |
-
model=model,
|
| 44 |
-
tokenizer=processor.tokenizer,
|
| 45 |
-
feature_extractor=processor.feature_extractor,
|
| 46 |
-
torch_dtype=torch_dtype,
|
| 47 |
-
device=device,
|
| 48 |
-
generate_kwargs={"language": "arabic", "task": "transcribe"},
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
print("Modele pret !")
|
| 52 |
-
|
| 53 |
-
# ── Fonctions ─────────────────────────────────────────────────────
|
| 54 |
-
def process_audio(audio):
|
| 55 |
-
"""Accepte tuple (sr, array) ou filepath string"""
|
| 56 |
-
if audio is None:
|
| 57 |
-
return None
|
| 58 |
-
|
| 59 |
-
if isinstance(audio, tuple):
|
| 60 |
-
sample_rate, data = audio
|
| 61 |
-
if data.ndim > 1:
|
| 62 |
-
data = data.mean(axis=1)
|
| 63 |
-
data = data.astype(np.float32)
|
| 64 |
-
if np.abs(data).max() > 1.0:
|
| 65 |
-
data = data / 32768.0
|
| 66 |
-
tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
| 67 |
-
sf.write(tmp.name, data, sample_rate)
|
| 68 |
-
return tmp.name
|
| 69 |
-
|
| 70 |
-
return audio # déjà un filepath
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def transcribe(audio):
|
| 74 |
-
path = process_audio(audio)
|
| 75 |
-
if path is None:
|
| 76 |
-
return "Aucun audio recu."
|
| 77 |
-
try:
|
| 78 |
-
result = pipe(path, return_timestamps=False)
|
| 79 |
-
return result["text"].strip()
|
| 80 |
-
except Exception as e:
|
| 81 |
-
return f"Erreur : {str(e)}"
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# ── CSS ───────────────────────────────────────────────────────────
|
| 85 |
-
css = """
|
| 86 |
-
@import url("https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600&display=swap");
|
| 87 |
-
body, .gradio-container { font-family: "DM Sans", sans-serif !important; background: #0f0f13 !important; }
|
| 88 |
-
.gradio-container { max-width: 860px !important; margin: 0 auto !important; padding: 2rem 1.5rem !important; }
|
| 89 |
-
#app-header { text-align: center; margin-bottom: 2rem; padding: 2rem 0 1.5rem; border-bottom: 1px solid #1e1e2a; }
|
| 90 |
-
#app-header h1 { font-size: 2rem; font-weight: 600; color: #f0f0f5; margin: 0 0 0.4rem; }
|
| 91 |
-
#app-header p { font-size: 0.9rem; color: #6b6b80; margin: 0; }
|
| 92 |
-
.section-label { font-size: 0.72rem; font-weight: 600; color: #4a4a60; letter-spacing: 0.12em; text-transform: uppercase; margin-bottom: 0.6rem; }
|
| 93 |
-
button.primary { background: #5a5aff !important; color: #fff !important; border: none !important; border-radius: 10px !important; font-weight: 500 !important; padding: 0.7rem 1.4rem !important; transition: background 0.2s !important; }
|
| 94 |
-
button.primary:hover { background: #4848e0 !important; }
|
| 95 |
-
button.secondary { background: #1e1e2a !important; color: #a0a0b8 !important; border: 1px solid #2a2a38 !important; border-radius: 10px !important; padding: 0.7rem 1.4rem !important; }
|
| 96 |
-
#transcription-box textarea { background: #0f0f13 !important; border: 1.5px solid #1e1e2a !important; border-radius: 14px !important; color: #e0e0ec !important; font-size: 1rem !important; line-height: 1.7 !important; padding: 1.2rem !important; direction: rtl !important; text-align: right !important; transition: border-color 0.4s, box-shadow 0.4s !important; min-height: 160px !important; }
|
| 97 |
-
#transcription-box.has-text textarea { border-color: #22c97a !important; box-shadow: 0 0 0 3px rgba(34,201,122,0.12) !important; }
|
| 98 |
-
footer { display: none !important; }
|
| 99 |
-
"""
|
| 100 |
-
|
| 101 |
-
js = """
|
| 102 |
-
function() {
|
| 103 |
-
const observer = new MutationObserver(() => {
|
| 104 |
-
const box = document.querySelector("#transcription-box textarea");
|
| 105 |
-
const wrapper = document.querySelector("#transcription-box");
|
| 106 |
-
if (!box || !wrapper) return;
|
| 107 |
-
if (box.value && box.value.trim().length > 0) { wrapper.classList.add("has-text"); }
|
| 108 |
-
else { wrapper.classList.remove("has-text"); }
|
| 109 |
-
});
|
| 110 |
-
observer.observe(document.body, { childList: true, subtree: true, characterData: true });
|
| 111 |
-
return [];
|
| 112 |
-
}
|
| 113 |
-
"""
|
| 114 |
-
|
| 115 |
-
# ── Interface ─────────────────────────────────────────────────────
|
| 116 |
-
with gr.Blocks(title="Whisper Darija", css=css, js=js) as demo:
|
| 117 |
-
|
| 118 |
-
gr.HTML('<div id="app-header"><h1>🎙 Whisper · Darija</h1><p>Transcription automatique — dialecte algerien</p></div>')
|
| 119 |
-
|
| 120 |
-
gr.HTML('<div class="section-label">Source audio</div>')
|
| 121 |
-
with gr.Row():
|
| 122 |
-
with gr.Column(scale=1):
|
| 123 |
-
mic_input = gr.Audio(
|
| 124 |
-
sources=["microphone"],
|
| 125 |
-
type="numpy",
|
| 126 |
-
label="🎤 Enregistrer ma voix",
|
| 127 |
-
)
|
| 128 |
-
mic_btn = gr.Button("Transcrire la voix", variant="primary")
|
| 129 |
-
|
| 130 |
-
with gr.Column(scale=1):
|
| 131 |
-
file_input = gr.Audio(
|
| 132 |
-
sources=["upload"],
|
| 133 |
-
type="numpy",
|
| 134 |
-
label="📂 Charger un fichier audio",
|
| 135 |
-
)
|
| 136 |
-
file_btn = gr.Button("Transcrire le fichier", variant="secondary")
|
| 137 |
-
|
| 138 |
-
gr.HTML("<hr style='border:none;border-top:1px solid #1e1e2a;margin:1.5rem 0;'>")
|
| 139 |
-
gr.HTML('<div class="section-label">Transcription</div>')
|
| 140 |
-
|
| 141 |
-
output = gr.Textbox(
|
| 142 |
-
label="",
|
| 143 |
-
lines=6,
|
| 144 |
-
placeholder="النص سيظهر هنا",
|
| 145 |
-
interactive=False,
|
| 146 |
-
elem_id="transcription-box",
|
| 147 |
-
show_label=False,
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
mic_btn.click(fn=transcribe, inputs=[mic_input], outputs=[output])
|
| 151 |
-
file_btn.click(fn=transcribe, inputs=[file_input], outputs=[output])
|
| 152 |
-
|
| 153 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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