ankban commited on
Commit
df83b64
Β·
verified Β·
1 Parent(s): fac8763

Update file_module.py

Browse files
Files changed (1) hide show
  1. file_module.py +113 -121
file_module.py CHANGED
@@ -15,127 +15,119 @@ def file_dashboard():
15
  quiz_data = gr.State({})
16
  total_score = gr.State({})
17
 
18
- file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
19
- btn_file_view = gr.Button("πŸ“ Files")
20
-
21
- file_upload = gr.File(label="πŸ“€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
22
- study_guide_output = gr.Markdown("Upload a file to generate a study guide.")
23
- file_chat_output = gr.Row(visible=False)
24
-
25
- with file_chat_output:
26
- file_preview = gr.Textbox(label="πŸ“„ File Content Preview", lines=15, interactive=False)
27
- chat_output = gr.Chatbot(label="πŸ’¬ Chat with Document")
28
-
29
- chat_input = gr.Textbox(label="Ask something about this file")
30
- generate_quiz_btn = gr.Button("🧠 Generate Quiz from File")
31
- quiz_container = gr.Column(visible=False)
32
- quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
33
-
34
- def store_file_and_generate_guide(file, current_files, section_map, text_map):
35
- if file is None:
36
- return current_files, section_map, text_map, "No file uploaded.", gr.update(choices=[])
37
- text = extract_text_from_file(file)
38
- filename = os.path.basename(file.name)
39
- prompt = f"Break the content into 4–6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
40
- response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
41
- updated_files = current_files + [filename] if filename not in current_files else current_files
42
- section_map[filename] = response
43
- text_map[filename] = text
44
- return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
45
-
46
- def load_study_guide(selected_filename, section_map):
47
- if not selected_filename or selected_filename not in section_map:
48
- return "No study guide available."
49
- return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
50
-
51
- def open_file_viewer(selected_file, text_map):
52
- content = text_map.get(selected_file, "No file content available.")
53
- return gr.update(visible=True), content, []
54
-
55
- def chat_with_file(question, selected_file, text_map, history_map):
56
- if not selected_file or selected_file not in text_map:
57
- return [], history_map
58
- text = text_map[selected_file]
59
- history = history_map.get(selected_file, [])
60
- prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
61
- answer = call_llm(prompt)
62
- new_history = history + [(question, answer)]
63
- history_map[selected_file] = new_history
64
- return new_history, history_map
65
-
66
- def generate_quiz_ui(selected_file, text_map):
67
- if not selected_file or selected_file not in text_map:
68
- return gr.update(visible=True), gr.update(visible=False, value="")
69
-
70
- text = text_map[selected_file]
71
- 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}"
72
- response = call_llm(prompt)
73
-
74
- try:
75
- questions = json.loads(response)
76
- quiz_elements = []
77
- score = {"correct": 0, "total": len(questions)}
78
-
79
- for i, q in enumerate(questions):
80
- question_md = gr.Markdown(f"**Q{i+1}. {q['question']}**")
81
- options = gr.Radio(choices=q['options'], label=f"Your Answer to Q{i+1}", interactive=True)
82
- feedback = gr.Textbox(label="Result", interactive=False)
83
-
84
- def evaluate(ans):
85
- def check(choice):
86
- correct = choice == ans
87
- return "βœ… Correct!" if correct else f"❌ Incorrect. Correct answer: {ans}"
88
- return check
89
-
90
- options.change(fn=evaluate(q['answer']), inputs=[options], outputs=[feedback])
91
- quiz_elements.extend([question_md, options, feedback])
92
-
93
- return gr.update(visible=True), gr.update(visible=True, value=f"Score: 0/{score['total']}")
94
-
95
- except Exception as e:
96
- return gr.update(visible=True), gr.update(visible=True, value="Error generating quiz.")
97
-
98
- file_upload.change(
99
- fn=store_file_and_generate_guide,
100
- inputs=[file_upload, uploaded_files, file_sections, file_texts],
101
- outputs=[uploaded_files, file_sections, file_texts, study_guide_output, file_guide_list],
102
- show_progress="minimal"
103
- )
104
-
105
- file_guide_list.change(
106
- fn=load_study_guide,
107
- inputs=[file_guide_list, file_sections],
108
- outputs=[study_guide_output],
109
- show_progress="minimal"
110
- )
111
-
112
- btn_file_view.click(
113
- fn=open_file_viewer,
114
- inputs=[file_guide_list, file_texts],
115
- outputs=[file_chat_output, file_preview, chat_output]
116
- )
117
-
118
- chat_input.submit(
119
- fn=chat_with_file,
120
- inputs=[chat_input, file_guide_list, file_texts, chat_history],
121
- outputs=[chat_output, chat_history],
122
- show_progress="minimal"
123
- )
124
-
125
- generate_quiz_btn.click(
126
- fn=generate_quiz_ui,
127
- inputs=[file_guide_list, file_texts],
128
- outputs=[quiz_container, quiz_score],
129
- show_progress="minimal"
130
- )
131
-
132
  with gr.Column(scale=4, elem_id="main-column") as file_content:
133
- file_upload.render()
134
- study_guide_output.render()
135
- file_chat_output.render()
136
- chat_input.render()
137
- generate_quiz_btn.render()
138
- quiz_container.render()
139
- quiz_score.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  return file_content
 
15
  quiz_data = gr.State({})
16
  total_score = gr.State({})
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  with gr.Column(scale=4, elem_id="main-column") as file_content:
19
+ file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
20
+ btn_file_view = gr.Button("πŸ“ Files")
21
+
22
+ file_upload = gr.File(label="πŸ“€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
23
+ study_guide_output = gr.Markdown("Upload a file to generate a study guide.")
24
+ file_chat_output = gr.Row(visible=False)
25
+
26
+ with file_chat_output:
27
+ file_preview = gr.Textbox(label="πŸ“„ File Content Preview", lines=15, interactive=False)
28
+ chat_output = gr.Chatbot(label="πŸ’¬ Chat with Document")
29
+
30
+ chat_input = gr.Textbox(label="Ask something about this file")
31
+ generate_quiz_btn = gr.Button("🧠 Generate Quiz from File")
32
+ quiz_container = gr.Column(visible=False)
33
+ quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
34
+
35
+ def store_file_and_generate_guide(file, current_files, section_map, text_map):
36
+ if file is None:
37
+ return current_files, section_map, text_map, "No file uploaded.", gr.update(choices=[])
38
+ text = extract_text_from_file(file)
39
+ filename = os.path.basename(file.name)
40
+ prompt = f"Break the content into 4–6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
41
+ response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
42
+ updated_files = current_files + [filename] if filename not in current_files else current_files
43
+ section_map[filename] = response
44
+ text_map[filename] = text
45
+ return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
46
+
47
+ def load_study_guide(selected_filename, section_map):
48
+ if not selected_filename or selected_filename not in section_map:
49
+ return "No study guide available."
50
+ return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
51
+
52
+ def open_file_viewer(selected_file, text_map):
53
+ content = text_map.get(selected_file, "No file content available.")
54
+ return gr.update(visible=True), content, []
55
+
56
+ def chat_with_file(question, selected_file, text_map, history_map):
57
+ if not selected_file or selected_file not in text_map:
58
+ return [], history_map
59
+ text = text_map[selected_file]
60
+ history = history_map.get(selected_file, [])
61
+ prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
62
+ answer = call_llm(prompt)
63
+ new_history = history + [(question, answer)]
64
+ history_map[selected_file] = new_history
65
+ return new_history, history_map
66
+
67
+ def generate_quiz_ui(selected_file, text_map):
68
+ if not selected_file or selected_file not in text_map:
69
+ return gr.update(visible=True), gr.update(visible=False, value="")
70
+
71
+ text = text_map[selected_file]
72
+ 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}"
73
+ response = call_llm(prompt)
74
+
75
+ try:
76
+ questions = json.loads(response)
77
+ quiz_elements = []
78
+ score = {"correct": 0, "total": len(questions)}
79
+
80
+ for i, q in enumerate(questions):
81
+ question_md = gr.Markdown(f"**Q{i+1}. {q['question']}**")
82
+ options = gr.Radio(choices=q['options'], label=f"Your Answer to Q{i+1}", interactive=True)
83
+ feedback = gr.Textbox(label="Result", interactive=False)
84
+
85
+ def evaluate(ans):
86
+ def check(choice):
87
+ correct = choice == ans
88
+ return "βœ… Correct!" if correct else f"❌ Incorrect. Correct answer: {ans}"
89
+ return check
90
+
91
+ options.change(fn=evaluate(q['answer']), inputs=[options], outputs=[feedback])
92
+ quiz_elements.extend([question_md, options, feedback])
93
+
94
+ return gr.update(visible=True), gr.update(visible=True, value=f"Score: 0/{score['total']}")
95
+
96
+ except Exception as e:
97
+ return gr.update(visible=True), gr.update(visible=True, value="Error generating quiz.")
98
+
99
+ file_upload.change(
100
+ fn=store_file_and_generate_guide,
101
+ inputs=[file_upload, uploaded_files, file_sections, file_texts],
102
+ outputs=[uploaded_files, file_sections, file_texts, study_guide_output, file_guide_list],
103
+ show_progress="minimal"
104
+ )
105
+
106
+ file_guide_list.change(
107
+ fn=load_study_guide,
108
+ inputs=[file_guide_list, file_sections],
109
+ outputs=[study_guide_output],
110
+ show_progress="minimal"
111
+ )
112
+
113
+ btn_file_view.click(
114
+ fn=open_file_viewer,
115
+ inputs=[file_guide_list, file_texts],
116
+ outputs=[file_chat_output, file_preview, chat_output]
117
+ )
118
+
119
+ chat_input.submit(
120
+ fn=chat_with_file,
121
+ inputs=[chat_input, file_guide_list, file_texts, chat_history],
122
+ outputs=[chat_output, chat_history],
123
+ show_progress="minimal"
124
+ )
125
+
126
+ generate_quiz_btn.click(
127
+ fn=generate_quiz_ui,
128
+ inputs=[file_guide_list, file_texts],
129
+ outputs=[quiz_container, quiz_score],
130
+ show_progress="minimal"
131
+ )
132
 
133
  return file_content