roshnn24 commited on
Commit
52169fe
·
verified ·
1 Parent(s): e5716f8

Upload 3 files

Browse files
Files changed (3) hide show
  1. requirements.txt +19 -0
  2. student_functions.py +356 -0
  3. teacher_function.py +168 -0
requirements.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Flask==3.0.2
2
+ Werkzeug==3.0.1
3
+ selenium==4.18.1
4
+ torch==1.10.0
5
+ torchvision==0.11.1
6
+ easyocr==1.4
7
+ youtube-transcript-api==0.6.2
8
+ pypdf==4.1.0
9
+ Pillow
10
+ PyPDF2==3.0.1
11
+ pdf2image==1.17.0
12
+ opencv-python-headless==4.8.1.78
13
+ numpy==1.26.3
14
+ pytesseract==0.3.10
15
+ joblib==1.3.2
16
+ requests==2.32.0
17
+ ai71==0.0.18
18
+ setuptools==69.0.3
19
+ gtts
student_functions.py ADDED
@@ -0,0 +1,356 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 json
9
+ from youtube_transcript_api import YouTubeTranscriptApi
10
+ from youtube_transcript_api.formatters import JSONFormatter
11
+ from urllib.parse import urlparse, parse_qs
12
+ from pypdf import PdfReader
13
+ from ai71 import AI71
14
+ import os
15
+
16
+ AI71_API_KEY = "api71-api-652e5c6c-8edf-41d0-9c34-28522b07bef9"
17
+
18
+
19
+ def extract_text_from_pdf_s(pdf_path):
20
+ text = ""
21
+ reader = PdfReader(pdf_path)
22
+ for page in reader.pages:
23
+ text += page.extract_text() + "\n"
24
+ generate_speech_from_pdf(text[:len(text) // 2])
25
+
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.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
+
70
+ def generate_ai_response(query):
71
+ ai_response = ''
72
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
73
+ model="tiiuae/falcon-180b-chat",
74
+ messages=[
75
+ {"role": "system", "content": "You are a teaching assistant."},
76
+ {"role": "user", "content": f'Assist the user clearly for his questions: {query}.'},
77
+ ],
78
+ stream=True,
79
+ ):
80
+ if chunk.choices[0].delta.content:
81
+ ai_response += chunk.choices[0].delta.content
82
+ return ai_response.replace('###', '')
83
+
84
+
85
+ def generate_project_idea(subject, topic, overview):
86
+ string = ''
87
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
88
+ model="tiiuae/falcon-180b-chat",
89
+ messages=[
90
+ {"role": "system", "content": "You are a project building assistant."},
91
+ {"role": "user",
92
+ "content": f'''Give the different project ideas to build project in {subject} specifically in {topic} for school students. {overview}.'''},
93
+ ],
94
+ stream=True,
95
+ ):
96
+ if chunk.choices[0].delta.content:
97
+ string += chunk.choices[0].delta.content
98
+ return string
99
+
100
+
101
+ def generate_project_idea_questions(project_idea, query):
102
+ project_idea_answer = ''
103
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
104
+ model="tiiuae/falcon-180b-chat",
105
+ messages=[
106
+ {"role": "system", "content": "You are a project building assistant."},
107
+ {"role": "user",
108
+ "content": f'''Assist me clearly for the following question for the given idea. Idea: {project_idea}. Question: {query}'''},
109
+ ],
110
+ stream=True,
111
+ ):
112
+ if chunk.choices[0].delta.content:
113
+ project_idea_answer += chunk.choices[0].delta.content
114
+ return project_idea_answer
115
+
116
+
117
+ def generate_step_by_step_explanation(query):
118
+ explanation = ''
119
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
120
+ model="tiiuae/falcon-180b-chat",
121
+ messages=[
122
+ {"role": "system", "content": "You are the best teaching assistant."},
123
+ {"role": "user",
124
+ "content": f'''Provide me the clear step by step explanation answer for the following question. Question: {query}'''},
125
+ ],
126
+ stream=True,
127
+ ):
128
+ if chunk.choices[0].delta.content:
129
+ explanation += chunk.choices[0].delta.content
130
+ return explanation.replace('###', '')
131
+
132
+
133
+ def study_plan(subjects, hours, arealag, goal):
134
+ plan = ''
135
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
136
+ model="tiiuae/falcon-180b-chat",
137
+ messages=[
138
+ {"role": "system", "content": "You are the best teaching assistant."},
139
+ {"role": "user",
140
+ "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'''},
141
+ ],
142
+ stream=True,
143
+ ):
144
+ if chunk.choices[0].delta.content:
145
+ plan += chunk.choices[0].delta.content
146
+ return plan.replace('\n', '<br>')
147
+
148
+
149
+ class ConversationBufferMemory:
150
+ def __init__(self, memory_key="chat_history"):
151
+ self.memory_key = memory_key
152
+ self.buffer = []
153
+
154
+ def add_to_memory(self, interaction):
155
+ self.buffer.append(interaction)
156
+
157
+ def get_memory(self):
158
+ return "\n".join([f"Human: {entry['user']}\nAssistant: {entry['assistant']}" for entry in self.buffer])
159
+
160
+
161
+ def spk_msg(user_input, memory):
162
+ chat_history = memory.get_memory()
163
+ msg = ''
164
+
165
+ # Construct the message for the API request
166
+ messages = [
167
+ {"role": "system",
168
+ "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"},
169
+ {"role": "user",
170
+ "content": f"Previous conversation:\n{chat_history}\n\nNew human question: {user_input}\nResponse:"}
171
+ ]
172
+
173
+ try:
174
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
175
+ model="tiiuae/falcon-180b-chat",
176
+ messages=messages,
177
+ stream=True,
178
+ ):
179
+ if chunk.choices[0].delta.content:
180
+ msg += chunk.choices[0].delta.content
181
+ except Exception as e:
182
+ print(f"An error occurred: {e}")
183
+
184
+ return msg
185
+
186
+
187
+ def get_first_youtube_video_link(query):
188
+ url = f'https://www.youtube.com/results?search_query={query}'
189
+
190
+ # Navigate to the URL
191
+ driver.get(url)
192
+
193
+ # Find the first video link
194
+ try:
195
+ # Find the first video element
196
+ first_video_element = driver.find_element(By.XPATH, '//a[@id="video-title"]')
197
+ video_link = first_video_element.get_attribute('href')
198
+ print(video_link)
199
+ finally:
200
+ # Close the WebDriver
201
+ driver.quit()
202
+
203
+
204
+
205
+ def content_translate(text):
206
+ translated_content = ''
207
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
208
+ model="tiiuae/falcon-180b-chat",
209
+ messages=[
210
+ {"role": "system", "content": "You are the best teaching assistant."},
211
+ {"role": "user", "content": f'''Translate the text to hindi. Text: {text}'''},
212
+ ],
213
+ stream=True,
214
+ ):
215
+ if chunk.choices[0].delta.content:
216
+ translated_content += chunk.choices[0].delta.content
217
+ return translated_content
218
+
219
+
220
+ def get_video_id(url):
221
+ """
222
+ Extract the video ID from a YouTube URL.
223
+ """
224
+ parsed_url = urlparse(url)
225
+ if parsed_url.hostname == 'www.youtube.com' or parsed_url.hostname == 'youtube.com':
226
+ video_id = parse_qs(parsed_url.query).get('v')
227
+ if video_id:
228
+ return video_id[0]
229
+ elif parsed_url.hostname == 'youtu.be':
230
+ return parsed_url.path[1:]
231
+ return None
232
+
233
+
234
+ def extract_captions(video_url):
235
+ """
236
+ Extract captions from a YouTube video URL.
237
+ """
238
+ video_id = get_video_id(video_url)
239
+ if not video_id:
240
+ print("Invalid YouTube URL.")
241
+ return
242
+
243
+ try:
244
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
245
+ formatter = JSONFormatter()
246
+ formatted_transcript = formatter.format_transcript(transcript)
247
+
248
+ # Save captions to a file
249
+ with open(f'youtube_captions.json', 'w') as file:
250
+ file.write(formatted_transcript)
251
+
252
+ print("Captions have been extracted and saved as JSON.")
253
+
254
+ except Exception as e:
255
+ print(f"An error occurred: {e}")
256
+
257
+
258
+ def extract_text_from_json(filename):
259
+ # Open and read the JSON file
260
+ with open(filename, 'r') as file:
261
+ data = json.load(file)
262
+
263
+ # Extract and print the text fields
264
+ texts = [entry['text'] for entry in data]
265
+ return texts
266
+
267
+
268
+ def get_simplified_explanation(text):
269
+ prompt = (
270
+ f"The following is a transcript of a video: \n\n{text}\n\n"
271
+ "Please provide a simplified explanation of the video for easy understanding."
272
+ )
273
+
274
+ response = ""
275
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
276
+ model="tiiuae/falcon-180b-chat",
277
+ messages=[
278
+ {"role": "system", "content": "You are a helpful assistant."},
279
+ {"role": "user", "content": prompt},
280
+ ],
281
+ stream=True,
282
+ ):
283
+ if chunk.choices[0].delta.content:
284
+ response += chunk.choices[0].delta.content
285
+
286
+ return response
287
+
288
+
289
+ def summarise_text(url):
290
+ extract_captions(url)
291
+ texts = extract_text_from_json(r'youtube_captions.json')
292
+ os.remove('youtube_captions.json')
293
+
294
+ first_half = (get_simplified_explanation(texts[:len(texts) // 2]))
295
+ second_half = (get_simplified_explanation(texts[len(texts) // 2:]))
296
+ return (first_half + second_half)
297
+
298
+
299
+ def generate_speech_from_pdf(content):
300
+ directory = 'speech'
301
+ keep_file = 'nil.txt'
302
+
303
+ # Check if the directory exists
304
+ if os.path.isdir(directory):
305
+ for filename in os.listdir(directory):
306
+ file_path = os.path.join(directory, filename)
307
+
308
+ # Check if the current file is not the one to keep and is a file
309
+ if filename != keep_file and os.path.isfile(file_path):
310
+ try:
311
+ os.remove(file_path) # Delete the file
312
+ print(f"Deleted {file_path}")
313
+ except Exception as e:
314
+ print(f"Error deleting {file_path}: {e}")
315
+ else:
316
+ print(f"Directory {directory} does not exist.")
317
+
318
+ speech = ''
319
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
320
+ model="tiiuae/falcon-180b-chat",
321
+ messages=[
322
+ {"role": "system", "content": "You are a summarising assistant."},
323
+ {"role": "user",
324
+ "content": f'''Summarise the given content for each chapter for 1 sentence.Content={content}'''},
325
+ ],
326
+ stream=True,
327
+ ):
328
+ if chunk.choices[0].delta.content:
329
+ speech += chunk.choices[0].delta.content
330
+ speech = speech[:-6].replace("###", '')
331
+ chapters = speech.split('\n\n')
332
+ pdf_audio(chapters[:4])
333
+ return
334
+
335
+
336
+ def pdf_audio(chapters):
337
+ for i in range(len(chapters)):
338
+ tts = gTTS(text=chapters[i], lang='en', slow=False)
339
+ tts.save(f'speech/chapter {i + 1}.mp3')
340
+ return
341
+
342
+ def content_translate(text):
343
+
344
+
345
+ translated_content = ''
346
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
347
+ model="tiiuae/falcon-180b-chat",
348
+ messages=[
349
+ {"role": "system", "content": "You are the best teaching assistant."},
350
+ {"role": "user", "content": f'''Translate the text to hindi. Text: {text}'''},
351
+ ],
352
+ stream=True,
353
+ ):
354
+ if chunk.choices[0].delta.content:
355
+ translated_content += chunk.choices[0].delta.content
356
+ return translated_content
teacher_function.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ai71 import AI71
2
+ from PyPDF2 import PdfReader
3
+ from pdf2image import convert_from_path
4
+ import cv2
5
+ import numpy as np
6
+ import pytesseract
7
+ import subprocess
8
+ from PIL import Image
9
+ AI71_API_KEY = "api71-api-652e5c6c-8edf-41d0-9c34-28522b07bef9"
10
+
11
+ subprocess.run(['apt-get','update'])
12
+ subprocess.run(['apt-get','install','-y','tesseract-ocr'])
13
+
14
+ def extract_text_from_pdf(pdf_file):
15
+ text = ""
16
+ reader = PdfReader(pdf_file)
17
+ for page in reader.pages:
18
+ text += page.extract_text()
19
+ return text
20
+
21
+ def generate_questions_from_text(text, no_of_questions, marks_per_part, no_parts):
22
+ ai71 = AI71(AI71_API_KEY)
23
+ messages = [
24
+ {"role": "system", "content": "You are a teaching assistant"},
25
+ {"role": "user",
26
+ "content": f"Give your own {no_of_questions} questions under each part for {no_parts} parts with {marks_per_part} marks for each part. Note that all questions must be from the topics of {text}"}
27
+ ]
28
+
29
+ questions = []
30
+ for chunk in ai71.chat.completions.create(
31
+ model="tiiuae/falcon-180b-chat",
32
+ messages=messages,
33
+ stream=True,
34
+ ):
35
+ if chunk.choices[0].delta.content:
36
+ questions.append(chunk.choices[0].delta.content)
37
+
38
+ return "".join(questions)
39
+
40
+ def extract_text_from_image(image_path):
41
+ # Load the image
42
+ img = cv2.imread(image_path)
43
+
44
+ # Ensure the image was loaded correctly
45
+ if img is None:
46
+ raise ValueError("Image not found or unable to load")
47
+
48
+ # Convert the image to RGB format
49
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
50
+
51
+ # Extract text from the image
52
+ text = pytesseract.image_to_string(img_rgb)
53
+
54
+ return text
55
+
56
+
57
+ def evaluate(question, answer, max_marks):
58
+ prompt = f"""Questions: {question}
59
+ Answer: {answer}.
60
+ Evaluate answers strictly one by one(if there are multiple) for each question and assign marks out of {max_marks} based on below guidelines.
61
+ guidelines:
62
+ - If the answer is wrong or incorrect or irrelevent to topic, give 0 marks.
63
+
64
+ - If the answer is somewhat accurate, give total marks minus 2, and so on.
65
+ - If the answer is very accurate and complete, give total marks.
66
+ - If the answer is good but not completely accurate, give total marks minus 1.
67
+
68
+
69
+ Note:Provide only marks for each answers. dont provide anything other than that.
70
+ Format:
71
+ 1.Question no: Marks,etc"""
72
+
73
+ messages = [
74
+ {"role": "system", "content": "You are a strict answer evaluator. "},
75
+ {"role": "user", "content": prompt}
76
+ ]
77
+
78
+ response_content = ""
79
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
80
+ model="tiiuae/falcon-180b-chat",
81
+ messages=messages,
82
+ stream=True
83
+ ):
84
+ if chunk.choices[0].delta.content:
85
+ response_content += chunk.choices[0].delta.content
86
+ print(response_content)
87
+
88
+ return response_content
89
+
90
+ def generate_student_report(name, age, cgpa, course, assigned_test, ai_test, interests, difficulty, courses_taken):
91
+ prompt = f"""
92
+ Name: {name}
93
+ Age: {age}
94
+ CGPA: {cgpa}
95
+ Course: {course}
96
+ Assigned Test Score: {assigned_test}
97
+ AI generated Test Score: {ai_test}
98
+ Interests: {interests}
99
+ Difficulty in: {difficulty}
100
+ Courses Taken: {courses_taken}
101
+ Use the above student data to generate a neat personalized report and suggested teaching methods."""
102
+
103
+ client = AI71(AI71_API_KEY)
104
+
105
+ response = client.chat.completions.create(
106
+ model="tiiuae/falcon-180B-chat",
107
+ messages=[
108
+ {"role": "system", "content": "You are a student report generator."},
109
+ {"role": "user", "content": prompt}
110
+ ]
111
+ )
112
+
113
+ report = response.choices[0].message.content if response.choices and response.choices[
114
+ 0].message else "No report generated."
115
+ print(report)
116
+
117
+ return report
118
+ def generate_timetable_module(data,hours_per_day,days_per_week,semester_end_date,subjects):
119
+ response = AI71(AI71_API_KEY).chat.completions.create(
120
+ model="tiiuae/falcon-180B-chat",
121
+ messages=[
122
+ {"role": "system", "content": "You are a helpful assistant."},
123
+ {"role": "user", "content": f"Create a timetable starting from Monday based on the following inputs:\n"
124
+ f"- Number of hours per day: {hours_per_day}\n"
125
+ f"- Number of days per week: {days_per_week}\n"
126
+ f"- Semester end date: {semester_end_date}\n"
127
+ f"- Subjects: {', '.join(subjects)}\n"}
128
+ ]
129
+ )
130
+
131
+ # Access the response content correctly
132
+ return( response.choices[0].message.content if response.choices and response.choices[0].message else "No timetable generated.")
133
+
134
+ def cluster_topics(academic_topics):
135
+ prompt = (
136
+ "Please cluster the following academic topics into their respective subjects such as Mathematics, Physics, etc.: "
137
+ + ", ".join(academic_topics))
138
+ response = ""
139
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
140
+ model="tiiuae/falcon-180b-chat",
141
+ messages=[
142
+ {"role": "system", "content": "You are a helpful assistant."},
143
+ {"role": "user", "content": prompt},
144
+ ],
145
+ stream=True,
146
+ ):
147
+ if chunk.choices[0].delta.content:
148
+ response += chunk.choices[0].delta.content
149
+ return response
150
+
151
+ def generate_timetable_weak(clustered_subjects, hours_per_day):
152
+ prompt = (
153
+ f"Using the following subjects and topics:\n{clustered_subjects}\n"
154
+ f"Generate a special class timetable for {hours_per_day} hours per day.\n"
155
+ f"Also provide reference books and methods to teach the slow learners for each subject"
156
+ )
157
+ response = ""
158
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
159
+ model="tiiuae/falcon-180b-chat",
160
+ messages=[
161
+ {"role": "system", "content": "You are a helpful assistant."},
162
+ {"role": "user", "content": prompt},
163
+ ],
164
+ stream=True,
165
+ ):
166
+ if chunk.choices[0].delta.content:
167
+ response += chunk.choices[0].delta.content
168
+ return response