Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
|
|
| 1 |
import os, time, torch, gradio as gr, spaces
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# ──
|
| 5 |
num_cpu_cores = os.cpu_count() or 1
|
| 6 |
torch.set_num_threads(num_cpu_cores)
|
| 7 |
print(f"✅ PyTorch настроены на {num_cpu_cores} ядраў CPU.")
|
| 8 |
|
| 9 |
-
# ── Пайплайн ──────────────────────────────────────────────
|
| 10 |
-
pipe = pipeline(
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
_model_on_gpu = False # каб
|
| 14 |
|
| 15 |
-
@spaces.GPU(duration=90)
|
| 16 |
def classify_audio(audio_path: str):
|
| 17 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
global _model_on_gpu
|
| 19 |
|
| 20 |
if audio_path is None:
|
| 21 |
return {"⚠️": "Загрузіце файл"}, "—"
|
| 22 |
|
|
|
|
| 23 |
if torch.cuda.is_available() and not _model_on_gpu:
|
| 24 |
pipe.model.to("cuda")
|
| 25 |
_model_on_gpu = True
|
| 26 |
|
| 27 |
-
start = time.perf_counter()
|
| 28 |
-
preds = pipe(audio_path)
|
| 29 |
-
elapsed = time.perf_counter() - start
|
| 30 |
|
| 31 |
top3 = {p["label"]: p["score"] for p in preds[:3]}
|
| 32 |
return top3, f"{elapsed:.2f} сек"
|
| 33 |
|
| 34 |
-
# ── Gradio-інтэрфейс ─────────────────────────────────────
|
| 35 |
app = gr.Interface(
|
| 36 |
fn=classify_audio,
|
| 37 |
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 38 |
outputs=[
|
| 39 |
gr.Label(num_top_classes=3, label="Predictions"),
|
| 40 |
-
gr.Textbox(label="⏱️
|
| 41 |
],
|
| 42 |
title="Audio Classification (MIT/AST) · ZeroGPU",
|
| 43 |
description="Загрузіце аўдыя-файл – атрымаеце 3 лепшыя катэгорыі гуку і час інферэнсу.",
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os, time, torch, gradio as gr, spaces
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# ── Аптымізацыя CPU ──────────────────────────────────────────────
|
| 6 |
num_cpu_cores = os.cpu_count() or 1
|
| 7 |
torch.set_num_threads(num_cpu_cores)
|
| 8 |
print(f"✅ PyTorch настроены на {num_cpu_cores} ядраў CPU.")
|
| 9 |
|
| 10 |
+
# ── Пайплайн ─────────────────────────────────────────────────────
|
| 11 |
+
pipe = pipeline(
|
| 12 |
+
"audio-classification",
|
| 13 |
+
model="MIT/ast-finetuned-audioset-10-10-0.448"
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
_model_on_gpu = False # каб перанесці мадэль на GPU толькі адзін раз
|
| 17 |
|
| 18 |
+
@spaces.GPU(duration=90) # ZeroGPU выдае GPU на час працы функцыі
|
| 19 |
def classify_audio(audio_path: str):
|
| 20 |
+
"""
|
| 21 |
+
Вяртае:
|
| 22 |
+
1) dict label→score (топ-3),
|
| 23 |
+
2) радок з часам інферэнсу ў секундах.
|
| 24 |
+
"""
|
| 25 |
global _model_on_gpu
|
| 26 |
|
| 27 |
if audio_path is None:
|
| 28 |
return {"⚠️": "Загрузіце файл"}, "—"
|
| 29 |
|
| 30 |
+
# адзін раз пераносім мадэль на GPU
|
| 31 |
if torch.cuda.is_available() and not _model_on_gpu:
|
| 32 |
pipe.model.to("cuda")
|
| 33 |
_model_on_gpu = True
|
| 34 |
|
| 35 |
+
start = time.perf_counter()
|
| 36 |
+
preds = pipe(audio_path) # інферэнс
|
| 37 |
+
elapsed = time.perf_counter() - start
|
| 38 |
|
| 39 |
top3 = {p["label"]: p["score"] for p in preds[:3]}
|
| 40 |
return top3, f"{elapsed:.2f} сек"
|
| 41 |
|
| 42 |
+
# ── Gradio-інтэрфейс ─────────────────────────────────────────────
|
| 43 |
app = gr.Interface(
|
| 44 |
fn=classify_audio,
|
| 45 |
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 46 |
outputs=[
|
| 47 |
gr.Label(num_top_classes=3, label="Predictions"),
|
| 48 |
+
gr.Textbox(label="⏱️ Inference time")
|
| 49 |
],
|
| 50 |
title="Audio Classification (MIT/AST) · ZeroGPU",
|
| 51 |
description="Загрузіце аўдыя-файл – атрымаеце 3 лепшыя катэгорыі гуку і час інферэнсу.",
|