1littlecoder commited on
Commit
99f3aa9
1 Parent(s): e0c831f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
app.py CHANGED
@@ -7,47 +7,41 @@ import os
7
  API_KEY = os.getenv('PLAY_API_KEY')
8
  USER_ID = os.getenv('PLAY_USER_ID')
9
 
10
- # Function to interact with Play.ht API
11
- def text_to_audio(text):
12
- url = "https://play.ht/api/v2/tts"
13
-
 
 
 
 
 
14
  headers = {
 
 
15
  "Authorization": API_KEY,
16
- "X-User-ID": USER_ID,
17
- "Content-Type": "application/json"
18
  }
19
-
20
- # Customize options here (e.g., voice, language, etc.)
21
- data = {
22
- "voice": "en_us-male-1", # Specify a voice of choice
23
- "content": [text],
24
- "speed": 1.0,
25
- "sample_rate": 24000,
26
- }
27
-
28
- response = requests.post(url, headers=headers, json=data)
29
 
30
- if response.status_code == 201:
31
- response_data = response.json()
32
- audio_url = response_data.get("audio_url")
33
-
34
- # Fetch the audio file
35
- audio_response = requests.get(audio_url)
36
  audio_path = "output_audio.mp3"
37
  with open(audio_path, "wb") as audio_file:
38
- audio_file.write(audio_response.content)
39
-
40
  return audio_path
41
  else:
42
- return f"Error: {response.status_code}, {response.text}"
43
 
44
- # Gradio app interface
45
  iface = gr.Interface(
46
- fn=text_to_audio,
47
  inputs="text",
48
  outputs="audio",
49
- title="Text to Speech with Play.ht",
50
- description="Convert text into realistic speech using Play.ht."
51
  )
52
 
53
  iface.launch(debug = True)
 
7
  API_KEY = os.getenv('PLAY_API_KEY')
8
  USER_ID = os.getenv('PLAY_USER_ID')
9
 
10
+ def text_to_speech(text):
11
+ url = "https://api.play.ht/api/v2/tts/stream"
12
+
13
+ # Customize the payload based on your Play.ht account setup
14
+ payload = {
15
+ "voice": "s3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json", # Replace with your desired voice
16
+ "output_format": "mp3",
17
+ "content": text # Text to be converted to speech
18
+ }
19
  headers = {
20
+ "accept": "audio/mpeg",
21
+ "content-type": "application/json",
22
  "Authorization": API_KEY,
23
+ "X-User-ID": USER_ID
 
24
  }
 
 
 
 
 
 
 
 
 
 
25
 
26
+ response = requests.post(url, json=payload, headers=headers)
27
+
28
+ # Check if the response was successful
29
+ if response.status_code == 200:
30
+ # Save the audio content to a file
 
31
  audio_path = "output_audio.mp3"
32
  with open(audio_path, "wb") as audio_file:
33
+ audio_file.write(response.content)
 
34
  return audio_path
35
  else:
36
+ return f"Error: {response.status_code} - {response.text}"
37
 
38
+ # Set up Gradio Interface
39
  iface = gr.Interface(
40
+ fn=text_to_speech,
41
  inputs="text",
42
  outputs="audio",
43
+ title="Play.ht Text-to-Speech",
44
+ description="Convert text into speech using Play.ht's TTS streaming API."
45
  )
46
 
47
  iface.launch(debug = True)