Update app.py
Browse files
app.py
CHANGED
|
@@ -54,21 +54,26 @@ HTML = """
|
|
| 54 |
} else if (data.audio) {
|
| 55 |
status.textContent = 'Audio generated successfully!';
|
| 56 |
|
| 57 |
-
// Create audio element
|
| 58 |
const audio = document.createElement('audio');
|
| 59 |
audio.controls = true;
|
| 60 |
audio.style.width = '100%';
|
| 61 |
-
|
|
|
|
| 62 |
audioContainer.appendChild(audio);
|
| 63 |
|
| 64 |
// Create download link
|
| 65 |
const downloadLink = document.createElement('a');
|
| 66 |
downloadLink.href = audio.src;
|
| 67 |
-
|
|
|
|
| 68 |
downloadLink.textContent = 'Download Audio';
|
| 69 |
downloadLink.style.display = 'block';
|
| 70 |
downloadLink.style.marginTop = '1rem';
|
| 71 |
audioContainer.appendChild(downloadLink);
|
|
|
|
|
|
|
|
|
|
| 72 |
} else {
|
| 73 |
status.textContent = 'Unexpected response payload';
|
| 74 |
}
|
|
@@ -82,12 +87,13 @@ HTML = """
|
|
| 82 |
</html>
|
| 83 |
"""
|
| 84 |
|
| 85 |
-
def generate_audio_from_gemini(prompt: str) -> bytes:
|
| 86 |
client = genai.Client(
|
| 87 |
api_key="AIzaSyDolbPUZBPUPvQUu-RGktJmvnUpkcEKIYo",
|
| 88 |
)
|
| 89 |
|
| 90 |
-
model = "gemini-2.
|
|
|
|
| 91 |
contents = [
|
| 92 |
types.Content(
|
| 93 |
role="user",
|
|
@@ -101,7 +107,7 @@ def generate_audio_from_gemini(prompt: str) -> bytes:
|
|
| 101 |
speech_config=types.SpeechConfig(
|
| 102 |
voice_config=types.VoiceConfig(
|
| 103 |
prebuilt_voice_config=types.PrebuiltVoiceConfig(
|
| 104 |
-
voice_name="Puck"
|
| 105 |
)
|
| 106 |
)
|
| 107 |
)
|
|
@@ -114,11 +120,18 @@ def generate_audio_from_gemini(prompt: str) -> bytes:
|
|
| 114 |
config=config,
|
| 115 |
)
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# Extract audio data from response
|
| 118 |
-
# The audio is in response.candidates[0].content.parts
|
| 119 |
for part in response.candidates[0].content.parts:
|
| 120 |
-
if part
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
raise ValueError("No audio data found in response")
|
| 124 |
|
|
@@ -134,11 +147,11 @@ def gen():
|
|
| 134 |
if not prompt:
|
| 135 |
return jsonify({"error": "No prompt provided"}), 400
|
| 136 |
try:
|
| 137 |
-
audio_bytes = generate_audio_from_gemini(prompt)
|
| 138 |
# Convert audio bytes to base64 for transmission
|
| 139 |
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 140 |
-
app.logger.info("Audio generation succeeded, size=%d bytes", len(audio_bytes))
|
| 141 |
-
return jsonify({"audio": audio_base64})
|
| 142 |
except Exception as e:
|
| 143 |
app.logger.exception("Audio generation failed")
|
| 144 |
return jsonify({"error": str(e)}), 500
|
|
@@ -146,4 +159,4 @@ def gen():
|
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
port = int(os.environ.get("PORT", 7860))
|
| 149 |
-
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 54 |
} else if (data.audio) {
|
| 55 |
status.textContent = 'Audio generated successfully!';
|
| 56 |
|
| 57 |
+
// Create audio element with correct MIME type
|
| 58 |
const audio = document.createElement('audio');
|
| 59 |
audio.controls = true;
|
| 60 |
audio.style.width = '100%';
|
| 61 |
+
const mimeType = data.mime_type || 'audio/wav';
|
| 62 |
+
audio.src = `data:${mimeType};base64,${data.audio}`;
|
| 63 |
audioContainer.appendChild(audio);
|
| 64 |
|
| 65 |
// Create download link
|
| 66 |
const downloadLink = document.createElement('a');
|
| 67 |
downloadLink.href = audio.src;
|
| 68 |
+
const ext = mimeType.includes('mp3') ? 'mp3' : 'wav';
|
| 69 |
+
downloadLink.download = `gemini_audio.${ext}`;
|
| 70 |
downloadLink.textContent = 'Download Audio';
|
| 71 |
downloadLink.style.display = 'block';
|
| 72 |
downloadLink.style.marginTop = '1rem';
|
| 73 |
audioContainer.appendChild(downloadLink);
|
| 74 |
+
|
| 75 |
+
// Auto-play
|
| 76 |
+
audio.play().catch(err => console.log('Autoplay prevented:', err));
|
| 77 |
} else {
|
| 78 |
status.textContent = 'Unexpected response payload';
|
| 79 |
}
|
|
|
|
| 87 |
</html>
|
| 88 |
"""
|
| 89 |
|
| 90 |
+
def generate_audio_from_gemini(prompt: str) -> tuple[bytes, str]:
|
| 91 |
client = genai.Client(
|
| 92 |
api_key="AIzaSyDolbPUZBPUPvQUu-RGktJmvnUpkcEKIYo",
|
| 93 |
)
|
| 94 |
|
| 95 |
+
model = "gemini-2.0-flash-exp"
|
| 96 |
+
# model = "gemini-2.5-flash-preview-tts"
|
| 97 |
contents = [
|
| 98 |
types.Content(
|
| 99 |
role="user",
|
|
|
|
| 107 |
speech_config=types.SpeechConfig(
|
| 108 |
voice_config=types.VoiceConfig(
|
| 109 |
prebuilt_voice_config=types.PrebuiltVoiceConfig(
|
| 110 |
+
voice_name="Puck"
|
| 111 |
)
|
| 112 |
)
|
| 113 |
)
|
|
|
|
| 120 |
config=config,
|
| 121 |
)
|
| 122 |
|
| 123 |
+
# Log response structure for debugging
|
| 124 |
+
app.logger.info(f"Response candidates: {len(response.candidates)}")
|
| 125 |
+
if response.candidates:
|
| 126 |
+
app.logger.info(f"Parts in response: {len(response.candidates[0].content.parts)}")
|
| 127 |
+
|
| 128 |
# Extract audio data from response
|
|
|
|
| 129 |
for part in response.candidates[0].content.parts:
|
| 130 |
+
if hasattr(part, 'inline_data') and part.inline_data:
|
| 131 |
+
mime_type = part.inline_data.mime_type
|
| 132 |
+
audio_data = part.inline_data.data
|
| 133 |
+
app.logger.info(f"Found audio: mime_type={mime_type}, size={len(audio_data)} bytes")
|
| 134 |
+
return audio_data, mime_type
|
| 135 |
|
| 136 |
raise ValueError("No audio data found in response")
|
| 137 |
|
|
|
|
| 147 |
if not prompt:
|
| 148 |
return jsonify({"error": "No prompt provided"}), 400
|
| 149 |
try:
|
| 150 |
+
audio_bytes, mime_type = generate_audio_from_gemini(prompt)
|
| 151 |
# Convert audio bytes to base64 for transmission
|
| 152 |
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 153 |
+
app.logger.info("Audio generation succeeded, size=%d bytes, mime=%s", len(audio_bytes), mime_type)
|
| 154 |
+
return jsonify({"audio": audio_base64, "mime_type": mime_type})
|
| 155 |
except Exception as e:
|
| 156 |
app.logger.exception("Audio generation failed")
|
| 157 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
port = int(os.environ.get("PORT", 7860))
|
| 162 |
+
app.run(host="0.0.0.0", port=port, debug=True)
|