Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,15 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
def generate_multiple_choice_question():
|
5 |
# Placeholder function to simulate question generation
|
6 |
question = "What is the capital of France?"
|
7 |
option_a = "Berlin"
|
@@ -16,16 +24,14 @@ def generate_multiple_choice_question():
|
|
16 |
"explanation": explanation
|
17 |
}
|
18 |
|
19 |
-
def check_answer(
|
20 |
success = selected_option == correct_option
|
21 |
-
|
22 |
-
|
23 |
-
"correct_option": correct_option,
|
24 |
-
"explanation": explanation
|
25 |
-
}
|
26 |
|
27 |
-
def main():
|
28 |
-
|
|
|
29 |
question = mcq["question"]
|
30 |
option_a = mcq["option_a"]
|
31 |
option_b = mcq["option_b"]
|
@@ -37,30 +43,23 @@ def main():
|
|
37 |
with gr.Blocks() as demo:
|
38 |
pdf_file = gr.File(label="Upload PDF", file_types=["pdf"])
|
39 |
question = gr.Textbox(label="Generated Question", interactive=False)
|
40 |
-
option_a = gr.
|
41 |
-
option_b = gr.
|
42 |
-
correct_option = gr.Textbox(label="Correct Option", interactive=False)
|
43 |
-
explanation = gr.Textbox(label="Explanation", interactive=False)
|
44 |
selected_option = gr.Radio(choices=["a", "b"], label="Select your answer")
|
45 |
-
result = gr.
|
46 |
|
47 |
-
def process(pdf_file
|
48 |
q, a, b, correct, expl = main(pdf_file)
|
49 |
-
return
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
"explanation": expl
|
55 |
-
}, selected_option, correct, expl
|
56 |
|
57 |
-
def evaluate(answer, correct_option, explanation):
|
58 |
-
return check_answer("", answer, correct_option, explanation)
|
59 |
-
|
60 |
process_button = gr.Button("Generate Question")
|
61 |
evaluate_button = gr.Button("Submit Answer")
|
62 |
|
63 |
-
process_button.click(fn=process, inputs=[pdf_file
|
64 |
-
evaluate_button.click(fn=evaluate, inputs=[selected_option,
|
65 |
|
66 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import gradio as gr
|
3 |
+
import PyPDF2
|
4 |
|
5 |
+
def extract_text_from_pdf(file):
|
6 |
+
pdf_reader = PyPDF2.PdfFileReader(file)
|
7 |
+
text = ""
|
8 |
+
for page_num in range(pdf_reader.getNumPages()):
|
9 |
+
text += pdf_reader.getPage(page_num).extract_text()
|
10 |
+
return text
|
11 |
|
12 |
+
def generate_multiple_choice_question(text):
|
13 |
# Placeholder function to simulate question generation
|
14 |
question = "What is the capital of France?"
|
15 |
option_a = "Berlin"
|
|
|
24 |
"explanation": explanation
|
25 |
}
|
26 |
|
27 |
+
def check_answer(selected_option, correct_option, explanation):
|
28 |
success = selected_option == correct_option
|
29 |
+
result_text = f"Correct! {explanation}" if success else f"Incorrect. {explanation}"
|
30 |
+
return result_text, success
|
|
|
|
|
|
|
31 |
|
32 |
+
def main(pdf_file):
|
33 |
+
text = extract_text_from_pdf(pdf_file)
|
34 |
+
mcq = generate_multiple_choice_question(text)
|
35 |
question = mcq["question"]
|
36 |
option_a = mcq["option_a"]
|
37 |
option_b = mcq["option_b"]
|
|
|
43 |
with gr.Blocks() as demo:
|
44 |
pdf_file = gr.File(label="Upload PDF", file_types=["pdf"])
|
45 |
question = gr.Textbox(label="Generated Question", interactive=False)
|
46 |
+
option_a = gr.Textbox(label="Option A", interactive=False)
|
47 |
+
option_b = gr.Textbox(label="Option B", interactive=False)
|
|
|
|
|
48 |
selected_option = gr.Radio(choices=["a", "b"], label="Select your answer")
|
49 |
+
result = gr.Textbox(label="Result", interactive=False)
|
50 |
|
51 |
+
def process(pdf_file):
|
52 |
q, a, b, correct, expl = main(pdf_file)
|
53 |
+
return q, a, b, correct, expl
|
54 |
+
|
55 |
+
def evaluate(selected_option, correct_option, explanation):
|
56 |
+
result_text, success = check_answer(selected_option, correct_option, explanation)
|
57 |
+
return result_text
|
|
|
|
|
58 |
|
|
|
|
|
|
|
59 |
process_button = gr.Button("Generate Question")
|
60 |
evaluate_button = gr.Button("Submit Answer")
|
61 |
|
62 |
+
process_button.click(fn=process, inputs=[pdf_file], outputs=[question, option_a, option_b, selected_option, result])
|
63 |
+
evaluate_button.click(fn=evaluate, inputs=[selected_option, selected_option, result], outputs=result)
|
64 |
|
65 |
demo.launch()
|