sachin commited on
Commit
22afc1d
·
1 Parent(s): 920bd09

add- text to speech

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ url = (f"https://slabstech-dhwani-server-workshop.hf.space/v1/audio/speech?"
7
+ f"input={text}&response_format=mp3")
8
+
9
+ headers = {
10
+ "accept": "application/json"
11
+ }
12
+
13
+ try:
14
+ response = requests.post(url, headers=headers, data='')
15
+ response.raise_for_status()
16
+
17
+ # Save the audio content to a temporary file
18
+ temp_file = "temp_output.mp3"
19
+ with open(temp_file, "wb") as f:
20
+ f.write(response.content)
21
+
22
+ return temp_file
23
+ except requests.exceptions.RequestException as e:
24
+ return f"Error: {str(e)}"
25
+
26
+ # Create Gradio interface
27
+ with gr.Blocks(title="Text to Speech API Interface") as demo:
28
+ gr.Markdown("# Text to Speech API Interface")
29
+
30
+ with gr.Row():
31
+ with gr.Column():
32
+ # Input components
33
+ text_input = gr.Textbox(
34
+ label="Text",
35
+ placeholder="Enter text to convert to speech",
36
+ value="ಕರ್ನಾಟಕದ ರಾಜಧಾನಿ ಬೆಂಗಳೂರು." # Example from curl
37
+ )
38
+
39
+ submit_btn = gr.Button("Generate Speech")
40
+
41
+ with gr.Column():
42
+ # Output component
43
+ audio_output = gr.Audio(
44
+ label="Generated Speech",
45
+ type="filepath",
46
+ interactive=False
47
+ )
48
+
49
+ # Connect the button click to the API function
50
+ submit_btn.click(
51
+ fn=text_to_speech,
52
+ inputs=[text_input],
53
+ outputs=audio_output
54
+ )
55
+
56
+ # Launch the interface
57
+ demo.launch()