Neurolingua
commited on
Update student_functions.py
Browse files- student_functions.py +373 -361
student_functions.py
CHANGED
@@ -1,362 +1,374 @@
|
|
1 |
-
from gtts import gTTS
|
2 |
-
import shutil
|
3 |
-
from selenium import webdriver
|
4 |
-
from selenium.webdriver.common.by import By
|
5 |
-
from selenium.webdriver.common.keys import Keys
|
6 |
-
from selenium.webdriver.support.ui import WebDriverWait
|
7 |
-
from selenium.webdriver.support import expected_conditions as EC
|
8 |
-
import easyocr
|
9 |
-
import json
|
10 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
11 |
-
from youtube_transcript_api.formatters import JSONFormatter
|
12 |
-
from urllib.parse import urlparse, parse_qs
|
13 |
-
from pypdf import PdfReader
|
14 |
-
from ai71 import AI71
|
15 |
-
import os
|
16 |
-
|
17 |
-
AI71_API_KEY = "api71-api-df260d58-62e0-46c9-b549-62daa9c409be"
|
18 |
-
|
19 |
-
|
20 |
-
def extract_text_from_pdf_s(pdf_path):
|
21 |
-
text = ""
|
22 |
-
reader = PdfReader(pdf_path)
|
23 |
-
for page in reader.pages:
|
24 |
-
text += page.extract_text() + "\n"
|
25 |
-
generate_speech_from_pdf(text[:len(text) // 2])
|
26 |
-
return text
|
27 |
-
|
28 |
-
|
29 |
-
def generate_response_from_pdf(query, pdf_text):
|
30 |
-
response = ''
|
31 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
32 |
-
model="tiiuae/falcon-180b-chat",
|
33 |
-
messages=[
|
34 |
-
{"role": "system", "content": "You are a pdf questioning assistant."},
|
35 |
-
{"role": "user",
|
36 |
-
"content": f'''Answer the querry based on the given content.Content:{pdf_text},query:{query}'''},
|
37 |
-
],
|
38 |
-
stream=True,
|
39 |
-
):
|
40 |
-
if chunk.choices[0].delta.content:
|
41 |
-
response += chunk.choices[0].delta.content
|
42 |
-
return response[:-6].replace("###", '')
|
43 |
-
|
44 |
-
|
45 |
-
def generate_quiz(subject, topic, count, difficult):
|
46 |
-
quiz_output = ""
|
47 |
-
|
48 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
49 |
-
model="tiiuae/falcon-180b-chat",
|
50 |
-
messages=[
|
51 |
-
{"role": "system", "content": "You are a teaching assistant."},
|
52 |
-
{"role": "user",
|
53 |
-
"content": f'''Generate {count} multiple-choice questions in the subject of {subject} for the topic {topic} for students at a {difficult} level. Ensure the questions are well-diversified and cover various aspects of the topic. Format the questions as follows:
|
54 |
-
Question: [Question text] [specific concept in a question]
|
55 |
-
<<o>> [Option1]
|
56 |
-
<<o>> [Option2]
|
57 |
-
<<o>> [Option3]
|
58 |
-
<<o>> [Option4],
|
59 |
-
Answer: [Option number]'''},
|
60 |
-
],
|
61 |
-
stream=True,
|
62 |
-
):
|
63 |
-
if chunk.choices[0].delta.content:
|
64 |
-
quiz_output += chunk.choices[0].delta.content
|
65 |
-
print("Quiz generated")
|
66 |
-
return quiz_output
|
67 |
-
|
68 |
-
|
69 |
-
def perform_ocr(image_path):
|
70 |
-
reader = easyocr.Reader(['en'])
|
71 |
-
try:
|
72 |
-
result = reader.readtext(image_path)
|
73 |
-
extracted_text = ''
|
74 |
-
for (bbox, text, prob) in result:
|
75 |
-
extracted_text += text + ' '
|
76 |
-
return extracted_text.strip()
|
77 |
-
except Exception as e:
|
78 |
-
print(f"Error during OCR: {e}")
|
79 |
-
return ''
|
80 |
-
|
81 |
-
|
82 |
-
def generate_ai_response(query):
|
83 |
-
ai_response = ''
|
84 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
85 |
-
model="tiiuae/falcon-180b-chat",
|
86 |
-
messages=[
|
87 |
-
{"role": "system", "content": "You are a teaching assistant."},
|
88 |
-
{"role": "user", "content": f'Assist the user clearly for his questions: {query}.'},
|
89 |
-
],
|
90 |
-
stream=True,
|
91 |
-
):
|
92 |
-
if chunk.choices[0].delta.content:
|
93 |
-
ai_response += chunk.choices[0].delta.content
|
94 |
-
return ai_response.replace('###', '')[:-6]
|
95 |
-
|
96 |
-
|
97 |
-
def generate_project_idea(subject, topic, overview):
|
98 |
-
string = ''
|
99 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
100 |
-
model="tiiuae/falcon-180b-chat",
|
101 |
-
messages=[
|
102 |
-
{"role": "system", "content": "You are a project building assistant."},
|
103 |
-
{"role": "user",
|
104 |
-
"content": f'''Give the different project ideas to build project in {subject} specifically in {topic} for school students. {overview}.'''},
|
105 |
-
],
|
106 |
-
stream=True,
|
107 |
-
):
|
108 |
-
if chunk.choices[0].delta.content:
|
109 |
-
string += chunk.choices[0].delta.content
|
110 |
-
return string
|
111 |
-
|
112 |
-
|
113 |
-
def generate_project_idea_questions(project_idea, query):
|
114 |
-
project_idea_answer = ''
|
115 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
116 |
-
model="tiiuae/falcon-180b-chat",
|
117 |
-
messages=[
|
118 |
-
{"role": "system", "content": "You are a project building assistant."},
|
119 |
-
{"role": "user",
|
120 |
-
"content": f'''Assist me clearly for the following question for the given idea. Idea: {project_idea}. Question: {query}'''},
|
121 |
-
],
|
122 |
-
stream=True,
|
123 |
-
):
|
124 |
-
if chunk.choices[0].delta.content:
|
125 |
-
project_idea_answer += chunk.choices[0].delta.content
|
126 |
-
return project_idea_answer
|
127 |
-
|
128 |
-
|
129 |
-
def generate_step_by_step_explanation(query):
|
130 |
-
explanation = ''
|
131 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
132 |
-
model="tiiuae/falcon-180b-chat",
|
133 |
-
messages=[
|
134 |
-
{"role": "system", "content": "You are the best teaching assistant."},
|
135 |
-
{"role": "user",
|
136 |
-
"content": f'''Provide me the clear step by step explanation answer for the following question. Question: {query}'''},
|
137 |
-
],
|
138 |
-
stream=True,
|
139 |
-
):
|
140 |
-
if chunk.choices[0].delta.content:
|
141 |
-
explanation += chunk.choices[0].delta.content
|
142 |
-
return explanation.replace('###', '')
|
143 |
-
|
144 |
-
|
145 |
-
def study_plan(subjects, hours, arealag, goal):
|
146 |
-
plan = ''
|
147 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
148 |
-
model="tiiuae/falcon-180b-chat",
|
149 |
-
messages=[
|
150 |
-
{"role": "system", "content": "You are the best teaching assistant."},
|
151 |
-
{"role": "user",
|
152 |
-
"content": f'''Provide me the clear personalised study plan for the subjects {subjects} i lag in areas like {arealag}, im available for {hours} hours per day and my study goal is to {goal}.Provide me like a timetable like day1,day2 for 5 days with concepts,also suggest some books'''},
|
153 |
-
],
|
154 |
-
stream=True,
|
155 |
-
):
|
156 |
-
if chunk.choices[0].delta.content:
|
157 |
-
plan += chunk.choices[0].delta.content
|
158 |
-
return plan.replace('\n', '<br>')
|
159 |
-
|
160 |
-
|
161 |
-
class ConversationBufferMemory:
|
162 |
-
def __init__(self, memory_key="chat_history"):
|
163 |
-
self.memory_key = memory_key
|
164 |
-
self.buffer = []
|
165 |
-
|
166 |
-
def add_to_memory(self, interaction):
|
167 |
-
self.buffer.append(interaction)
|
168 |
-
|
169 |
-
def get_memory(self):
|
170 |
-
return "\n".join([f"Human: {entry['user']}\nAssistant: {entry['assistant']}" for entry in self.buffer])
|
171 |
-
|
172 |
-
|
173 |
-
def spk_msg(user_input, memory):
|
174 |
-
chat_history = memory.get_memory()
|
175 |
-
msg = ''
|
176 |
-
|
177 |
-
# Construct the message for the API request
|
178 |
-
messages = [
|
179 |
-
{"role": "system",
|
180 |
-
"content": "You are a nice speaker having a conversation with a human.You ask the question the user choose the topic and let user answer.Provide the response only within 2 sentence"},
|
181 |
-
{"role": "user",
|
182 |
-
"content": f"Previous conversation:\n{chat_history}\n\nNew human question: {user_input}\nResponse:"}
|
183 |
-
]
|
184 |
-
|
185 |
-
try:
|
186 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
187 |
-
model="tiiuae/falcon-180b-chat",
|
188 |
-
messages=messages,
|
189 |
-
stream=True,
|
190 |
-
):
|
191 |
-
if chunk.choices[0].delta.content:
|
192 |
-
msg += chunk.choices[0].delta.content
|
193 |
-
except Exception as e:
|
194 |
-
print(f"An error occurred: {e}")
|
195 |
-
|
196 |
-
return msg
|
197 |
-
|
198 |
-
|
199 |
-
def get_first_youtube_video_link(query):
|
200 |
-
chrome_options = webdriver.ChromeOptions()
|
201 |
-
chrome_options.add_argument("--headless")
|
202 |
-
chrome_options.add_argument("--disable-gpu")
|
203 |
-
chrome_options.add_argument("--disable-extensions")
|
204 |
-
chrome_options.add_argument("--no-sandbox")
|
205 |
-
chrome_options.add_argument("--disable-dev-shm-usage")
|
206 |
-
driver = webdriver.Chrome(options=chrome_options)
|
207 |
-
try:
|
208 |
-
driver.get('https://www.youtube.com')
|
209 |
-
search_box = driver.find_element(By.NAME, 'search_query')
|
210 |
-
search_box.send_keys(query)
|
211 |
-
search_box.send_keys(Keys.RETURN)
|
212 |
-
WebDriverWait(driver, 10).until(
|
213 |
-
EC.presence_of_element_located((By.CSS_SELECTOR, 'a#video-title')))
|
214 |
-
first_video = driver.find_element(By.CSS_SELECTOR, 'a#video-title')
|
215 |
-
first_video_link = first_video.get_attribute('href')
|
216 |
-
video_title = first_video.get_attribute('title')
|
217 |
-
return first_video_link, video_title
|
218 |
-
finally:
|
219 |
-
driver.quit()
|
220 |
-
return
|
221 |
-
|
222 |
-
|
223 |
-
def content_translate(text):
|
224 |
-
translated_content = ''
|
225 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
226 |
-
model="tiiuae/falcon-180b-chat",
|
227 |
-
messages=[
|
228 |
-
{"role": "system", "content": "You are the best teaching assistant."},
|
229 |
-
{"role": "user", "content": f'''Translate the text to hindi. Text: {text}'''},
|
230 |
-
],
|
231 |
-
stream=True,
|
232 |
-
):
|
233 |
-
if chunk.choices[0].delta.content:
|
234 |
-
translated_content += chunk.choices[0].delta.content
|
235 |
-
return translated_content
|
236 |
-
|
237 |
-
|
238 |
-
def get_video_id(url):
|
239 |
-
"""
|
240 |
-
Extract the video ID from a YouTube URL.
|
241 |
-
"""
|
242 |
-
parsed_url = urlparse(url)
|
243 |
-
if parsed_url.hostname == 'www.youtube.com' or parsed_url.hostname == 'youtube.com':
|
244 |
-
video_id = parse_qs(parsed_url.query).get('v')
|
245 |
-
if video_id:
|
246 |
-
return video_id[0]
|
247 |
-
elif parsed_url.hostname == 'youtu.be':
|
248 |
-
return parsed_url.path[1:]
|
249 |
-
return None
|
250 |
-
|
251 |
-
|
252 |
-
def extract_captions(video_url):
|
253 |
-
"""
|
254 |
-
Extract captions from a YouTube video URL.
|
255 |
-
"""
|
256 |
-
video_id = get_video_id(video_url)
|
257 |
-
if not video_id:
|
258 |
-
print("Invalid YouTube URL.")
|
259 |
-
return
|
260 |
-
|
261 |
-
try:
|
262 |
-
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
263 |
-
formatter = JSONFormatter()
|
264 |
-
formatted_transcript = formatter.format_transcript(transcript)
|
265 |
-
|
266 |
-
# Save captions to a file
|
267 |
-
with open(f'youtube_captions.json', 'w') as file:
|
268 |
-
file.write(formatted_transcript)
|
269 |
-
|
270 |
-
print("Captions have been extracted and saved as JSON.")
|
271 |
-
|
272 |
-
except Exception as e:
|
273 |
-
print(f"An error occurred: {e}")
|
274 |
-
|
275 |
-
|
276 |
-
def extract_text_from_json(filename):
|
277 |
-
# Open and read the JSON file
|
278 |
-
with open(filename, 'r') as file:
|
279 |
-
data = json.load(file)
|
280 |
-
|
281 |
-
# Extract and print the text fields
|
282 |
-
texts = [entry['text'] for entry in data]
|
283 |
-
return texts
|
284 |
-
|
285 |
-
|
286 |
-
def get_simplified_explanation(text):
|
287 |
-
prompt = (
|
288 |
-
f"The following is a transcript of a video: \n\n{text}\n\n"
|
289 |
-
"Please provide a simplified explanation of the video for easy understanding."
|
290 |
-
)
|
291 |
-
|
292 |
-
response = ""
|
293 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
294 |
-
model="tiiuae/falcon-180b-chat",
|
295 |
-
messages=[
|
296 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
297 |
-
{"role": "user", "content": prompt},
|
298 |
-
],
|
299 |
-
stream=True,
|
300 |
-
):
|
301 |
-
if chunk.choices[0].delta.content:
|
302 |
-
response += chunk.choices[0].delta.content
|
303 |
-
|
304 |
-
return response
|
305 |
-
|
306 |
-
|
307 |
-
def summarise_text(url):
|
308 |
-
extract_captions(url)
|
309 |
-
texts = extract_text_from_json(r'youtube_captions.json')
|
310 |
-
os.remove('youtube_captions.json')
|
311 |
-
|
312 |
-
first_half = (get_simplified_explanation(texts[:len(texts) // 2]))[:-6]
|
313 |
-
second_half = (get_simplified_explanation(texts[len(texts) // 2:]))[:-6]
|
314 |
-
return (first_half + second_half)
|
315 |
-
|
316 |
-
|
317 |
-
def generate_speech_from_pdf(content):
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
speech =
|
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 |
return translated_content
|
|
|
1 |
+
from gtts import gTTS
|
2 |
+
import shutil
|
3 |
+
from selenium import webdriver
|
4 |
+
from selenium.webdriver.common.by import By
|
5 |
+
from selenium.webdriver.common.keys import Keys
|
6 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
7 |
+
from selenium.webdriver.support import expected_conditions as EC
|
8 |
+
import easyocr
|
9 |
+
import json
|
10 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
11 |
+
from youtube_transcript_api.formatters import JSONFormatter
|
12 |
+
from urllib.parse import urlparse, parse_qs
|
13 |
+
from pypdf import PdfReader
|
14 |
+
from ai71 import AI71
|
15 |
+
import os
|
16 |
+
|
17 |
+
AI71_API_KEY = "api71-api-df260d58-62e0-46c9-b549-62daa9c409be"
|
18 |
+
|
19 |
+
|
20 |
+
def extract_text_from_pdf_s(pdf_path):
|
21 |
+
text = ""
|
22 |
+
reader = PdfReader(pdf_path)
|
23 |
+
for page in reader.pages:
|
24 |
+
text += page.extract_text() + "\n"
|
25 |
+
generate_speech_from_pdf(text[:len(text) // 2])
|
26 |
+
return text
|
27 |
+
|
28 |
+
|
29 |
+
def generate_response_from_pdf(query, pdf_text):
|
30 |
+
response = ''
|
31 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
32 |
+
model="tiiuae/falcon-180b-chat",
|
33 |
+
messages=[
|
34 |
+
{"role": "system", "content": "You are a pdf questioning assistant."},
|
35 |
+
{"role": "user",
|
36 |
+
"content": f'''Answer the querry based on the given content.Content:{pdf_text},query:{query}'''},
|
37 |
+
],
|
38 |
+
stream=True,
|
39 |
+
):
|
40 |
+
if chunk.choices[0].delta.content:
|
41 |
+
response += chunk.choices[0].delta.content
|
42 |
+
return response[:-6].replace("###", '')
|
43 |
+
|
44 |
+
|
45 |
+
def generate_quiz(subject, topic, count, difficult):
|
46 |
+
quiz_output = ""
|
47 |
+
|
48 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
49 |
+
model="tiiuae/falcon-180b-chat",
|
50 |
+
messages=[
|
51 |
+
{"role": "system", "content": "You are a teaching assistant."},
|
52 |
+
{"role": "user",
|
53 |
+
"content": f'''Generate {count} multiple-choice questions in the subject of {subject} for the topic {topic} for students at a {difficult} level. Ensure the questions are well-diversified and cover various aspects of the topic. Format the questions as follows:
|
54 |
+
Question: [Question text] [specific concept in a question]
|
55 |
+
<<o>> [Option1]
|
56 |
+
<<o>> [Option2]
|
57 |
+
<<o>> [Option3]
|
58 |
+
<<o>> [Option4],
|
59 |
+
Answer: [Option number]'''},
|
60 |
+
],
|
61 |
+
stream=True,
|
62 |
+
):
|
63 |
+
if chunk.choices[0].delta.content:
|
64 |
+
quiz_output += chunk.choices[0].delta.content
|
65 |
+
print("Quiz generated")
|
66 |
+
return quiz_output
|
67 |
+
|
68 |
+
|
69 |
+
def perform_ocr(image_path):
|
70 |
+
reader = easyocr.Reader(['en'])
|
71 |
+
try:
|
72 |
+
result = reader.readtext(image_path)
|
73 |
+
extracted_text = ''
|
74 |
+
for (bbox, text, prob) in result:
|
75 |
+
extracted_text += text + ' '
|
76 |
+
return extracted_text.strip()
|
77 |
+
except Exception as e:
|
78 |
+
print(f"Error during OCR: {e}")
|
79 |
+
return ''
|
80 |
+
|
81 |
+
|
82 |
+
def generate_ai_response(query):
|
83 |
+
ai_response = ''
|
84 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
85 |
+
model="tiiuae/falcon-180b-chat",
|
86 |
+
messages=[
|
87 |
+
{"role": "system", "content": "You are a teaching assistant."},
|
88 |
+
{"role": "user", "content": f'Assist the user clearly for his questions: {query}.'},
|
89 |
+
],
|
90 |
+
stream=True,
|
91 |
+
):
|
92 |
+
if chunk.choices[0].delta.content:
|
93 |
+
ai_response += chunk.choices[0].delta.content
|
94 |
+
return ai_response.replace('###', '')[:-6]
|
95 |
+
|
96 |
+
|
97 |
+
def generate_project_idea(subject, topic, overview):
|
98 |
+
string = ''
|
99 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
100 |
+
model="tiiuae/falcon-180b-chat",
|
101 |
+
messages=[
|
102 |
+
{"role": "system", "content": "You are a project building assistant."},
|
103 |
+
{"role": "user",
|
104 |
+
"content": f'''Give the different project ideas to build project in {subject} specifically in {topic} for school students. {overview}.'''},
|
105 |
+
],
|
106 |
+
stream=True,
|
107 |
+
):
|
108 |
+
if chunk.choices[0].delta.content:
|
109 |
+
string += chunk.choices[0].delta.content
|
110 |
+
return string
|
111 |
+
|
112 |
+
|
113 |
+
def generate_project_idea_questions(project_idea, query):
|
114 |
+
project_idea_answer = ''
|
115 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
116 |
+
model="tiiuae/falcon-180b-chat",
|
117 |
+
messages=[
|
118 |
+
{"role": "system", "content": "You are a project building assistant."},
|
119 |
+
{"role": "user",
|
120 |
+
"content": f'''Assist me clearly for the following question for the given idea. Idea: {project_idea}. Question: {query}'''},
|
121 |
+
],
|
122 |
+
stream=True,
|
123 |
+
):
|
124 |
+
if chunk.choices[0].delta.content:
|
125 |
+
project_idea_answer += chunk.choices[0].delta.content
|
126 |
+
return project_idea_answer
|
127 |
+
|
128 |
+
|
129 |
+
def generate_step_by_step_explanation(query):
|
130 |
+
explanation = ''
|
131 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
132 |
+
model="tiiuae/falcon-180b-chat",
|
133 |
+
messages=[
|
134 |
+
{"role": "system", "content": "You are the best teaching assistant."},
|
135 |
+
{"role": "user",
|
136 |
+
"content": f'''Provide me the clear step by step explanation answer for the following question. Question: {query}'''},
|
137 |
+
],
|
138 |
+
stream=True,
|
139 |
+
):
|
140 |
+
if chunk.choices[0].delta.content:
|
141 |
+
explanation += chunk.choices[0].delta.content
|
142 |
+
return explanation.replace('###', '')
|
143 |
+
|
144 |
+
|
145 |
+
def study_plan(subjects, hours, arealag, goal):
|
146 |
+
plan = ''
|
147 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
148 |
+
model="tiiuae/falcon-180b-chat",
|
149 |
+
messages=[
|
150 |
+
{"role": "system", "content": "You are the best teaching assistant."},
|
151 |
+
{"role": "user",
|
152 |
+
"content": f'''Provide me the clear personalised study plan for the subjects {subjects} i lag in areas like {arealag}, im available for {hours} hours per day and my study goal is to {goal}.Provide me like a timetable like day1,day2 for 5 days with concepts,also suggest some books'''},
|
153 |
+
],
|
154 |
+
stream=True,
|
155 |
+
):
|
156 |
+
if chunk.choices[0].delta.content:
|
157 |
+
plan += chunk.choices[0].delta.content
|
158 |
+
return plan.replace('\n', '<br>')
|
159 |
+
|
160 |
+
|
161 |
+
class ConversationBufferMemory:
|
162 |
+
def __init__(self, memory_key="chat_history"):
|
163 |
+
self.memory_key = memory_key
|
164 |
+
self.buffer = []
|
165 |
+
|
166 |
+
def add_to_memory(self, interaction):
|
167 |
+
self.buffer.append(interaction)
|
168 |
+
|
169 |
+
def get_memory(self):
|
170 |
+
return "\n".join([f"Human: {entry['user']}\nAssistant: {entry['assistant']}" for entry in self.buffer])
|
171 |
+
|
172 |
+
|
173 |
+
def spk_msg(user_input, memory):
|
174 |
+
chat_history = memory.get_memory()
|
175 |
+
msg = ''
|
176 |
+
|
177 |
+
# Construct the message for the API request
|
178 |
+
messages = [
|
179 |
+
{"role": "system",
|
180 |
+
"content": "You are a nice speaker having a conversation with a human.You ask the question the user choose the topic and let user answer.Provide the response only within 2 sentence"},
|
181 |
+
{"role": "user",
|
182 |
+
"content": f"Previous conversation:\n{chat_history}\n\nNew human question: {user_input}\nResponse:"}
|
183 |
+
]
|
184 |
+
|
185 |
+
try:
|
186 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
187 |
+
model="tiiuae/falcon-180b-chat",
|
188 |
+
messages=messages,
|
189 |
+
stream=True,
|
190 |
+
):
|
191 |
+
if chunk.choices[0].delta.content:
|
192 |
+
msg += chunk.choices[0].delta.content
|
193 |
+
except Exception as e:
|
194 |
+
print(f"An error occurred: {e}")
|
195 |
+
|
196 |
+
return msg
|
197 |
+
|
198 |
+
|
199 |
+
def get_first_youtube_video_link(query):
|
200 |
+
chrome_options = webdriver.ChromeOptions()
|
201 |
+
chrome_options.add_argument("--headless")
|
202 |
+
chrome_options.add_argument("--disable-gpu")
|
203 |
+
chrome_options.add_argument("--disable-extensions")
|
204 |
+
chrome_options.add_argument("--no-sandbox")
|
205 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
206 |
+
driver = webdriver.Chrome(options=chrome_options)
|
207 |
+
try:
|
208 |
+
driver.get('https://www.youtube.com')
|
209 |
+
search_box = driver.find_element(By.NAME, 'search_query')
|
210 |
+
search_box.send_keys(query)
|
211 |
+
search_box.send_keys(Keys.RETURN)
|
212 |
+
WebDriverWait(driver, 10).until(
|
213 |
+
EC.presence_of_element_located((By.CSS_SELECTOR, 'a#video-title')))
|
214 |
+
first_video = driver.find_element(By.CSS_SELECTOR, 'a#video-title')
|
215 |
+
first_video_link = first_video.get_attribute('href')
|
216 |
+
video_title = first_video.get_attribute('title')
|
217 |
+
return first_video_link, video_title
|
218 |
+
finally:
|
219 |
+
driver.quit()
|
220 |
+
return
|
221 |
+
|
222 |
+
|
223 |
+
def content_translate(text):
|
224 |
+
translated_content = ''
|
225 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
226 |
+
model="tiiuae/falcon-180b-chat",
|
227 |
+
messages=[
|
228 |
+
{"role": "system", "content": "You are the best teaching assistant."},
|
229 |
+
{"role": "user", "content": f'''Translate the text to hindi. Text: {text}'''},
|
230 |
+
],
|
231 |
+
stream=True,
|
232 |
+
):
|
233 |
+
if chunk.choices[0].delta.content:
|
234 |
+
translated_content += chunk.choices[0].delta.content
|
235 |
+
return translated_content
|
236 |
+
|
237 |
+
|
238 |
+
def get_video_id(url):
|
239 |
+
"""
|
240 |
+
Extract the video ID from a YouTube URL.
|
241 |
+
"""
|
242 |
+
parsed_url = urlparse(url)
|
243 |
+
if parsed_url.hostname == 'www.youtube.com' or parsed_url.hostname == 'youtube.com':
|
244 |
+
video_id = parse_qs(parsed_url.query).get('v')
|
245 |
+
if video_id:
|
246 |
+
return video_id[0]
|
247 |
+
elif parsed_url.hostname == 'youtu.be':
|
248 |
+
return parsed_url.path[1:]
|
249 |
+
return None
|
250 |
+
|
251 |
+
|
252 |
+
def extract_captions(video_url):
|
253 |
+
"""
|
254 |
+
Extract captions from a YouTube video URL.
|
255 |
+
"""
|
256 |
+
video_id = get_video_id(video_url)
|
257 |
+
if not video_id:
|
258 |
+
print("Invalid YouTube URL.")
|
259 |
+
return
|
260 |
+
|
261 |
+
try:
|
262 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
263 |
+
formatter = JSONFormatter()
|
264 |
+
formatted_transcript = formatter.format_transcript(transcript)
|
265 |
+
|
266 |
+
# Save captions to a file
|
267 |
+
with open(f'youtube_captions.json', 'w') as file:
|
268 |
+
file.write(formatted_transcript)
|
269 |
+
|
270 |
+
print("Captions have been extracted and saved as JSON.")
|
271 |
+
|
272 |
+
except Exception as e:
|
273 |
+
print(f"An error occurred: {e}")
|
274 |
+
|
275 |
+
|
276 |
+
def extract_text_from_json(filename):
|
277 |
+
# Open and read the JSON file
|
278 |
+
with open(filename, 'r') as file:
|
279 |
+
data = json.load(file)
|
280 |
+
|
281 |
+
# Extract and print the text fields
|
282 |
+
texts = [entry['text'] for entry in data]
|
283 |
+
return texts
|
284 |
+
|
285 |
+
|
286 |
+
def get_simplified_explanation(text):
|
287 |
+
prompt = (
|
288 |
+
f"The following is a transcript of a video: \n\n{text}\n\n"
|
289 |
+
"Please provide a simplified explanation of the video for easy understanding."
|
290 |
+
)
|
291 |
+
|
292 |
+
response = ""
|
293 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
294 |
+
model="tiiuae/falcon-180b-chat",
|
295 |
+
messages=[
|
296 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
297 |
+
{"role": "user", "content": prompt},
|
298 |
+
],
|
299 |
+
stream=True,
|
300 |
+
):
|
301 |
+
if chunk.choices[0].delta.content:
|
302 |
+
response += chunk.choices[0].delta.content
|
303 |
+
|
304 |
+
return response
|
305 |
+
|
306 |
+
|
307 |
+
def summarise_text(url):
|
308 |
+
extract_captions(url)
|
309 |
+
texts = extract_text_from_json(r'youtube_captions.json')
|
310 |
+
os.remove('youtube_captions.json')
|
311 |
+
|
312 |
+
first_half = (get_simplified_explanation(texts[:len(texts) // 2]))[:-6]
|
313 |
+
second_half = (get_simplified_explanation(texts[len(texts) // 2:]))[:-6]
|
314 |
+
return (first_half + second_half)
|
315 |
+
|
316 |
+
|
317 |
+
def generate_speech_from_pdf(content):
|
318 |
+
directory = 'speech'
|
319 |
+
keep_file = 'nil.txt'
|
320 |
+
|
321 |
+
# Check if the directory exists
|
322 |
+
if os.path.isdir(directory):
|
323 |
+
for filename in os.listdir(directory):
|
324 |
+
file_path = os.path.join(directory, filename)
|
325 |
+
|
326 |
+
# Check if the current file is not the one to keep and is a file
|
327 |
+
if filename != keep_file and os.path.isfile(file_path):
|
328 |
+
try:
|
329 |
+
os.remove(file_path) # Delete the file
|
330 |
+
print(f"Deleted {file_path}")
|
331 |
+
except Exception as e:
|
332 |
+
print(f"Error deleting {file_path}: {e}")
|
333 |
+
else:
|
334 |
+
print(f"Directory {directory} does not exist.")
|
335 |
+
|
336 |
+
speech = ''
|
337 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
338 |
+
model="tiiuae/falcon-180b-chat",
|
339 |
+
messages=[
|
340 |
+
{"role": "system", "content": "You are a summarising assistant."},
|
341 |
+
{"role": "user",
|
342 |
+
"content": f'''Summarise the given content for each chapter for 1 sentence.Content={content}'''},
|
343 |
+
],
|
344 |
+
stream=True,
|
345 |
+
):
|
346 |
+
if chunk.choices[0].delta.content:
|
347 |
+
speech += chunk.choices[0].delta.content
|
348 |
+
speech = speech[:-6].replace("###", '')
|
349 |
+
chapters = speech.split('\n\n')
|
350 |
+
pdf_audio(chapters[:4])
|
351 |
+
return
|
352 |
+
|
353 |
+
|
354 |
+
def pdf_audio(chapters):
|
355 |
+
for i in range(len(chapters)):
|
356 |
+
tts = gTTS(text=chapters[i], lang='en', slow=False)
|
357 |
+
tts.save(f'speech/chapter {i + 1}.mp3')
|
358 |
+
return
|
359 |
+
|
360 |
+
def content_translate(text):
|
361 |
+
|
362 |
+
|
363 |
+
translated_content = ''
|
364 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
365 |
+
model="tiiuae/falcon-180b-chat",
|
366 |
+
messages=[
|
367 |
+
{"role": "system", "content": "You are the best teaching assistant."},
|
368 |
+
{"role": "user", "content": f'''Translate the text to hindi. Text: {text}'''},
|
369 |
+
],
|
370 |
+
stream=True,
|
371 |
+
):
|
372 |
+
if chunk.choices[0].delta.content:
|
373 |
+
translated_content += chunk.choices[0].delta.content
|
374 |
return translated_content
|