Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,40 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
-
from typing import List, Tuple
|
4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
15 |
-
|
16 |
-
def respond(
|
17 |
-
message: str,
|
18 |
-
history: List[Tuple[str, str]],
|
19 |
-
system_message: str,
|
20 |
-
max_tokens: int,
|
21 |
-
temperature: float,
|
22 |
-
top_p: float,
|
23 |
-
) -> str:
|
24 |
-
|
25 |
-
prompt = system_message
|
26 |
-
for user_msg, assistant_msg in history:
|
27 |
-
prompt += f"\nUser: {user_msg}\nAssistant: {assistant_msg}"
|
28 |
-
prompt += f"\nUser: {message}\nAssistant:"
|
29 |
-
|
30 |
-
|
31 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
32 |
-
outputs = model.generate(
|
33 |
-
**inputs,
|
34 |
-
max_length=max_tokens,
|
35 |
-
temperature=temperature,
|
36 |
-
top_p=top_p,
|
37 |
-
do_sample=True,
|
38 |
-
)
|
39 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Assistant:")[-1].strip()
|
40 |
-
return response
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
-
|
55 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import (
|
4 |
+
AutomaticSpeechRecognitionPipeline,
|
5 |
+
WhisperForConditionalGeneration,
|
6 |
+
WhisperTokenizer,
|
7 |
+
WhisperProcessor,
|
8 |
+
)
|
9 |
+
from peft import PeftModel, PeftConfig
|
10 |
|
11 |
+
peft_model_id = "Moustapha91/whisper-small-wolof"
|
12 |
+
language = "French"
|
13 |
+
task = "transcribe"
|
14 |
|
15 |
+
peft_config = PeftConfig.from_pretrained(peft_model_id)
|
16 |
+
model = WhisperForConditionalGeneration.from_pretrained(
|
17 |
+
peft_config.base_model_name_or_path,
|
18 |
+
device_map="auto" # On supprime la quantization en 8 bits
|
19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
22 |
+
tokenizer = WhisperTokenizer.from_pretrained(peft_config.base_model_name_or_path, language=language, task=task)
|
23 |
+
processor = WhisperProcessor.from_pretrained(peft_config.base_model_name_or_path, language=language, task=task)
|
24 |
+
feature_extractor = processor.feature_extractor
|
25 |
+
forced_decoder_ids = processor.get_decoder_prompt_ids(language=language, task=task)
|
26 |
+
pipe = AutomaticSpeechRecognitionPipeline(model=model, tokenizer=tokenizer, feature_extractor=feature_extractor)
|
27 |
+
|
28 |
+
def transcribe(audio):
|
29 |
+
text = pipe(audio, generate_kwargs={"forced_decoder_ids": forced_decoder_ids}, max_new_tokens=255)["text"]
|
30 |
+
return text
|
31 |
+
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=transcribe,
|
34 |
+
inputs=gr.Audio(type="filepath"), # On supprime 'source' pour éviter l'erreur
|
35 |
+
outputs="text",
|
36 |
+
title="PEFT LoRA + Whisper Small Wolof",
|
37 |
+
description="Realtime demo for Wolof speech recognition using `PEFT-LoRA` fine-tuned Whisper Small model.",
|
38 |
)
|
39 |
|
40 |
+
iface.launch(share=True)
|
|