VIATEUR-AI's picture
import gradio as gr from transformers import pipeline from fpdf import FPDF import tempfile import os import sqlite3 import hashlib # ====================== # DATABASE SETUP # ====================== conn = sqlite3.connect("teachers.db", check_same_thread=False) c = conn.cursor() c.execute("""CREATE TABLE IF NOT EXISTS users ( username TEXT PRIMARY KEY, password TEXT )""") conn.commit() # ====================== # USER MANAGEMENT # ====================== def add_user(username, password): hashed = hashlib.sha256(password.encode()).hexdigest() try: c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed)) conn.commit() return f"User {username} added successfully!" except sqlite3.IntegrityError: return f"User {username} already exists." def login_user(username, password): hashed = hashlib.sha256(password.encode()).hexdigest() c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, hashed)) return c.fetchone() is not None # ====================== # LOAD MODEL # ====================== generator = pipeline( "text2text-generation", model="google/flan-t5-base", max_new_tokens=800, do_sample=True, temperature=0.7 ) # ====================== # PDF FUNCTION # ====================== def make_pdf(text): font_path = "DejaVuSans.ttf" if not os.path.exists(font_path): raise FileNotFoundError("Font file 'DejaVuSans.ttf' not found in python.appy folder.") with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp: pdf = FPDF() pdf.add_page() pdf.set_auto_page_break(auto=True, margin=15) pdf.add_font("DejaVu", "", font_path, uni=True) pdf.set_font("DejaVu", "", 12) pdf.multi_cell(0, 8, text) pdf.output(tmp.name) return tmp.name # ====================== # EDUCATIONAL FUNCTIONS # ====================== def lesson_plan(info, level, language): prompt = f""" Uri AI ifasha abarimu bo mu Rwanda. Kurikiza REB curriculum. Tegura LESSON PLAN yuzuye ku rwego rwa {level}. Koresha ururimi: {language}. Amakuru: {info} Format: Class & Subject Topic Objectives Materials Lesson Steps Assessment Homework """ text = generator(prompt)[0]["generated_text"] pdf_file = make_pdf(text) return text, pdf_file def exams(info, level, language): prompt = f""" Uri AI itegura EXAMS & QUIZZES. Kurikiza REB curriculum. Urwego: {level} Ururimi: {language} Amakuru: {info} Tegura: - 5 Multiple Choice - 5 Short Answer - Total Marks """ text = generator(prompt)[0]["generated_text"] pdf_file = make_pdf(text) return text, pdf_file def marking_scheme(info, level, language): prompt = f""" Uri AI ikora MARKING SCHEME y'ikizamini. Kurikiza REB curriculum. Urwego: {level} Ururimi: {language} Ikizamini: {info} Subiza: - Question - Expected Answer - Marks """ text = generator(prompt)[0]["generated_text"] pdf_file = make_pdf(text) return text, pdf_file def scheme_of_work(info, level, language): prompt = f""" Uri AI ikora SCHEME OF WORK. Kurikiza REB curriculum. Urwego: {level} Ururimi: {language} Amakuru: {info} Tegura table ikubiyemo: Week | Topic | Objectives | Activities | Assessment """ text = generator(prompt)[0]["generated_text"] pdf_file = make_pdf(text) return text, pdf_file # ====================== # GRADIO UI # ====================== with gr.Blocks(title="AI Ifasha Abarimu (REB)") as app: # Track login state login_state = gr.State(False) # --- LOGIN / REGISTER TAB --- with gr.Tab("πŸ”‘ Login / Register"): gr.Markdown("### Injira cyangwa wiyandikishe") username = gr.Textbox(label="Username") password = gr.Textbox(label="Password", type="password") login_btn = gr.Button("Login") register_btn = gr.Button("Register") login_status = gr.Textbox(label="Status") logout_btn = gr.Button("Logout", visible=False) # Login function def do_login(u, p, state): success = login_user(u, p) if success: return f"Welcome {u}!", True, gr.update(visible=True) else: return "Invalid username or password", False, gr.update(visible=False) login_btn.click(do_login, [username, password, login_state], [login_status, login_state, logout_btn]) register_btn.click(add_user, [username, password], login_status) # Logout function def do_logout(): return gr.update(value="Logged out"), False, gr.update(visible=False) logout_btn.click(do_logout, [], [login_status, login_state, logout_btn]) # --- INPUTS (always visible) --- info = gr.Textbox(lines=6, label="Andika amakuru (subject, topic, class, term, etc)") level = gr.Dropdown(["Primary", "Secondary"], label="Level") language = gr.Dropdown(["Ikinyarwanda", "English"], label="Ururimi") # --- MAIN FUNCTIONALITY TABS --- with gr.Tab("πŸ“˜ Lesson Plan") as lp_tab: lp_out = gr.Textbox(label="Lesson Plan Text") lp_pdf = gr.File(label="Download PDF") gr.Button("Generate").click(lesson_plan, [info, level, language], [lp_out, lp_pdf]) with gr.Tab("πŸ“ Exams & Quizzes") as ex_tab: ex_out = gr.Textbox(label="Exams Text") ex_pdf = gr.File(label="Download PDF") gr.Button("Generate").click(exams, [info, level, language], [ex_out, ex_pdf]) with gr.Tab("πŸ“Š Marking Scheme") as ms_tab: ms_out = gr.Textbox(label="Marking Scheme Text") ms_pdf = gr.File(label="Download PDF") gr.Button("Generate").click(marking_scheme, [info, level, language], [ms_out, ms_pdf]) with gr.Tab("πŸ“š Scheme of Work") as sw_tab: sw_out = gr.Textbox(label="Scheme of Work Text") sw_pdf = gr.File(label="Download PDF") gr.Button("Generate").click(scheme_of_work, [info, level, language], [sw_out, sw_pdf]) # --- Show/Hide main tabs depending on login --- def toggle_main_tabs(is_logged_in): return gr.update(visible=is_logged_in), gr.update(visible=is_logged_in), gr.update(visible=is_logged_in), gr.update(visible=is_logged_in) login_state.change(toggle_main_tabs, login_state, [lp_tab, ex_tab, ms_tab, sw_tab]) app.launch()
11baccd verified