AdarshJi commited on
Commit
8a17995
1 Parent(s): 9ad00e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -22
app.py CHANGED
@@ -1,45 +1,57 @@
1
- import os
2
- import base64
3
- import uvicorn
4
- import json
5
  from flask import Flask, request, jsonify
6
- import edge_tts
7
- import asyncio
8
-
9
  app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- async def TextToAudioFile(text: str, model: str) -> str:
12
- """Converts text to an audio file and returns its base64 encoded string."""
13
- file_path = "Output/main.mp3"
 
 
 
14
 
 
15
 
 
 
 
 
 
 
16
 
17
- # Convert text to audio
18
- communicate = edge_tts.Communicate(text, voice=model, pitch='+5Hz', rate='+10%')
19
- await communicate.save(file_path)
20
 
21
- # Read the audio file and encode it to base64
22
- with open(file_path, 'rb') as audio_file:
23
- audio_data = audio_file.read()
24
- audio_base64 = base64.b64encode(audio_data).decode('utf-8')
25
 
26
- return audio_base64
27
 
28
  @app.route('/tts', methods=['POST'])
29
  def tts():
30
  """Handles POST requests for text-to-speech conversion."""
31
  data = request.get_json()
32
- model = data.get('model', 'en-GB-SoniaNeural')
33
  if not data or 'text' not in data:
34
  return jsonify({'error': 'Text is required'}), 400
35
 
36
  text = data['text']
37
 
38
  # Run the async function in the event loop
39
- audio_base64 = asyncio.run(TextToAudioFile(text,model))
40
 
41
  return jsonify({'audio': audio_base64}), 200
42
 
43
  if __name__ == '__main__':
44
- # Run the Flask app
45
- app.run(debug=True, port=5000)
 
 
 
 
 
1
  from flask import Flask, request, jsonify
2
+ import requests
3
+ import uvicorn
 
4
  app = Flask(__name__)
5
+ RQ = requests.Session()
6
+
7
+
8
+ def GenAudio(
9
+ text:str,
10
+ model:str
11
+ )-> dict:
12
+
13
+ url = "https://deepgram.com/api/ttsAudioGeneration"
14
+
15
+ headers = {
16
+ "content-type": "application/json",
17
+ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
18
+ }
19
+
20
 
21
+ payload = {
22
+ "text": text,
23
+ "model": model,
24
+ "demoType": "landing-page",
25
+ "params": "tag=landingpage-product-texttospeech"
26
+ }
27
 
28
+ response = RQ.post(url, json=payload, headers=headers)
29
 
30
+ if response.status_code == 200:
31
+ print("Request was successful.")
32
+ return response.json()
33
+
34
+ else:
35
+ print(f"Request failed with status code: {response.status_code}")
36
 
 
 
 
37
 
 
 
 
 
38
 
 
39
 
40
  @app.route('/tts', methods=['POST'])
41
  def tts():
42
  """Handles POST requests for text-to-speech conversion."""
43
  data = request.get_json()
44
+ model = data.get('model', 'aura-luna-en')
45
  if not data or 'text' not in data:
46
  return jsonify({'error': 'Text is required'}), 400
47
 
48
  text = data['text']
49
 
50
  # Run the async function in the event loop
51
+ audio_base64 = GenAudio(text, model)
52
 
53
  return jsonify({'audio': audio_base64}), 200
54
 
55
  if __name__ == '__main__':
56
+
57
+ app.run(debug=True, port=5000)