Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,48 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import spaces
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
# ──
|
| 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 |
-
|
| 15 |
-
model="MIT/ast-finetuned-audioset-10-10-0.448"
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
_model_on_gpu = False
|
| 20 |
|
| 21 |
-
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# ──
|
| 43 |
app = gr.Interface(
|
| 44 |
fn=classify_audio,
|
| 45 |
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 46 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
|
|
| 1 |
+
import os, time, torch, gradio as gr, spaces
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# ── CPU-наладкі ───────────────────────────────────────────
|
| 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("audio-classification",
|
| 11 |
+
model="MIT/ast-finetuned-audioset-10-10-0.448")
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
_model_on_gpu = False # каб адзін раз перанесці на GPU
|
|
|
|
| 14 |
|
| 15 |
+
@spaces.GPU(duration=90)
|
|
|
|
| 16 |
def classify_audio(audio_path: str):
|
| 17 |
+
"""Вяртае (dict label→score, час_інферэнсу_сек)."""
|
|
|
|
|
|
|
|
|
|
| 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="⏱️ Time taken")
|
| 41 |
+
],
|
| 42 |
title="Audio Classification (MIT/AST) · ZeroGPU",
|
| 43 |
+
description="Загрузіце аўдыя-файл – атрымаеце 3 лепшыя катэгорыі гуку і час інферэнсу.",
|
|
|
|
|
|
|
| 44 |
cache_examples=False,
|
| 45 |
)
|
| 46 |
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
app.launch()
|