def on_click_gemini()

in experiments/babel/app/pages/gemini_studio.py [0:0]


def on_click_gemini(e: me.ClickEvent):
    """uses the Gemini voices to create audio"""

    state = me.state(PageState)
    state.is_loading = True
    if not state.gemini_statement:
        print("no statement provided. not synthesizing.")
        return

    state.audio_output_infos.clear()
    yield

    post_object = {
        "statement": state.gemini_statement,
        #"instructions": "say the following",
        "voiceName": state.gemini_voice,
    }
    print(post_object)
    endpoint = f"{config.BABEL_ENDPOINT}/gemini"
    print(f"endpoint: {endpoint}")
    req = urllib.request.Request(endpoint)

    if "localhost" not in endpoint:
        credentials, project_id = google.auth.default()
        print(f"project id: {project_id}")
        credentials.refresh(googlerequests.Request())
        print(f"credentials.token {credentials.token}")

        urlinfo = urllib.parse.urlparse(endpoint)
        audience = f"{urlinfo.scheme}://{urlinfo.netloc}/"
        print(f"audience: {audience}")
        auth_req = google.auth.transport.requests.Request()
        id_token = google.oauth2.id_token.fetch_id_token(auth_req, audience)
        print(f"id token {id_token}")

        req.add_header("Authorization", f"Bearer {id_token}")

    try:
        req.add_header("Content-Type", "application/json; charset=utf-8")
        bindata = str(json.dumps(post_object)).encode("utf-8")
        response = urllib.request.urlopen(req, bindata)
        response_as_string = response.read().decode("utf-8")
        print(response_as_string)

        data = json.loads(response_as_string)

        # state.audio_output_uri = f"{BUCKET_PATH}{data.get("outputfiles")[0]}"
        # state.audio_output_infos.clear()
        # for f in data.get("audio_metadata"):
        #  state.audio_output_infos.append(f"{BUCKET_PATH}{f}")

        print(data.get("audio_metadata"))

        state.gemini_output_metadata.clear()
        state.gemini_output_metadata = [
            BabelMetadata(item) for item in data.get("audio_metadata")
        ]

    except urllib.error.HTTPError as err:
        print(f"HTTP Error: {err.code} - {err.reason}")
        # Handle the HTTP error (e.g., log it, retry the request, etc.)

    except urllib.error.URLError as err:
        print(f"URL Error: {err.reason}")
        # Handle the URL error (e.g., check network connectivity)

    except socket.error as err:
        print(f"Socket Error: {err}")
        # Handle the socket error (e.g., retry the request, check network
        state.gemini_statement = ""

    state.is_loading = False
    yield