donnamae commited on
Commit
1fad1ae
Β·
verified Β·
1 Parent(s): 8753de2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -39
app.py CHANGED
@@ -1,39 +1,44 @@
1
- import torch
2
- import librosa
3
- import gradio as gr
4
- from transformers import WhisperProcessor, WhisperForConditionalGeneration, AutoTokenizer, AutoModelForCausalLM
5
-
6
- # Load models from the Space or from Hugging Face Hub
7
- whisper_model = WhisperForConditionalGeneration.from_pretrained("donnamae/whisper-finetuned-cebuano-accent", use_auth_token=True)
8
- whisper_processor = WhisperProcessor.from_pretrained("donnamae/whisper-finetuned-cebuano-accent", use_auth_token=True)
9
-
10
- code_tokenizer = AutoTokenizer.from_pretrained("meta-llama/CodeLlama-7b-Instruct-hf")
11
- code_model = AutoModelForCausalLM.from_pretrained("meta-llama/CodeLlama-7b-Instruct-hf").to("cuda" if torch.cuda.is_available() else "cpu")
12
-
13
- def transcribe_and_generate(audio):
14
- audio_data, sr = librosa.load(audio, sr=16000)
15
- input_features = whisper_processor(audio_data, sampling_rate=sr, return_tensors="pt").input_features
16
- predicted_ids = whisper_model.generate(input_features)
17
- transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
18
-
19
- # Format prompt for code generation
20
- prompt = f"# Task: {transcription.strip()}\n\n```python\n"
21
- inputs = code_tokenizer(prompt, return_tensors="pt").to(code_model.device)
22
-
23
- outputs = code_model.generate(**inputs, max_length=256)
24
- generated_text = code_tokenizer.decode(outputs[0], skip_special_tokens=True)
25
-
26
- # Extract clean code
27
- generated_code = generated_text.replace(prompt, "").strip().split("```")[0]
28
-
29
- return transcription, generated_code
30
-
31
- demo = gr.Interface(
32
- fn=transcribe_and_generate,
33
- inputs=gr.Audio(type="filepath"),
34
- outputs=[gr.Text(label="Transcribed Command"), gr.Code(label="Generated Code")],
35
- title="Voice-to-Code Generator",
36
- description="Speak your coding command. The system will transcribe and generate the corresponding code."
37
- )
38
-
39
- demo.launch()
 
 
 
 
 
 
1
+ import torch
2
+ import librosa
3
+ import gradio as gr
4
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration, AutoTokenizer, AutoModelForCausalLM
5
+
6
+ # Load models from the Space or from Hugging Face Hub
7
+ whisper_model = WhisperForConditionalGeneration.from_pretrained("donnamae/whisper-finetuned-cebuano-accent", token=True)
8
+ whisper_processor = WhisperProcessor.from_pretrained("donnamae/whisper-finetuned-cebuano-accent", token=True)
9
+
10
+ code_tokenizer = AutoTokenizer.from_pretrained("meta-llama/CodeLlama-7b-Instruct-hf")
11
+ code_model = AutoModelForCausalLM.from_pretrained(
12
+ "meta-llama/CodeLlama-7b-Instruct-hf",
13
+ torch_dtype="auto",
14
+ device_map="auto",
15
+ trust_remote_code=True
16
+ ).to("cuda" if torch.cuda.is_available() else "cpu")
17
+
18
+ def transcribe_and_generate(audio):
19
+ audio_data, sr = librosa.load(audio, sr=16000)
20
+ input_features = whisper_processor(audio_data, sampling_rate=sr, return_tensors="pt").input_features
21
+ predicted_ids = whisper_model.generate(input_features)
22
+ transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
23
+
24
+ # Format prompt for code generation
25
+ prompt = f"# Task: {transcription.strip()}\n\n```python\n"
26
+ inputs = code_tokenizer(prompt, return_tensors="pt").to(code_model.device)
27
+
28
+ outputs = code_model.generate(**inputs, max_length=256)
29
+ generated_text = code_tokenizer.decode(outputs[0], skip_special_tokens=True)
30
+
31
+ # Extract clean code
32
+ generated_code = generated_text.replace(prompt, "").strip().split("```")[0]
33
+
34
+ return transcription, generated_code
35
+
36
+ demo = gr.Interface(
37
+ fn=transcribe_and_generate,
38
+ inputs=gr.Audio(type="filepath"),
39
+ outputs=[gr.Text(label="Transcribed Command"), gr.Code(label="Generated Code")],
40
+ title="Voice-to-Code Generator",
41
+ description="Speak your coding command. The system will transcribe and generate the corresponding code."
42
+ )
43
+
44
+ demo.launch()