Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
6d40a3f
1
Parent(s):
34df38a
test asr model
Browse files- README.md +7 -8
- app.py +87 -52
- packages.txt +2 -0
- requirements.txt +7 -1
README.md
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license:
|
11 |
-
short_description: demo sachi
|
12 |
---
|
13 |
|
14 |
-
|
|
|
1 |
---
|
2 |
+
title: 🗣️Live ASR Speech Recognition Gradio🧠💾
|
3 |
+
emoji: 2-Live🗣️
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.5
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: apache-2.0
|
|
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -1,64 +1,99 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
|
|
|
|
8 |
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import librosa
|
4 |
+
import soundfile
|
5 |
+
import whisper
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
+
import uuid
|
9 |
+
import csv
|
10 |
+
from datetime import datetime
|
11 |
+
from huggingface_hub import hf_hub_download, Repository
|
12 |
+
from transformers import pipeline
|
13 |
|
14 |
+
# Dataset configuration (Change with your Hugging Face details)
|
15 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/Sachi_demo_dataset"
|
16 |
+
DATASET_REPO_ID = "awacke1/Sachi_demo_dataset"
|
17 |
+
DATA_FILENAME = "Sachi_demo_dataset.csv"
|
18 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
19 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
20 |
|
21 |
+
PersistToDataset = False # Change to True to save output to dataset
|
22 |
|
23 |
+
# Download the dataset if needed
|
24 |
+
if PersistToDataset:
|
25 |
+
try:
|
26 |
+
hf_hub_download(
|
27 |
+
repo_id=DATASET_REPO_ID,
|
28 |
+
filename=DATA_FILENAME,
|
29 |
+
cache_dir="data",
|
30 |
+
force_filename=DATA_FILENAME
|
31 |
+
)
|
32 |
+
except:
|
33 |
+
print("file not found")
|
34 |
|
35 |
+
repo = Repository(
|
36 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
37 |
+
)
|
|
|
|
|
38 |
|
39 |
+
def store_message(name: str, message: str):
|
40 |
+
if name and message:
|
41 |
+
with open(DATA_FILE, "a") as csvfile:
|
42 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
43 |
+
writer.writerow({"name": name.strip(), "message": message.strip(), "time": str(datetime.now())})
|
44 |
+
if PersistToDataset:
|
45 |
+
commit_url = repo.push_to_hub()
|
46 |
+
ret = ""
|
47 |
+
with open(DATA_FILE, "r") as csvfile:
|
48 |
+
reader = csv.DictReader(csvfile)
|
49 |
+
for row in reader:
|
50 |
+
ret += row
|
51 |
+
ret += "\r\n"
|
52 |
+
return ret
|
53 |
|
54 |
+
# Load Whisper model
|
55 |
+
model = whisper.load_model("base") # You can switch to "small", "medium", or "large" based on your needs
|
56 |
|
57 |
+
SAMPLE_RATE = 16000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
def process_audio_file(file):
|
60 |
+
data, sr = librosa.load(file, sr=SAMPLE_RATE)
|
61 |
+
data = librosa.to_mono(data)
|
62 |
+
return data
|
63 |
|
64 |
+
def transcribe(audio, state=""):
|
65 |
+
audio_data = process_audio_file(audio)
|
66 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
67 |
+
audio_path = os.path.join(tmpdir, f'audio_{uuid.uuid4()}.wav')
|
68 |
+
soundfile.write(audio_path, audio_data, SAMPLE_RATE)
|
69 |
+
|
70 |
+
# Transcribe audio using Whisper
|
71 |
+
transcriptions = pipe(audio)["text"]
|
72 |
|
73 |
+
# Persisting transcription to dataset
|
74 |
+
if PersistToDataset:
|
75 |
+
ret = store_message(transcriptions, state) # Save to dataset
|
76 |
+
state = state + transcriptions + " " + ret
|
77 |
+
else:
|
78 |
+
state = state + transcriptions
|
79 |
+
|
80 |
+
return state, state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
gr.Interface(
|
83 |
+
fn=transcribe,
|
84 |
+
inputs=[
|
85 |
+
gr.Audio(source="microphone", type='filepath', streaming=True),
|
86 |
+
"state",
|
87 |
+
],
|
88 |
+
outputs=[
|
89 |
+
"textbox",
|
90 |
+
"state"
|
91 |
+
],
|
92 |
+
layout="horizontal",
|
93 |
+
theme="huggingface",
|
94 |
+
title="🗣️ASR-Whisper Live🧠💾",
|
95 |
+
description=f"Live Automatic Speech Recognition (ASR) using Whisper.",
|
96 |
+
allow_flagging='never',
|
97 |
+
live=True,
|
98 |
+
article=f"Result💾 Dataset: [{DATASET_REPO_URL}]({DATASET_REPO_URL})"
|
99 |
+
).launch(debug=True)
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
ffmpeg
|
2 |
+
libsndfile1
|
requirements.txt
CHANGED
@@ -1 +1,7 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
nemo_toolkit[asr]
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
gradio
|
5 |
+
Werkzeug
|
6 |
+
huggingface_hub
|
7 |
+
Pillow
|