-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme.txt
450 lines (372 loc) · 13.3 KB
/
readme.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
I used assembly ai api for transciprting youtube video speech into text and use python library as well for first download audio of this video file
-------------------------------------------------------
aiohttp==3.8.4
aiosignal==1.3.1
anyio==3.6.2
arabic-reshaper==3.0.0
asgiref==3.6.0
asn1crypto==1.5.1
assemblyai==0.17.0
async-timeout==4.0.2
attrs==22.2.0
cachetools==4.2.4
certifi==2022.12.7
cffi==1.15.1
charset-normalizer==3.0.1
click==8.1.3
cryptography==39.0.2
cssselect2==0.7.0
defusedxml==0.7.1
Django==4.1.7
django-allauth==0.54.0
django-extensions==3.2.1
django-googledrive-storage==1.6.0
django-storages==1.13.2
fastapi==0.94.1
frozenlist==1.3.3
google-api-core==2.10.2
google-api-python-client==2.97.0
google-auth==1.35.0
google-auth-httplib2==0.1.0
googleapis-common-protos==1.60.0
h11==0.14.0
html5lib==1.1
httpcore==0.17.3
httplib2==0.22.0
httpx==0.24.1
idna==3.4
iniconfig==2.0.0
lxml==4.9.2
multidict==6.0.4
openai==0.27.5
psycopg2==2.9.6
psycopg2-binary==2.9.6
pytube==15.0.0
requests==2.28.2
sqlparse==0.4.3
webencodings==0.5.1
websockets==11.0.3
-----------------------------------------------------------------------------------------------
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
deactivate
source venv/bin/activate # Use `venv\Scripts\activate` on Windows
pip install --upgrade httpcore
pip install httpcore==0.13.6
pip install --upgrade googletrans
pip install googletrans==4.0.0-rc1
pip install -r requirements.txt
python manage.py runserver
--------------------------------------------------------------------------------------------
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.conf import settings
import json
from pytube import YouTube
import os
import assemblyai as aai
from .models import BlogPost
# Create your views here.
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def generate_blog(request):
if request.method == 'POST':
try:
data = json.loads(request.body)
yt_link = data['link']
except (KeyError, json.JSONDecodeError):
return JsonResponse({'error': 'Invalid data sent'}, status=400)
# get yt title
title = yt_title(yt_link)
# get transcript
transcription = get_transcription(yt_link)
if not transcription:
return JsonResponse({'error': "Failed to get transcript"}, status=500)
# Generate blog content from the transcript
blog_content = generate_blog_from_transcription(transcription)
if not blog_content:
return JsonResponse({'error': "Failed to generate blog article"}, status=500)
# Save blog article to database
new_blog_article = BlogPost.objects.create(
user=request.user,
youtube_title=title,
youtube_link=yt_link,
generated_content=blog_content,
)
new_blog_article.save()
# Return blog article as a response
return JsonResponse({'content': blog_content})
else:
return JsonResponse({'error': 'Invalid request method'}, status=405)
def yt_title(link):
yt = YouTube(link)
title = yt.title
return title
def download_audio(link):
yt = YouTube(link)
video = yt.streams.filter(only_audio=True).first()
out_file = video.download(output_path=settings.MEDIA_ROOT)
base, ext = os.path.splitext(out_file)
new_file = base + '.mp3'
os.rename(out_file, new_file)
return new_file
def get_transcription(link):
audio_file = download_audio(link)
# Your AssemblyAI API key
aai.settings.api_key = "84d29c9d45e04dc2870c581dfd197bff"
transcriber = aai.Transcriber()
transcript = transcriber.transcribe(audio_file)
return transcript.text
def generate_blog_from_transcription(transcription):
# No OpenAI code here, only AssemblyAI
# Process the transcription here as needed for generating the blog content
return "Mp3 file has been downloaded"
def blog_list(request):
blog_articles = BlogPost.objects.filter(user=request.user)
return render(request, "all-blogs.html", {'blog_articles': blog_articles})
def blog_details(request, pk):
blog_article_detail = BlogPost.objects.get(id=pk)
if request.user == blog_article_detail.user:
return render(request, 'blog-details.html', {'blog_article_detail': blog_article_detail})
else:
return redirect('/')
def user_login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
error_message = "Invalid username or password"
return render(request, 'login.html', {'error_message': error_message})
return render(request, 'login.html')
def user_signup(request):
if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
repeatPassword = request.POST['repeatPassword']
if password == repeatPassword:
try:
user = User.objects.create_user(username, email, password)
user.save()
login(request, user)
return redirect('/')
except:
error_message = 'Error creating account'
return render(request, 'signup.html', {'error_message':error_message})
else:
error_message = 'Password do not match'
return render(request, 'signup.html', {'error_message':error_message})
return render(request, 'signup.html')
def user_logout(request):
logout(request)
return redirect('/')
------------------------------------------------------------------------------------------------------
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.conf import settings
import json
from pytube import YouTube
import os
import assemblyai as aai
from .models import BlogPost
# View for rendering the index page, accessible only to logged-in users
@login_required
def index(request):
"""
Renders the index page.
Returns:
Rendered index page.
"""
return render(request, 'index.html')
# View for generating blog content from a YouTube video link
@csrf_exempt
def generate_blog(request):
"""
Generates blog content from a YouTube video link.
If the request method is POST, it extracts the YouTube link from the request data,
retrieves the video title and transcript, generates blog content from the transcript,
saves the blog article to the database, and returns the generated content as a response.
If the request method is not POST, it returns an error response.
Returns:
JSON response containing the generated blog content or an error message.
"""
if request.method == 'POST':
try:
data = json.loads(request.body)
yt_link = data['link']
except (KeyError, json.JSONDecodeError):
return JsonResponse({'error': 'Invalid data sent'}, status=400)
# get YouTube video title
title = yt_title(yt_link)
# get transcript from the YouTube video
transcription = get_transcription(yt_link)
if not transcription:
return JsonResponse({'error': "Failed to get transcript"}, status=500)
# generate blog content from the transcript
blog_content = generate_blog_from_transcription(transcription)
if not blog_content:
return JsonResponse({'error': "Failed to generate blog article"}, status=500)
# save blog article to database
new_blog_article = BlogPost.objects.create(
user=request.user,
youtube_title=title,
youtube_link=yt_link,
generated_content=blog_content,
)
new_blog_article.save()
# return blog article as a response
return JsonResponse({'content': blog_content})
else:
return JsonResponse({'error': 'Invalid request method'}, status=405)
# Helper function to get the YouTube video title
def yt_title(link):
"""
Retrieves the title of a YouTube video.
Args:
link (str): The YouTube video link.
Returns:
str: The title of the YouTube video.
"""
yt = YouTube(link)
title = yt.title
return title
# Helper function to download audio from a YouTube video
def download_audio(link):
"""
Downloads audio from a YouTube video.
Args:
link (str): The YouTube video link.
Returns:
str: The path to the downloaded audio file.
"""
yt = YouTube(link)
video = yt.streams.filter(only_audio=True).first()
out_file = video.download(output_path=settings.MEDIA_ROOT)
base, ext = os.path.splitext(out_file)
new_file = base + '.mp3'
os.rename(out_file, new_file)
return new_file
# Helper function to get transcription from the audio file using AssemblyAI
def get_transcription(link):
"""
Retrieves transcription from an audio file using AssemblyAI.
Args:
link (str): The YouTube video link.
Returns:
str: The transcription text.
"""
audio_file = download_audio(link)
aai.settings.api_key = "84d29c9d45e04dc2870c581dfd197bff" # Your AssemblyAI API key
transcriber = aai.Transcriber()
transcript = transcriber.transcribe(audio_file)
return transcript.text
# Helper function to generate blog content from the transcription
def generate_blog_from_transcription(transcription):
"""
Generates blog content from a transcription.
Args:
transcription (str): The transcription text.
Returns:
str: The generated blog content.
"""
# For now, simply return the transcription itself as the blog content
return transcription
# View for rendering the list of blog articles
def blog_list(request):
"""
Renders the list of blog articles.
Args:
request: The HTTP request.
Returns:
Rendered page displaying the list of blog articles.
"""
blog_articles = BlogPost.objects.filter(user=request.user)
return render(request, "all-blogs.html", {'blog_articles': blog_articles})
# View for rendering details of a specific blog article
def blog_details(request, pk):
"""
Renders details of a specific blog article.
Args:
request: The HTTP request.
pk (int): The primary key of the blog article.
Returns:
Rendered page displaying details of the specified blog article.
"""
blog_article_detail = BlogPost.objects.get(id=pk)
if request.user == blog_article_detail.user:
return render(request, 'blog-details.html', {'blog_article_detail': blog_article_detail})
else:
return redirect('/')
# View for user login
def user_login(request):
"""
Handles user login.
Args:
request: The HTTP request.
Returns:
Rendered login page with error message if login fails.
"""
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
error_message = "Invalid username or password"
return render(request, 'login.html', {'error_message': error_message})
return render(request, 'login.html')
# View for user signup
def user_signup(request):
"""
Handles user signup.
Args:
request: The HTTP request.
Returns:
Rendered signup page with error message if signup fails.
"""
if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
repeatPassword = request.POST['repeatPassword']
if password == repeatPassword:
try:
user = User.objects.create_user(username, email, password)
user.save()
login(request, user)
return redirect('/')
except:
error_message = 'Error creating account'
return render(request, 'signup.html', {'error_message':error_message})
else:
error_message = 'Password do not match'
return render(request, 'signup.html', {'error_message':error_message})
return render(request, 'signup.html')
# View for user logout
def user_logout(request):
"""
Handles user logout.
Args:
request: The HTTP request.
Returns:
Redirects to the homepage after logout.
"""
logout(request)
return redirect('/')