# app.py import gradio as gr import fitz # PyMuPDF import os import requests # --------------- GROQ GPT CALL --------------- GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "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" GROQ_MODEL = "llama3-70b-8192" def query_gpt(prompt): headers = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" } data = { "model": GROQ_MODEL, "messages": [ {"role": "user", "content": prompt} ] } response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=data, headers=headers) return response.json()['choices'][0]['message']['content'] # --------------- PDF TEXT EXTRACTION --------------- def extract_text_from_pdf(pdf_path): doc = fitz.open(pdf_path) text = "" for page in doc: text += page.get_text() return text # --------------- MAIN TASKS --------------- def summarize_textbook(text): prompt = f"Summarize the following content into important bullet points:\n\n{text}" return query_gpt(prompt) def generate_mcqs(text): prompt = f"Generate 5 multiple choice questions (MCQs) with 4 options each from the following content:\n\n{text}" return query_gpt(prompt) def simplify_concepts(text): prompt = f"Simplify and explain the following concepts for a student who is 14 years old:\n\n{text}" return query_gpt(prompt) def process_text_inputs(book, chapter, action_type): user_prompt = f"Give a detailed explanation and key points for the chapter '{chapter}' from the book '{book}'" if action_type == "Summarize Important Points": return query_gpt(f"Summarize the following chapter:\n\n{user_prompt}") elif action_type == "Generate MCQs": return query_gpt(f"Generate 5 MCQs with 4 options each from:\n\n{user_prompt}") elif action_type == "Simplify Concepts": return query_gpt(f"Explain in simple terms the concepts from:\n\n{user_prompt}") # --------------- GRADIO UI --------------- with gr.Blocks(title="AI Textbook Tutor") as app: gr.Markdown("# 📘 AI Textbook Tutor\nUpload your textbook or type a chapter and get summaries, MCQs, and simplified explanations!") with gr.Tab("📄 Upload PDF"): with gr.Row(): pdf_input = gr.File(label="Upload PDF", file_types=None) action_pdf = gr.Radio([ "Summarize Important Points", "Generate MCQs", "Simplify Concepts" ], label="Select Task") run_pdf = gr.Button("Run 🧠 on PDF") output_pdf = gr.Textbox(label="📤 Output", lines=15) with gr.Tab("🔍 Search by Book & Chapter"): with gr.Row(): book_input = gr.Textbox(label="Book Name", placeholder="e.g., Physics 9th Class") chapter_input = gr.Textbox(label="Chapter or Topic Name", placeholder="e.g., Measurement") action_text = gr.Radio([ "Summarize Important Points", "Generate MCQs", "Simplify Concepts" ], label="Select Task") run_text = gr.Button("Run 🧠 on Chapter") output_text = gr.Textbox(label="📤 Output", lines=15) def process_pdf(pdf_file, action_type): text = extract_text_from_pdf(pdf_file) if len(text) > 5000: text = text[:5000] if action_type == "Summarize Important Points": return summarize_textbook(text) elif action_type == "Generate MCQs": return generate_mcqs(text) elif action_type == "Simplify Concepts": return simplify_concepts(text) run_pdf.click(fn=process_pdf, inputs=[pdf_input, action_pdf], outputs=[output_pdf]) run_text.click(fn=process_text_inputs, inputs=[book_input, chapter_input, action_text], outputs=[output_text]) app.launch()