iffazainab commited on
Commit
3080e60
Β·
verified Β·
1 Parent(s): d912c19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -24,12 +24,7 @@ def query_gpt(prompt):
24
 
25
  # --------------- PDF TEXT EXTRACTION ---------------
26
  def extract_text_from_pdf(pdf_path):
27
- doc = fitz.open(pdf_path) # open directly using path
28
- text = ""
29
- for page in doc:
30
- text += page.get_text()
31
- return text
32
-
33
  text = ""
34
  for page in doc:
35
  text += page.get_text()
@@ -48,28 +43,46 @@ def simplify_concepts(text):
48
  prompt = f"Simplify and explain the following concepts for a student who is 14 years old:\n\n{text}"
49
  return query_gpt(prompt)
50
 
 
 
 
 
 
 
 
 
 
51
  # --------------- GRADIO UI ---------------
52
  with gr.Blocks(title="AI Textbook Tutor") as app:
53
- gr.Markdown("# πŸ“˜ AI Textbook Tutor\nUpload your textbook or notes and get summaries, MCQs, and simplified explanations!")
54
-
55
- with gr.Row():
56
- pdf_input = gr.File(label="πŸ“„ Upload PDF", file_types=[".pdf"])
57
 
58
- with gr.Row():
59
- action = gr.Radio([
60
- "Summarize Important Points",
61
- "Generate MCQs",
62
- "Simplify Concepts"
63
- ], label="Select Task")
 
 
 
 
64
 
65
- run_btn = gr.Button("Run 🧠")
66
- output = gr.Textbox(label="πŸ“€ Output", lines=15)
 
 
 
 
 
 
 
 
 
67
 
68
- def process(pdf_file, action_type):
69
  text = extract_text_from_pdf(pdf_file)
70
  if len(text) > 5000:
71
- text = text[:5000] # truncate to avoid long prompts
72
-
73
  if action_type == "Summarize Important Points":
74
  return summarize_textbook(text)
75
  elif action_type == "Generate MCQs":
@@ -77,6 +90,8 @@ with gr.Blocks(title="AI Textbook Tutor") as app:
77
  elif action_type == "Simplify Concepts":
78
  return simplify_concepts(text)
79
 
80
- run_btn.click(fn=process, inputs=[pdf_input, action], outputs=[output])
 
81
 
82
  app.launch()
 
 
24
 
25
  # --------------- PDF TEXT EXTRACTION ---------------
26
  def extract_text_from_pdf(pdf_path):
27
+ doc = fitz.open(pdf_path)
 
 
 
 
 
28
  text = ""
29
  for page in doc:
30
  text += page.get_text()
 
43
  prompt = f"Simplify and explain the following concepts for a student who is 14 years old:\n\n{text}"
44
  return query_gpt(prompt)
45
 
46
+ def process_text_inputs(book, chapter, action_type):
47
+ user_prompt = f"Give a detailed explanation and key points for the chapter '{chapter}' from the book '{book}'"
48
+ if action_type == "Summarize Important Points":
49
+ return query_gpt(f"Summarize the following chapter:\n\n{user_prompt}")
50
+ elif action_type == "Generate MCQs":
51
+ return query_gpt(f"Generate 5 MCQs with 4 options each from:\n\n{user_prompt}")
52
+ elif action_type == "Simplify Concepts":
53
+ return query_gpt(f"Explain in simple terms the concepts from:\n\n{user_prompt}")
54
+
55
  # --------------- GRADIO UI ---------------
56
  with gr.Blocks(title="AI Textbook Tutor") as app:
57
+ gr.Markdown("# πŸ“˜ AI Textbook Tutor\nUpload your textbook or type a chapter and get summaries, MCQs, and simplified explanations!")
 
 
 
58
 
59
+ with gr.Tab("πŸ“„ Upload PDF"):
60
+ with gr.Row():
61
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
62
+ action_pdf = gr.Radio([
63
+ "Summarize Important Points",
64
+ "Generate MCQs",
65
+ "Simplify Concepts"
66
+ ], label="Select Task")
67
+ run_pdf = gr.Button("Run 🧠 on PDF")
68
+ output_pdf = gr.Textbox(label="πŸ“€ Output", lines=15)
69
 
70
+ with gr.Tab("πŸ” Search by Book & Chapter"):
71
+ with gr.Row():
72
+ book_input = gr.Textbox(label="Book Name", placeholder="e.g., Physics 9th Class")
73
+ chapter_input = gr.Textbox(label="Chapter or Topic Name", placeholder="e.g., Measurement")
74
+ action_text = gr.Radio([
75
+ "Summarize Important Points",
76
+ "Generate MCQs",
77
+ "Simplify Concepts"
78
+ ], label="Select Task")
79
+ run_text = gr.Button("Run 🧠 on Chapter")
80
+ output_text = gr.Textbox(label="πŸ“€ Output", lines=15)
81
 
82
+ def process_pdf(pdf_file, action_type):
83
  text = extract_text_from_pdf(pdf_file)
84
  if len(text) > 5000:
85
+ text = text[:5000]
 
86
  if action_type == "Summarize Important Points":
87
  return summarize_textbook(text)
88
  elif action_type == "Generate MCQs":
 
90
  elif action_type == "Simplify Concepts":
91
  return simplify_concepts(text)
92
 
93
+ run_pdf.click(fn=process_pdf, inputs=[pdf_input, action_pdf], outputs=[output_pdf])
94
+ run_text.click(fn=process_text_inputs, inputs=[book_input, chapter_input, action_text], outputs=[output_text])
95
 
96
  app.launch()
97
+