I am trying to send a POST request to an API for each row in a vector, and I want to mutate the response given by the API as new columns (for each row aswell).
I get a 200 response with this query:
this_works <- POST(url_predict, body= '{"text": "This is a bad thing"}')
But now I want to substitute the text "This is a bad thing" for the text contained in each row of a dataframe.
My attempt so far:
bquery <- function(df){
df3 <- data.frame()
text_col <- toString(df4$text)
url_predict = "http://127.0.0.1:8000/predict"
this_doesnt_work <- POST(url_predict, body = paste("'{'", "text", "':'", text_col, "'}'", sep = ""))
content1 <- content(this_doesnt_work)
df3$bert_sentiment <- content1$sentiment
df3$probability_negative <- content1$probabilities$negative
df3$probability_neutral <- content1$probabilities$neutral
df3$probability_positive <- content1$probabilities$positive
df3$confidence <- content1$confidence
df3
}
This doesn't work, however the output of my paste function appears as though it should work:
"'{'text':'This is a bad thing.'}'"
Any help would be greatly appreciated.
The response values from the server are:
$probabilities
$probabilities$negative
[1] 0.999736
$probabilities$neutral
[1] 0.0001732047
$probabilities$positive
[1] 9.086558e-05
$sentiment
[1] "negative"
$confidence
[1] 0.999736
I would like to mutate these values to the original dataset for each response/row.
Try sending one string value at a time :
bquery <- function(string){
df3 <- data.frame()
url_predict = "http://127.0.0.1:8000/predict"
this_should_work <- POST(url_predict, body = sprintf('{"text":"%s"}',string))
#Rest of the code
#.....
#.....
}
Then check if it works for one value :
bquery(df4$text[1])
If it does then use lapply
for all :
do.call(rbind, lapply(df4$text, bquery))
Or with purrr::map_df
purrr::map_df(df4$text, bquery)