"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Reported speech provide one service predict through a POST request. The parameters, is a list of the prediction with the following JSON format:
{'tweets': [{'tweet_id': 1, 'text': 'the first tweet'}, {'tweet_id': '2', 'text': 'the second tweet'}, ...] }
If the prediction is successful, Reported Speech will return a JSON with the following format:
{
"tweets": [
{
"tweet_id": 1,
"text": "the first tweet",
"reported_speech": True,
},
{
"tweet_id": "2",
"text": "the second tweet",
"reported_speech": False,
}
,...
]
}
'''
Created on Aug 24, 2020
@author: dweissen
'''
import requests
import pandas as pd
import json
if __name__ == '__main__':
exJSON = {}
#For testing the service if needed:
exJSON = {
"tweets": [
{"tweetID":"10", "text":"My first tweet","label":"1"},
{"tweetID":"11", "text":"My second tweet","label":"2"},
{"tweetID":"12", "text":"My third tweet","label":"1"},
{"tweetID":"13", "text":"My fourth tweet","label":"3"}]
}
# POST
resp = requests.post('https://hlp.ibi.upenn.edu/drugintake/v0.1', json=exJSON)
if resp.status_code != 200:
raise Exception(f'POST /predict/ ERROR: {resp.status_code}')
else:
exJSON = resp.json()
if 'errors' in exJSON:
raise Exception(f'POST /predict/ ERROR: {resp.json()}')
elif 'tweets' in exJSON:
#TODO: to see what is the format sent gand normalize that in a dataframe!
df = pd.json_normalize(exJSON['tweets'])
lg.info(f'Prediction done for {len(df)} tweets.')
lg.info(f'First 10 tweets:\n{df.head(10)}')
df.to_csv('/tmp/outDrug.tsv', sep='\t')
else:
raise Exception(f'POST /predict/ ERROR: unexpected json received from the rest: {exJSON}')