Hawoly18 commited on
Commit
99640fb
1 Parent(s): f91de80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -49
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
- model_name = "Hawoly18/Adia_Llama3.1"
8
-
9
- # Vérifier si un GPU est disponible
10
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
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
- demo = gr.ChatInterface(
44
- respond,
45
- additional_inputs=[
46
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
47
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
48
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), # Fixed syntax error
49
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
50
- ],
51
- title="Chatbot Interface"
 
 
 
 
 
 
 
 
52
  )
53
 
54
- if __name__ == "__main__":
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)