added downloader and bam as default
Browse files- app.py +26 -49
- asr.py +2 -2
- csv_processor.py +6 -5
app.py
CHANGED
@@ -1,61 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
from asr import transcribe_multiple_files, ASR_LANGUAGES, model
|
3 |
from lid import identify, LID_EXAMPLES
|
|
|
4 |
import logging
|
5 |
import soundfile as sf
|
|
|
6 |
|
7 |
# Set up logging
|
8 |
logging.basicConfig(level=logging.DEBUG)
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
#
|
21 |
-
|
22 |
-
# logger.debug(f"Loading audio from file path: {file_path}")
|
23 |
-
|
24 |
-
# # Use soundfile to read the audio file
|
25 |
-
# audio, sr = sf.read(file_path)
|
26 |
-
|
27 |
-
# safe_name = f"audio_{hash(file_path)}.wav"
|
28 |
-
# logger.debug(f"File processed successfully: {safe_name}")
|
29 |
-
# return audio, sr, safe_name
|
30 |
-
# except Exception as e:
|
31 |
-
# logger.error(f"Error processing file {getattr(file_obj, 'name', 'unknown')}: {str(e)}")
|
32 |
-
# raise
|
33 |
-
|
34 |
-
# def transcribe_multiple_files(audio_files, lang, transcription):
|
35 |
-
# transcriptions = []
|
36 |
-
# for audio_file in audio_files:
|
37 |
-
# try:
|
38 |
-
# audio, sr, safe_name = safe_process_file(audio_file)
|
39 |
-
# logger.debug(f"Transcribing file: {safe_name}")
|
40 |
-
# logger.debug(f"Language selected: {lang}")
|
41 |
-
# logger.debug(f"User-provided transcription: {transcription}")
|
42 |
-
|
43 |
-
# result = transcribe_multiple_files(model, audio, lang, transcription)
|
44 |
-
# logger.debug(f"Transcription result: {result}")
|
45 |
-
|
46 |
-
# transcriptions.append(f"File: {safe_name}\nTranscription: {result}\n")
|
47 |
-
# except Exception as e:
|
48 |
-
# logger.error(f"Error in transcription process: {str(e)}")
|
49 |
-
# transcriptions.append(f"Error processing file: {str(e)}\n")
|
50 |
-
# return "\n".join(transcriptions)
|
51 |
-
|
52 |
# Prepare language options for Dropdown
|
53 |
language_options = [f"{k} ({v})" for k, v in ASR_LANGUAGES.items()]
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
|
61 |
mms_transcribe = gr.Interface(
|
@@ -65,7 +42,7 @@ mms_transcribe = gr.Interface(
|
|
65 |
gr.Dropdown(
|
66 |
choices=language_options,
|
67 |
label="Language",
|
68 |
-
value=language_options[
|
69 |
),
|
70 |
gr.Textbox(label="Optional: Provide your own transcription"),
|
71 |
],
|
@@ -86,8 +63,8 @@ mms_identify = gr.Interface(
|
|
86 |
)
|
87 |
|
88 |
tabbed_interface = gr.TabbedInterface(
|
89 |
-
[mms_transcribe, mms_identify],
|
90 |
-
["Speech-to-text", "Language Identification"],
|
91 |
)
|
92 |
|
93 |
with gr.Blocks() as demo:
|
|
|
1 |
import gradio as gr
|
2 |
from asr import transcribe_multiple_files, ASR_LANGUAGES, model
|
3 |
from lid import identify, LID_EXAMPLES
|
4 |
+
from csv_processor import CSV_FILE_PATH
|
5 |
import logging
|
6 |
import soundfile as sf
|
7 |
+
import os
|
8 |
|
9 |
# Set up logging
|
10 |
logging.basicConfig(level=logging.DEBUG)
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
+
def download_csv():
|
14 |
+
file_path = CSV_FILE_PATH # Путь к вашему файлу
|
15 |
+
if os.path.exists(file_path): # Проверяем, существует ли файл
|
16 |
+
return file_path # Если файл существует, возвращаем путь для скачивания
|
17 |
+
else:
|
18 |
+
logger.error(f"file {file_path} not found!")
|
19 |
+
full_path = "/home/user/app/"+ file_path
|
20 |
+
exist_full_path = os.path.exists(full_path)
|
21 |
+
logger.error(f"file {exist_full_path} {"found" if exist_full_path else "not found"}!")
|
22 |
+
return None # Возвращаем None, если файла нет
|
23 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Prepare language options for Dropdown
|
25 |
language_options = [f"{k} ({v})" for k, v in ASR_LANGUAGES.items()]
|
26 |
+
bam_val = "bam (Bamanankan)"
|
27 |
+
bam_index = 0 if bam_val not in language_options else language_options.index(bam_val)
|
28 |
+
|
29 |
+
download_interface = gr.Interface(
|
30 |
+
fn=download_csv,
|
31 |
+
inputs=[],
|
32 |
+
outputs=gr.File(label="Скачать CSV"),
|
33 |
+
title="Скачать CSV файл",
|
34 |
+
description="Скачайте файл audio_plus_hash_uniq_07102024.csv"
|
35 |
+
)
|
36 |
|
37 |
|
38 |
mms_transcribe = gr.Interface(
|
|
|
42 |
gr.Dropdown(
|
43 |
choices=language_options,
|
44 |
label="Language",
|
45 |
+
value=language_options[bam_index] if language_options else None,
|
46 |
),
|
47 |
gr.Textbox(label="Optional: Provide your own transcription"),
|
48 |
],
|
|
|
63 |
)
|
64 |
|
65 |
tabbed_interface = gr.TabbedInterface(
|
66 |
+
[mms_transcribe, mms_identify, download_interface],
|
67 |
+
["Speech-to-text", "Language Identification", "Load to laptop CSV"],
|
68 |
)
|
69 |
|
70 |
with gr.Blocks() as demo:
|
asr.py
CHANGED
@@ -49,7 +49,7 @@ def transcribe_multiple_files(audio_files, lang, transcription):
|
|
49 |
# for audio_file in audio_files:
|
50 |
try:
|
51 |
audio_samples, sr, safe_name = safe_process_file(audio_files)
|
52 |
-
logger.debug(f"Transcribing file: {safe_name}")
|
53 |
logger.debug(f"Language selected: {lang}")
|
54 |
logger.debug(f"User-provided transcription: {transcription}")
|
55 |
|
@@ -61,7 +61,7 @@ def transcribe_multiple_files(audio_files, lang, transcription):
|
|
61 |
except Exception as e:
|
62 |
logger.error(f"Error in transcription process: {str(e)}")
|
63 |
transcriptions.append(f"Error processing file: {str(e)}\n")
|
64 |
-
|
65 |
|
66 |
@spaces.GPU
|
67 |
def transcribe_file(model, audio_samples, lang, user_transcription):
|
|
|
49 |
# for audio_file in audio_files:
|
50 |
try:
|
51 |
audio_samples, sr, safe_name = safe_process_file(audio_files)
|
52 |
+
logger.debug(f"Transcribing file {audio_files}: {safe_name}")
|
53 |
logger.debug(f"Language selected: {lang}")
|
54 |
logger.debug(f"User-provided transcription: {transcription}")
|
55 |
|
|
|
61 |
except Exception as e:
|
62 |
logger.error(f"Error in transcription process: {str(e)}")
|
63 |
transcriptions.append(f"Error processing file: {str(e)}\n")
|
64 |
+
return "\n".join(transcriptions)
|
65 |
|
66 |
@spaces.GPU
|
67 |
def transcribe_file(model, audio_samples, lang, user_transcription):
|
csv_processor.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import hashlib
|
2 |
import csv
|
|
|
3 |
|
4 |
|
5 |
def hash_md5(filepath):
|
@@ -11,16 +12,16 @@ def hash_md5(filepath):
|
|
11 |
return md5_hash.hexdigest()
|
12 |
except Exception as e:
|
13 |
return str(e)
|
14 |
-
|
15 |
|
16 |
def update_csv_with_files(csv_file_path, audio_old_path, audio_16000_path, new_transcription):
|
17 |
hash_old = hash_md5(audio_old_path)
|
18 |
hash_new = hash_md5(audio_16000_path)
|
19 |
-
update_csv(csv_file_path, hash_old, hash_new)
|
20 |
|
21 |
-
def update_csv(
|
22 |
# Use read/write mode to modify the relevant line or append if not found
|
23 |
-
with open(
|
24 |
reader = csv.DictReader(file)
|
25 |
fieldnames = reader.fieldnames
|
26 |
rows = list(reader)
|
@@ -42,7 +43,7 @@ def update_csv(file_path, search_hash, hash_16000, new_path, new_transcription):
|
|
42 |
writer.writerows(rows) # Write all rows back, with the updated one
|
43 |
return
|
44 |
# Append a new row if the hash is not found
|
45 |
-
with open(
|
46 |
writer = csv.DictWriter(append_file, fieldnames=fieldnames)
|
47 |
writer.writerow({
|
48 |
'hash': search_hash,
|
|
|
1 |
import hashlib
|
2 |
import csv
|
3 |
+
import os
|
4 |
|
5 |
|
6 |
def hash_md5(filepath):
|
|
|
12 |
return md5_hash.hexdigest()
|
13 |
except Exception as e:
|
14 |
return str(e)
|
15 |
+
CSV_FILE_PATH = "audio_plus_hash_uniq_07102024.csv"
|
16 |
|
17 |
def update_csv_with_files(csv_file_path, audio_old_path, audio_16000_path, new_transcription):
|
18 |
hash_old = hash_md5(audio_old_path)
|
19 |
hash_new = hash_md5(audio_16000_path)
|
20 |
+
update_csv(csv_file_path, hash_old, hash_new, audio_old_path, new_transcription)
|
21 |
|
22 |
+
def update_csv(csv_file_path, search_hash, hash_16000, new_path, new_transcription):
|
23 |
# Use read/write mode to modify the relevant line or append if not found
|
24 |
+
with open(csv_file_path, mode='r+', newline='', encoding='utf-8') as file:
|
25 |
reader = csv.DictReader(file)
|
26 |
fieldnames = reader.fieldnames
|
27 |
rows = list(reader)
|
|
|
43 |
writer.writerows(rows) # Write all rows back, with the updated one
|
44 |
return
|
45 |
# Append a new row if the hash is not found
|
46 |
+
with open(csv_file_path, mode='a', newline='', encoding='utf-8') as append_file:
|
47 |
writer = csv.DictWriter(append_file, fieldnames=fieldnames)
|
48 |
writer.writerow({
|
49 |
'hash': search_hash,
|