Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import fitz # PyMuPDF | |
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| # β Load Granite model | |
| model_name = "ibm-granite/granite-3.3-2b-instruct" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32) | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model.to(device) | |
| # β In-memory user data | |
| user_db = {} | |
| progress_file = "progress.json" | |
| # β Load progress from file if exists | |
| if os.path.exists(progress_file): | |
| with open(progress_file, "r") as f: | |
| user_progress = json.load(f) | |
| else: | |
| user_progress = {} | |
| def save_progress(): | |
| with open(progress_file, "w") as f: | |
| json.dump(user_progress, f, indent=2) | |
| def generate_response(prompt): | |
| inputs = tokenizer(prompt, return_tensors="pt").to(device) | |
| outputs = model.generate(**inputs, max_new_tokens=300) | |
| return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| def extract_text_from_pdf(pdf_file): | |
| doc = fitz.open(stream=pdf_file.read(), filetype="pdf") | |
| return "".join(page.get_text() for page in doc) | |
| def register_user(username, password, grade): | |
| if username in user_db: | |
| return "β Username exists." | |
| user_db[username] = {"password": password, "grade": grade} | |
| user_progress[username] = {} | |
| save_progress() | |
| return "β Registered!" | |
| def login_user(username, password): | |
| if username in user_db and user_db[username]["password"] == password: | |
| return "β Login successful!" | |
| return "β Invalid credentials." | |
| def generate_questions_from_pdf(pdf_file, username="anonymous"): | |
| text = extract_text_from_pdf(pdf_file) | |
| prompt = f"You are a teacher. Generate 5 mixed questions from the text:\n\n{text}" | |
| quiz = generate_response(prompt) | |
| if username in user_progress: | |
| user_progress[username]["PDF Quiz"] = "Generated from uploaded PDF" | |
| save_progress() | |
| return quiz | |
| def generate_quiz_from_topic(topic, level="medium", username="anonymous"): | |
| prompt = f"Create a {level} difficulty quiz with 5 questions (MCQs and descriptive) on the topic: {topic}." | |
| quiz = generate_response(prompt) | |
| if username in user_progress: | |
| user_progress[username][topic] = f"Quiz generated ({level})" | |
| save_progress() | |
| return quiz | |
| def summarize_and_explain_from_pdf(pdf_file, age="15", username="anonymous"): | |
| text = extract_text_from_pdf(pdf_file) | |
| summary_prompt = f"Summarize the key concepts from this text in bullet points:\n{text}" | |
| summary = generate_response(summary_prompt) | |
| explain_prompt = f"Explain the following concepts for a {age}-year-old student:\n{summary}" | |
| explanation = generate_response(explain_prompt) | |
| if username in user_progress: | |
| user_progress[username]["PDF Concepts"] = "Summarized and explained" | |
| save_progress() | |
| return f"πΉ Summary:\n{summary}\n\nπ Explanations:\n{explanation}" | |
| def handle_input(input_text, task_type, language_or_age, username="anonymous"): | |
| if task_type == "Concept Understanding": | |
| age = "an adult" if language_or_age == "More than 18" else f"a {language_or_age}-year-old" | |
| prompt = f"Explain this concept simply for {age}:\n{input_text}" | |
| if username in user_progress: | |
| user_progress[username][input_text] = "Concept explained" | |
| elif task_type == "Language Learning": | |
| prompt = f"Explain grammar and parts of speech in {language_or_age} for:\n{input_text}" | |
| if username in user_progress: | |
| user_progress[username][input_text] = f"Grammar explained in {language_or_age}" | |
| else: | |
| prompt = input_text | |
| save_progress() | |
| return generate_response(prompt) | |
| def view_progress(username): | |
| if username in user_progress: | |
| progress = user_progress[username] | |
| return "\n".join([f"{k}: {v}" for k, v in progress.items()]) or "No progress yet." | |
| return "User not found or no progress." | |
| # β Gradio UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π EduTutor AI - With Progress Tracker") | |
| with gr.Tab("π Register"): | |
| u = gr.Textbox(label="Username") | |
| p = gr.Textbox(label="Password", type="password") | |
| g = gr.Dropdown(["6", "7", "8", "9", "10"], label="Grade") | |
| b = gr.Button("Register") | |
| o = gr.Textbox(label="Status") | |
| b.click(register_user, [u, p, g], o) | |
| with gr.Tab("π Login"): | |
| lu = gr.Textbox(label="Username") | |
| lp = gr.Textbox(label="Password", type="password") | |
| lb = gr.Button("Login") | |
| lo = gr.Textbox(label="Status") | |
| lb.click(login_user, [lu, lp], lo) | |
| with gr.Tab("π Quiz Generator (PDF or Topic)"): | |
| gr.Markdown("### π Upload a PDF") | |
| pdf = gr.File(label="Upload PDF", type="binary") | |
| uname1 = gr.Textbox(label="Username") | |
| btn_pdf = gr.Button("Generate Quiz from PDF") | |
| out_pdf = gr.Textbox(label="PDF-based Quiz") | |
| btn_pdf.click(generate_questions_from_pdf, [pdf, uname1], out_pdf) | |
| gr.Markdown("### β Topic-based Quiz") | |
| topic_input = gr.Textbox(label="Enter Topic or Concept") | |
| topic_level = gr.Dropdown(["beginner", "medium", "advanced"], label="Difficulty") | |
| topic_user = gr.Textbox(label="Username") | |
| topic_btn = gr.Button("Generate Quiz from Topic") | |
| topic_out = gr.Textbox(label="Topic-based Quiz") | |
| topic_btn.click(generate_quiz_from_topic, [topic_input, topic_level, topic_user], topic_out) | |
| with gr.Tab("π Concept Understanding"): | |
| t = gr.Textbox(label="Enter Concept") | |
| a = gr.Dropdown(["10", "12", "15", "18", "More than 18"], label="Age") | |
| uname2 = gr.Textbox(label="Username") | |
| b = gr.Button("Explain") | |
| o = gr.Textbox(label="Explanation") | |
| b.click(handle_input, [t, gr.State("Concept Understanding"), a, uname2], o) | |
| with gr.Tab("π PDF Concept Summarizer"): | |
| pdf2 = gr.File(label="Upload PDF", type="binary") | |
| age2 = gr.Dropdown(["10", "12", "15", "18", "More than 18"], label="Age") | |
| uname3 = gr.Textbox(label="Username") | |
| btn2 = gr.Button("Summarize & Explain Concepts") | |
| out2 = gr.Textbox(label="Summary & Explanation") | |
| btn2.click(summarize_and_explain_from_pdf, [pdf2, age2, uname3], out2) | |
| with gr.Tab("π Progress Tracker"): | |
| pu = gr.Textbox(label="Username") | |
| pb = gr.Button("Show Progress") | |
| po = gr.Textbox(label="Your Progress") | |
| pb.click(view_progress, pu, po) | |
| demo.launch() | |