Update app.py to use firebase storage
Browse files
app.py
CHANGED
@@ -5,16 +5,24 @@ from transformers import Wav2Vec2Processor, AutoModelForCTC
|
|
5 |
import zipfile
|
6 |
import os
|
7 |
import firebase_admin
|
8 |
-
from firebase_admin import credentials, firestore
|
9 |
-
from datetime import datetime
|
10 |
import json
|
11 |
import tempfile
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
firebase_config = json.loads(os.environ.get('firebase_creds'))
|
15 |
cred = credentials.Certificate(firebase_config)
|
16 |
-
firebase_admin.initialize_app(cred
|
|
|
|
|
17 |
db = firestore.client()
|
|
|
18 |
|
19 |
# Load the ASR model and processor
|
20 |
MODEL_NAME = "eleferrand/xlsr53_Amis"
|
@@ -37,23 +45,37 @@ def transcribe(audio_file):
|
|
37 |
def transcribe_both(audio_file):
|
38 |
start_time = datetime.now()
|
39 |
transcription = transcribe(audio_file)
|
40 |
-
|
41 |
-
return transcription, transcription, processing_time
|
42 |
|
43 |
def store_correction(original_transcription, corrected_transcription, audio_file, age, native_speaker):
|
44 |
try:
|
45 |
audio_metadata = {}
|
|
|
|
|
|
|
46 |
if audio_file and os.path.exists(audio_file):
|
47 |
audio, sr = librosa.load(audio_file, sr=16000)
|
48 |
duration = librosa.get_duration(y=audio, sr=sr)
|
49 |
file_size = os.path.getsize(audio_file)
|
50 |
audio_metadata = {'duration': duration, 'file_size': file_size}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
combined_data = {
|
53 |
'original_text': original_transcription,
|
54 |
'corrected_text': corrected_transcription,
|
55 |
'timestamp': datetime.now().isoformat(),
|
56 |
'audio_metadata': audio_metadata,
|
|
|
57 |
'model_name': MODEL_NAME,
|
58 |
'user_info': {
|
59 |
'native_amis_speaker': native_speaker,
|
@@ -117,7 +139,6 @@ def toggle_language(switch):
|
|
117 |
"Download ZIP File"
|
118 |
)
|
119 |
|
120 |
-
# Interface
|
121 |
# Interface
|
122 |
with gr.Blocks() as demo:
|
123 |
lang_switch = gr.Checkbox(label="切換到繁體中文 (Switch to Traditional Chinese)")
|
@@ -125,26 +146,22 @@ with gr.Blocks() as demo:
|
|
125 |
title = gr.Markdown("Amis ASR Transcription & Correction System")
|
126 |
step1 = gr.Markdown("Step 1: Audio Upload & Transcription")
|
127 |
|
128 |
-
# Audio input and playback (Original section)
|
129 |
with gr.Row():
|
130 |
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio Input")
|
131 |
|
132 |
step2 = gr.Markdown("Step 2: Review & Edit Transcription")
|
133 |
-
|
134 |
-
with gr.Row(): # Added this Row to position the button below the audio input
|
135 |
transcribe_button = gr.Button("Transcribe Audio")
|
136 |
|
137 |
original_text = gr.Textbox(label="Original Transcription", interactive=False, lines=5)
|
138 |
corrected_text = gr.Textbox(label="Corrected Transcription", interactive=True, lines=5)
|
139 |
|
140 |
step3 = gr.Markdown("Step 3: User Information")
|
141 |
-
|
142 |
with gr.Row():
|
143 |
age_input = gr.Slider(minimum=0, maximum=100, step=1, label="Age", value=25)
|
144 |
native_speaker_input = gr.Checkbox(label="Native Amis Speaker?", value=True)
|
145 |
|
146 |
step4 = gr.Markdown("Step 4: Save & Download")
|
147 |
-
|
148 |
with gr.Row():
|
149 |
save_button = gr.Button("Save Correction")
|
150 |
save_status = gr.Textbox(label="Save Status", interactive=False)
|
@@ -153,7 +170,6 @@ with gr.Blocks() as demo:
|
|
153 |
download_button = gr.Button("Download ZIP File")
|
154 |
download_output = gr.File()
|
155 |
|
156 |
-
# Toggle language dynamically
|
157 |
lang_switch.change(
|
158 |
toggle_language,
|
159 |
inputs=lang_switch,
|
|
|
5 |
import zipfile
|
6 |
import os
|
7 |
import firebase_admin
|
8 |
+
from firebase_admin import credentials, firestore, storage
|
9 |
+
from datetime import datetime, timedelta
|
10 |
import json
|
11 |
import tempfile
|
12 |
+
import uuid
|
13 |
|
14 |
+
''' LOCAL INITIALIZATION - ONLY USE ON YOUR OWN DEVICE
|
15 |
+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
16 |
+
cred = credentials.Certificate("serviceAccountKey.json")
|
17 |
+
'''
|
18 |
+
# Deployed Initialization
|
19 |
firebase_config = json.loads(os.environ.get('firebase_creds'))
|
20 |
cred = credentials.Certificate(firebase_config)
|
21 |
+
firebase_admin.initialize_app(cred, {
|
22 |
+
"storageBucket": "amis-asr-corrections-dem-8cf3d.firebasestorage.app"
|
23 |
+
})
|
24 |
db = firestore.client()
|
25 |
+
bucket = storage.bucket()
|
26 |
|
27 |
# Load the ASR model and processor
|
28 |
MODEL_NAME = "eleferrand/xlsr53_Amis"
|
|
|
45 |
def transcribe_both(audio_file):
|
46 |
start_time = datetime.now()
|
47 |
transcription = transcribe(audio_file)
|
48 |
+
return transcription, transcription
|
|
|
49 |
|
50 |
def store_correction(original_transcription, corrected_transcription, audio_file, age, native_speaker):
|
51 |
try:
|
52 |
audio_metadata = {}
|
53 |
+
audio_file_url = None
|
54 |
+
|
55 |
+
# If an audio file is provided, upload it to Firebase Storage
|
56 |
if audio_file and os.path.exists(audio_file):
|
57 |
audio, sr = librosa.load(audio_file, sr=16000)
|
58 |
duration = librosa.get_duration(y=audio, sr=sr)
|
59 |
file_size = os.path.getsize(audio_file)
|
60 |
audio_metadata = {'duration': duration, 'file_size': file_size}
|
61 |
+
|
62 |
+
# Generate a unique identifier for the audio file
|
63 |
+
unique_id = str(uuid.uuid4())
|
64 |
+
destination_path = f"audio/{unique_id}.mp3"
|
65 |
+
|
66 |
+
# Create a blob and upload the file
|
67 |
+
blob = bucket.blob(destination_path)
|
68 |
+
blob.upload_from_filename(audio_file)
|
69 |
+
|
70 |
+
# Generate a signed download URL valid for 1 hour (adjust expiration as needed)
|
71 |
+
audio_file_url = blob.generate_signed_url(expiration=timedelta(hours=1))
|
72 |
|
73 |
combined_data = {
|
74 |
'original_text': original_transcription,
|
75 |
'corrected_text': corrected_transcription,
|
76 |
'timestamp': datetime.now().isoformat(),
|
77 |
'audio_metadata': audio_metadata,
|
78 |
+
'audio_file_url': audio_file_url,
|
79 |
'model_name': MODEL_NAME,
|
80 |
'user_info': {
|
81 |
'native_amis_speaker': native_speaker,
|
|
|
139 |
"Download ZIP File"
|
140 |
)
|
141 |
|
|
|
142 |
# Interface
|
143 |
with gr.Blocks() as demo:
|
144 |
lang_switch = gr.Checkbox(label="切換到繁體中文 (Switch to Traditional Chinese)")
|
|
|
146 |
title = gr.Markdown("Amis ASR Transcription & Correction System")
|
147 |
step1 = gr.Markdown("Step 1: Audio Upload & Transcription")
|
148 |
|
|
|
149 |
with gr.Row():
|
150 |
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio Input")
|
151 |
|
152 |
step2 = gr.Markdown("Step 2: Review & Edit Transcription")
|
153 |
+
with gr.Row():
|
|
|
154 |
transcribe_button = gr.Button("Transcribe Audio")
|
155 |
|
156 |
original_text = gr.Textbox(label="Original Transcription", interactive=False, lines=5)
|
157 |
corrected_text = gr.Textbox(label="Corrected Transcription", interactive=True, lines=5)
|
158 |
|
159 |
step3 = gr.Markdown("Step 3: User Information")
|
|
|
160 |
with gr.Row():
|
161 |
age_input = gr.Slider(minimum=0, maximum=100, step=1, label="Age", value=25)
|
162 |
native_speaker_input = gr.Checkbox(label="Native Amis Speaker?", value=True)
|
163 |
|
164 |
step4 = gr.Markdown("Step 4: Save & Download")
|
|
|
165 |
with gr.Row():
|
166 |
save_button = gr.Button("Save Correction")
|
167 |
save_status = gr.Textbox(label="Save Status", interactive=False)
|
|
|
170 |
download_button = gr.Button("Download ZIP File")
|
171 |
download_output = gr.File()
|
172 |
|
|
|
173 |
lang_switch.change(
|
174 |
toggle_language,
|
175 |
inputs=lang_switch,
|