Giter Site home page Giter Site logo

Comments (14)

missuo avatar missuo commented on July 18, 2024

Show me Screenshot Pls.

from deeplx.

littleblack111 avatar littleblack111 commented on July 18, 2024
截屏2024-04-24 下午2 18 02

Show me Screenshot Pls.

the commands are from README.
At first I thought it was my code's issue. So I tested the command with multiple lines. Which didn't work

from deeplx.

missuo avatar missuo commented on July 18, 2024

In JSON, strings cannot be written directly across lines. You should use newline characters.

curl -X POST http://localhost:1188/translate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_access_token" \
-d '{
    "text": "Hello\nworld!",
    "source_lang": "EN",
    "target_lang": "DE"
}'

from deeplx.

missuo avatar missuo commented on July 18, 2024
image

If you use this type of API debugging tool, you'll notice that when you paste something that contains line breaks, it's automatically converted to \n.

from deeplx.

littleblack111 avatar littleblack111 commented on July 18, 2024

so what should I do if I want to make it able to do multiple lines. Or is it just not possible?

from deeplx.

missuo avatar missuo commented on July 18, 2024

so what should I do if I want to make it able to do multiple lines. Or is it just not possible?

Is my expression not clear enough? I have already given you quite clear examples above.

from deeplx.

missuo avatar missuo commented on July 18, 2024

I hope you have some basic knowledge of coding, at least to the extent that you understand what \n represents. Otherwise, I think you will not be able to use DeepLX properly.

from deeplx.

littleblack111 avatar littleblack111 commented on July 18, 2024

understand, i need to convert it to \n then reconvert it back to lines

from deeplx.

missuo avatar missuo commented on July 18, 2024

understand, i need to convert it to \n then reconvert it back to lines

image

It seems you still haven't understood what I meant. If you extract the data from the Response, printing it will automatically line break because it contains \n.

from deeplx.

littleblack111 avatar littleblack111 commented on July 18, 2024

I need to convert it as well right?(like var=var.replace('\n', '\n')
oh, I'm not printing it. I'm displaying it like a widget so, i convert it back (var=var.replace('\n', '\n'))

from deeplx.

missuo avatar missuo commented on July 18, 2024

I need to convert it as well right?(like var=var.replace('\n', '\n') oh, I'm not printing it. I'm displaying it like a widget so, i convert it back (var=var.replace('\n', '\n'))

Show me your full code.

from deeplx.

littleblack111 avatar littleblack111 commented on July 18, 2024

    text = data["q"]
    target_lang = data["target"]

    if "source" in data:
        source_lang = data["source"]

    deeplx_base_url = os.environ.get("DEEPLX_BASE_URL")

    if not deeplx_base_url:
        return Response(
            status_code=500,
            content=json.dumps(
                {
                    "error": {
                        "message": "No DEEPLX_BASE_URL provided",
                    }
                }
            ),
        )
    print(text)
    text = text.replace('\n', '\\n')
    print(text)
    body = {
        "text": text,
        "target_lang": target_lang,
    }
    if "source" in data:
        body["source_lang"] = source_lang

    try:
        req = ProxyRequest(
            deeplx_base_url, "POST", '', json.dumps(body), query_params={}
#             deeplx_base_url, "POST", headers, json.dumps(body), query_params={}
        )
        resp = await pass_through_request(http_client, req, nohttps=True, noheaders=True)
        resp = json.loads(resp.content.decode("utf-8"))
        try:
            translated_text = resp["alternatives"][0]
            print(translated_text)
            translated_text = translated_text.replace('\\nn', '\n')
            print(translated_text)
            res = {"data": {"translations": [{"translatedText": translated_text}]}}
        except TypeError:
            # res = {"error": {"message": "Failed to translate"}}
            # res = {"data": {"translations": [{"translatedText": "Failed to translate"}]}}
            res = {"data": {"translations": [{"translatedText": text}]}}

        if "source" not in raycast_data:
            res["data"]["translations"][0]["detectedSourceLanguage"] = resp[
                "source_lang"
            ].lower()

        return Response(status_code=200, content=json.dumps(res))
    except Exception as e:
        logger.error(f"DEEPLX error: {e}")
        return Response(
            status_code=500,
            content=json.dumps(
                {
                    "error": {
                        "message": "Unknown error",
                    }
                }
            ),
        )

from deeplx.

missuo avatar missuo commented on July 18, 2024

replace('\n', '\\n')

Why do this?

from deeplx.

littleblack111 avatar littleblack111 commented on July 18, 2024

because otherwise it will return {"code":404,"message":"No text to translate}

from deeplx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.