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

Update storage to be more robust (#10)

Browse files

- Update storage to be more robust (dc1678e598c264723d900b4f722a205ff2939ebf)


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

Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -53,26 +53,34 @@ def transcribe_both(audio_file):
53
  def store_correction(original_transcription, corrected_transcription, audio_file, age, native_speaker):
54
  try:
55
  audio_metadata = {}
56
- audio_file_url = None
 
 
 
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)
71
  blob.upload_from_filename(audio_file)
72
 
73
- # Generate a signed download URL valid for 1 hour (adjust expiration as needed)
74
- audio_file_url = blob.generate_signed_url(expiration=timedelta(hours=1))
75
-
 
 
 
 
 
 
76
  combined_data = {
77
  'transcription_info': {
78
  'original_text': original_transcription,
@@ -81,7 +89,9 @@ def store_correction(original_transcription, corrected_transcription, audio_file
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,
@@ -90,8 +100,11 @@ def store_correction(original_transcription, corrected_transcription, audio_file
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})"
 
53
  def store_correction(original_transcription, corrected_transcription, audio_file, age, native_speaker):
54
  try:
55
  audio_metadata = {}
56
+ audio_ref = None # This will store our storage reference
57
+
58
+ # Generate a unique identifier that will be shared between storage and Firestore
59
+ unique_id = str(uuid.uuid4())
60
 
 
61
  if audio_file and os.path.exists(audio_file):
62
+ # Process audio metadata
63
  audio, sr = librosa.load(audio_file, sr=44100)
64
  duration = librosa.get_duration(y=audio, sr=sr)
65
  file_size = os.path.getsize(audio_file)
66
  audio_metadata = {'duration': duration, 'file_size': file_size}
67
 
68
+ # Create storage path using UUID
 
69
  destination_path = f"audio/{lang}/{unique_id}.wav"
70
 
71
+ # Upload to Firebase Storage
72
  blob = bucket.blob(destination_path)
73
  blob.upload_from_filename(audio_file)
74
 
75
+ # Get permanent reference to the file (not temporary URL)
76
+ audio_ref = destination_path
77
+
78
+ # Optional: Store both the permanent path and temporary URL
79
+ audio_file_url = blob.generate_signed_url(timedelta(hours=1))
80
+ else:
81
+ audio_file_url = None
82
+
83
+ # Create document data with explicit audio reference
84
  combined_data = {
85
  'transcription_info': {
86
  'original_text': original_transcription,
 
89
  },
90
  'audio_data': {
91
  'audio_metadata': audio_metadata,
92
+ 'storage_path': audio_ref, # Permanent reference
93
+ 'audio_url': audio_file_url, # Temporary URL
94
+ 'file_id': unique_id # Explicit unique ID
95
  },
96
  'user_info': {
97
  'native_amis_speaker': native_speaker,
 
100
  'timestamp': datetime.now().isoformat(),
101
  'model_name': MODEL_NAME
102
  }
103
+
104
+ # Create document with UUID as ID instead of auto-generated ID
105
+ doc_ref = db.collection('amis_transcriptions').document(unique_id)
106
+ doc_ref.set(combined_data)
107
+
108
  return "校正保存成功! (Correction saved successfully!)"
109
  except Exception as e:
110
  return f"保存失败: {e} (Error saving correction: {e})"