ankban commited on
Commit
8a9bac8
Β·
verified Β·
1 Parent(s): d33edec

Update file_module.py

Browse files
Files changed (1) hide show
  1. file_module.py +134 -137
file_module.py CHANGED
@@ -8,140 +8,137 @@ from file_utils import (
8
 
9
 
10
  def file_dashboard():
11
- with gr.Blocks(elem_id="file-learning-layout") as file_panel:
12
- uploaded_files = gr.State([])
13
- file_sections = gr.State({})
14
- file_texts = gr.State({})
15
- chat_history = gr.State({})
16
- quiz_data = gr.State({})
17
- total_score = gr.State({})
18
-
19
- with gr.Row():
20
- with gr.Column(scale=1, min_width=220):
21
- gr.Markdown("### πŸŽ“ Educare", elem_id="sidebar-title")
22
- gr.Markdown("---")
23
- with gr.Accordion("🧭 Learning Modes", open=True):
24
- btn_spoken = gr.Button("πŸ—£ Spoken Communication")
25
- btn_written = gr.Button("✍️ Written Communication")
26
- with gr.Accordion("πŸ“„ File-Based Learning", open=True):
27
- btn_study_guides = gr.Button("πŸ“˜ Study Guides")
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")
36
- file_upload = gr.File(label="πŸ“€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
37
- study_guide_output = gr.Markdown("Upload a file to generate a study guide.")
38
- file_chat_output = gr.Row(visible=False)
39
-
40
- with file_chat_output:
41
- file_preview = gr.Textbox(label="πŸ“„ File Content Preview", lines=15, interactive=False)
42
- chat_output = gr.Chatbot(label="πŸ’¬ Chat with Document")
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):
50
- if file is None:
51
- return current_files, section_map, text_map, "No file uploaded.", gr.update(choices=[])
52
- text = extract_text_from_file(file)
53
- filename = os.path.basename(file.name)
54
- prompt = f"Break the content into 4–6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
55
- response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
56
- updated_files = current_files + [filename] if filename not in current_files else current_files
57
- section_map[filename] = response
58
- text_map[filename] = text
59
- return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
60
-
61
- def load_study_guide(selected_filename, section_map):
62
- if not selected_filename or selected_filename not in section_map:
63
- return "No study guide available."
64
- return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
65
-
66
- def open_file_viewer(selected_file, text_map):
67
- content = text_map.get(selected_file, "No file content available.")
68
- return gr.update(visible=True), content, []
69
-
70
- def chat_with_file(question, selected_file, text_map, history_map):
71
- if not selected_file or selected_file not in text_map:
72
- return [], history_map
73
- text = text_map[selected_file]
74
- history = history_map.get(selected_file, [])
75
- prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
76
- answer = call_llm(prompt)
77
- new_history = history + [(question, answer)]
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,
115
- inputs=[file_upload, uploaded_files, file_sections, file_texts],
116
- outputs=[uploaded_files, file_sections, file_texts, study_guide_output, file_guide_list],
117
- show_progress="minimal"
118
- )
119
-
120
- file_guide_list.change(
121
- fn=load_study_guide,
122
- inputs=[file_guide_list, file_sections],
123
- outputs=[study_guide_output],
124
- show_progress="minimal"
125
- )
126
-
127
- btn_file_view.click(
128
- fn=open_file_viewer,
129
- inputs=[file_guide_list, file_texts],
130
- outputs=[file_chat_output, file_preview, chat_output]
131
- )
132
-
133
- chat_input.submit(
134
- fn=chat_with_file,
135
- inputs=[chat_input, file_guide_list, file_texts, chat_history],
136
- outputs=[chat_output, chat_history],
137
- show_progress="minimal"
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
-
147
- return file_panel
 
8
 
9
 
10
  def file_dashboard():
11
+ uploaded_files = gr.State([])
12
+ file_sections = gr.State({})
13
+ file_texts = gr.State({})
14
+ chat_history = gr.State({})
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
+ return gr.Column(
133
+ components=[
134
+ file_upload,
135
+ study_guide_output,
136
+ file_chat_output,
137
+ chat_input,
138
+ generate_quiz_btn,
139
+ quiz_container,
140
+ quiz_score
141
+ ],
142
+ scale=4,
143
+ elem_id="main-column"
144
+ )