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

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +39 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ librosa
4
+ gradio