Home >Web Front-end >JS Tutorial >Creating AI-based Summaries in a Django Website

Creating AI-based Summaries in a Django Website

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-30 06:13:10986browse

Summarizing lengthy text can be tedious, especially on platforms like PyBazaar, where concise summaries improve user experience. In this post, I'll share how I used Simplemind and Gemini to automate this process in my Django-based project.

Background Info

Recently, I launched PyBazaar.com, a website for Python developers to show their skills, find job offers, and post and find development resources. Its purpose is to have a central place where Python developers can market their services, products, or projects.

PyBazaar shows lengthy descriptions of career opportunities and resources in the detail views and short summaries in the list views. Summaries help users quickly grasp the content of resources and career opportunities without opening each detailed view, enhancing the overall browsing experience on PyBazaar. To make the editing smoother, I introduced automatic summarization based on AI.

Creating AI-based Summaries in a Django Website

Choosing Simplemind for Communication with LLMs

Kenneth Reitz, the author of the famous package requests, recently published his newest creation—Simplemind—which improves the developer experience with the APIs of large language models (LLMs). I thought it would be a good opportunity to try integrating his package into PyBazaar.

While I chose Google Gemini for its free tier, Simplemind's support for providers like OpenAI or Claude means developers can scale up for more advanced features or more precise results if needed.

Setting Up API Keys

At first, I had to get an API Key at Google AI Studio.

Django Integration

Then I installed Simplemind:

(venv)$ pip install 'simplemind[full]'

However, while waiting for one of the dependencies (grpcio) to compile on my Mac, I had time for an energy drink and enough time to scroll through half my social media feed.

Simplemind expects the LLM API keys to be defined in the environment variables. In my Django projects, I store the secrets in JSON files, which Git ignores, and I read those values with a utility function I wrote, get_secret().

So, I added these lines in the Django settings:

import os
os.environ["GEMINI_API_KEY"] = get_secret("GEMINI_API_KEY")
DEFAULT_LLM_PROVIDER = "gemini"

I created a straightforward view that takes posted HTML content, asks LLM to summarize it, and returns the summary to the user:

import json
import simplemind
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.http import JsonResponse
from django.utils.html import strip_tags


@login_required
def summarize(request):
    summary = ""
    try:
        if (
            request.method == "POST"
            and (data := json.loads(request.body))
            and (content := data.get("content"))
            and (text := strip_tags(content).strip())
        ):
            summary = simplemind.generate_text(
                prompt=f"Condense the following information in 2 sentences:\n\n{text}",
                llm_provider=settings.DEFAULT_LLM_PROVIDER,
            ).strip()
    except json.JSONDecodeError:
        pass
    data = {"summary": summary}
    return JsonResponse(data)

As you can see, Simplemind is as elegant as the requests app. I could easily switch to OpenAI or Claude if I needed more advanced results or smarter queries.

I used strip_tags() to reduce the token count and strip() to remove leading and trailing whitespaces.

To improve the view's performance, I could also use ASGI or a background task, but that's something to consider when there are more users at PyBazaar.

The summarization button had its template, which I included in my Django Crispy Forms layout with layout.HTML("""{% include "summarizer/includes/summarize_button.html" %}"""):

(venv)$ pip install 'simplemind[full]'

When a user clicks on the "Summarize by AI" button, the Javascript temporarily disabls the button, changes its text to "Summarizing...", reads the HTML value from the QuillJS field, and posts it as {"content": "..."} to the summarize view. After receiving the summary as {"summary": "..."}, the Javascript fills in the summary textarea and makes the button clickable again.

Conclusion

Simplemind makes working with LLMs easier using smart defaults, so developers don't have to adjust complicated settings like temperature or max_tokens.

Gemini LLM can be used for free, and that seems good enough for simple features like this with a moderate number of active users.

I implemented this summarization feature at PyBazaar in just half a day, and I could easily adapt this integration to generate meta descriptions, email drafts, or personalized recommendations.

If you're a Python developer looking to showcase your skills, share resources, or find opportunities, visit PyBazaar.com today!


Cover photo by Caio

The above is the detailed content of Creating AI-based Summaries in a Django Website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn