import json

import streamlit as st
from audiorecorder import audiorecorder
import requests

STT_API_KEY = "gV6KiOm_eDrOisgI9B9hBbcTeNEwYQT4XNLJNLaI6DMk" #see discord
STT_URL = "https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/de074216-e340-4d56-b391-406532c98440/v1/recognize" #see discord

st.markdown("<h1 style='text-align: center;'>Tell us how you feel</h1>", unsafe_allow_html=True)

st.write("<h3 style='text-align: center; color: lightSeaGreen;'>we'll generate the insights</h3>", unsafe_allow_html=True)

audio = audiorecorder("Click to record", "Click to stop recording")


if not audio.empty():
    # To play audio in frontend:
    st.audio(audio.export().read())  

    # To save audio to a file, use pydub export method:
    audio.export("audio.flac", format="flac")

    # Convert audio to text

    headers = {
        "Content-Type": "audio/flac"
    }

    with open("audio.flac", "rb") as f:
        response = requests.post(STT_URL, auth=("apikey", STT_API_KEY), headers=headers, files={'audio.flac': f})

    response_json = response.json()
    print(response_json)
    if response_json["results"]:
        response_text = response_json["results"][0]["alternatives"][0]["transcript"]
    else:
        response_text = "No audio detected"
    print(response_text)

    # To get audio properties, use pydub AudioSegment properties:
    st.text_area(label="Output", 
                value=response_text,
                height=300)