Athspi commited on
Commit
a12d4b6
·
verified ·
1 Parent(s): 2d4c672

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -1
app.py CHANGED
@@ -1 +1,57 @@
1
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import base64
4
+ import requests
5
+ from flask import Flask, request, send_file
6
+
7
+ app = Flask(__name__)
8
+
9
+ API_KEY = os.getenv("GEMINI_API_KEY")
10
+ API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-tts:generateContent"
11
+
12
+ headers = {
13
+ "Content-Type": "application/json",
14
+ "x-goog-api-key": API_KEY,
15
+ }
16
+
17
+ def generate_audio(text):
18
+ payload = {
19
+ "contents": [{
20
+ "parts": [{
21
+ "text": f"Say cheerfully: {text}"
22
+ }]
23
+ }],
24
+ "generationConfig": {
25
+ "responseModalities": ["AUDIO"],
26
+ "speechConfig": {
27
+ "voiceConfig": {
28
+ "prebuiltVoiceConfig": {
29
+ "voiceName": "Kore"
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+
36
+ response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
37
+ if response.status_code != 200:
38
+ raise Exception(f"API Error: {response.text}")
39
+
40
+ res_json = response.json()
41
+ data = res_json["candidates"][0]["content"]["parts"][0]["inlineData"]["data"]
42
+ audio_bytes = base64.b64decode(data)
43
+
44
+ output_path = "out.wav"
45
+ with open(output_path, "wb") as f:
46
+ f.write(audio_bytes)
47
+
48
+ return output_path
49
+
50
+ @app.route("/speak", methods=["POST"])
51
+ def speak():
52
+ text = request.json.get("text", "Have a wonderful day!")
53
+ output_path = generate_audio(text)
54
+ return send_file(output_path, mimetype="audio/wav")
55
+
56
+ if __name__ == "__main__":
57
+ app.run(host="0.0.0.0", port=7860)