usmanyousaf commited on
Commit
61028a5
·
verified ·
1 Parent(s): 5affd96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -25,15 +25,18 @@ app.secret_key = os.urandom(24)
25
  # Store conversation history
26
  conversation_history = []
27
 
28
- # Synthesize therapist response to speech without permanent storage
29
- def synthesize_audio(text, model="aura-asteria-en"):
30
  try:
 
 
31
  options = SpeakOptions(model=model)
32
- # Use a temporary file to hold audio data briefly
 
33
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_file:
34
  tmp_filename = tmp_file.name
35
 
36
- # Synthesize the response and save it to the temporary file
37
  deepgram.speak.v("1").save(tmp_filename, {"text": text}, options)
38
 
39
  # Read the audio data into memory
@@ -43,8 +46,10 @@ def synthesize_audio(text, model="aura-asteria-en"):
43
  # Remove the temporary file
44
  os.remove(tmp_filename)
45
 
46
- # Return the audio data as base64 encoded string
47
- return base64.b64encode(audio_data).decode('utf-8')
 
 
48
  except Exception as e:
49
  raise ValueError(f"Speech synthesis failed: {str(e)}")
50
 
@@ -63,7 +68,7 @@ def process_audio():
63
  global conversation_history
64
 
65
  # Step 1: Accept audio input
66
- audio_data = request.files.get('audio_data') # Retrieve audio blob from client
67
  if not audio_data:
68
  return jsonify({'error': 'No audio file uploaded'}), 400
69
 
@@ -106,13 +111,14 @@ def process_audio():
106
  # Append assistant reply to conversation history
107
  conversation_history.append({"role": "assistant", "content": assistant_reply})
108
 
109
- # Step 4: Synthesize therapist response to speech (no permanent saving)
110
- audio_base64 = synthesize_audio(assistant_reply)
111
 
 
112
  return jsonify({
113
  'transcription': user_input,
114
  'response': assistant_reply,
115
- 'audioBase64': audio_base64
116
  })
117
 
118
  except Exception as e:
 
25
  # Store conversation history
26
  conversation_history = []
27
 
28
+ # Synthesize therapist response to speech without permanently storing the file
29
+ def synthesize_audio(text):
30
  try:
31
+ # Retrieve the selected voice or default to "aura-asteria-en"
32
+ model = session.get('selected_voice', 'aura-asteria-en')
33
  options = SpeakOptions(model=model)
34
+
35
+ # Use a temporary file to store the synthesized audio
36
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_file:
37
  tmp_filename = tmp_file.name
38
 
39
+ # Synthesize the response and save to the temporary file
40
  deepgram.speak.v("1").save(tmp_filename, {"text": text}, options)
41
 
42
  # Read the audio data into memory
 
46
  # Remove the temporary file
47
  os.remove(tmp_filename)
48
 
49
+ # Encode the audio data as base64 and return a data URI
50
+ audio_base64 = base64.b64encode(audio_data).decode('utf-8')
51
+ audio_data_uri = f"data:audio/mpeg;base64,{audio_base64}"
52
+ return audio_data_uri
53
  except Exception as e:
54
  raise ValueError(f"Speech synthesis failed: {str(e)}")
55
 
 
68
  global conversation_history
69
 
70
  # Step 1: Accept audio input
71
+ audio_data = request.files.get('audio_data')
72
  if not audio_data:
73
  return jsonify({'error': 'No audio file uploaded'}), 400
74
 
 
111
  # Append assistant reply to conversation history
112
  conversation_history.append({"role": "assistant", "content": assistant_reply})
113
 
114
+ # Step 4: Synthesize therapist response to speech (in memory, no permanent files)
115
+ audio_url = synthesize_audio(assistant_reply)
116
 
117
+ # Return data URI instead of file URL
118
  return jsonify({
119
  'transcription': user_input,
120
  'response': assistant_reply,
121
+ 'audioUrl': audio_url
122
  })
123
 
124
  except Exception as e: