Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,73 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from models.model_wav2vec import Wav2VecIntent
|
| 4 |
-
from huggingface_hub import hf_hub_download
|
| 5 |
-
import torch
|
| 6 |
-
import soundfile as sf
|
| 7 |
-
import gradio as gr
|
| 8 |
-
import numpy as np
|
| 9 |
-
|
| 10 |
-
app = FastAPI()
|
| 11 |
-
|
| 12 |
-
# Download model from Hugging Face
|
| 13 |
-
MODEL_PATH = hf_hub_download(repo_id="avi292423/speech-intent-recognition-project", filename="wav2vec_best_model.pt")
|
| 14 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
-
|
| 16 |
-
label_map = {
|
| 17 |
-
"activate_lamp": 0, "activate_lights": 1, "activate_lights_bedroom": 2, "activate_lights_kitchen": 3,
|
| 18 |
-
"activate_lights_washroom": 4, "activate_music": 5, "bring_juice": 6, "bring_newspaper": 7,
|
| 19 |
-
"bring_shoes": 8, "bring_socks": 9, "change_language_Chinese": 10, "change_language_English": 11,
|
| 20 |
-
"change_language_German": 12, "change_language_Korean": 13, "change_language_none": 14,
|
| 21 |
-
"deactivate_lamp": 15, "deactivate_lights": 16, "deactivate_lights_bedroom": 17, "deactivate_lights_kitchen": 18,
|
| 22 |
-
"deactivate_lights_washroom": 19, "deactivate_music": 20, "decrease_heat": 21, "decrease_heat_bedroom": 22,
|
| 23 |
-
"decrease_heat_kitchen": 23, "decrease_heat_washroom": 24, "decrease_volume": 25, "increase_heat": 26,
|
| 24 |
-
"increase_heat_bedroom": 27, "increase_heat_kitchen": 28, "increase_heat_washroom": 29, "increase_volume": 30
|
| 25 |
-
}
|
| 26 |
-
index_to_label = {v: k for k, v in label_map.items()}
|
| 27 |
-
|
| 28 |
-
num_classes = 31
|
| 29 |
-
pretrained_model = "facebook/wav2vec2-
|
| 30 |
-
model = Wav2VecIntent(num_classes=num_classes, pretrained_model=pretrained_model).to(device)
|
| 31 |
-
state_dict = torch.load(MODEL_PATH, map_location=device)
|
| 32 |
-
model.load_state_dict(state_dict)
|
| 33 |
-
model.eval()
|
| 34 |
-
|
| 35 |
-
@app.post("/predict")
|
| 36 |
-
async def predict(file: UploadFile = File(...)):
|
| 37 |
-
audio_bytes = await file.read()
|
| 38 |
-
with open("temp.wav", "wb") as f:
|
| 39 |
-
f.write(audio_bytes)
|
| 40 |
-
audio, sample_rate = sf.read("temp.wav")
|
| 41 |
-
if sample_rate != 16000:
|
| 42 |
-
return JSONResponse({"error": "Audio must have a sample rate of 16kHz."}, status_code=400)
|
| 43 |
-
waveform = torch.tensor(audio, dtype=torch.float32).unsqueeze(0).to(device)
|
| 44 |
-
with torch.no_grad():
|
| 45 |
-
output = model(waveform)
|
| 46 |
-
predicted_class = torch.argmax(output, dim=1).item()
|
| 47 |
-
predicted_label = index_to_label.get(predicted_class, "Unknown Class")
|
| 48 |
-
return {"prediction": predicted_label}
|
| 49 |
-
|
| 50 |
-
def predict_intent(audio):
|
| 51 |
-
if audio is None:
|
| 52 |
-
return "No audio provided."
|
| 53 |
-
# Gradio provides (sample_rate, numpy array)
|
| 54 |
-
sr, y = audio
|
| 55 |
-
if sr != 16000:
|
| 56 |
-
return "Audio must have a sample rate of 16kHz."
|
| 57 |
-
waveform = torch.tensor(y, dtype=torch.float32).unsqueeze(0).to(device)
|
| 58 |
-
with torch.no_grad():
|
| 59 |
-
output = model(waveform)
|
| 60 |
-
predicted_class = torch.argmax(output, dim=1).item()
|
| 61 |
-
predicted_label = index_to_label.get(predicted_class, "Unknown Class")
|
| 62 |
-
return predicted_label
|
| 63 |
-
|
| 64 |
-
demo = gr.Interface(
|
| 65 |
-
fn=predict_intent,
|
| 66 |
-
inputs=gr.Audio(source="microphone", type="numpy", label="Record or Upload Audio (16kHz WAV)"),
|
| 67 |
-
outputs=gr.Textbox(label="Predicted Intent"),
|
| 68 |
-
title="Speech Intent Recognition",
|
| 69 |
-
description="Record or upload a 16kHz WAV audio file to predict the intent."
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
if __name__ == "__main__":
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from models.model_wav2vec import Wav2VecIntent
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import torch
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Download model from Hugging Face
|
| 13 |
+
MODEL_PATH = hf_hub_download(repo_id="avi292423/speech-intent-recognition-project", filename="wav2vec_best_model.pt")
|
| 14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
|
| 16 |
+
label_map = {
|
| 17 |
+
"activate_lamp": 0, "activate_lights": 1, "activate_lights_bedroom": 2, "activate_lights_kitchen": 3,
|
| 18 |
+
"activate_lights_washroom": 4, "activate_music": 5, "bring_juice": 6, "bring_newspaper": 7,
|
| 19 |
+
"bring_shoes": 8, "bring_socks": 9, "change_language_Chinese": 10, "change_language_English": 11,
|
| 20 |
+
"change_language_German": 12, "change_language_Korean": 13, "change_language_none": 14,
|
| 21 |
+
"deactivate_lamp": 15, "deactivate_lights": 16, "deactivate_lights_bedroom": 17, "deactivate_lights_kitchen": 18,
|
| 22 |
+
"deactivate_lights_washroom": 19, "deactivate_music": 20, "decrease_heat": 21, "decrease_heat_bedroom": 22,
|
| 23 |
+
"decrease_heat_kitchen": 23, "decrease_heat_washroom": 24, "decrease_volume": 25, "increase_heat": 26,
|
| 24 |
+
"increase_heat_bedroom": 27, "increase_heat_kitchen": 28, "increase_heat_washroom": 29, "increase_volume": 30
|
| 25 |
+
}
|
| 26 |
+
index_to_label = {v: k for k, v in label_map.items()}
|
| 27 |
+
|
| 28 |
+
num_classes = 31
|
| 29 |
+
pretrained_model = "facebook/wav2vec2-large" # Use base for less RAM, or keep large if needed
|
| 30 |
+
model = Wav2VecIntent(num_classes=num_classes, pretrained_model=pretrained_model).to(device)
|
| 31 |
+
state_dict = torch.load(MODEL_PATH, map_location=device)
|
| 32 |
+
model.load_state_dict(state_dict)
|
| 33 |
+
model.eval()
|
| 34 |
+
|
| 35 |
+
@app.post("/predict")
|
| 36 |
+
async def predict(file: UploadFile = File(...)):
|
| 37 |
+
audio_bytes = await file.read()
|
| 38 |
+
with open("temp.wav", "wb") as f:
|
| 39 |
+
f.write(audio_bytes)
|
| 40 |
+
audio, sample_rate = sf.read("temp.wav")
|
| 41 |
+
if sample_rate != 16000:
|
| 42 |
+
return JSONResponse({"error": "Audio must have a sample rate of 16kHz."}, status_code=400)
|
| 43 |
+
waveform = torch.tensor(audio, dtype=torch.float32).unsqueeze(0).to(device)
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
output = model(waveform)
|
| 46 |
+
predicted_class = torch.argmax(output, dim=1).item()
|
| 47 |
+
predicted_label = index_to_label.get(predicted_class, "Unknown Class")
|
| 48 |
+
return {"prediction": predicted_label}
|
| 49 |
+
|
| 50 |
+
def predict_intent(audio):
|
| 51 |
+
if audio is None:
|
| 52 |
+
return "No audio provided."
|
| 53 |
+
# Gradio provides (sample_rate, numpy array)
|
| 54 |
+
sr, y = audio
|
| 55 |
+
if sr != 16000:
|
| 56 |
+
return "Audio must have a sample rate of 16kHz."
|
| 57 |
+
waveform = torch.tensor(y, dtype=torch.float32).unsqueeze(0).to(device)
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
output = model(waveform)
|
| 60 |
+
predicted_class = torch.argmax(output, dim=1).item()
|
| 61 |
+
predicted_label = index_to_label.get(predicted_class, "Unknown Class")
|
| 62 |
+
return predicted_label
|
| 63 |
+
|
| 64 |
+
demo = gr.Interface(
|
| 65 |
+
fn=predict_intent,
|
| 66 |
+
inputs=gr.Audio(source="microphone", type="numpy", label="Record or Upload Audio (16kHz WAV)"),
|
| 67 |
+
outputs=gr.Textbox(label="Predicted Intent"),
|
| 68 |
+
title="Speech Intent Recognition",
|
| 69 |
+
description="Record or upload a 16kHz WAV audio file to predict the intent."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
demo.launch()
|