Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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",
|
8 |
-
whisper_processor = WhisperProcessor.from_pretrained("donnamae/whisper-finetuned-cebuano-accent",
|
9 |
-
|
10 |
-
code_tokenizer = AutoTokenizer.from_pretrained("meta-llama/CodeLlama-7b-Instruct-hf")
|
11 |
-
code_model = AutoModelForCausalLM.from_pretrained(
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
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", 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()
|