|
import gradio as gr |
|
import requests |
|
|
|
def text_to_speech(text): |
|
|
|
|
|
import os |
|
|
|
|
|
base_url = os.getenv("DWANI_AI_API_BASE_URL") |
|
|
|
if not base_url: |
|
raise ValueError("DWANI_AI_API_BASE_URL environment variable is not set") |
|
|
|
|
|
endpoint = "/v1/audio/speech?" |
|
|
|
|
|
url = f"{base_url.rstrip('/')}{endpoint}" |
|
|
|
url = (f"{url}" |
|
f"input={text}&response_format=mp3") |
|
|
|
headers = { |
|
"accept": "application/json" |
|
} |
|
|
|
try: |
|
response = requests.post(url, headers=headers, data='') |
|
response.raise_for_status() |
|
|
|
|
|
temp_file = "temp_output.mp3" |
|
with open(temp_file, "wb") as f: |
|
f.write(response.content) |
|
|
|
return temp_file |
|
except requests.exceptions.RequestException as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
with gr.Blocks(title="Text to Speech API Interface") as demo: |
|
gr.Markdown("# Text to Speech API Interface") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
text_input = gr.Textbox( |
|
label="Text", |
|
placeholder="Enter text to convert to speech", |
|
value="ಕರ್ನಾಟಕದ ರಾಜಧಾನಿ ಬೆಂಗಳೂರು." |
|
) |
|
|
|
submit_btn = gr.Button("Generate Speech") |
|
|
|
with gr.Column(): |
|
|
|
audio_output = gr.Audio( |
|
label="Generated Speech", |
|
type="filepath", |
|
interactive=False |
|
) |
|
|
|
|
|
submit_btn.click( |
|
fn=text_to_speech, |
|
inputs=[text_input], |
|
outputs=audio_output |
|
) |
|
|
|
|
|
demo.launch() |