Recently I was involved in Project that touches a least use case, I’m writing this tutorial to you through building a Django web application that simplifies complex English text using a combination of Hugging Face Transformers (for local models) and Google’s Gemini API (for advanced AI reasoning).
π§ Use Case: Text Simplification for Non-Native English Speakers
This use case is particularly useful for students, language learners, or professionals needing to understand dense academic or technical texts.
β Prerequisites
Before we begin, ensure the following are installed:
- Python 3.8+
- pip
- Virtual environment (
venv
) - Django
- Git (optional)
π¦ Tools & Services Used
Tool/Service | Purpose |
---|---|
Django | Backend web framework |
Hugging Face | Local NLP model inference |
Gemini API | Cloud-based advanced summarization |
PostgreSQL (or SQLite) | Database for storing history |
π Step-by-Step Implementation
π Step 1: Set Up Your Project
mkdir text_simplifier
cd text_simplifier
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install dependencies:
pip install django transformers torch google-generativeai python-dotenv
Start a new Django project:
django-admin startproject simplifier_project .
python manage.py startapp simplifier_app
Add app to INSTALLED_APPS
in simplifier_project/settings.py
:
INSTALLED_APPS = [
...
'simplifier_app',
]
Apply migrations:
python manage.py migrate
ποΈ Step 2: Create Models
In simplifier_app/models.py
, define a model to store user input and results:
from django.db import models
class SimplifiedText(models.Model):
original_text = models.TextField()
simplified_text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Simplified {self.created_at}"
Run migrations:
python manage.py makemigrations
python manage.py migrate
π Step 3: Configure Gemini API Key
Create .env
file at root:
GEMINI_API_KEY=your_gemini_api_key_here
Update simplifier_project/settings.py
to load it:
import os
from dotenv import load_dotenv
load_dotenv() # Load environment variables from .env
π€ Step 4: Add Hugging Face and Gemini Logic
Create a utility file utils.py
inside simplifier_app
:
from transformers import pipeline
import google.generativeai as genai
import os
# Load Hugging Face local model
text_simplifier = pipeline("text2text-generation", model="facebook/bart-large-cnn")
def hf_simplify(text):
result = text_simplifier(text, max_length=100, clean_up_tokenization_spaces=True)
return result[0]['generated_text']
# Initialize Gemini
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
gemini_model = genai.GenerativeModel("gemini-pro")
def gemini_simplify(text):
prompt = f"Please simplify this text for a non-native English speaker:\n\n{text}"
response = gemini_model.generate_content(prompt)
return response.text
π Step 5: Build Views and Templates
In views.py
:
from django.shortcuts import render, redirect
from .models import SimplifiedText
from .utils import hf_simplify, gemini_simplify
def simplify_text(request):
if request.method == "POST":
original_text = request.POST.get("text")
method = request.POST.get("method")
if method == "hf":
simplified = hf_simplify(original_text)
else:
simplified = gemini_simplify(original_text)
SimplifiedText.objects.create(
original_text=original_text,
simplified_text=simplified
)
return redirect("history")
return render(request, "simplify_form.html")
def history(request):
entries = SimplifiedText.objects.all().order_by("-created_at")[:10]
return render(request, "history.html", {"entries": entries})
π Step 6: Create HTML Templates
Create a folder templates/
in the project root and add:
templates/simplify_form.html
<h1>Text Simplifier</h1>
<form method="post">
{% csrf_token %}
<textarea name="text" rows="10" cols="80" placeholder="Paste your complex text here..."></textarea><br/>
<label>
<input type="radio" name="method" value="hf" checked/> Use Hugging Face (local)
</label>
<label>
<input type="radio" name="method" value="gemini"/> Use Gemini API
</label><br/>
<button type="submit">Simplify</button>
</form>
templates/history.html
<h1>History</h1>
{% for entry in entries %}
<div style="margin-bottom: 20px; border: 1px solid #ccc; padding: 10px;">
<strong>Original:</strong><p>{{ entry.original_text }}</p>
<strong>Simplified:</strong><p>{{ entry.simplified_text }}</p>
</div>
{% endfor %}
<a href="{% url 'simplify' %}">Back</a>
π§ Step 7: Configure URLs
In simplifier_app/urls.py
:
from django.urls import path
from . import views
urlpatterns = [
path('', views.simplify_text, name='simplify'),
path('history/', views.history, name='history'),
]
In simplifier_project/urls.py
:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('simplifier_app.urls')),
]
Set templates directory in settings.py
:
TEMPLATES = [
{
...
'DIRS': [BASE_DIR / 'templates'],
...
}
]
πββοΈ Step 8: Run Server and Test
python manage.py runserver
Open browser at http://localhost:8000
Try pasting a complex paragraph like:
“The process of photosynthesis involves the green pigmentation within chloroplasts capturing light energy, which subsequently facilitates the conversion of carbon dioxide and water into glucose and oxygen.”
Choose either Hugging Face or Gemini, then submit. You’ll be redirected to the simplified version and history page.
π Optional Enhancements
- Add user authentication to track per-user history
- Allow document uploads (.txt, .pdf)
- Cache frequent queries
- Add comparison between both methods
- Deploy on Vercel + Hugging Face Inference API
π§ͺ Sample Output Comparison
Input:
“Photosynthesis is a process used by plants and other organisms to convert light energy into chemical energy.”
Hugging Face BART Output:
“Photosynthesis helps plants change light into energy.”
Gemini Output:
“Plants use sunlight to make food through a process called photosynthesis.”
π¦ Deployment Tips
Use gunicorn
and whitenoise
for production:
pip install gunicorn whitenoise
Deploy via:
- Heroku
- Render
- Railway
- Docker + AWS/GCP
π§ Why This Use Case?
While most apps focus on translation or summarization, text simplification for ESL (English as Second Language) users is an underutilized but highly impactful task. It helps improve accessibility and comprehension across diverse educational and professional backgrounds.
β Summary
You now have a full-stack Django app that leverages:
- Hugging Face Transformers for fast, offline text simplification
- Google Gemini API for more nuanced AI-generated explanations
- A simple UI for interaction and history tracking
This architecture can easily scale into a larger NLP service hub!
πββοΈ Further scope of work
- Admin panel customization
- PDF upload support
- REST API version
- Multilingual support
Happy coding! π