Spaces:
Running
Running
import streamlit as st | |
import requests | |
import base64 | |
from io import BytesIO | |
# Backend API URL | |
API_URL = "https://www.neuralaudio.solutions/api/text2speache" | |
st.set_page_config(page_title="NeuralAudioAI TTS", layout="centered") | |
st.image("https://storage.googleapis.com/crypto-utils-1/Header%20with%20Pattern.png", use_container_width=True) | |
st.title("π Neural Audio AI - TTS Demo") | |
st.write("Enter text and generate speech using **NeuralAudioAI/NA_base**.") | |
# Text Input | |
text = st.text_area("π Enter text to synthesize:", "Hello! This is Neural Audio AI speaking. How may I help you today?") | |
if st.button("ποΈ Generate Speech"): | |
if not text.strip(): | |
st.warning("β οΈ Please enter some text.") | |
else: | |
with st.spinner("Generating audio... πΆ"): | |
try: | |
# Mimic the working frontend FormData: | |
# - "text" field with the text value. | |
# - "speaker_audio" left empty so backend uses its default. | |
files = { | |
"text": (None, text), | |
"speaker_audio": (None, "") # This will be treated as not provided. | |
} | |
response = requests.post(API_URL, files=files) | |
if response.status_code == 200: | |
audio_bytes = response.content | |
# Use BytesIO so st.audio plays the file directly from memory. | |
audio_io = BytesIO(audio_bytes) | |
st.audio(audio_io, format="audio/mpeg") | |
else: | |
try: | |
error_message = response.json().get("error", "Unknown error occurred") | |
except Exception: | |
error_message = response.text | |
st.error(f"β Failed to generate speech: {error_message}") | |
except requests.RequestException as e: | |
st.error(f"β Error communicating with the server: {e}") | |