|
import os |
|
import torch |
|
import faiss |
|
import wikipediaapi |
|
from fpdf import FPDF |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
from sentence_transformers import SentenceTransformer |
|
import gradio as gr |
|
from googletrans import Translator |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
wiki_wiki = wikipediaapi.Wikipedia( |
|
language="en", |
|
user_agent="AdaptiveLearningApp/1.0 (Contact: [email protected])" |
|
) |
|
|
|
|
|
def generate_pdf_from_wikipedia(subject, topic): |
|
page = wiki_wiki.page(topic) |
|
if not page.exists(): |
|
return None, f"Topic '{topic}' not found on Wikipedia." |
|
|
|
pdf = FPDF() |
|
pdf.add_page() |
|
pdf.set_font("Arial", size=12) |
|
pdf.cell(200, 10, txt=f"{subject.upper()} - {topic.upper()}", ln=True, align="C") |
|
pdf.ln(10) |
|
|
|
|
|
for line in page.text.split("\n"): |
|
pdf.multi_cell(0, 10, line) |
|
pdf.ln(5) |
|
|
|
pdf_path = f"{topic.replace(' ', '_')}.pdf" |
|
pdf.output(pdf_path) |
|
return pdf_path, f"PDF for topic '{topic}' has been generated successfully." |
|
|
|
|
|
def chunk_text(text, chunk_size=300): |
|
sentences = text.split(". ") |
|
chunks, current_chunk = [], "" |
|
for sentence in sentences: |
|
if len(current_chunk) + len(sentence) < chunk_size: |
|
current_chunk += sentence + ". " |
|
else: |
|
chunks.append(current_chunk.strip()) |
|
current_chunk = sentence + ". " |
|
if current_chunk: |
|
chunks.append(current_chunk.strip()) |
|
return chunks |
|
|
|
|
|
def create_embeddings(chunks): |
|
embeddings = sentence_model.encode(chunks, convert_to_tensor=False) |
|
return embeddings |
|
|
|
|
|
def store_in_faiss(chunks, embeddings): |
|
dimension = embeddings[0].shape[0] |
|
res = faiss.StandardGpuResources() |
|
index = faiss.IndexFlatL2(dimension) |
|
index = faiss.index_cpu_to_gpu(res, 0, index) |
|
index.add(embeddings) |
|
return index |
|
|
|
|
|
def generate_quiz(content): |
|
prompt = f"Generate 10 quiz questions from the following content:\n{content}" |
|
inputs = bloom_tokenizer(prompt, return_tensors="pt", truncation=True).to(device) |
|
outputs = bloom_model.generate(inputs["input_ids"], max_length=512, num_return_sequences=1) |
|
quiz = bloom_tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
return quiz |
|
|
|
|
|
def translate_to_urdu(content): |
|
translator = Translator() |
|
translation = translator.translate(content, src="en", dest="ur") |
|
return translation.text |
|
|
|
|
|
def get_content_by_topic(topic): |
|
page = wiki_wiki.page(topic) |
|
if not page.exists(): |
|
return f"Topic '{topic}' not found on Wikipedia." |
|
return page.text |
|
|
|
|
|
def evaluate_quiz(user_answers, correct_answers): |
|
score = 0 |
|
feedback = [] |
|
for user, correct in zip(user_answers, correct_answers): |
|
if user.strip().lower() == correct.strip().lower(): |
|
score += 1 |
|
feedback.append("Correct") |
|
else: |
|
feedback.append(f"Incorrect. Correct answer: {correct}") |
|
return score, feedback |
|
|
|
|
|
def adaptive_learning_app(subject, topic): |
|
content = get_content_by_topic(topic) |
|
if "not found" in content: |
|
return None, content |
|
|
|
|
|
chunks = chunk_text(content) |
|
embeddings = create_embeddings(chunks) |
|
faiss_index = store_in_faiss(chunks, embeddings) |
|
return content, chunks, embeddings, faiss_index, "Processing complete." |
|
|
|
|
|
def main_ui(): |
|
def process_input(subject, topic): |
|
global content, chunks, embeddings, faiss_index |
|
content, chunks, embeddings, faiss_index, message = adaptive_learning_app(subject, topic) |
|
return content, message |
|
|
|
def generate_pdf(subject, topic): |
|
pdf_path, message = generate_pdf_from_wikipedia(subject, topic) |
|
return pdf_path, message |
|
|
|
def interactive_quiz(content): |
|
quiz = generate_quiz(content) |
|
return quiz |
|
|
|
def urdu_translation(content): |
|
return translate_to_urdu(content) |
|
|
|
def submit_answers(user_answers, correct_answers): |
|
score, feedback = evaluate_quiz(user_answers, correct_answers) |
|
return f"Your Score: {score}/{len(correct_answers)}", feedback |
|
|
|
|
|
interface = gr.Blocks() |
|
|
|
with interface: |
|
with gr.Row(): |
|
gr.Markdown("### Adaptive Learning App with Wikipedia Integration") |
|
|
|
with gr.Row(): |
|
subject_input = gr.Textbox(label="Enter Subject") |
|
topic_input = gr.Textbox(label="Enter Topic") |
|
process_button = gr.Button("Process") |
|
course_material = gr.TextArea(label="Course Material", lines=15) |
|
process_button.click( |
|
process_input, |
|
inputs=[subject_input, topic_input], |
|
outputs=[course_material, gr.Textbox(label="Status")] |
|
) |
|
|
|
with gr.Row(): |
|
pdf_button = gr.Button("Generate PDF") |
|
pdf_download = gr.File(label="Download PDF") |
|
pdf_button.click( |
|
generate_pdf, |
|
inputs=[subject_input, topic_input], |
|
outputs=[pdf_download, gr.Textbox(label="PDF Status")] |
|
) |
|
|
|
with gr.Row(): |
|
quiz_button = gr.Button("Generate Quiz") |
|
quiz_view = gr.TextArea(label="Quiz Questions", lines=10) |
|
quiz_button.click( |
|
interactive_quiz, |
|
inputs=course_material, |
|
outputs=quiz_view |
|
) |
|
|
|
with gr.Row(): |
|
urdu_button = gr.Button("Translate to Urdu") |
|
urdu_translation_view = gr.TextArea(label="Urdu Translation", lines=10) |
|
urdu_button.click( |
|
urdu_translation, |
|
inputs=course_material, |
|
outputs=urdu_translation_view |
|
) |
|
|
|
with gr.Row(): |
|
user_answers = gr.Textbox(label="Your Answers (comma-separated)") |
|
submit_button = gr.Button("Submit Answers") |
|
result_output = gr.Textbox(label="Quiz Result") |
|
feedback_output = gr.TextArea(label="Feedback", lines=10) |
|
submit_button.click( |
|
submit_answers, |
|
inputs=[user_answers, quiz_view], |
|
outputs=[result_output, feedback_output] |
|
) |
|
|
|
interface.launch() |
|
|
|
|
|
bloom_model = AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m").to(device) |
|
bloom_tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-560m") |
|
sentence_model = SentenceTransformer("all-MiniLM-L6-v2", device=device) |
|
|
|
|
|
main_ui() |