Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| import librosa | |
| import numpy as np | |
| from transformers import AutoModelForAudioClassification, AutoFeatureExtractor | |
| # ========= 1. LOAD MODEL (ุงูุชุนุฏูู ููุง) ========= | |
| # ุงุณุชุฎุฏู ูุง AutoModel ุนุดุงู ูู ุงููู ุจูุนุฑู ููุฑุฃ ู ููุงุช .safetensors ู .json | |
| model_path = "." # ุงูููุทุฉ ุชุนูู ุงููููุฏุฑ ุงูุญุงูู ุงููู ููู ุงูู ููุงุช | |
| model = AutoModelForAudioClassification.from_pretrained(model_path) | |
| feature_extractor = AutoFeatureExtractor.from_pretrained(model_path) | |
| model.eval() | |
| # ========= 2. PREPROCESS AUDIO (ุชุนุฏูู ุจุณูุท ูููุงุณุจ Transformers) ========= | |
| def predict(audio_path): | |
| # ุชุญู ูู ุงูุตูุช | |
| audio, sr = librosa.load(audio_path, sr=feature_extractor.sampling_rate) | |
| # ุชุญููู ุงูุตูุช ูู Tensors ู ูุงุณุจุฉ ููู ูุฏูู | |
| inputs = feature_extractor(audio, sampling_rate=sr, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| # ุงูุญุตูู ุนูู ุงูุชููุน (Label) | |
| predicted_class_ids = torch.argmax(logits, dim=-1).item() | |
| prediction = model.config.id2label[predicted_class_ids] | |
| return prediction | |
| # ========= 3. UI (ุฒู ู ุง ูู ู ุน ุชุนุฏูู ุจุณูุท ูู ุงูู fn) ========= | |
| interface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Audio(type="filepath", label="Upload Audio ๐ค"), | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="Audio AI Model ๐ง" | |
| ) | |
| interface.launch() |