Json Decode Error in Uvicorn + FastApi Docker

  docker, fastapi, json, uvicorn

Hey everyone I’m trying to build a docker image from Huggingface T5. What I understand from error log is that it is due to error in decoding Json on receiving data from Curl Post. Coudn’t find any piece of code explaining how to decode, so asking here.

Here’s my server.py code –

import random

import uvicorn
from fastapi import FastAPI, Request

from transformers import T5ForConditionalGeneration, T5Tokenizer
import torch

import tensorflow_hub as hub

app = FastAPI()

output_cache = []
input_sentence = ""


def run_model(sentence, top_k, top_p, tokenizer, model):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = model.to(device)

    text = "paraphrase: " + sentence + " </s>"

    max_len = 256

    encoding = tokenizer.encode_plus(text, padding='longest', return_tensors="pt")
    input_ids, attention_masks = encoding["input_ids"].to(device), encoding["attention_mask"].to(device)
    
    outputs = model.generate(
            input_ids=input_ids, attention_mask=attention_masks,
            do_sample=True,
            max_length=max_len,
            top_k=top_k,
            top_p=top_p,
            early_stopping=False,
            # temperature=decoding_params["temperature"],
            num_return_sequences=1  # Number of sentences to return
        )

    return outputs



def preprocess_output(model_output, tokenizer, temp, sentence, model):
    for line in model_output:
        paraphrase = tokenizer.decode(line, skip_special_tokens=True, clean_up_tokenization_spaces=True)
        if paraphrase.lower() != sentence.lower() and paraphrase not in temp:
            temp.append(paraphrase)

    if len(temp) < 1:
        temp1 = temp
        sentence = temp1[random.randint(0, len(temp1)-1)]

        model_output = run_model(sentence, top_k, top_p, tokenizer, model)
        temp = preprocess_output(model_output, tokenizer, temp, sentence, model)
    return temp


@app.post("/spin")
async def spinner(request: Request):
    params = await request.json()
    sentence = params['sentence']
    top_k1 = params['top_k']
    top_p1 = params['top_p']
    print(sentence)

    global input_sentence
    global top_k
    global top_p
    input_sentence = sentence
    top_p = top_p1
    top_k = top_k1

    model = T5ForConditionalGeneration.from_pretrained('Vamsi/T5_Paraphrase_Paws')
    tokenizer = T5Tokenizer.from_pretrained('Vamsi/T5_Paraphrase_Paws')

    model_output = run_model(sentence, top_k, top_p, tokenizer, model)

    paraphrases = []
    temp = []

    temp = preprocess_output(model_output, tokenizer, temp, sentence, model)

    global output_cache
    output_cache = temp

    for i, line in enumerate(temp):
        paraphrases.append(f"{i + 1}. {line}")

    print({"data": paraphrases})
    return {"data": paraphrases}


@app.post("/hello")
def helo():
    return {"data": "Hello World"}


if __name__ == "__main__":
    uvicorn.run(app)

And Dockerfile

FROM tensorflow/tensorflow

RUN apt-get update

COPY ./requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt

COPY . .

WORKDIR ./Server

CMD ["python", "server.py"]

This is the error log –

najnar-transpin | 2021-03-31 01:23:10 INFO:     10.244.5.9:57988 - "POST /hello HTTP/1.1" 200 OK
najnar-transpin | 2021-03-31 01:23:10 INFO:     10.244.5.9:58038 - "POST /spin HTTP/1.1" 500 Internal Server Error
najnar-transpin | 2021-03-31 01:23:10 ERROR:    Exception in ASGI application
najnar-transpin | 2021-03-31 01:23:10 Traceback (most recent call last):
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
najnar-transpin | 2021-03-31 01:23:10     result = await app(self.scope, self.receive, self.send)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
najnar-transpin | 2021-03-31 01:23:10     return await self.app(scope, receive, send)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/fastapi/applications.py", line 181, in __call__
najnar-transpin | 2021-03-31 01:23:10     await super().__call__(scope, receive, send)  # pragma: no cover
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/applications.py", line 111, in __call__
najnar-transpin | 2021-03-31 01:23:10     await self.middleware_stack(scope, receive, send)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/middleware/errors.py", line 181, in __call__
najnar-transpin | 2021-03-31 01:23:10     raise exc from None
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/middleware/errors.py", line 159, in __call__
najnar-transpin | 2021-03-31 01:23:10     await self.app(scope, receive, _send)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/exceptions.py", line 82, in __call__
najnar-transpin | 2021-03-31 01:23:10     raise exc from None
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/exceptions.py", line 71, in __call__
najnar-transpin | 2021-03-31 01:23:10     await self.app(scope, receive, sender)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/routing.py", line 566, in __call__
najnar-transpin | 2021-03-31 01:23:10     await route.handle(scope, receive, send)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/routing.py", line 227, in handle
najnar-transpin | 2021-03-31 01:23:10     await self.app(scope, receive, send)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/routing.py", line 41, in app
najnar-transpin | 2021-03-31 01:23:10     response = await func(request)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/fastapi/routing.py", line 183, in app
najnar-transpin | 2021-03-31 01:23:10     dependant=dependant, values=values, is_coroutine=is_coroutine
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/fastapi/routing.py", line 133, in run_endpoint_function
najnar-transpin | 2021-03-31 01:23:10     return await dependant.call(**values)
najnar-transpin | 2021-03-31 01:23:10   File "./server.py", line 60, in spinner
najnar-transpin | 2021-03-31 01:23:10     params = await request.json()
najnar-transpin | 2021-03-31 01:23:10   File "/usr/local/lib/python3.6/dist-packages/starlette/requests.py", line 227, in json
najnar-transpin | 2021-03-31 01:23:10     self._json = json.loads(body)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
najnar-transpin | 2021-03-31 01:23:10     return _default_decoder.decode(s)
najnar-transpin | 2021-03-31 01:23:10   File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
najnar-transpin | 2021-03-31 01:23:10     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
najnar-transpin | 2021-03-31 01:23:10   File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
najnar-transpin | 2021-03-31 01:23:10     raise JSONDecodeError("Expecting value", s, err.value) from None
najnar-transpin | 2021-03-31 01:23:10 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

It is all new for me, so any help is appeciated in decoding or finding whatever the main cause of failed Post request is. Thanks!

Source: Docker Questions

LEAVE A COMMENT