Update file_module.py
Browse files- file_module.py +28 -9
file_module.py
CHANGED
|
@@ -11,6 +11,7 @@ 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 ===
|
|
@@ -40,12 +41,14 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
|
| 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
|
|
|
|
|
|
|
| 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)
|
|
@@ -53,11 +56,11 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
|
| 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):
|
|
@@ -65,14 +68,23 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
|
| 65 |
return "No study guide available."
|
| 66 |
return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
|
| 67 |
|
| 68 |
-
# ===
|
| 69 |
def open_file_viewer():
|
| 70 |
-
return gr.update(visible=True), "Simulated PDF viewer content...", "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -89,4 +101,11 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
|
| 89 |
outputs=[file_chat_output, file_preview, chat_output]
|
| 90 |
)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
return file_panel
|
|
|
|
| 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 |
|
| 16 |
with gr.Row():
|
| 17 |
# === LEFT SIDEBAR ===
|
|
|
|
| 41 |
|
| 42 |
with file_chat_output:
|
| 43 |
file_preview = gr.Textbox(label="π File Viewer (Simulated)", lines=15, interactive=False)
|
| 44 |
+
chat_output = gr.Textbox(label="π¬ Chat Response", lines=8, interactive=False)
|
| 45 |
+
|
| 46 |
+
chat_input = gr.Textbox(label="Ask something about this file")
|
| 47 |
|
| 48 |
# === Upload Handler ===
|
| 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 |
|
| 53 |
text = extract_text_from_file(file)
|
| 54 |
filename = os.path.basename(file.name)
|
|
|
|
| 56 |
prompt = f"Break the content into 4β6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
|
| 57 |
response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
|
| 58 |
|
|
|
|
| 59 |
updated_files = current_files + [filename] if filename not in current_files else current_files
|
| 60 |
section_map[filename] = response
|
| 61 |
+
text_map[filename] = text
|
| 62 |
|
| 63 |
+
return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
|
| 64 |
|
| 65 |
# === Select Study Guide ===
|
| 66 |
def load_study_guide(selected_filename, section_map):
|
|
|
|
| 68 |
return "No study guide available."
|
| 69 |
return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
|
| 70 |
|
| 71 |
+
# === Show File Viewer + Chat ===
|
| 72 |
def open_file_viewer():
|
| 73 |
+
return gr.update(visible=True), "Simulated PDF viewer content...", ""
|
| 74 |
+
|
| 75 |
+
# === GPT Chat Handler ===
|
| 76 |
+
def chat_with_file(question, selected_file, text_map):
|
| 77 |
+
if not selected_file or selected_file not in text_map:
|
| 78 |
+
return "Please select a file first."
|
| 79 |
+
|
| 80 |
+
text = text_map[selected_file]
|
| 81 |
+
prompt = f"Answer the following question using only this document:\n\n{text}\n\nQuestion: {question}"
|
| 82 |
+
return call_llm(prompt, system_message="You are a tutor helping a student understand their document.")
|
| 83 |
|
| 84 |
file_upload.change(
|
| 85 |
fn=store_file_and_generate_guide,
|
| 86 |
+
inputs=[file_upload, uploaded_files, file_sections, file_texts],
|
| 87 |
+
outputs=[uploaded_files, file_sections, file_texts, study_guide_output, file_guide_list],
|
| 88 |
show_progress="minimal"
|
| 89 |
)
|
| 90 |
|
|
|
|
| 101 |
outputs=[file_chat_output, file_preview, chat_output]
|
| 102 |
)
|
| 103 |
|
| 104 |
+
chat_input.change(
|
| 105 |
+
fn=chat_with_file,
|
| 106 |
+
inputs=[chat_input, file_guide_list, file_texts],
|
| 107 |
+
outputs=[chat_output],
|
| 108 |
+
show_progress="minimal"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
return file_panel
|