Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| # β Set page config (must be the first Streamlit command) | |
| st.set_page_config(page_title="AI-Powered Coding Question Generator", layout="centered") | |
| import os | |
| import subprocess | |
| import sqlite3 | |
| import random | |
| from fpdf import FPDF | |
| from transformers import pipeline | |
| # β Force install missing dependencies | |
| missing_packages = ["transformers", "torch", "sentence-transformers", "fpdf"] | |
| for package in missing_packages: | |
| try: | |
| __import__(package) | |
| except ImportError: | |
| subprocess.call(["pip", "install", package]) | |
| # β Create database | |
| def create_database(): | |
| conn = sqlite3.connect("questions.db") | |
| cursor = conn.cursor() | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS questions ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| subject TEXT, | |
| topic TEXT, | |
| question TEXT | |
| ) | |
| """) | |
| conn.commit() | |
| conn.close() | |
| # β Insert question into database | |
| def insert_question(subject, topic, question): | |
| conn = sqlite3.connect("questions.db") | |
| cursor = conn.cursor() | |
| cursor.execute("INSERT INTO questions (subject, topic, question) VALUES (?, ?, ?)", (subject, topic, question)) | |
| conn.commit() | |
| conn.close() | |
| # β Retrieve stored questions | |
| def get_questions(subject, topic, num): | |
| conn = sqlite3.connect("questions.db") | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT question FROM questions WHERE subject = ? AND topic = ? ORDER BY RANDOM() LIMIT ?", (subject, topic, num)) | |
| questions = [q[0] for q in cursor.fetchall()] | |
| conn.close() | |
| return questions | |
| # β Load AI Model for Question Generation | |
| def load_model(): | |
| try: | |
| model = pipeline("text-generation", model="microsoft/CodeGPT-small-py") | |
| return model | |
| except Exception as e: | |
| st.error(f"β Error loading AI Model: {e}") | |
| return None | |
| model = load_model() | |
| # β Generate AI Questions | |
| def generate_ai_questions(subject, topic, num): | |
| if not model: | |
| return ["β AI Model not loaded. Try restarting the app."] | |
| try: | |
| prompt = f"Generate {num} coding-related questions specifically about {topic} in {subject}. Ensure the questions are clear and relevant to programming." | |
| generated = model(prompt, max_length=100, num_return_sequences=num) | |
| return [q["generated_text"].strip() for q in generated] | |
| except Exception as e: | |
| return [f"β Error generating questions: {e}"] | |
| # β Generate PDF | |
| def generate_pdf(subject, topic, questions): | |
| pdf = FPDF() | |
| pdf.set_auto_page_break(auto=True, margin=15) | |
| pdf.add_page() | |
| pdf.set_font("Arial", style='B', size=16) | |
| pdf.cell(200, 10, f"{subject} - {topic} Questions", ln=True, align='C') | |
| pdf.ln(10) | |
| pdf.set_font("Arial", size=12) | |
| for i, question in enumerate(questions, 1): | |
| pdf.multi_cell(0, 10, f"{i}. {question}") | |
| pdf.ln(5) | |
| pdf.output("generated_questions.pdf") | |
| return "generated_questions.pdf" | |
| # β Main Application | |
| def main(): | |
| st.title("π» AI-Based Coding Question Generator") | |
| st.sidebar.header("π Settings") | |
| subject = st.sidebar.selectbox("Select Subject", ["DSA", "Python", "Java", "DBMS"]) | |
| topic = st.sidebar.text_input("Enter a topic (e.g., 'Recursion', 'SQL Joins')") | |
| num = st.sidebar.slider("Number of Questions:", 1, 5, 2) | |
| if st.sidebar.button("π Generate Questions"): | |
| if topic.strip(): | |
| questions = get_questions(subject, topic, num) | |
| if not questions: | |
| questions = generate_ai_questions(subject, topic, num) | |
| for q in questions: | |
| insert_question(subject, topic, q) | |
| st.subheader(f"π {subject} - {topic} Questions") | |
| for i, q in enumerate(questions, 1): | |
| st.write(f"{i}. {q}") | |
| if st.button("π₯ Download as PDF"): | |
| pdf_path = generate_pdf(subject, topic, questions) | |
| with open(pdf_path, "rb") as f: | |
| st.download_button("π₯ Download PDF", f, file_name="questions.pdf", mime="application/pdf") | |
| else: | |
| st.warning("β οΈ Please enter a valid topic!") | |
| if __name__ == "__main__": | |
| create_database() | |
| main() | |