Skip to content

Commit

Permalink
AI Integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
UTSAVS26 committed Aug 14, 2024
1 parent 76264b3 commit 720f6b9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Set up environment variables
run: echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> $GITHUB_ENV

- name: Run tests
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/test_db
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
python manage.py migrate
python manage.py test
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pip install -r requirements.txt
```dotenv
DJANGO_SECRET_KEY='your-secret-key'
DEBUG=True
OPENAI_API_KEY='your-openai-key'
```

### 5. Apply Migrations
Expand Down
6 changes: 5 additions & 1 deletion news_aggregator/apps/news/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = '__all__'
fields = ['id', 'title', 'content', 'published_at']

class ArticleSummarySerializer(serializers.Serializer):
title = serializers.CharField(max_length=200)
summary = serializers.CharField(max_length=1000)
26 changes: 18 additions & 8 deletions news_aggregator/apps/news/templates/news/article_detail.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{% extends "base.html" %}

{% block content %}
<h1>{{ article.title }}</h1>
<p>By {{ article.author }} on {{ article.published_at }}</p>
<p>{{ article.content }}</p>
<p><a href="{{ article.url }}" target="_blank">Read more at {{ article.source }}</a></p>
{% endblock %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ article.title }}</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<div class="article-container">
<h1>{{ article.title }}</h1>
<p>{{ article.content }}</p>

<h2>Summary:</h2>
<p>{{ summary }}</p>
</div>
</body>
</html>
22 changes: 17 additions & 5 deletions news_aggregator/apps/news/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from django.shortcuts import render, get_object_or_404
from .models import Article
import openai
from django.conf import settings

def index(request):
articles = Article.objects.all().order_by('-published_at')
def article_list(request):
articles = Article.objects.all()
return render(request, 'news/index.html', {'articles': articles})

def article_detail(request, pk):
article = get_object_or_404(Article, pk=pk)
return render(request, 'news/article_detail.html', {'article': article})
def article_detail(request, article_id):
article = get_object_or_404(Article, id=article_id)

# Summarize the article using ChatGPT
openai.api_key = settings.OPENAI_API_KEY
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Summarize the following article: {article.content}",
max_tokens=150
)
summary = response.choices[0].text.strip()

return render(request, 'news/article_detail.html', {'article': article, 'summary': summary})
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Django>=4.0,<5.0
djangorestframework>=3.12.0,<4.0
python-dotenv>=0.19.0
gunicorn>=20.1.0
gunicorn>=20.1.0
openai

0 comments on commit 720f6b9

Please sign in to comment.