sachin commited on
Commit
aefdced
·
1 Parent(s): 38a081b
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -1,42 +1,45 @@
1
  import gradio as gr
2
  import requests
 
 
 
3
 
4
  def text_to_speech(text):
5
- # Construct URL with plain text as query parameter
 
 
6
 
7
- import os
8
-
9
- # Get the base URL (IP or domain) from environment variable
10
  base_url = os.getenv("DWANI_AI_API_BASE_URL")
11
-
12
  if not base_url:
13
  raise ValueError("DWANI_AI_API_BASE_URL environment variable is not set")
14
 
15
  # Define the endpoint path
16
- endpoint = "/v1/audio/speech?"
17
-
18
- # Construct the full API URL
19
- url = f"{base_url.rstrip('/')}{endpoint}"
20
-
21
- url = (f"{url}"
22
- f"input={text}&response_format=mp3")
23
 
 
 
 
 
24
  headers = {
25
- "accept": "application/json"
 
26
  }
27
 
28
  try:
29
- response = requests.post(url, headers=headers, data='')
30
  response.raise_for_status()
31
 
32
  # Save the audio content to a temporary file
33
- temp_file = "temp_output.mp3"
34
- with open(temp_file, "wb") as f:
35
- f.write(response.content)
36
 
37
- return temp_file
38
  except requests.exceptions.RequestException as e:
39
- return f"Error: {str(e)}"
 
 
40
 
41
  # Create Gradio interface
42
  with gr.Blocks(title="Text to Speech API Interface") as demo:
@@ -48,9 +51,8 @@ with gr.Blocks(title="Text to Speech API Interface") as demo:
48
  text_input = gr.Textbox(
49
  label="Text",
50
  placeholder="Enter text to convert to speech",
51
- value="ಕರ್ನಾಟಕದ ರಾಜಧಾನಿ ಬೆಂಗಳೂರು." # Example from curl
52
  )
53
-
54
  submit_btn = gr.Button("Generate Speech")
55
 
56
  with gr.Column():
@@ -69,4 +71,7 @@ with gr.Blocks(title="Text to Speech API Interface") as demo:
69
  )
70
 
71
  # Launch the interface
72
- demo.launch()
 
 
 
 
1
  import gradio as gr
2
  import requests
3
+ import urllib.parse
4
+ import tempfile
5
+ import os
6
 
7
  def text_to_speech(text):
8
+ # Validate input
9
+ if not text or not isinstance(text, str):
10
+ raise ValueError("Input text must be a non-empty string")
11
 
12
+ # Get the base URL from environment variable
 
 
13
  base_url = os.getenv("DWANI_AI_API_BASE_URL")
 
14
  if not base_url:
15
  raise ValueError("DWANI_AI_API_BASE_URL environment variable is not set")
16
 
17
  # Define the endpoint path
18
+ endpoint = "/v1/audio/speech"
 
 
 
 
 
 
19
 
20
+ # Construct query parameters
21
+ query_params = urllib.parse.urlencode({"input": text, "response_format": "mp3"})
22
+ url = f"{base_url.rstrip('/')}{endpoint}?{query_params}"
23
+
24
  headers = {
25
+ "accept": "application/json",
26
+ "Content-Type": "application/json"
27
  }
28
 
29
  try:
30
+ response = requests.post(url, headers=headers, json={})
31
  response.raise_for_status()
32
 
33
  # Save the audio content to a temporary file
34
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
35
+ temp_file.write(response.content)
36
+ temp_file_path = temp_file.name
37
 
38
+ return temp_file_path
39
  except requests.exceptions.RequestException as e:
40
+ raise ValueError(f"API request failed: {str(e)}")
41
+ except IOError as e:
42
+ raise ValueError(f"Failed to save audio file: {str(e)}")
43
 
44
  # Create Gradio interface
45
  with gr.Blocks(title="Text to Speech API Interface") as demo:
 
51
  text_input = gr.Textbox(
52
  label="Text",
53
  placeholder="Enter text to convert to speech",
54
+ value="ಕರ್ನಾಟಕದ ರಾಜಧಾನಿ ಬೆಂಗಳೂರು."
55
  )
 
56
  submit_btn = gr.Button("Generate Speech")
57
 
58
  with gr.Column():
 
71
  )
72
 
73
  # Launch the interface
74
+ try:
75
+ demo.launch(server_name="0.0.0.0", server_port=7860)
76
+ except Exception as e:
77
+ print(f"Failed to launch Gradio interface: {str(e)}")