Update file_module.py
Browse files- file_module.py +30 -21
file_module.py
CHANGED
|
@@ -28,7 +28,8 @@ def file_dashboard():
|
|
| 28 |
file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
|
| 29 |
btn_file_view = gr.Button("π Files")
|
| 30 |
gr.Markdown("---")
|
| 31 |
-
gr.Markdown("""Ankush Bansal
|
|
|
|
| 32 |
|
| 33 |
with gr.Column(scale=4, elem_id="main-column") as main_content:
|
| 34 |
mode_view = gr.State("upload")
|
|
@@ -42,7 +43,7 @@ def file_dashboard():
|
|
| 42 |
|
| 43 |
chat_input = gr.Textbox(label="Ask something about this file")
|
| 44 |
generate_quiz_btn = gr.Button("π§ Generate Quiz from File")
|
| 45 |
-
|
| 46 |
quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
|
| 47 |
|
| 48 |
def store_file_and_generate_guide(file, current_files, section_map, text_map):
|
|
@@ -77,29 +78,37 @@ def file_dashboard():
|
|
| 77 |
history_map[selected_file] = new_history
|
| 78 |
return new_history, history_map
|
| 79 |
|
| 80 |
-
def
|
| 81 |
if not selected_file or selected_file not in text_map:
|
| 82 |
-
return gr.update(visible=True),
|
|
|
|
| 83 |
text = text_map[selected_file]
|
| 84 |
prompt = f"Create 5 multiple-choice questions based on the content below.\nReturn as JSON list with 'question', 'options' (list), and 'answer'.\n\n{text}"
|
| 85 |
response = call_llm(prompt)
|
|
|
|
| 86 |
try:
|
| 87 |
questions = json.loads(response)
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
file_upload.change(
|
| 105 |
fn=store_file_and_generate_guide,
|
|
@@ -129,9 +138,9 @@ def file_dashboard():
|
|
| 129 |
)
|
| 130 |
|
| 131 |
generate_quiz_btn.click(
|
| 132 |
-
fn=
|
| 133 |
inputs=[file_guide_list, file_texts],
|
| 134 |
-
outputs=[
|
| 135 |
show_progress="minimal"
|
| 136 |
)
|
| 137 |
|
|
|
|
| 28 |
file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
|
| 29 |
btn_file_view = gr.Button("π Files")
|
| 30 |
gr.Markdown("---")
|
| 31 |
+
gr.Markdown("""**Ankush Bansal**
|
| 32 |
+
ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
| 33 |
|
| 34 |
with gr.Column(scale=4, elem_id="main-column") as main_content:
|
| 35 |
mode_view = gr.State("upload")
|
|
|
|
| 43 |
|
| 44 |
chat_input = gr.Textbox(label="Ask something about this file")
|
| 45 |
generate_quiz_btn = gr.Button("π§ Generate Quiz from File")
|
| 46 |
+
quiz_container = gr.Column(visible=False)
|
| 47 |
quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
|
| 48 |
|
| 49 |
def store_file_and_generate_guide(file, current_files, section_map, text_map):
|
|
|
|
| 78 |
history_map[selected_file] = new_history
|
| 79 |
return new_history, history_map
|
| 80 |
|
| 81 |
+
def generate_quiz_ui(selected_file, text_map):
|
| 82 |
if not selected_file or selected_file not in text_map:
|
| 83 |
+
return gr.update(visible=True), gr.update(visible=False, value="")
|
| 84 |
+
|
| 85 |
text = text_map[selected_file]
|
| 86 |
prompt = f"Create 5 multiple-choice questions based on the content below.\nReturn as JSON list with 'question', 'options' (list), and 'answer'.\n\n{text}"
|
| 87 |
response = call_llm(prompt)
|
| 88 |
+
|
| 89 |
try:
|
| 90 |
questions = json.loads(response)
|
| 91 |
+
quiz_elements = []
|
| 92 |
+
score = {"correct": 0, "total": len(questions)}
|
| 93 |
+
|
| 94 |
+
for i, q in enumerate(questions):
|
| 95 |
+
question_md = gr.Markdown(f"**Q{i+1}. {q['question']}**")
|
| 96 |
+
options = gr.Radio(choices=q['options'], label=f"Your Answer to Q{i+1}", interactive=True)
|
| 97 |
+
feedback = gr.Textbox(label="Result", interactive=False)
|
| 98 |
+
|
| 99 |
+
def evaluate(ans):
|
| 100 |
+
def check(choice):
|
| 101 |
+
correct = choice == ans
|
| 102 |
+
return "β
Correct!" if correct else f"β Incorrect. Correct answer: {ans}"
|
| 103 |
+
return check
|
| 104 |
+
|
| 105 |
+
options.change(fn=evaluate(q['answer']), inputs=[options], outputs=[feedback])
|
| 106 |
+
quiz_elements.extend([question_md, options, feedback])
|
| 107 |
+
|
| 108 |
+
return gr.update(visible=True), gr.update(visible=True, value=f"Score: 0/{score['total']}")
|
| 109 |
+
|
| 110 |
+
except Exception as e:
|
| 111 |
+
return gr.update(visible=True), gr.update(visible=True, value="Error generating quiz.")
|
| 112 |
|
| 113 |
file_upload.change(
|
| 114 |
fn=store_file_and_generate_guide,
|
|
|
|
| 138 |
)
|
| 139 |
|
| 140 |
generate_quiz_btn.click(
|
| 141 |
+
fn=generate_quiz_ui,
|
| 142 |
inputs=[file_guide_list, file_texts],
|
| 143 |
+
outputs=[quiz_container, quiz_score],
|
| 144 |
show_progress="minimal"
|
| 145 |
)
|
| 146 |
|