Java Api Speechmatics
Приведенный ниже код используется для подключения к API речи для преобразования речи в текст в Java. Но не удается подключиться и возвращает ошибку. https://app.speechmatics.com/api-details Вот ссылка, которая содержит полную информацию, и я использую это:
curl -F data_file=@my_audio_file.mp3 -F model = en-US " https://api.speechmatics.com/v1.0/user/$ MY_API_USER_ID / jobs /? auth_token = $ MY_API_AUTH_TOKEN" # транскрипция
try {
URL url = new URL("https://api.speechmatics.com/v1.0/user/17879/jobs/?auth_token=ZmQzODNiMGUtMzQwYS00MzUxLWJkZDEtZTBlYzUxMTg2YWVm");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("model", "en-US");
conn.setRequestProperty("data_file", "open('RecordAudio.wav', 'rb')");
conn.setRequestProperty("Ocp-Apim-Subscription-Key", "2:5a703eadae214b0bbe91344e546eac4a");
conn.setDoOutput(true);
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
// s= s.concat(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1 ответ
Приведенный ниже код будет запрашивать транскрипцию с использованием почтового запроса.
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.FileInputStream;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.entity.mime.*;
public class Test {
public static void main(String[] args){
System.out.println("Hello");
try {
URI uri = new URIBuilder("https://api.speechmatics.com/v1.0/user/$user_id/jobs/")
.addParameter("auth_token", "$auth_token")
.build();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(uri);
httppost.setHeader("Accept", "application/json");
File imgFile = new File("/filepath/test_short.aac");
HttpEntity entity = MultipartEntityBuilder.create()
.addTextBody("model", "en-US")
.addTextBody("notification", "none")
.addBinaryBody("data_file", imgFile, ContentType.DEFAULT_BINARY ,imgFile.getName())
.build();
httppost.setEntity(entity);
//System.out.println(entity.getContent());
System.out.println("Executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
System.out.println(response);
httpclient.close();
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}