semakoc hunterschep commited on
Commit
daefd88
·
verified ·
1 Parent(s): babce84

segment database by language (#9)

Browse files

- segment database by language (0b24e727e18a34154af419cf6c0e8fd5746db855)


Co-authored-by: Hunter S <[email protected]>

Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -11,13 +11,15 @@ 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
  })
@@ -26,6 +28,7 @@ bucket = storage.bucket()
26
 
27
  # Load the ASR model and processor
28
  MODEL_NAME = "eleferrand/xlsr53_Amis"
 
29
  processor = Wav2Vec2Processor.from_pretrained(MODEL_NAME)
30
  model = AutoModelForCTC.from_pretrained(MODEL_NAME)
31
 
@@ -54,14 +57,14 @@ def store_correction(original_transcription, corrected_transcription, audio_file
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)
@@ -71,18 +74,24 @@ def store_correction(original_transcription, corrected_transcription, audio_file
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,
82
  'age': age
83
- }
 
 
84
  }
85
- db.collection('transcriptions').add(combined_data)
 
86
  return "校正保存成功! (Correction saved successfully!)"
87
  except Exception as e:
88
  return f"保存失败: {e} (Error saving correction: {e})"
 
11
  import tempfile
12
  import uuid
13
 
14
+ # LOCAL INITIALIZATION - ONLY USE ON YOUR OWN DEVICE
15
+ '''
16
  os.chdir(os.path.dirname(os.path.abspath(__file__)))
17
  cred = credentials.Certificate("serviceAccountKey.json")
18
  '''
19
  # Deployed Initialization
20
  firebase_config = json.loads(os.environ.get('firebase_creds'))
21
  cred = credentials.Certificate(firebase_config)
22
+
23
  firebase_admin.initialize_app(cred, {
24
  "storageBucket": "amis-asr-corrections-dem-8cf3d.firebasestorage.app"
25
  })
 
28
 
29
  # Load the ASR model and processor
30
  MODEL_NAME = "eleferrand/xlsr53_Amis"
31
+ lang = "ami"
32
  processor = Wav2Vec2Processor.from_pretrained(MODEL_NAME)
33
  model = AutoModelForCTC.from_pretrained(MODEL_NAME)
34
 
 
57
 
58
  # If an audio file is provided, upload it to Firebase Storage
59
  if audio_file and os.path.exists(audio_file):
60
+ audio, sr = librosa.load(audio_file, sr=44100)
61
  duration = librosa.get_duration(y=audio, sr=sr)
62
  file_size = os.path.getsize(audio_file)
63
  audio_metadata = {'duration': duration, 'file_size': file_size}
64
 
65
  # Generate a unique identifier for the audio file
66
  unique_id = str(uuid.uuid4())
67
+ destination_path = f"audio/{lang}/{unique_id}.wav"
68
 
69
  # Create a blob and upload the file
70
  blob = bucket.blob(destination_path)
 
74
  audio_file_url = blob.generate_signed_url(expiration=timedelta(hours=1))
75
 
76
  combined_data = {
77
+ 'transcription_info': {
78
+ 'original_text': original_transcription,
79
+ 'corrected_text': corrected_transcription,
80
+ 'language': lang,
81
+ },
82
+ 'audio_data': {
83
+ 'audio_metadata': audio_metadata,
84
+ 'audio_file_url': audio_file_url,
85
+ },
86
  'user_info': {
87
  'native_amis_speaker': native_speaker,
88
  'age': age
89
+ },
90
+ 'timestamp': datetime.now().isoformat(),
91
+ 'model_name': MODEL_NAME
92
  }
93
+ # Save data to a collection for that language
94
+ db.collection('amis_transcriptions').add(combined_data)
95
  return "校正保存成功! (Correction saved successfully!)"
96
  except Exception as e:
97
  return f"保存失败: {e} (Error saving correction: {e})"