Skip to content

Instantly share code, notes, and snippets.

@akulbe
Last active June 21, 2023 04:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
How do I loop through this routine X number of times where the text on line 7, and the filename on line 13 change every time? Is there a sane way to make a loop of this?
curl -X 'POST' \
'https://api.url.foo/v1/foo/someID' \
--header 'accept: audio/mpeg' \
--header 'api-key: somekey' \
--header 'Content-Type: application/json' \
--data '{
"text": "This text will change every time.",
"model_id": "id_v1",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5
}
}' --output "this_title_will_change.mp3"
@djgoku
Copy link

djgoku commented Jun 21, 2023

first

curl -X 'POST' \                                                                                                                                                  
  'https://api.url.foo/v1/foo/someID' \
  --header 'accept: audio/mpeg' \
  --header 'api-key: somekey' \
  --header 'Content-Type: application/json' \
  --data '{
     "text": "This text will change every time.",
     "model_id": "id_v1",
     "voice_settings": {
     "stability": 0.5,
     "similarity_boost": 0.5
    }
  }' --output "this_title_will_change.mp3"

second

curl -X 'POST' \                                                                                                                                                  
  'https://api.url.foo/v1/foo/someID' \
  --header 'accept: audio/mpeg' \
  --header 'api-key: somekey' \
  --header 'Content-Type: application/json' \
  --data '{
     "text": "Second new text will change every time.",
     "model_id": "id_v1",
     "voice_settings": {
     "stability": 0.5,
     "similarity_boost": 0.5
    }
  }' --output "second_new_title_will_change.mp3"

third

curl -X 'POST' \                                                                                                                                                  
  'https://api.url.foo/v1/foo/someID' \
  --header 'accept: audio/mpeg' \
  --header 'api-key: somekey' \
  --header 'Content-Type: application/json' \
  --data '{
     "text": "Third new text will change every time.",
     "model_id": "id_v1",
     "voice_settings": {
     "stability": 0.5,
     "similarity_boost": 0.5
    }
  }' --output "Third_new_title_will_change.mp3"

Just so I understand you mean something like this even though it is manual. Is there a reason you want to do this with curl verses a programming language?

@djgoku
Copy link

djgoku commented Jun 21, 2023

import requests

CHUNK_SIZE = 1024
url = "https://api.elevenlabs.io/v1/text-to-speech/<voice-id>"

headers = {"Accept": "audio/mpeg", "Content-Type": "application/json", "xi-api-key": "<xi-api-key>"}

data = [
    {
        "text": "Hi! My name is Bella, nice to meet you!",
        "model_id": "eleven_monolingual_v1",
        "voice_settings": {"stability": 0.5, "similarity_boost": 0.5},
    },
    {
        "text": "A new Hi! My name is Bella, nice to meet you!",
        "model_id": "eleven_monolingual_v2",
        "voice_settings": {"stability": 0.5, "similarity_boost": 0.5},
    },
]

for d in data:
    response = requests.post(url, json=d, headers=headers)
    model_id = d["model_id"]
    with open(f"output_{model_id}.mp3", "wb") as f:
        for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
            if chunk:
                f.write(chunk)

@djgoku
Copy link

djgoku commented Jun 21, 2023

import requests

CHUNK_SIZE = 1024
url = "https://api.elevenlabs.io/v1/text-to-speech/<voice-id>"

headers = {"Accept": "audio/mpeg", "Content-Type": "application/json", "xi-api-key": "<xi-api-key>"}

data = [
    {
        "TEXT1_ABOUT_STEP1": "This is what you do on step 1, you download a Win11 ISO by doing a, b, c",
        "TITLE_ABOUT_STEP1": "HowToDownloadWin11ISO.mp3",
    },
    {
        "TEXT2_ABOUT_STEP2": "This is what you do on step 2, you download an Ubuntu ISO by doing a, b, c",
        "TITLE_ABOUT_STEP2": "HowToDownloadLUbuntuinuxISO.mp3",
    },
    {
        "TEXT3_ABOUT_STEP3": "This is what you do in step 3 to enable Hyper-V on Windows 11",
        "TITLE_ABOUT_STEP3": "HowToSetUpHyper-V.mp3",
    },
    {
        "TEXT4_ABOUT_STEP4": "This is what you do in step 4 to install VMware Player on Windows",
        "TITLE_ABOUT_STEP4": "HowToInstallVMwarePlayer.mp3",
    },
]

for d in data:
    response = requests.post(url, json=d, headers=headers)
    model_id = d["model_id"]
    with open(f"output_{model_id}.mp3", "wb") as f:
        for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
            if chunk:
                f.write(chunk)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment