-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy-assistant.py
More file actions
438 lines (372 loc) · 14.5 KB
/
Copy pathstudy-assistant.py
File metadata and controls
438 lines (372 loc) · 14.5 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
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
# -*- coding: utf-8 -*-
"""NLP Miniproject (updated)
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1fwoE5oYvmKxWKM2D25Mioy3etSbKJw9n
"""
# @title Install all the Libraries
!pip install googletrans==3.1.0a0
!pip install pymupdf
!pip install -q transformers==4.43.1 einops accelerate langchain bitsandbytes sentencepiece langchain_community langchain_huggingface
!pip install spacy
!pip install Together
!pip install deep-translator
!pip install -q transformers==4.43.1 einops accelerate langchain bitsandbytes sentencepiece langchain_community langchain_huggingface
!pip install spacy
"""##Model Implementation"""
# @title Load the model
from transformers import AutoTokenizer
import transformers
import torch
import fitz # PyMuPDF for handling PDFs
import spacy
import os
from googletrans import Translator # For multilingual translation
from IPython.display import Markdown
model = "meta-llama/Meta-Llama-3.1-8B-Instruct"
pipeline = transformers.pipeline(
"text-generation",
model=model,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto",
)
# @title All the functions
def printmd(string):
display(Markdown(string))
def generate_study_plan(days, syllabus):
input_prompt = f"Create a detailed roadmap to complete the following syllabus in {days} days:\n{syllabus}"
prompt = """
You are an expert in making study plans. You are an academic assistant who helps users plan their studies.
You have to make a detailed study plan. Share the study resources for each topic, the number of focused hours to study.
Dont give me any outline for the study plan, I need the detailed brief study plan for the specified days.
Cover the Important topics from the syllabus first and then cover rest of the things.
"""
messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": input_prompt},
]
outputs = pipeline(
messages,
max_new_tokens=1024,
do_sample=True,
top_p=0.9,
temperature=0.6,
return_full_text=False,
)
printmd(outputs[0]["generated_text"])
# Translate the plan to user's preferred language
def translate_study_plan(plan_text, target_lang):
translator = Translator()
translated = translator.translate(plan_text, dest=target_lang)
return translated.text
# Read PDF content using PyMuPDF
def read_pdf_input(pdf_path):
doc = fitz.open(pdf_path)
text = ""
for page_num in range(len(doc)):
page = doc.load_page(page_num) # Load each page
text += page.get_text("text") # Extract text from page
return text
# Simple function to read syllabus input if it's in plain text
def read_text_input(text_path):
if os.path.exists(text_path):
with open(text_path, 'r') as file:
return file.read()
return text_path
# NLP function to extract key topics using NER (Named Entity Recognition)
def extract_topics(text):
nlp = spacy.load("en_core_web_sm") # Load a small English model
doc = nlp(text)
topics = set()
for ent in doc.ents:
if ent.label_ in {"ORG", "GPE", "PERSON", "WORK_OF_ART", "EVENT"}: # Extract named entities
topics.add(ent.text)
return list(topics)
def generate_summary(syllabus):
input_prompt = f"Generate a simple summary for this syllabus: {syllabus}"
prompt = """
You are an expert in genearting summaries and getting the important topics for the user syllabus.
Be friendly with the user, suggest the summary with proper points.
"""
messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": input_prompt},
]
outputs = pipeline(
messages,
max_new_tokens=1024,
do_sample=True,
top_p=0.9,
temperature=0.6,
return_full_text=False,
)
printmd(outputs[0]["generated_text"])
def split_text(text, max_length=5000):
# Split text into chunks with a max length of 5000 characters
chunks = []
while len(text) > max_length:
# Find the last space within the max_length to avoid splitting words
split_point = text[:max_length].rfind(' ')
chunks.append(text[:split_point])
text = text[split_point:]
chunks.append(text)
return chunks
# @title Main function using Model
def main():
print("Enter syllabus (text or PDF path):")
input_syllabus = input().strip()
# Handle PDF input if provided
if input_syllabus.endswith('.pdf'):
syllabus_text = read_pdf_input(input_syllabus)
else:
syllabus_text = read_text_input(input_syllabus)
print("Enter the number of days to complete the syllabus (or press Enter to skip):")
days_input = input().strip()
days = int(days_input) if days_input else 30 # Default to 30 days
# NLP Step 1: Extract Key Topics using NER
key_topics = extract_topics(syllabus_text)
print(f"Key Topics extracted from the syllabus: {key_topics}")
# NLP Step 2: Summarize the syllabus for an overview
syllabus_summary = generate_summary(syllabus_text)
print(f"\nSummary of the syllabus: {syllabus_summary}")
# Generate a study roadmap using LLaMA
study_plan = generate_study_plan(days, syllabus_text)
print("\nGenerated Study Plan:")
print(study_plan)
# Translation: Translate the roadmap to the desired language
print("Enter the target language code (e.g., 'es' for Spanish):")
target_lang = input().strip()
translated_plan = translate_study_plan(study_plan, target_lang)
# Output the translated study plan
print(f"\nTranslated Study Plan in {target_lang}:")
print(translated_plan)
if __name__ == "__main__":
main()
"""## API Implementation"""
# @title Load the API Key and Model
import os
from together import Together
from deep_translator import GoogleTranslator
from google.colab import userdata
import torch
import spacy
import fitz
import time
import ipywidgets as widgets
from IPython.display import Markdown
Lkey = userdata.get('Llama_Key')
client = Together(api_key=Lkey)
model = "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"
# @title All the functions
def printmd(string):
display(Markdown(string))
def generate_study_plan(days, syllabus):
input_prompt = f"Create a detailed roadmap to complete the following syllabus in {days} days:\n{syllabus}, not before the days i specified. It should cover {days}"
prompt = """
You are an expert in making study plans. You are an academic assistant who helps users plan their studies.
You have to make a detailed study plan. Share the study resources for each topic, the number of focused hours to study.
Dont give me any outline for the study plan, I need the detailed brief study plan with covering the important topics first and taking proper time to cover them.
Cover the Important topics from the syllabus first and then cover rest of the things.
Dont respond with anything else other than the study plan. When you tell the day along with the day also tell what should be done for the day.
Share some important resources for each topic like youtube videos with Youtube video title, books, etc. Dont share if the resources or youtube videos are wrong, if wrong then try sharing the topic name that they can search and get better results.
"""
messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": input_prompt},
]
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens=4096,
temperature=0.6,
top_p=0.7,
top_k=50,
repetition_penalty=1,
stop=["<|eot_id|>","<|eom_id|>"],
stream=False
)
return (response.choices[0].message.content)
# Translate the plan to user's preferred language
def translate_study_plan(plan_text, target_lang):
translator = GoogleTranslator(source='auto', target=target_lang)
translated_chunks = []
# if target_lang == 'en':
# chunks = split_text(plan_text, max_length=5000)
# # Translate each chunk and store the result
# for chunk in chunks:
# translated_chunks.append(translator.translate(chunk))
# # Join the translated chunks back into a single string
# translated_plan = " ".join(translated_chunks)
# return translated_plan
chunks = split_text(plan_text, max_length=1000) # Try smaller chunks
for chunk in chunks:
attempts = 3
for attempt in range(attempts):
try:
translated_chunks.append(translator.translate(chunk))
time.sleep(1) # Short sleep to avoid rate limit
break # Exit loop on success
except RequestError as e:
print(f"Attempt {attempt + 1} failed: {e}")
time.sleep(2 ** attempt) # Exponential backoff
else:
print("All attempts failed for chunk:", chunk) # Log failure
# Join the translated chunks back into a single string
translated_plan = " ".join(translated_chunks)
return translated_plan
# Read PDF content using PyMuPDF
def read_pdf_input(pdf_path):
doc = fitz.open(pdf_path)
text = ""
for page_num in range(len(doc)):
page = doc.load_page(page_num) # Load each page
text += page.get_text("text") # Extract text from page
return text
def read_text_input(text_path):
if os.path.exists(text_path):
with open(text_path, 'r') as file:
return file.read()
return text_path
def extract_topics(text):
nlp = spacy.load("en_core_web_sm")
doc = nlp(text)
topics = set()
for ent in doc.ents:
if ent.label_ in {"ORG", "GPE", "PERSON", "WORK_OF_ART", "EVENT"}:
topics.add(ent.text)
return list(topics)
def generate_summary(syllabus):
input_prompt = f"Generate a simple summary for this syllabus: {syllabus}"
prompt = """
You are an expert in genearting summaries and getting the important topics for the user syllabus.
Be friendly with the user, suggest the summary with proper points.
Dont respond with anything else than the summary.
"""
messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": input_prompt},
]
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens=512,
temperature=0.6,
top_p=0.7,
top_k=50,
repetition_penalty=1,
stop=["<|eot_id|>","<|eom_id|>"],
stream=False
)
return (response.choices[0].message.content)
def split_text(text, max_length):
chunks = []
# print("slippting")
while len(text) > max_length:
# Find the last space within the max_length to avoid splitting words
split_point = text[:max_length].rfind(' ')
chunks.append(text[:split_point])
text = text[split_point:]
chunks.append(text)
return chunks
import ipywidgets as widgets
def get_languages():
languages = {
'English': 'en',
'Hindi': 'hi',
'Marathi': 'mr',
'Bengali': 'bn',
'Gujarati': 'gu',
'Tamil': 'ta',
'Telugu': 'te',
'Kannada': 'kn',
'Malayalam': 'ml',
'Punjabi': 'pa',
'Odia': 'or',
'Assamese': 'as',
'Nepali': 'ne',
'Urdu': 'ur',
'Sindhi': 'sd',
'Bhojpuri': 'bho',
'Manipuri': 'mni',
'Spanish': 'es',
'French': 'fr',
'German': 'de',
'Italian': 'it',
'Japanese': 'ja',
'Korean': 'ko',
'Russian': 'ru',
}
# Create a dropdown for input language selection
input_langauge = widgets.Dropdown(
options=languages.keys(),
description='Select Input Language:',
)
# Create a dropdown for input language selection
output_langauge = widgets.Dropdown(
options=languages.keys(),
description='Select Output Language:',
)
display(input_langauge)
display(output_langauge)
return languages, input_langauge, output_langauge
def get_code(languages, input_langauge, output_langauge):
for key, value in languages.items():
if key == input_langauge.value:
input_lang = value
if key == output_langauge.value:
output_lang = value
return input_lang, output_lang
"""## Main working"""
languages, input_langauge, output_langauge = get_languages()
input_code, output_code = get_code(languages, input_langauge, output_langauge)
print(input_code, " to ", output_code)
# Get syllabus input from the user
# print("Enter syllabus (text or PDF path):")
print(translate_study_plan("Enter syllabus (text or PDF path):", input_code))
input_syllabus = input().strip()
# Handle PDF input if provided
syllabus = ""
try:
if input_syllabus.endswith('.pdf'):
syllabus = read_pdf_input(input_syllabus)
else:
syllabus = read_text_input(input_syllabus)
except FileNotFoundError:
printmd("**Error:** The specified file was not found. Please check the path and try again.")
raise # Optional: re-raise the exception if you want to stop the program
except Exception as e:
printmd(f"**Error:** An unexpected error: {e}")
raise # Optional: re-raise the exception if you want to stop the program
# Convert syllabus text to English
if syllabus:
syllabus_text = translate_study_plan(syllabus, 'en')
printmd("---")
printmd("### Syllabus Content")
printmd(syllabus_text)
printmd("---")
# Get the number of days to complete the syllabus
# print("Enter the number of days to complete the syllabus (or press Enter to skip):")
print(translate_study_plan("Enter the number of days to complete the syllabus (or press Enter to skip):", input_code))
days_input = input().strip()
days = int(days_input) if days_input else 7 # Default to 7 days
printmd("---")
printmd("### Summary of the syllabus:")
summary = generate_summary(syllabus_text)
# Translate the summary to the user's preferred language
translated_summ = translate_study_plan(summary, output_code)
printmd(translated_summ)
printmd("---")
printmd("### Study Plan:")
study_plan = generate_study_plan(days, syllabus_text)
# Output the translated study plan
translated_plan = translate_study_plan(study_plan, output_code)
printmd(translated_plan)
printmd("---")
printmd("### Key Topics")
array_topics = extract_topics(syllabus_text)
key_topics = ", ".join(array_topics)
# printmd(key_topics)
# Translate key topics to the user's preferred language
translated_topics = translate_study_plan(key_topics, output_code)
printmd(translated_topics)
printmd("---")