sawadogosalif commited on
Commit
6d40a3f
·
1 Parent(s): 34df38a

test asr model

Browse files
Files changed (4) hide show
  1. README.md +7 -8
  2. app.py +87 -52
  3. packages.txt +2 -0
  4. requirements.txt +7 -1
README.md CHANGED
@@ -1,14 +1,13 @@
1
  ---
2
- title: Sachi ASR Demo
3
- emoji: 💬
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
- short_description: demo sachi
12
  ---
13
 
14
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
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
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
8
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- response = ""
 
29
 
30
- for message in client.chat_completion(
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
- response += token
40
- yield response
 
 
41
 
 
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
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
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- huggingface_hub==0.25.2
 
 
 
 
 
 
 
1
+ nemo_toolkit[asr]
2
+ transformers
3
+ torch
4
+ gradio
5
+ Werkzeug
6
+ huggingface_hub
7
+ Pillow