jsurfer's language selection page >
       jsurfer's homepage >
              Web App >



On-the-Fly Auto translation by ChatGPT -From English to Japanese-
- "With no html tags&no linebreaks" version -

Created on: August 21, 2023
Last updated on: September 01, 2023



This Web App translates English Web page into Japanese on-the-fly by using OpenAI API(ChatGPT) "with no html tags&no linebreaks".


Download this version from here.  Check my web page for "With html tags" version on how to use because the usage is exactly the same.


Like the previous version, we will have this Web App translate this English page.
http://cheezsj.com/ios/ios_en.html
 


The result is this. As you can see, the translation is done as expected but since this is "With no html tags&no linebreaks" version, there are no linebreaks so it is not easy to read:
 



And just for the comparison, here is the "With html tags" version:
 


==========



For your reference, below is the codes for this Web App:

app.py
from flask import Flask, request, render_template
import requests
import openai
from bs4 import BeautifulSoup  # Added

openai.api_key = "ENTER-YOUR-API-KEY-HERE"

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # Get URL from the user's input
        url = request.form.get('url')

        # Get info of the Web page
        response = requests.get(url)
        page_content = response.text

        # Get rid of HTML tags
        soup = BeautifulSoup(page_content, 'html.parser')
        text_only = soup.get_text()

        # Translate to Japanese
        prompt_text = "Translate English to Japanese:\n" + text_only[:3000]  # Only for the first 3000 characters
        translation = openai.Completion.create(
            model="text-davinci-003",
            prompt=prompt_text,
            max_tokens=2000,
            n=1,
            stop=None,
            temperature=0.5
        )
        translated_content = translation.choices[0].text.strip()

        # Display translation
        return render_template('translated.html', content=translated_content)

    # If 'GET'request, then display URL entry page
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True)

			



index.html
<!DOCTYPE html>
<html>
<head>
	<title>URL Translator</title>
</head>
<body>
	<form action="/" method="post">
		<label for="url">Enter a URL:</label>
		<input type="text" name="url" id="url">
		<button type="submit">Translate</button>
	</form>
</body>
</html>
			



translated.html
<!DOCTYPE html>
<html>
<head>
	<title>Translated Page</title>
</head>
<body>
	<h1>Translated Content:</h1>
	<div>{{ content }}</div>
	<a href="/">Back</a>
</body>
</html>