sachin commited on
Commit
8322498
·
1 Parent(s): 972eb61
Files changed (2) hide show
  1. app.py +62 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import urllib.parse
4
+ import tempfile
5
+ import os
6
+
7
+ import dwani
8
+ dwani.api_key = os.getenv("DWANI_API_KEY")
9
+
10
+ dwani.api_base = os.getenv("DWANI_API_BASE_URL")
11
+
12
+ def text_to_speech(text):
13
+ # Validate input
14
+
15
+ try:
16
+ response = dwani.Audio.speech(input=text, response_format="mp3")
17
+
18
+ # Save the audio content to a temporary file
19
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
20
+ temp_file.write(response)
21
+ temp_file_path = temp_file.name
22
+
23
+ return temp_file_path
24
+ except requests.exceptions.RequestException as e:
25
+ raise ValueError(f"API request failed: {str(e)}")
26
+ except IOError as e:
27
+ raise ValueError(f"Failed to save audio file: {str(e)}")
28
+
29
+ # Create Gradio interface
30
+ with gr.Blocks(title="Text to Speech API Interface") as demo:
31
+ gr.Markdown("# Text to Speech API Interface")
32
+
33
+ with gr.Row():
34
+ with gr.Column():
35
+ # Input components
36
+ text_input = gr.Textbox(
37
+ label="Text",
38
+ placeholder="Enter text to convert to speech",
39
+ value="ಕರ್ನಾಟಕದ ರಾಜಧಾನಿ ಬೆಂಗಳೂರು."
40
+ )
41
+ submit_btn = gr.Button("Generate Speech")
42
+
43
+ with gr.Column():
44
+ # Output component
45
+ audio_output = gr.Audio(
46
+ label="Generated Speech",
47
+ type="filepath",
48
+ interactive=False
49
+ )
50
+
51
+ # Connect the button click to the API function
52
+ submit_btn.click(
53
+ fn=text_to_speech,
54
+ inputs=[text_input],
55
+ outputs=audio_output
56
+ )
57
+
58
+ # Launch the interface
59
+ try:
60
+ demo.launch(server_name="0.0.0.0", server_port=7860)
61
+ except Exception as e:
62
+ print(f"Failed to launch Gradio interface: {str(e)}")
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ dwani