-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackend.py
More file actions
300 lines (241 loc) · 9.89 KB
/
Copy pathBackend.py
File metadata and controls
300 lines (241 loc) · 9.89 KB
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
import openai
import os
import re
import pdfplumber
from dotenv import load_dotenv, dotenv_values
import cv2
import yt_dlp as youtube_dl
from youtube_transcript_api import YouTubeTranscriptApi
import vertexai
from vertexai.generative_models import GenerativeModel, Part
from google.cloud import storage
import prompts
from moviepy.editor import VideoFileClip
import math
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import pdfkit
from PyPDF2 import PdfReader, PdfWriter, PdfFileReader
import time, logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from pymongo import MongoClient
load_dotenv()
openai.api_key = os.getenv('openAI_token')
project_id = os.getenv('project_id')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'service_account.json'
storage_client = storage.Client()
location = 'asia-southeast1'
bucket_name = os.getenv('bucket_name')
vertexai.init(project=project_id, location=location)
quantity, type, content = "", "", ""
clips = []
pdfs = []
def generate_qns_googleapi(prompt, data_type, file) -> str:
vision_model = GenerativeModel("gemini-1.0-pro-vision")
response = vision_model.generate_content(
[
Part.from_uri(
"gs://"+ bucket_name +"/"+ file, mime_type=data_type
),
prompt,
]
)
return response
def upload_blob(bucket_name, source_file_name):
"""Uploads a file to the bucket."""
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_file_name)
blob.upload_from_filename(source_file_name)
print(
"File {} uploaded to {}.".format(
source_file_name, source_file_name
)
)
def delete_blob(bucket_name, blob_name):
"""Deletes a blob from the bucket."""
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)
blob.delete()
print(f"Blob {blob_name} deleted.")
def get_transcript(video_url):
regex = r'(?:youtube\.com\/(?:[^\/\n\s]+\/\s*[^\/\n\s]+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})'
match = re.search(regex, video_url)
video_id = math.group(1) if match else None
if video_id:
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
for entry in transcript:
print(f"Time: {entry['start']} - Text: {entry['text']}")
except Exception as e:
print("An error occurred:", e)
else:
print("Invalid YouTube URL")
def download_video(video_url, id):
ydl_opts = {
'format': 'worst',
'outtmpl': f'{id}.%(ext)s',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
clip_length = 120 # 2 minutes
video = VideoFileClip(f'{id}.mp4')
video_length = int(video.duration)
num_clips = math.ceil(video_length / clip_length)
for i in range(num_clips):
start_time = i * clip_length
end_time = min((i + 1) * clip_length, video_length)
clip = video.subclip(start_time, end_time)
clip.write_videofile(f"{id}_clip_{i+1}.mp4", codec="libx264", audio_codec="aac")
clips.append(f"{id}_clip_{i+1}.mp4")
def split_pdf(input_pdf, id):
pdf_reader = PdfReader(input_pdf)
for i in range(len(pdf_reader.pages)):
pdf_writer = PdfWriter()
pdf_writer.add_page(pdf_reader.pages[i])
with open(f"{id}_page_{i + 1}.pdf", "wb") as output_pdf:
pdf_writer.write(output_pdf)
pdfs.append(f"{id}_page_{i + 1}.pdf")
#main functions to generate output
def compilationcontent(video_url, clips, id):
prompt = "Provide as much information as possible from the video that is revelant."
context = ""
download_video(video_url, id)
for clip in clips:
upload_blob(bucket_name, clip)
context = generate_qns_googleapi(prompt, 'video/mp4', clip).text + context
delete_blob(bucket_name, clip)
os.remove(clip)
os.remove(f'{id}.mp4')
return context
def contexttoQns(context, quantity, type):
if type == 'flashcard':
prompt = prompts.flashcard
else:
prompt = prompts.question_paper
vision_model = GenerativeModel("gemini-1.0-pro-vision")
context = context + prompt + quantity
response = vision_model.generate_content(context).text
return response
def upload_file(file):
try:
upload_blob(bucket_name, file)
return file
except Exception as e:
logging.error(f"Error uploading {file}: {e}")
return None
def make_api_request(file, prompt):
try:
text = generate_qns_googleapi("Extract as much information as possible", 'application/pdf', file).text
print(text)
return text
except Exception as e:
logging.error(f"Error in API request for {file}: {e}")
return ""
# Function to delete a file
def delete_file(file):
delete_blob(bucket_name, file)
os.remove(file)
def pdftoQns(quantity, type, name):
if type == 'flashcard':
prompt = prompts.flashcard
else:
prompt = prompts.question_paper
split_pdf(name + '.pdf')
# Upload files in parallel
with ThreadPoolExecutor() as executor:
futures = [executor.submit(upload_file, pdf) for pdf in pdfs]
uploaded_files = [future.result() for future in as_completed(futures)]
print("upload done")
# Make API requests in parallel
with ThreadPoolExecutor(max_workers= 8) as executor:
futures = []
for file in uploaded_files:
if file is not None:
future = executor.submit(make_api_request, file, prompt)
futures.append(future)
time.sleep(1) # Wait for 0.5 seconds before starting the next thread
contexts = [future.result() for future in as_completed(futures)]
full_context = ''.join(contexts)
print("deleting files now")
print(contexttoQns(full_context, quantity, type).text)
# Delete files
with ThreadPoolExecutor() as executor:
futures = [executor.submit(delete_file, pdf) for pdf in pdfs]
def websitetopdf():
link = input("Enter the website URL: ")
type = input("What type of questions do you want? (flashcard or question_paper): ")
quantity = input("How many questions do you want? ")
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
# Initialize Selenium WebDriver with headless Chrome
browser = webdriver.Chrome(options=chrome_options)
browser.get(link)
# Saving page HTML to a variable
html = browser.page_source
#Ignoring load errors due to bad request / no access to certain resources
options = {
'load-error-handling': 'ignore',
'enable-local-file-access': ''
}
# Saving HTML to PDF
pdfkit.from_string(html, 'test.pdf', options=options)
pdftoQns(quantity, type, 'test')
os.remove('test.pdf')
# Close the browser
browser.quit()
def linktoqns(link, quantity, type):
content = compilationcontent(link, clips, 'nihaalmanaf')
return contexttoQns(content, quantity, type)
link = str(input("Enter the video link: "))
type = input("What type of questions do you want? (flashcard or question_paper): ")
quantity = input("How many questions do you want? ")
print(linktoqns(link, quantity, type))
logging.basicConfig(level=logging.INFO)
# websitetopdf() #not all websites work, some websites have restrictions on scraping
# pdftoQns("10", 'flashcard', 'test')
# Things to consider.
# Video name is not unique. So when multiple people use this, it may end up crashing the system. So once we are
# closer to completing the back end and begin front end production, we should consider how this will function
# with multiple users. possibly requiring mongodb
# Things to consider for optimization of youtube video to Questions
# Video Splitting: If your hardware supports it, process multiple video clips in parallel. This can be done by creating separate threads or processes for each video clip.
# Uploading: Upload multiple clips simultaneously using asynchronous requests or multi-threading.
# Batch Processing: Instead of processing each video clip individually, see if it's possible to batch-process multiple clips together in a single request, if the API supports it. This can significantly reduce the number of API calls and waiting time.
# Reducing Video Quality
# Direct Cloud Processing: If possible, perform the video splitting and processing directly in the cloud.
# Compress data where appropriate to reduce upload and download times
# Output qns as soon as it loads in instead of loading all at once after all pdfs/clips has been processed
# video maxumum length is 2 minutes
# maximum number of pages in a pdf is 16
# extract_frames('set.mp4', 5) # Extract frames from downloaded videso | DEEMED REDUNDANT
# get_transcript(video_url) #downloads transcipt from video | DEEMED REDUNDANT
# content = extract_text_from_pdf(pdf_path) #GPT-3.5 API Call | DEEMED REDUNDANT
# def extract_frames(video_path, interval): | DEEMED REDUNDANT
# cap = cv2.VideoCapture(video_path)
# fps = cap.get(cv2.CAP_PROP_FPS)
# frame_interval = int(fps * interval)
# frame_count = 0
# saved_frame_count = 0
# while True:
# ret, frame = cap.read()
# if not ret:
# break
# if frame_count % frame_interval == 0:
# filename = f'frame_{saved_frame_count}.jpg'
# cv2.imwrite(filename, frame)
# saved_frame_count += 1
# frame_count += 1
# cap.release()
# def extract_text_from_pdf(pdf_path):
# text = ''
# with pdfplumber.open(pdf_path) as pdf:
# for page in pdf.pages:
# text += page.extract_text()
# return text
#Things to add to MongoDB
# Clips array containing names of clips for each user id
# PDFs array containing names of pages for each user id