Update file_module.py
Browse files- file_module.py +52 -13
file_module.py
CHANGED
|
@@ -9,10 +9,13 @@ from file_utils import (
|
|
| 9 |
# === FILE-BASED LEARNING LAYOUT WITH NAVIGATION ===
|
| 10 |
def file_dashboard():
|
| 11 |
with gr.Blocks(elem_id="file-learning-layout") as file_panel:
|
|
|
|
|
|
|
|
|
|
| 12 |
with gr.Row():
|
| 13 |
# === LEFT SIDEBAR ===
|
| 14 |
with gr.Column(scale=1, min_width=220):
|
| 15 |
-
gr.Markdown("
|
| 16 |
gr.Markdown("---")
|
| 17 |
|
| 18 |
with gr.Accordion("π§ Learning Modes", open=True):
|
|
@@ -21,33 +24,69 @@ def file_dashboard():
|
|
| 21 |
|
| 22 |
with gr.Accordion("π File-Based Learning", open=True):
|
| 23 |
btn_study_guides = gr.Button("π Study Guides")
|
|
|
|
| 24 |
btn_file_view = gr.Button("π Files")
|
| 25 |
|
| 26 |
gr.Markdown("---")
|
| 27 |
gr.Markdown("""**Ankush Bansal**
|
| 28 |
ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
| 29 |
|
| 30 |
-
# ===
|
| 31 |
with gr.Column(scale=4, elem_id="main-column") as main_content:
|
|
|
|
| 32 |
file_upload = gr.File(label="π€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# ===
|
| 36 |
-
def
|
| 37 |
if file is None:
|
| 38 |
-
return "No file uploaded."
|
| 39 |
|
| 40 |
text = extract_text_from_file(file)
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
file_upload.change(
|
| 47 |
-
fn=
|
| 48 |
-
inputs=[file_upload],
|
| 49 |
-
outputs=[
|
| 50 |
show_progress="minimal"
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# === FILE-BASED LEARNING LAYOUT WITH NAVIGATION ===
|
| 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 |
+
|
| 15 |
with gr.Row():
|
| 16 |
# === LEFT SIDEBAR ===
|
| 17 |
with gr.Column(scale=1, min_width=220):
|
| 18 |
+
gr.Markdown("### π Educare", elem_id="sidebar-title")
|
| 19 |
gr.Markdown("---")
|
| 20 |
|
| 21 |
with gr.Accordion("π§ Learning Modes", open=True):
|
|
|
|
| 24 |
|
| 25 |
with gr.Accordion("π File-Based Learning", open=True):
|
| 26 |
btn_study_guides = gr.Button("π Study Guides")
|
| 27 |
+
file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
|
| 28 |
btn_file_view = gr.Button("π Files")
|
| 29 |
|
| 30 |
gr.Markdown("---")
|
| 31 |
gr.Markdown("""**Ankush Bansal**
|
| 32 |
ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
| 33 |
|
| 34 |
+
# === CENTER + RIGHT PANEL ===
|
| 35 |
with gr.Column(scale=4, elem_id="main-column") as main_content:
|
| 36 |
+
mode_view = gr.State("upload")
|
| 37 |
file_upload = gr.File(label="π€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 38 |
+
study_guide_output = gr.Markdown("Upload a file to generate a study guide.")
|
| 39 |
+
file_chat_output = gr.Row(visible=False)
|
| 40 |
+
|
| 41 |
+
with file_chat_output:
|
| 42 |
+
file_preview = gr.Textbox(label="π File Viewer (Simulated)", lines=15, interactive=False)
|
| 43 |
+
chat_output = gr.Textbox(label="π¬ Chat with Document (Simulated)", lines=15, interactive=False)
|
| 44 |
|
| 45 |
+
# === Upload Handler ===
|
| 46 |
+
def store_file_and_generate_guide(file, current_files, section_map):
|
| 47 |
if file is None:
|
| 48 |
+
return current_files, section_map, "No file uploaded.", gr.update(choices=[])
|
| 49 |
|
| 50 |
text = extract_text_from_file(file)
|
| 51 |
+
filename = os.path.basename(file.name)
|
| 52 |
+
|
| 53 |
+
prompt = f"Break the content into 4β6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
|
| 54 |
+
response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
|
| 55 |
+
|
| 56 |
+
# Save file name and content
|
| 57 |
+
updated_files = current_files + [filename] if filename not in current_files else current_files
|
| 58 |
+
section_map[filename] = response
|
| 59 |
+
|
| 60 |
+
return updated_files, section_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
|
| 61 |
+
|
| 62 |
+
# === Select Study Guide ===
|
| 63 |
+
def load_study_guide(selected_filename, section_map):
|
| 64 |
+
if not selected_filename or selected_filename not in section_map:
|
| 65 |
+
return "No study guide available."
|
| 66 |
+
return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
|
| 67 |
|
| 68 |
+
# === Switch to Chat/File Viewer ===
|
| 69 |
+
def open_file_viewer():
|
| 70 |
+
return gr.update(visible=True), "Simulated PDF viewer content...", "Ask me anything about this file."
|
| 71 |
|
| 72 |
file_upload.change(
|
| 73 |
+
fn=store_file_and_generate_guide,
|
| 74 |
+
inputs=[file_upload, uploaded_files, file_sections],
|
| 75 |
+
outputs=[uploaded_files, file_sections, study_guide_output, file_guide_list],
|
| 76 |
show_progress="minimal"
|
| 77 |
)
|
| 78 |
|
| 79 |
+
file_guide_list.change(
|
| 80 |
+
fn=load_study_guide,
|
| 81 |
+
inputs=[file_guide_list, file_sections],
|
| 82 |
+
outputs=[study_guide_output],
|
| 83 |
+
show_progress="minimal"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
btn_file_view.click(
|
| 87 |
+
fn=open_file_viewer,
|
| 88 |
+
inputs=[],
|
| 89 |
+
outputs=[file_chat_output, file_preview, chat_output]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return file_panel
|