1

my question is how do I post the output using json as backend? just need to post translate text my backend coding

from fastapi import FastAPI, Form, Depends, Request
from fastapi.templating import Jinja2Templates
from googletrans import Translator
import uvicorn

#connect to translator
translator = Translator(service_urls=['translate.googleapis.com'])

app = FastAPI(debug=True)
templates = Jinja2Templates(directory="template")

@app.get("/")
async def home(request: Request):
    return ('index.html',{'request': request})


@app.post("/")
async def trans(request: Request):
    text = request.get('Input_text')
    lang = request.get('lang_select')

    # detect language text
    #dt = translator.detect(text)
    # detect input language
    #dt2 = dt.lang

    # translate the text
    translated = translator.translate(text,lang)
    out_text = translated.text
    pronouce = out_text.pronunciation

#transData={'request': request ,'text_translate': out_text,'text_pronouce':pronouce}

    return ({'request': request ,'text_translate': out_text,'text_pronouce':pronouce})

if __name__=="__main__":
   uvicorn.run(app,host="127.0.0.1",port=8000)

and json format to post the output

{
"text_translate": "out_text"
"text_pronouce":"pronouce"
}

clearly, my post-return make this code not function properly. Any help or solution is appreciated...stuck for days now

Fahmi
  • 115
  • 2
  • 7
  • What is the return you get now? – Harshana Dec 14 '20 at 07:02
  • where do you call this POST method? – Harshana Dec 14 '20 at 09:04
  • @HarshanaSerasinghe sorry now edited it with full coding, post and get method is use in html and fast api already cover with app.get and app.post as method value...still get that error tho "ValueError: [TypeError("'re.Pattern' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]" – Fahmi Dec 14 '20 at 09:09

1 Answers1

0

Try encoding the response with jsonable_encoder and then return the data with JSONResponse

Code:

from fastapi import FastAPI, Form, Depends, Request
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from fastapi.templating import Jinja2Templates
from googletrans import Translator
import uvicorn

#connect to translator
translator = Translator(service_urls=['translate.googleapis.com'])

app = FastAPI(debug=True)
templates = Jinja2Templates(directory="template")

@app.get("/")
async def home(request: Request):
    return ('index.html',{'request': request})


@app.post("/")
async def trans(request: Request):
    text = request.get('Input_text')
    lang = request.get('lang_select')

    # detect language text
    #dt = translator.detect(text)
    # detect input language
    #dt2 = dt.lang

    # translate the text
    translated = translator.translate(text,lang)
    out_text = translated.text
    pronouce = out_text.pronunciation

#transData={'request': request ,'text_translate': out_text,'text_pronouce':pronouce}
    data = jsonable_encoder({'text_translate': out_text,'text_pronouce':pronouce})
    return JSONResponse(content=data)

if __name__=="__main__":
   uvicorn.run(app,host="127.0.0.1",port=8000)

Harshana
  • 5,151
  • 1
  • 17
  • 27