File size: 5,114 Bytes
e0baf3c e79e5f0 7a0a3a3 e79e5f0 e0baf3c 5206c58 e0baf3c 0a0c7fa 78b6fc4 c47a4d5 d3d2ca6 78b6fc4 0a0c7fa 5206c58 0a0c7fa 78b6fc4 5206c58 e0baf3c 5206c58 e0baf3c 5206c58 78b6fc4 5206c58 e79e5f0 5206c58 e79e5f0 78b6fc4 5206c58 78b6fc4 5206c58 78b6fc4 d3d2ca6 c47a4d5 0a0c7fa 78b6fc4 c47a4d5 0a0c7fa c47a4d5 0a0c7fa 7a0a3a3 78b6fc4 c47a4d5 78b6fc4 c47a4d5 78b6fc4 7a0a3a3 c47a4d5 d3d2ca6 c47a4d5 d3d2ca6 c47a4d5 d3d2ca6 c47a4d5 d3d2ca6 e0baf3c 78b6fc4 c47a4d5 0a0c7fa e0baf3c 78b6fc4 d3d2ca6 78b6fc4 d3d2ca6 c47a4d5 d3d2ca6 c47a4d5 78b6fc4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import gradio as gr
import os
from file_utils import (
extract_text_from_file,
call_llm
)
# === FILE-BASED LEARNING LAYOUT WITH NAVIGATION ===
def file_dashboard():
with gr.Blocks(elem_id="file-learning-layout") as file_panel:
uploaded_files = gr.State([])
file_sections = gr.State({})
file_texts = gr.State({})
chat_history = gr.State({})
with gr.Row():
# === LEFT SIDEBAR ===
with gr.Column(scale=1, min_width=220):
gr.Markdown("### π Educare", elem_id="sidebar-title")
gr.Markdown("---")
with gr.Accordion("π§ Learning Modes", open=True):
btn_spoken = gr.Button("π£ Spoken Communication")
btn_written = gr.Button("βοΈ Written Communication")
with gr.Accordion("π File-Based Learning", open=True):
btn_study_guides = gr.Button("π Study Guides")
file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
btn_file_view = gr.Button("π Files")
gr.Markdown("---")
gr.Markdown("""**Ankush Bansal**
ankban@wharton.upenn.edu""", elem_id="sidebar-user")
# === CENTER + RIGHT PANEL ===
with gr.Column(scale=4, elem_id="main-column") as main_content:
mode_view = gr.State("upload")
file_upload = gr.File(label="π€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
study_guide_output = gr.Markdown("Upload a file to generate a study guide.")
file_chat_output = gr.Row(visible=False)
with file_chat_output:
file_preview = gr.Textbox(label="π File Content Preview", lines=15, interactive=False)
chat_output = gr.Chatbot(label="π¬ Chat with Document")
chat_input = gr.Textbox(label="Ask something about this file")
# === Upload Handler ===
def store_file_and_generate_guide(file, current_files, section_map, text_map):
if file is None:
return current_files, section_map, text_map, "No file uploaded.", gr.update(choices=[])
text = extract_text_from_file(file)
filename = os.path.basename(file.name)
prompt = f"Break the content into 4β6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
updated_files = current_files + [filename] if filename not in current_files else current_files
section_map[filename] = response
text_map[filename] = text
return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
# === Select Study Guide ===
def load_study_guide(selected_filename, section_map):
if not selected_filename or selected_filename not in section_map:
return "No study guide available."
return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
# === Show File Viewer + Chat ===
def open_file_viewer(selected_file, text_map):
content = text_map.get(selected_file, "No file content available.")
return gr.update(visible=True), content, []
# === GPT Chat Handler ===
def chat_with_file(question, selected_file, text_map, history_map):
if not selected_file or selected_file not in text_map:
return [], history_map
text = text_map[selected_file]
history = history_map.get(selected_file, [])
prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
answer = call_llm(prompt)
new_history = history + [(question, answer)]
history_map[selected_file] = new_history
return new_history, history_map
file_upload.change(
fn=store_file_and_generate_guide,
inputs=[file_upload, uploaded_files, file_sections, file_texts],
outputs=[uploaded_files, file_sections, file_texts, study_guide_output, file_guide_list],
show_progress="minimal"
)
file_guide_list.change(
fn=load_study_guide,
inputs=[file_guide_list, file_sections],
outputs=[study_guide_output],
show_progress="minimal"
)
btn_file_view.click(
fn=open_file_viewer,
inputs=[file_guide_list, file_texts],
outputs=[file_chat_output, file_preview, chat_output]
)
chat_input.submit(
fn=chat_with_file,
inputs=[chat_input, file_guide_list, file_texts, chat_history],
outputs=[chat_output, chat_history],
show_progress="minimal"
)
return file_panel
|