Skip to content

Commit

Permalink
Main reason for the JSON formatting error: the Function call puts the…
Browse files Browse the repository at this point in the history
… JSON content inside a 'data' field, and it also expects it back that way! #53
  • Loading branch information
MrCsabaToth committed Sep 22, 2024
1 parent 7041b83 commit 3a1490e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
20 changes: 8 additions & 12 deletions functions/fn_impl/chirp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from firebase_admin import initialize_app, storage
import firebase_admin
import google.cloud.logging
import json
import logging

from flask import jsonify
from google.api_core.client_options import ClientOptions
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech
Expand Down Expand Up @@ -71,14 +71,6 @@ def chirp(req: https_fn.Request) -> https_fn.Response:

return ('', 204, headers)

# Set CORS headers for the main request
headers = {
'Content-Type':'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
}
# END CORS

if not firebase_admin._apps:
initialize_app()

Expand All @@ -90,8 +82,8 @@ def chirp(req: https_fn.Request) -> https_fn.Response:
project_id = 'open-mmpa'
region = 'us-central1'

if request_json and 'recording_file_name' in request_json:
recording_file_name = request_json['recording_file_name']
if request_json and 'data' in request_json and 'recording_file_name' in request_json['data']:
recording_file_name = request_json['data']['recording_file_name']
elif request_args and 'recording_file_name' in request_args:
recording_file_name = request_args['recording_file_name']
elif request_form and 'recording_file_name' in request_form:
Expand All @@ -113,4 +105,8 @@ def chirp(req: https_fn.Request) -> https_fn.Response:
logging.exception(e)
return transcripts, 500

return (jsonify(dict(transcripts=transcripts)), 200, headers)
return https_fn.Response(
json.dumps(dict(data=transcripts)),
status=200,
content_type='application/json',
)
17 changes: 7 additions & 10 deletions lib/speech/service/transcription_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ class Transcription {
class Transcriptions {
Transcriptions();

Transcriptions.fromJson(Map<String, Object?> jsonObject) {
Transcriptions.fromJson(List<Object?> transcriptList) {
transcriptions.clear();
if (jsonObject.containsKey('transcripts') &&
jsonObject['transcripts'] != null) {
final json = jsonObject['transcripts']! as List<dynamic>;
final stringList = json.map((e) => e as String).toList(growable: false);
for (var i = 0; i < stringList.length; i += 2) {
final transcript = stringList[i].trim();
final language = i + 1 < stringList.length ? stringList[i + 1] : '';
transcriptions.add(Transcription(transcript, language.trim()));
}
final stringList =
transcriptList.nonNulls.map((e) => e as String).toList(growable: false);
for (var i = 0; i < stringList.length; i += 2) {
final transcript = stringList[i].trim();
final language = i + 1 < stringList.length ? stringList[i + 1] : '';
transcriptions.add(Transcription(transcript, language.trim()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/speech/view/stt_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ mixin SttMixin {
final transcriptionResponse = await FirebaseFunctions.instance
.httpsCallable(chirpFunctionName)
.call<dynamic>({'recording_file_name': recordingFileName});
final transcriptJson = transcriptionResponse.data as Map<String, dynamic>;
final transcripts = Transcriptions.fromJson(transcriptJson);
final transcriptList = transcriptionResponse.data as List<Object?>;
final transcripts = Transcriptions.fromJson(transcriptList);
addToDeferredQueueFunction?.call(
DeferredAction(
ActionKind.speechTranscripted,
Expand Down

0 comments on commit 3a1490e

Please sign in to comment.