Chinnatip Taemkaeo commited on
Commit
939ef3b
·
1 Parent(s): e128e07

Add application file

Browse files
Files changed (8) hide show
  1. Dockerfile +20 -0
  2. README.md +26 -1
  3. app.py +28 -0
  4. download_model.py +5 -0
  5. interface.py +26 -0
  6. luna-cry-detection.ipynb +120 -0
  7. requirements.txt +6 -0
  8. sample.wav +0 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ใช้ Python base image
2
+ FROM python:3.9-slim
3
+
4
+ # ติดตั้ง dependencies
5
+ WORKDIR /app
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ # โหลดโมเดลล่วงหน้า
10
+ COPY download_model.py .
11
+ RUN python download_model.py
12
+
13
+ # คัดลอกโค้ดทั้งหมด
14
+ COPY . .
15
+
16
+ # เปิดพอร์ต 7860 สำหรับ Gradio
17
+ EXPOSE 7860
18
+
19
+ # คำสั่งรัน FastAPI หรือ Gradio
20
+ CMD ["python", "interface.py"]
README.md CHANGED
@@ -8,4 +8,29 @@ pinned: false
8
  short_description: baby cry classification
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  short_description: baby cry classification
9
  ---
10
 
11
+ # Baby Cry Detector - Hugging Face Space
12
+
13
+ ## 📝 รายละเอียดโปรเจค
14
+ โมเดลนี้ใช้ **DistilHuBERT** ที่ fine-tune เพื่อแยกเสียงร้องของเด็กทารกจากเสียงอื่น ๆ
15
+ สามารถใช้งานผ่าน API (**FastAPI**) หรือ **Gradio** UI
16
+
17
+ ## 🚀 วิธีใช้งาน
18
+ 1. **ผ่าน Gradio UI:**
19
+ - อัปโหลดไฟล์เสียง และดูผลการจำแนก
20
+
21
+ 2. **ผ่าน FastAPI:**
22
+ - รัน API โดยใช้คำสั่ง
23
+ ```
24
+ uvicorn app:app --host 0.0.0.0 --port 7860
25
+ ```
26
+ - ส่ง request ไปยัง
27
+ ```
28
+ POST http://localhost:7860/predict
29
+ ```
30
+
31
+ ## 📡 Deploy บน Hugging Face Spaces
32
+ 1. สร้าง Spaces ใหม่
33
+ 2. เลือก **Docker-based app**
34
+ 3. อัปโหลด repository นี้ไปที่ Spaces
35
+ 4. รอการ Build และ Deploy
36
+ 5. ใช้งานได้เลย!
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import JSONResponse
3
+ from transformers import pipeline
4
+ import uvicorn
5
+ import tempfile
6
+
7
+ app = FastAPI()
8
+
9
+ # โหลดโมเดล (ใช้ CPU เท่านั้น)
10
+ classifier = pipeline("audio-classification",
11
+ model="AmeerHesham/distilhubert-finetuned-baby_cry",
12
+ device=-1)
13
+
14
+ @app.post("/predict")
15
+ async def predict_audio(file: UploadFile = File(...)):
16
+ try:
17
+ contents = await file.read()
18
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
19
+ tmp.write(contents)
20
+ tmp_path = tmp.name
21
+
22
+ results = classifier(tmp_path)
23
+ return JSONResponse(content={"results": results})
24
+ except Exception as e:
25
+ return JSONResponse(content={"error": str(e)}, status_code=500)
26
+
27
+ if __name__ == "__main__":
28
+ uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
download_model.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ print("Downloading pre-trained model...")
4
+ classifier = pipeline("audio-classification", model="AmeerHesham/distilhubert-finetuned-baby_cry")
5
+ print("Model downloaded successfully!")
interface.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import tempfile
4
+
5
+ # โหลดโมเดลล่วงหน้า
6
+ classifier = pipeline("audio-classification",
7
+ model="AmeerHesham/distilhubert-finetuned-baby_cry",
8
+ device=-1)
9
+
10
+ # ฟังก์ชันสำหรับรัน Gradio
11
+ def classify_audio(audio):
12
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
13
+ tmp.write(audio[1])
14
+ tmp_path = tmp.name
15
+ result = classifier(tmp_path)
16
+ return result
17
+
18
+ # สร้างอินเทอร์เฟซ
19
+ iface = gr.Interface(fn=classify_audio,
20
+ inputs=gr.Audio(source="upload", type="file"),
21
+ outputs="json",
22
+ title="Baby Cry Detector",
23
+ description="Upload an audio file to classify whether it contains a baby's cry.")
24
+
25
+ # รันแอป
26
+ iface.launch()
luna-cry-detection.ipynb ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "5b8f8811-e9ef-4607-ab97-1f1ca28d370a",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stdout",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "Requirement already satisfied: transformers in /Users/zinatip/miniconda3/lib/python3.11/site-packages (4.37.2)\n",
14
+ "Requirement already satisfied: torchaudio in /Users/zinatip/miniconda3/lib/python3.11/site-packages (2.2.0.dev20240131)\n",
15
+ "Requirement already satisfied: filelock in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (3.13.1)\n",
16
+ "Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (0.28.1)\n",
17
+ "Requirement already satisfied: numpy>=1.17 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (1.26.3)\n",
18
+ "Requirement already satisfied: packaging>=20.0 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (24.1)\n",
19
+ "Requirement already satisfied: pyyaml>=5.1 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (6.0.1)\n",
20
+ "Requirement already satisfied: regex!=2019.12.17 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (2024.11.6)\n",
21
+ "Requirement already satisfied: requests in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (2.32.3)\n",
22
+ "Requirement already satisfied: tokenizers<0.19,>=0.14 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (0.15.1)\n",
23
+ "Requirement already satisfied: safetensors>=0.4.1 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (0.4.2)\n",
24
+ "Requirement already satisfied: tqdm>=4.27 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from transformers) (4.65.0)\n",
25
+ "Requirement already satisfied: torch==2.3.0.dev20240131 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from torchaudio) (2.3.0.dev20240131)\n",
26
+ "Requirement already satisfied: typing-extensions>=4.8.0 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from torch==2.3.0.dev20240131->torchaudio) (4.12.2)\n",
27
+ "Requirement already satisfied: sympy in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from torch==2.3.0.dev20240131->torchaudio) (1.12)\n",
28
+ "Requirement already satisfied: networkx in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from torch==2.3.0.dev20240131->torchaudio) (3.2.1)\n",
29
+ "Requirement already satisfied: jinja2 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from torch==2.3.0.dev20240131->torchaudio) (3.1.4)\n",
30
+ "Requirement already satisfied: fsspec in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from torch==2.3.0.dev20240131->torchaudio) (2023.12.2)\n",
31
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from requests->transformers) (2.0.4)\n",
32
+ "Requirement already satisfied: idna<4,>=2.5 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from requests->transformers) (3.4)\n",
33
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from requests->transformers) (2.0.0)\n",
34
+ "Requirement already satisfied: certifi>=2017.4.17 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from requests->transformers) (2023.11.17)\n",
35
+ "Requirement already satisfied: MarkupSafe>=2.0 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from jinja2->torch==2.3.0.dev20240131->torchaudio) (2.1.4)\n",
36
+ "Requirement already satisfied: mpmath>=0.19 in /Users/zinatip/miniconda3/lib/python3.11/site-packages (from sympy->torch==2.3.0.dev20240131->torchaudio) (1.3.0)\n"
37
+ ]
38
+ }
39
+ ],
40
+ "source": [
41
+ "!pip install transformers torchaudio"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 2,
47
+ "id": "ae2a06e5-6325-4c0b-bc34-3af09e4968b6",
48
+ "metadata": {},
49
+ "outputs": [
50
+ {
51
+ "name": "stderr",
52
+ "output_type": "stream",
53
+ "text": [
54
+ "/Users/zinatip/miniconda3/lib/python3.11/site-packages/huggingface_hub/file_download.py:795: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
55
+ " warnings.warn(\n"
56
+ ]
57
+ }
58
+ ],
59
+ "source": [
60
+ "from transformers import pipeline\n",
61
+ "\n",
62
+ "# โหลด pipeline สำหรับ audio classification พร้อมกับโมเดลที่ต้องการ\n",
63
+ "classifier = pipeline(\"audio-classification\", model=\"AmeerHesham/distilhubert-finetuned-baby_cry\")\n",
64
+ "\n",
65
+ "# ระบุ path ของไฟล์เสียง (.wav หรือรูปแบบอื่นๆ ที่รองรับ)"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 3,
71
+ "id": "92d9e206-537b-42d7-b3ee-c76f589cb3a5",
72
+ "metadata": {},
73
+ "outputs": [
74
+ {
75
+ "name": "stdout",
76
+ "output_type": "stream",
77
+ "text": [
78
+ "[{'score': 0.9975179433822632, 'label': 'hungry'}, {'score': 0.002165452344343066, 'label': 'discomfort'}, {'score': 0.00031626736745238304, 'label': 'tired'}, {'score': 3.506626171656535e-07, 'label': 'belly_pain'}, {'score': 2.5131891234764225e-08, 'label': 'burping'}]\n"
79
+ ]
80
+ }
81
+ ],
82
+ "source": [
83
+ "audio_file = \"/Users/zinatip/project/cry-detection/sample.wav\"\n",
84
+ "# https://huggingface.co/datasets/Nooon/Donate_a_cry/tree/main/belly_pain\n",
85
+ "\n",
86
+ "results = classifier(audio_file)\n",
87
+ "print(results)\n"
88
+ ]
89
+ },
90
+ {
91
+ "cell_type": "code",
92
+ "execution_count": null,
93
+ "id": "1e14e3f3-b2a7-44c6-9029-cf9b49afbef8",
94
+ "metadata": {},
95
+ "outputs": [],
96
+ "source": []
97
+ }
98
+ ],
99
+ "metadata": {
100
+ "kernelspec": {
101
+ "display_name": "Python 3 (ipykernel)",
102
+ "language": "python",
103
+ "name": "python3"
104
+ },
105
+ "language_info": {
106
+ "codemirror_mode": {
107
+ "name": "ipython",
108
+ "version": 3
109
+ },
110
+ "file_extension": ".py",
111
+ "mimetype": "text/x-python",
112
+ "name": "python",
113
+ "nbconvert_exporter": "python",
114
+ "pygments_lexer": "ipython3",
115
+ "version": "3.11.5"
116
+ }
117
+ },
118
+ "nbformat": 4,
119
+ "nbformat_minor": 5
120
+ }
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ gradio
4
+ transformers
5
+ torchaudio
6
+ torch
sample.wav ADDED
Binary file (112 kB). View file