Spaces:
Sleeping
Sleeping
Rename app (22).py to app.py
Browse files- app (22).py +0 -50
- app.py +56 -0
app (22).py
DELETED
|
@@ -1,50 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
import os
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
# --- Performance Improvement ---
|
| 7 |
-
# Configure PyTorch for CPU performance
|
| 8 |
-
num_cpu_cores = os.cpu_count() or 1 # Default to 1 if os.cpu_count() is None
|
| 9 |
-
torch.set_num_threads(num_cpu_cores)
|
| 10 |
-
print(f"✅ PyTorch is configured to use {num_cpu_cores} CPU cores.")
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# --- Model and Pipeline ---
|
| 14 |
-
# Initialize the pipeline. It will default to the CPU.
|
| 15 |
-
# Using a specific revision for reproducibility
|
| 16 |
-
pipe = pipeline(
|
| 17 |
-
"audio-classification",
|
| 18 |
-
model="MIT/ast-finetuned-audioset-10-10-0.4593"
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# --- Core Logic Function ---
|
| 23 |
-
def classify_audio(audio):
|
| 24 |
-
"""
|
| 25 |
-
Classifies the audio, takes the top 3 predictions,
|
| 26 |
-
and formats them into a single, human-readable string.
|
| 27 |
-
"""
|
| 28 |
-
if audio is None:
|
| 29 |
-
return "Please upload an audio file first."
|
| 30 |
-
|
| 31 |
-
result = pipe(audio)
|
| 32 |
-
return {label['label']: label['score'] for label in result}
|
| 33 |
-
|
| 34 |
-
# --- Gradio Interface ---
|
| 35 |
-
# Create the Gradio app interface
|
| 36 |
-
app = gr.Interface(
|
| 37 |
-
fn=classify_audio,
|
| 38 |
-
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 39 |
-
outputs=gr.Label(num_top_classes=3), # This will now receive a simple string
|
| 40 |
-
title="Audio Classification with MIT/AST",
|
| 41 |
-
description=(
|
| 42 |
-
"Upload an audio file to classify it. The model will identify the top 3 most likely sound categories. "
|
| 43 |
-
),
|
| 44 |
-
cache_examples=False,
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
# --- App Launch ---
|
| 48 |
-
# Launch the app with sharing enabled for Hugging Face Spaces
|
| 49 |
-
if __name__ == "__main__":
|
| 50 |
-
app.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
# ── Аптымізацыя CPU (падае запасны варыянт, калі GPU няма) ──
|
| 8 |
+
num_cpu_cores = os.cpu_count() or 1
|
| 9 |
+
torch.set_num_threads(num_cpu_cores)
|
| 10 |
+
print(f"✅ PyTorch настроены на {num_cpu_cores} ядраў CPU.")
|
| 11 |
+
|
| 12 |
+
# ── Ініцыялізацыя мадэлі ──
|
| 13 |
+
pipe = pipeline(
|
| 14 |
+
task="audio-classification",
|
| 15 |
+
model="MIT/ast-finetuned-audioset-10-10-0.448"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Флаг, каб не пераносіць мадэль на GPU паўторна
|
| 19 |
+
_model_on_gpu = False
|
| 20 |
+
|
| 21 |
+
# ── Асноўная функцыя, якая патрабуе GPU ──
|
| 22 |
+
@spaces.GPU(duration=20) # 90 с хопіць для большасці запытаў
|
| 23 |
+
def classify_audio(audio_path: str):
|
| 24 |
+
"""
|
| 25 |
+
Класыфікуе аўдыя, вяртае 3 лепшыя тэгі і іх верагоднасці.
|
| 26 |
+
ZeroGPU выдзяляе GPU толькі на час працы гэтай функцыі.
|
| 27 |
+
"""
|
| 28 |
+
global _model_on_gpu
|
| 29 |
+
|
| 30 |
+
if audio_path is None:
|
| 31 |
+
return {"⚠️": "Загрузіце аўдыя-файл."}
|
| 32 |
+
|
| 33 |
+
# Аднойчы пераносім мадэль на GPU
|
| 34 |
+
if torch.cuda.is_available() and not _model_on_gpu:
|
| 35 |
+
pipe.model.to("cuda")
|
| 36 |
+
_model_on_gpu = True
|
| 37 |
+
|
| 38 |
+
preds = pipe(audio_path) # інферэнс
|
| 39 |
+
# вяртаем dict label→score для gr.Label
|
| 40 |
+
return {p["label"]: p["score"] for p in preds[:3]}
|
| 41 |
+
|
| 42 |
+
# ── Інтэрфейс Gradio ──
|
| 43 |
+
app = gr.Interface(
|
| 44 |
+
fn=classify_audio,
|
| 45 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 46 |
+
outputs=gr.Label(num_top_classes=3),
|
| 47 |
+
title="Audio Classification (MIT/AST) · ZeroGPU",
|
| 48 |
+
description=(
|
| 49 |
+
"Загрузіце аўдыя-файл – мадэль дасць тры найбольш верагодныя катэгорыі гуку."
|
| 50 |
+
),
|
| 51 |
+
cache_examples=False,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# ── Запуск (у Spaces прапускаем share=True – яго непатрэбна) ──
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
app.launch()
|