{"id":3900,"date":"2023-11-04T23:13:55","date_gmt":"2023-11-04T23:13:55","guid":{"rendered":"http:\/\/localhost:10003\/how-to-build-a-text-summarizer-with-openai-gpt-3-and-django\/"},"modified":"2023-11-05T05:48:28","modified_gmt":"2023-11-05T05:48:28","slug":"how-to-build-a-text-summarizer-with-openai-gpt-3-and-django","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-build-a-text-summarizer-with-openai-gpt-3-and-django\/","title":{"rendered":"How to Build a Text Summarizer with OpenAI GPT-3 and Django"},"content":{"rendered":"
In this tutorial, we will walk through the steps to build a text summarizer using OpenAI GPT-3 and Django, a popular Python web framework. We will utilize the power of GPT-3 to generate concise summaries of long texts.<\/p>\n
To follow along with this tutorial, you should have the following prerequisites:<\/p>\n
To get started, let’s create a new Django project and set up the basic structure. Open your terminal or command prompt and run the following commands:<\/p>\n
$ django-admin startproject text_summarizer\n$ cd text_summarizer\n$ python manage.py startapp summarizer\n<\/code><\/pre>\nThis will create a new Django project called “text_summarizer” and a new app called “summarizer” within the project.<\/p>\n
Next, let’s update the project settings. Open the settings.py<\/code> file located in the text_summarizer<\/code> directory and add ‘summarizer’ to the INSTALLED_APPS<\/code> list:<\/p>\nINSTALLED_APPS = [\n ...\n 'summarizer',\n]\n<\/code><\/pre>\nIn the same file, locate the TEMPLATES<\/code> variable and add the following 'DIRS'<\/code> option:<\/p>\nTEMPLATES = [\n {\n ...\n 'DIRS': [os.path.join(BASE_DIR, 'templates')],\n ...\n },\n]\n<\/code><\/pre>\nWe have added the directory where our templates will be stored.<\/p>\n
Next, let’s create a template for our summarizer. Create a new directory called ‘templates’ in the project’s root directory, and then create a new file named ‘index.html’ inside the templates directory.<\/p>\n
Creating the Text Summarization Form<\/h2>\n
Open the index.html<\/code> file you just created and add the following code:<\/p>\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>Text Summarization<\/title>\n<\/head>\n<body>\n <h1>Text Summarization<\/h1>\n <form method=\"POST\" action=\"{% url 'summarizer:summarize' %}\">\n {% csrf_token %}\n <textarea name=\"text\" rows=\"10\" cols=\"50\"><\/textarea>\n <button type=\"submit\">Summarize<\/button>\n <\/form>\n {% if summary %}\n <h2>Summary:<\/h2>\n <p>{{ summary }}<\/p>\n {% endif %}\n<\/body>\n<\/html>\n<\/code><\/pre>\nThis HTML code creates a basic form where users can input the text they want to summarize. When the form is submitted, it will make a POST request to the summarize<\/code> URL.<\/p>\nAdding Views and URLs<\/h2>\n
Now, let’s define the views and URLs for our summarizer app. Open the views.py<\/code> file located inside the summarizer<\/code> app directory and replace the existing code with the following:<\/p>\nfrom django.shortcuts import render\nimport openai\n\ndef summarize(request):\n if request.method == 'POST':\n text = request.POST.get('text')\n api_key = 'YOUR_OPENAI_API_KEY' # Replace with your actual API key\n openai.api_key = api_key\n\n response = openai.Completion.create(\n engine='text-davinci-003',\n prompt=text,\n max_tokens=100,\n temperature=0.3,\n n=1,\n stop=None\n )\n\n summary = response.choices[0].text\n\n return render(request, 'index.html', {'summary': summary})\n\n return render(request, 'index.html')\n<\/code><\/pre>\nIn the summarize<\/code> view, we first check if the request method is POST. If it is, we retrieve the text entered by the user using request.POST.get('text')<\/code>.<\/p>\nNext, we specify our OpenAI API key by replacing 'YOUR_OPENAI_API_KEY'<\/code> with the actual key you obtained.<\/p>\nWe then use the OpenAI Python package to make an API call to GPT-3 for text summarization. The openai.Completion.create<\/code> method takes various parameters such as the engine, prompt, max_tokens, temperature, n, and stop.<\/p>\nThe summary<\/code> variable contains the generated text summary obtained from the API response. Finally, we render the index.html<\/code> template, passing the summary<\/code> as a context variable.<\/p>\nNext, open the urls.py<\/code> file located in the summarizer<\/code> app directory and add the following code:<\/p>\nfrom django.urls import path\nfrom . import views\n\napp_name = 'summarizer'\n\nurlpatterns = [\n path('', views.summarize, name='summarize'),\n]\n<\/code><\/pre>\nThis code sets up the URL patterns for our summarizer app. We map the root URL to the summarize<\/code> view.<\/p>\nTesting the Text Summarizer<\/h2>\n
Now that we have set up our Django project, let’s test our text summarizer. Start the development server by running the following command in your terminal or command prompt:<\/p>\n
$ python manage.py runserver\n<\/code><\/pre>\nOpen your web browser and navigate to `http:\/\/localhost:8000` to access the text summarizer form.<\/p>\n
Enter a piece of text in the input field and click on the “Summarize” button. The form will be submitted, and the summary will be displayed on the same page.<\/p>\n
Deploying the Text Summarizer<\/h2>\n
Once you are satisfied with the functionality of your text summarizer, you can deploy it to a hosting platform to make it publicly accessible. There are several options to choose from, such as Heroku, AWS, or PythonAnywhere. Below, we will cover a simple deployment using Heroku as an example.<\/p>\n
Deploying to Heroku<\/h3>\n
To deploy your Django app on Heroku, follow these steps:<\/p>\n
\n- Create a new Heroku account if you don’t have one already.<\/p>\n<\/li>\n
- \n
Install the Heroku CLI by following the instructions on the Heroku Dev Center<\/a>.<\/p>\n<\/li>\n- \n
Login to the Heroku CLI using the following command:<\/p>\n
$ heroku login\n<\/code><\/pre>\n<\/li>\n- Create a new Heroku app by running the following command:\n
$ heroku create your-app-name\n<\/code><\/pre>\nReplace “your-app-name” with your desired app name.<\/p>\n<\/li>\n
- \n
Add the following buildpacks for Python and Node.js:<\/p>\n
$ heroku buildpacks:add heroku\/python\n$ heroku buildpacks:add heroku\/nodejs\n<\/code><\/pre>\n<\/li>\n- Commit any uncommitted changes to your Git repository.<\/p>\n<\/li>\n
- \n
Push your code to the Heroku remote:<\/p>\n
$ git push heroku master\n<\/code><\/pre>\n<\/li>\n- Run the following command to scale your Heroku app to one web dyno:\n
$ heroku ps:scale web=1\n<\/code><\/pre>\n<\/li>\n- Set your OpenAI API key as an environment variable on Heroku using the following command:\n
$ heroku config:set OPENAI_API_KEY=YOUR_OPENAI_API_KEY\n<\/code><\/pre>\nReplace “YOUR_OPENAI_API_KEY” with your actual OpenAI API key.<\/p>\n<\/li>\n
- \n
Run the following command to open your deployed Django app in a web browser:<\/p>\n
$ heroku open\n<\/code><\/pre>\n<\/li>\n<\/ol>\nCongratulations! You have successfully deployed your text summarizer app to Heroku.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we explored how to build a text summarizer using OpenAI GPT-3 and Django. We covered the necessary steps to set up a Django project, wrote the views, and created the necessary templates. We also discussed how to deploy the app to Heroku for public access.<\/p>\n
With this text summarizer, you can now generate concise summaries of long texts using the power of OpenAI GPT-3. The possibilities for this type of technology are endless, and you can further enhance and optimize the summarizer based on your specific use case and requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this tutorial, we will walk through the steps to build a text summarizer using OpenAI GPT-3 and Django, a popular Python web framework. We will utilize the power of GPT-3 to generate concise summaries of long texts. Prerequisites To follow along with this tutorial, you should have the following Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[207,117,205,41,40,206,116,75,49,204],"yoast_head":"\nHow to Build a Text Summarizer with OpenAI GPT-3 and Django - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n