Search code examples
pythonhuggingface-transformers

Dictionaries and punctuation


url = "https://api-inference.huggingface.co/"

payload = json.dumps({
  "inputs": "My name is Eesha I like surfing"
})
headers = {
  'Authorization': 'Bearer Test',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

Response.text:

[
    {
        "entity_group": "PERIOD",
        "score": 0.8200734853744507,
        "word": "eesha",
        "start": 11,
        "end": 15
    },
    {
        "entity_group": "PERIOD",
        "score": 0.8993015885353088,
        "word": "surfing",
        "start": 23,
        "end": 30
    }
]

I'm trying to iterate through the dictionaries but I'm getting string indices must be integers. My goal is to add the correct punctuation after the number of characters given by end. Any advice?


Solution

  • You should probably use response.json() instead of response.text to convert response to a JSON dictionary. After that you can iterate through your dictionary as usual.