File size: 3,879 Bytes
3bb5e5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d912c19
3080e60
3bb5e5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3080e60
 
 
 
 
 
 
 
 
3bb5e5a
 
3080e60
3bb5e5a
3080e60
 
49bc48e
3080e60
 
 
 
 
 
 
3bb5e5a
3080e60
 
 
 
 
 
 
 
 
 
 
3bb5e5a
3080e60
3bb5e5a
 
3080e60
3bb5e5a
 
 
 
 
 
 
3080e60
 
3bb5e5a
 
3080e60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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()