ankban commited on
Commit
d33edec
Β·
verified Β·
1 Parent(s): 0be1519

Update file_module.py

Browse files
Files changed (1) hide show
  1. 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""", elem_id="sidebar-user")
 
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
- quiz_display = gr.Column(visible=False)
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 generate_quiz(selected_file, text_map):
81
  if not selected_file or selected_file not in text_map:
82
- return gr.update(visible=True), [], {}, gr.update(visible=True, value="")
 
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
- score_state = {"score": 0, "total": len(questions)}
89
- with quiz_display:
90
- for i, q in enumerate(questions):
91
- q_text = gr.Markdown(f"**Q{i+1}. {q['question']}**")
92
- q_radio = gr.Radio(choices=q['options'], label="Select an answer")
93
- q_feedback = gr.Textbox(label="Result", interactive=False)
94
- def handle_answer(choice, ans=q['answer'], score_state=score_state):
95
- if choice == ans:
96
- score_state["score"] += 1
97
- return "βœ… Correct!"
98
- return f"❌ Incorrect. Correct answer: {ans}"
99
- q_radio.change(handle_answer, inputs=[q_radio], outputs=[q_feedback])
100
- return gr.update(visible=True), questions, {selected_file: questions}, gr.update(visible=True, value="Score: 0/5")
101
- except:
102
- return gr.update(visible=True), [], {}, gr.update(visible=True, value="Error generating quiz.")
 
 
 
 
 
 
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=generate_quiz,
133
  inputs=[file_guide_list, file_texts],
134
- outputs=[quiz_display, quiz_display, quiz_data, quiz_score],
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