Python equivalent of curl for posting data from file for gentle library

Viewed 441

I am trying to recreate a Curl command from the Gentle forced aligner:

https://github.com/lowerquality/gentle

Curl command:

curl -F "audio=@audio.mp3" -F "transcript=@words.txt" "http://localhost:8765/transcriptions?async=false"

Code so far:

import requests

url = 'http://localhost:8765/transcriptions/'
files = {'audio': open('C://Users//user//Desktop//gentle//audio.mp3','rb'), 'transcript': 
open('C://Users//andrey_user//Desktop//gentle//words.txt', 'rb')}

headers = {}

response = requests.post(url,
          files=files,
          headers=headers)

print(response.text)

But it only returns the HTML and says it is a GET request, however the curl command gives me the correct result. Thanks for your help!

2 Answers

Try this code :

import requests

params = (
    ('async', 'false'),
)

files = {
    'audio': ('audio.mp3', open('audio.mp3', 'rb')),
    'transcript': ('words.txt', open('words.txt', 'rb')),
}

response = requests.post('http://localhost:8765/transcriptions', params=params, files=files)

print(response.text)

I believe you also have to convert the response to JSON, assuming that the library you're trying to send the request to is a REST API. I'm not sure why it says its a get request, but if that's the case, just try changing it to a get request and see if it works out.

So adding r1=response.json() should give you access to the correct JSON data

Related