Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import json | |
| import base64 | |
| import io | |
| import tempfile | |
| from streamlit_lottie import st_lottie, st_lottie_spinner | |
| import streamlit.components.v1 as st1 | |
| import time | |
| from cloudhands import CloudHandsPayment | |
| from database_center import db_transaction | |
| import json | |
| import time | |
| import os | |
| import uuid | |
| #import dotenv | |
| #dotenv.load_dotenv() | |
| payment_key=os.getenv('Payment_Key') | |
| def load_local_lottie(path): | |
| with open(path, 'r') as file: | |
| return json.load(file) | |
| def complete_payment(): | |
| if st.session_state.token : | |
| chPay=st.session_state.chPay | |
| try: | |
| result = chPay.charge( | |
| charge=0.5, | |
| event_name="Sample cloudhands charge", | |
| ) | |
| st.success(f"You payment is succeeded") | |
| st.session_state.transaction_id=result.transaction_id | |
| db_transaction.add({ | |
| 'id':str(uuid.uuid4()), | |
| 'app':'app_title', | |
| 'transaction-id':result.transaction_id, | |
| 'price':0.5 | |
| }) | |
| except Exception as e: | |
| st.error(f"Charge failed: {e}") | |
| else: | |
| st.error('Please generate your Tokens.') | |
| def pay(): | |
| chPay = st.session_state.chPay | |
| # Step 1: Show auth link only once | |
| auth_url = chPay.get_authorization_url() | |
| st.link_button("Authenticate", url=auth_url) | |
| # Step 2: User pastes the code | |
| code = st.text_input("Place your code") | |
| if st.button("Exchange Code"): | |
| try: | |
| token = chPay.exchange_code_for_token(code) | |
| st.session_state.token = token | |
| st.success("Code exchanged successfully! Token stored.") | |
| except Exception as e: | |
| st.error(f"Failed: {e}") | |
| # β Only initialize the state if not already set | |
| if "loading_state" not in st.session_state: | |
| st.session_state.loading_state = True | |
| if 'db_transaction' not in st.session_state: | |
| st.session_state.db_transaction = db_transaction | |
| if 'chPay' not in st.session_state: | |
| st.session_state.chPay = CloudHandsPayment(payment_key) | |
| if 'transaction_id' not in st.session_state: | |
| st.session_state.transaction_id = None | |
| # β Show loading animation only once at first load | |
| if st.session_state.loading_state: | |
| with st_lottie_spinner(load_local_lottie('./src/Loading.json'), key='hello'): | |
| time.sleep(3) | |
| st.session_state.loading_state = False | |
| # β App main UI | |
| st.title("Welcome to Spark Anime TTS Demo website.") | |
| st.sidebar.title("Before making the your faviorate charecter sound, authenicate your code") | |
| Authenication=st.sidebar.button('Authenicate') | |
| if Authenication: | |
| pay() | |
| st.markdown("#### This model is fined tuned on Spark TTS model and fined tuned on the AnimeVox dataset. (Stay tuned with demo chatbot)") | |
| text = st.text_area("Enter text to convert to speech:") | |
| character = st.selectbox("Select Character", [ | |
| 'Power', 'Asuna Yuuki', 'Shiro', 'Frieren', 'Mai Sakurajima', 'Megumin', 'Fern', | |
| 'Madoka Kaname', 'Homura Akemi', 'Momo Ayase', 'Mikasa Ackerman', 'Saber', | |
| 'Rin Tohsaka', 'Rem', 'Kurisu Makise', 'Lucy', 'Marin Kitagawa', 'Emilia', 'Makima' | |
| ]) | |
| if st.button("Play Audio"): | |
| with st_lottie_spinner(load_local_lottie('./src/voiceline.json'), key='download'): | |
| url = "https://8000-01jynw0agcdpzps7kn9c8gfs4p.cloudspaces.litng.ai/predict" | |
| data = {"prompt": text, "charecter": character} | |
| response = requests.post(url, json=data) | |
| if response.status_code==200: | |
| complete_payment() | |
| if st.session_state.transaction_id: | |
| audio_data = response.json()['audio'] | |
| audio_bytes = base64.b64decode(audio_data) | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file: | |
| temp_file.write(audio_bytes) | |
| temp_file_path = temp_file.name | |
| st.audio(temp_file_path, format="audio/mp3") | |
| else: | |
| st.error('Please complete your payment to get the audio.') | |