from flask import Flask, request, render_template
import requests
import openai
from bs4 import BeautifulSoup
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 = "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()
translated_content = translated_content.replace("\n", "<br>") # Replace the linebreak with <br>
# Display translation
summary_prompt_text = translated_content.replace("<br>", "\n") # Change <br> back to linebreak
summary_prompt = "Summarize this text in Japanese:\n" + summary_prompt_text
summary = openai.Completion.create(
model="text-davinci-003",
prompt=summary_prompt,
max_tokens=500,
n=1,
stop=None,
temperature=0.5
)
summarized_content = summary.choices[0].text.strip()
# Display translation and summary
return render_template('translated.html', content=translated_content, summary=summarized_content)
# If 'GET'request, then display URL entry page
return render_template('index.html')
if __name__ == "__main__":
|