= True OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') openai.api_key = OPENAI_API_KEY @app.route('/') def index(): return {'hello': 'world'} @app.route('/summarize', methods=['POST'], content_types=['application/json']) def summarize(): request = app.current_request url = request.json_body.get('url') if not url: return Response(body={'error': 'URL is required'}, status_code=400) try: page_content = fetch_page_content(url) summary = summarize_content(page_content) return Response(body=json.dumps({'summary': summary}, ensure_ascii=False), status_code=200, headers={'Content-Type': 'application/json'}) except Exception as e: return Response(body=json.dumps({'error': str(e)}, ensure_ascii=False), status_code=500, headers={'Content-Type': 'application/json'}) def fetch_page_content(url): response = requests.get(url) if response.status_code != 200: raise ValueError('Failed to fetch the page content') soup = BeautifulSoup(response.text, 'html.parser') paragraphs = soup.find_all('p') page_text = ' '.join([p.get_text() for p in paragraphs]) return page_text def summarize_content(content): response = openai.chat.completions.create( model='gpt-3.5-turbo', messages=[ {"role": "system", "content": "⼤学⽣でもわかるように要約してください"}, {"role": "user", "content": f"次のサイトを200⽂字程度で要約してください:{content}"} ], max_tokens=500 ) summary_text = response.choices[0].message.content.strip() return summary_text 最終的なapp.pyのコード。9割はChatGPTに⽣成してもらい、⼈間が⼿で修正した。※importは省略