Update file_module.py
Browse files- file_module.py +38 -20
file_module.py
CHANGED
|
@@ -1,39 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
from file_utils import (
|
| 4 |
extract_text_from_file,
|
| 5 |
call_llm
|
| 6 |
)
|
| 7 |
|
| 8 |
|
| 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 |
file_texts = gr.State({})
|
| 15 |
chat_history = gr.State({})
|
|
|
|
|
|
|
| 16 |
|
| 17 |
with gr.Row():
|
| 18 |
-
# === LEFT SIDEBAR ===
|
| 19 |
with gr.Column(scale=1, min_width=220):
|
| 20 |
gr.Markdown("### π Educare", elem_id="sidebar-title")
|
| 21 |
gr.Markdown("---")
|
| 22 |
-
|
| 23 |
with gr.Accordion("π§ Learning Modes", open=True):
|
| 24 |
btn_spoken = gr.Button("π£ Spoken Communication")
|
| 25 |
btn_written = gr.Button("βοΈ Written Communication")
|
| 26 |
-
|
| 27 |
with gr.Accordion("π File-Based Learning", open=True):
|
| 28 |
btn_study_guides = gr.Button("π Study Guides")
|
| 29 |
file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
|
| 30 |
btn_file_view = gr.Button("π Files")
|
| 31 |
-
|
| 32 |
gr.Markdown("---")
|
| 33 |
-
gr.Markdown("""**Ankush Bansal**
|
| 34 |
-
ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
| 35 |
|
| 36 |
-
# === CENTER + RIGHT PANEL ===
|
| 37 |
with gr.Column(scale=4, elem_id="main-column") as main_content:
|
| 38 |
mode_view = gr.State("upload")
|
| 39 |
file_upload = gr.File(label="π€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
|
@@ -45,51 +41,66 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
|
| 45 |
chat_output = gr.Chatbot(label="π¬ Chat with Document")
|
| 46 |
|
| 47 |
chat_input = gr.Textbox(label="Ask something about this file")
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
# === Upload Handler ===
|
| 50 |
def store_file_and_generate_guide(file, current_files, section_map, text_map):
|
| 51 |
if file is None:
|
| 52 |
return current_files, section_map, text_map, "No file uploaded.", gr.update(choices=[])
|
| 53 |
-
|
| 54 |
text = extract_text_from_file(file)
|
| 55 |
filename = os.path.basename(file.name)
|
| 56 |
-
|
| 57 |
prompt = f"Break the content into 4β6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
|
| 58 |
response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
|
| 59 |
-
|
| 60 |
updated_files = current_files + [filename] if filename not in current_files else current_files
|
| 61 |
section_map[filename] = response
|
| 62 |
text_map[filename] = text
|
| 63 |
-
|
| 64 |
return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
|
| 65 |
|
| 66 |
-
# === Select Study Guide ===
|
| 67 |
def load_study_guide(selected_filename, section_map):
|
| 68 |
if not selected_filename or selected_filename not in section_map:
|
| 69 |
return "No study guide available."
|
| 70 |
return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
|
| 71 |
|
| 72 |
-
# === Show File Viewer + Chat ===
|
| 73 |
def open_file_viewer(selected_file, text_map):
|
| 74 |
content = text_map.get(selected_file, "No file content available.")
|
| 75 |
return gr.update(visible=True), content, []
|
| 76 |
|
| 77 |
-
# === GPT Chat Handler ===
|
| 78 |
def chat_with_file(question, selected_file, text_map, history_map):
|
| 79 |
if not selected_file or selected_file not in text_map:
|
| 80 |
return [], history_map
|
| 81 |
-
|
| 82 |
text = text_map[selected_file]
|
| 83 |
history = history_map.get(selected_file, [])
|
| 84 |
-
|
| 85 |
prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
|
| 86 |
answer = call_llm(prompt)
|
| 87 |
-
|
| 88 |
new_history = history + [(question, answer)]
|
| 89 |
history_map[selected_file] = new_history
|
| 90 |
-
|
| 91 |
return new_history, history_map
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
file_upload.change(
|
| 94 |
fn=store_file_and_generate_guide,
|
| 95 |
inputs=[file_upload, uploaded_files, file_sections, file_texts],
|
|
@@ -117,4 +128,11 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
|
| 117 |
show_progress="minimal"
|
| 118 |
)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
return file_panel
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import json
|
| 4 |
from file_utils import (
|
| 5 |
extract_text_from_file,
|
| 6 |
call_llm
|
| 7 |
)
|
| 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** \nankban@wharton.upenn.edu""", elem_id="sidebar-user")
|
|
|
|
| 32 |
|
|
|
|
| 33 |
with gr.Column(scale=4, elem_id="main-column") as main_content:
|
| 34 |
mode_view = gr.State("upload")
|
| 35 |
file_upload = gr.File(label="π€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
|
|
|
| 41 |
chat_output = gr.Chatbot(label="π¬ Chat with Document")
|
| 42 |
|
| 43 |
chat_input = gr.Textbox(label="Ask something about this file")
|
| 44 |
+
generate_quiz_btn = gr.Button("π§ Generate Quiz from File")
|
| 45 |
+
quiz_display = gr.Column(visible=False)
|
| 46 |
+
quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
|
| 47 |
|
|
|
|
| 48 |
def store_file_and_generate_guide(file, current_files, section_map, text_map):
|
| 49 |
if file is None:
|
| 50 |
return current_files, section_map, text_map, "No file uploaded.", gr.update(choices=[])
|
|
|
|
| 51 |
text = extract_text_from_file(file)
|
| 52 |
filename = os.path.basename(file.name)
|
|
|
|
| 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 |
updated_files = current_files + [filename] if filename not in current_files else current_files
|
| 56 |
section_map[filename] = response
|
| 57 |
text_map[filename] = text
|
|
|
|
| 58 |
return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
|
| 59 |
|
|
|
|
| 60 |
def load_study_guide(selected_filename, section_map):
|
| 61 |
if not selected_filename or selected_filename not in section_map:
|
| 62 |
return "No study guide available."
|
| 63 |
return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
|
| 64 |
|
|
|
|
| 65 |
def open_file_viewer(selected_file, text_map):
|
| 66 |
content = text_map.get(selected_file, "No file content available.")
|
| 67 |
return gr.update(visible=True), content, []
|
| 68 |
|
|
|
|
| 69 |
def chat_with_file(question, selected_file, text_map, history_map):
|
| 70 |
if not selected_file or selected_file not in text_map:
|
| 71 |
return [], history_map
|
|
|
|
| 72 |
text = text_map[selected_file]
|
| 73 |
history = history_map.get(selected_file, [])
|
|
|
|
| 74 |
prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
|
| 75 |
answer = call_llm(prompt)
|
|
|
|
| 76 |
new_history = history + [(question, answer)]
|
| 77 |
history_map[selected_file] = new_history
|
|
|
|
| 78 |
return new_history, history_map
|
| 79 |
|
| 80 |
+
def generate_quiz(selected_file, text_map):
|
| 81 |
+
if not selected_file or selected_file not in text_map:
|
| 82 |
+
return gr.update(visible=True), [], {}, gr.update(visible=True, value="")
|
| 83 |
+
text = text_map[selected_file]
|
| 84 |
+
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}"
|
| 85 |
+
response = call_llm(prompt)
|
| 86 |
+
try:
|
| 87 |
+
questions = json.loads(response)
|
| 88 |
+
score_state = {"score": 0, "total": len(questions)}
|
| 89 |
+
with quiz_display:
|
| 90 |
+
for i, q in enumerate(questions):
|
| 91 |
+
q_text = gr.Markdown(f"**Q{i+1}. {q['question']}**")
|
| 92 |
+
q_radio = gr.Radio(choices=q['options'], label="Select an answer")
|
| 93 |
+
q_feedback = gr.Textbox(label="Result", interactive=False)
|
| 94 |
+
def handle_answer(choice, ans=q['answer'], score_state=score_state):
|
| 95 |
+
if choice == ans:
|
| 96 |
+
score_state["score"] += 1
|
| 97 |
+
return "β
Correct!"
|
| 98 |
+
return f"β Incorrect. Correct answer: {ans}"
|
| 99 |
+
q_radio.change(handle_answer, inputs=[q_radio], outputs=[q_feedback])
|
| 100 |
+
return gr.update(visible=True), questions, {selected_file: questions}, gr.update(visible=True, value="Score: 0/5")
|
| 101 |
+
except:
|
| 102 |
+
return gr.update(visible=True), [], {}, gr.update(visible=True, value="Error generating quiz.")
|
| 103 |
+
|
| 104 |
file_upload.change(
|
| 105 |
fn=store_file_and_generate_guide,
|
| 106 |
inputs=[file_upload, uploaded_files, file_sections, file_texts],
|
|
|
|
| 128 |
show_progress="minimal"
|
| 129 |
)
|
| 130 |
|
| 131 |
+
generate_quiz_btn.click(
|
| 132 |
+
fn=generate_quiz,
|
| 133 |
+
inputs=[file_guide_list, file_texts],
|
| 134 |
+
outputs=[quiz_display, quiz_display, quiz_data, quiz_score],
|
| 135 |
+
show_progress="minimal"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
return file_panel
|