Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,3 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
-
import pandas as pd
|
4 |
-
from mcq_generator import ImprovedMCQGenerator, is_suitable_for_students
|
5 |
-
import io
|
6 |
-
import tempfile
|
7 |
-
import os
|
8 |
-
|
9 |
-
|
10 |
-
# Load MCQ generator once
|
11 |
-
mcq_generator = ImprovedMCQGenerator()
|
12 |
-
|
13 |
def generate_mcqs_ui(paragraph, num_questions):
|
14 |
if not paragraph.strip():
|
15 |
return None, None, "β οΈ Please enter a valid paragraph."
|
@@ -32,7 +20,7 @@ def generate_mcqs_ui(paragraph, num_questions):
|
|
32 |
question_html += "</div>"
|
33 |
pretty_mcqs.append(question_html)
|
34 |
|
35 |
-
# Prepare
|
36 |
txt_output = ""
|
37 |
csv_data = []
|
38 |
|
@@ -51,42 +39,21 @@ def generate_mcqs_ui(paragraph, num_questions):
|
|
51 |
'Answer': chr(65+mcq['answer_index'])
|
52 |
})
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
return pretty_mcqs, [
|
64 |
|
65 |
except Exception as e:
|
66 |
return None, None, f"β Error generating MCQs: {str(e)}"
|
67 |
-
|
68 |
-
# Gradio Interface
|
69 |
-
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
70 |
-
gr.Markdown("<h1 style='text-align:center;'>π Smart MCQ Generator</h1>")
|
71 |
-
with gr.Row():
|
72 |
-
paragraph_input = gr.Textbox(lines=8, label="Enter Paragraph for MCQs", placeholder="Paste your study material here...")
|
73 |
-
with gr.Row():
|
74 |
-
num_questions_slider = gr.Slider(1, 10, step=1, value=5, label="Number of Questions")
|
75 |
-
with gr.Row():
|
76 |
-
generate_btn = gr.Button("π Generate MCQs")
|
77 |
-
status = gr.Textbox(label="Status", interactive=False)
|
78 |
-
|
79 |
-
with gr.Row():
|
80 |
-
mcq_output = gr.HTML()
|
81 |
-
|
82 |
-
with gr.Row():
|
83 |
-
download_output = gr.File(label="Download MCQs (TXT/CSV)")
|
84 |
-
|
85 |
-
generate_btn.click(
|
86 |
-
fn=generate_mcqs_ui,
|
87 |
-
inputs=[paragraph_input, num_questions_slider],
|
88 |
-
outputs=[mcq_output, download_output, status]
|
89 |
-
)
|
90 |
-
|
91 |
-
demo.launch(ssr_mode=False)
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def generate_mcqs_ui(paragraph, num_questions):
|
2 |
if not paragraph.strip():
|
3 |
return None, None, "β οΈ Please enter a valid paragraph."
|
|
|
20 |
question_html += "</div>"
|
21 |
pretty_mcqs.append(question_html)
|
22 |
|
23 |
+
# Prepare text output and CSV data
|
24 |
txt_output = ""
|
25 |
csv_data = []
|
26 |
|
|
|
39 |
'Answer': chr(65+mcq['answer_index'])
|
40 |
})
|
41 |
|
42 |
+
# Create in-memory text file
|
43 |
+
txt_buffer = io.StringIO()
|
44 |
+
txt_buffer.write(txt_output)
|
45 |
+
txt_buffer.seek(0)
|
46 |
+
|
47 |
+
# Create in-memory CSV file
|
48 |
+
csv_buffer = io.StringIO()
|
49 |
+
pd.DataFrame(csv_data).to_csv(csv_buffer, index=False)
|
50 |
+
csv_buffer.seek(0)
|
51 |
|
52 |
+
# Return Gradio-compatible downloadable files
|
53 |
+
txt_file = gr.File(value=txt_buffer, file_name="mcqs.txt", file_type="text/plain")
|
54 |
+
csv_file = gr.File(value=csv_buffer, file_name="mcqs.csv", file_type="text/csv")
|
55 |
|
56 |
+
return pretty_mcqs, [txt_file, csv_file], "β
MCQs generated successfully!"
|
57 |
|
58 |
except Exception as e:
|
59 |
return None, None, f"β Error generating MCQs: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|